const system = require("../../../system");
const Dao = require("../../dao.base");

class CclueDao extends Dao {
  constructor() {
    super(Dao.getModelName(CclueDao));
  }

  /**
   * 线索列表统计
   * @param params
   * @returns {Promise<*>}
   */
  async ordersCount(params){
    let sql = [];
    sql.push('SELECT COUNT(1) AS count FROM c_clue WHERE 1 = 1 ');
    this.setClueCount(sql,params);
    return await this.customQuery(sql.join(" "), params);
  }

  setClueCount(sql,params){
    if(params.id){
      sql.push(`and id=:id`);
    }
    if(params.deliver_id){
      sql.push(`and deliver_id=:deliver_id`);
    }
    if(params.status){
      sql.push(`and status=:status`);
    }
    if(params.status_arr && params.status_arr.length>0){
      sql.push(`and status in (:status_arr)`);
    }
    if (params.createdBegin) {
      sql.push("AND created_at >= :createdBegin");
    }
    if (params.createdEnd) {
      sql.push("AND created_at <= :createdEnd");
    }
    if (params.operator_id) {
      sql.push("AND operator_path = :operator_id");
    }
    if (params.operator_path) {
      params.operator_path_alias = params.operator_path + "%";
      sql.push("AND operator_path LIKE :operator_path_alias");
    }
  }
  /**
   * fn:线索列表
   * @param params
   * @returns {Promise<*|{count: number}>}
   */
  async listClue(params){
    var sql = [];
    sql.push("SELECT * FROM `c_clue` WHERE 1 = 1");
    this.setClueCount(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;
  }

  /**
   * fn:查询明细
   * @param params
   * @returns {Promise<*|{count: number}>}
   */
  async getCclueInfo(params){
    var sql = [];
    sql.push("SELECT * FROM `c_clue` WHERE 1 = 1");
    this.setClueCount(sql,params);
    return await this.customQuery(sql.join(" "), params) || {};
  }
}

module.exports = CclueDao;
