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

  /**
   * 订单管理列表 分页总数查询
   * @param {*} params 
   */
  async ordersCount(params){
    let sql = [];
    sql.push('SELECT COUNT(1) AS orderCount FROM o_order WHERE 1 = 1 ');
    this.setOrderCount(sql,params);
    return await this.customQuery(sql.join(" "), params);
  }

  /**
   * 订单列表 分页
   * @param {*} params 
   */
  async findAll(params){
    let sql = [];
    sql.push(`SELECT * FROM o_order WHERE 1=1 `);
    this.setOrderCount(sql,params);
    sql.push(`limit ${params.startRow} ,${params.pageSize}`);
    return await this.customQuery(sql.join(" "), params);

  }

  /**
   * 订单管理列表查询条件参数设置
   * @param {*} params 
   */
  async setOrderCount(sql,params){
    if(params.id){
      sql.push(`AND id = :id `);
    }
    if(params.progress){
      sql.push(`AND progress = :progress `);
    }
    if(params.deliver_id){
      sql.push(`AND deliver_id = :deliver_id `);
    }
    if (params.begin) {
      sql.push(`AND created_at >= :begin `);
    }
    if (params.end) {
      sql.push(`AND created_at <= :end`);
    }
    
  }

  async findMapByIds(ids, attrs) {
    var result = {};
    if (!ids || ids.length == 0) {
      return result;
    }
    attrs = attrs || "*";
    var sql = "SELECT " + attrs + " FROM `o_order` WHERE id IN (:ids)";

    var list = await this.customQuery(sql, {
      ids: ids
    });

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

    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }

  /**
   * 平台数据概览
   * @param {*} begin 
   * @param {*} end 
   */
  async statOrderByTime(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 `o_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 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 `o_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;
  }

  /**
   * 计数
   * @param params
   * @returns {Promise<void>}
   */
  async aliCountOrder(params){
    var result = {};
    var sql = [];
    sql.push("SELECT");
    sql.push("COUNT(1) AS count");
    sql.push("FROM `o_order` t1 inner join `o_order_deliver` t2 on t1.deliver_id = t2.id");
    sql.push("WHERE 1 = 1");
    this.aliSetParams(sql,params);
    // sql.push("ORDER BY t1.created_at DESC");
    // sql.push("LIMIT :startRow, :pageSize");

    let list = await this.customQuery(sql.join(" "), params);
    if(!list || list.length==0){
      return {count:0};
    }
    return list[0];
  }

  /**
   * ali全部订单设置参数
   * @param params
   */
  aliSetParams(sql,params){
    if(params.source_id){
      sql.push(`AND t1.source_id = :source_id`);
    }
    if(params.deliver_id){
      sql.push(`AND t2.deliver_id = :deliver_id`);
    }
    if (params.id) {
      sql.push("AND t1.id = :id");
    }
    if (params.status) {
      sql.push("AND t1.status = :status");
    }
    if (params.statusArray && params.statusArray.length >=1) {
      sql.push("AND t1.status in (:statusArray)");
    }
    if (params.createdBegin) {
      sql.push("AND t1.created_at >= :createdBegin");
    }
    if (params.createdEnd) {
      sql.push("AND t1.created_at <= :createdEnd");
    }
    if (params.delier_user_id) {
      sql.push("AND t2.deliver_user_id = :deliver_user_id");
    }
    if (params.deliver_user_path) {
      params.deliver_user_path_like = params.deliver_user_path + "%";
      sql.push("AND t2.deliver_user_path like :deliver_user_path_like");
    }
    if (params.saas_id) {
      sql.push("AND t1.saas_id = :saas_id");
    }
  }

  /**
   * ali 全部订单
   * @param params
   * @returns {Promise<void>}
   */
  async aliOrderPageAll(params){
    var result = {};
    var sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM `o_order` t1 inner join `o_order_deliver` t2 on t1.deliver_id = t2.id");
    sql.push("WHERE 1 = 1");
    this.aliSetParams(sql,params);
    sql.push("ORDER BY t1.created_at DESC");
    sql.push("LIMIT :startRow, :pageSize");
    let list = await this.customQuery(sql.join(" "), params);
    if(!list || list.length==0){
      return {count:0};
    }
    return list;
  }

}
module.exports = OorderDao;