const system = require("../../../system");
const Dao = require("../../dao.base");
class IborderDao extends Dao {
  constructor() {
    super(Dao.getModelName(IborderDao));
  }

  async statOrderByTime(begin, end) {
    var sql = [];
    sql.push("SELECT");
    sql.push("COUNT(1) AS orderCount, SUM(price) AS orderPrice");
    sql.push("FROM `ib_order`");
    sql.push("WHERE 1 = 1");

    var params = {
      begin: begin,
      end: end
    };
    if (begin) {
      sql.push("AND created_at >= :begin");
    }
    if (end) {
      sql.push("AND created_at <= :end");
    }
    var list = await this.customQuery(sql.join(" "), params);

    if (!list || list.length == 0) {
      return {
        orderCount: 0,
        orderPrice: 0,
      }
    }
    var item = list[0];
    return {
      orderCount: item.orderCount || 0,
      orderPrice: item.orderPrice || 0,
    }
  }

  async statDayByTime(begin, end) {
    var result = {};
    var sql = [];
    sql.push("SELECT");
    sql.push("DATE_FORMAT(created_at, '%Y-%m-%d') AS `day`, SUM(price) AS orderPrice, COUNT(1) AS orderCount");
    sql.push("FROM `ib_order`");
    sql.push("WHERE 1 = 1");

    var params = {
      begin: begin,
      end: end
    };
    if (begin) {
      sql.push("AND created_at >= :begin");
    }
    if (end) {
      sql.push("AND created_at <= :end");
    }
    sql.push("GROUP BY `day` ORDER BY `day` ASC");
    var list = await this.customQuery(sql.join(" "), params);

    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.day] = item;
    }
    return result;
  }

  async statMonthByTime(begin, end) {
    var result = {};
    var sql = [];
    sql.push("SELECT");
    sql.push("DATE_FORMAT(created_at, '%Y-%m') AS `month`, SUM(price) AS orderPrice, COUNT(1) AS orderCount");
    sql.push("FROM `ib_order`");
    sql.push("WHERE 1 = 1");

    var params = {
      begin: begin,
      end: end
    };
    if (begin) {
      sql.push("AND created_at >= :begin");
    }
    if (end) {
      sql.push("AND created_at <= :end");
    }

    sql.push("GROUP BY `month` ORDER BY `month` ASC");
    var list = await this.customQuery(sql.join(" "), params);

    if (!list || list.length == 0) {
      return result;
    }

    for (var idx = 0; idx < list.length; idx++) {
      var item = list[idx];
      result[item.month] = item;

      if (idx == 0) {
        result.begin = item.month;
      }

      if (idx == list.length - 1) {
        result.end = item.month;
      }
    }

    return result;
  }

  async statByStatus(begin, end) {
    var sql = [];
    sql.push("SELECT");
    sql.push("`status`, COUNT(1) AS orderCount ");
    sql.push("FROM `ib_order`");
    sql.push("WHERE 1 = 1");
    var params = {
      begin: begin,
      end: end
    };

    if (begin) {
      sql.push("AND created_at >= :begin");
    }
    if (end) {
      sql.push("AND created_at <= :end");
    }

    sql.push("GROUP BY `status`");

    var result = {};
    var list = await this.customQuery(sql.join(" "), params);
    if(!list || list.length == 0) {
      return result;
    }

    for(var item of list) {
      result[item.status] = item.orderCount || 0;
    }
    return result;
  }

}
module.exports = IborderDao;