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

  async findMapByOrderIds(orderIds, attrs) {
    var result = [];
    if(!orderIds || orderIds.length == 0) {
      return result;
    }

    attrs = attrs || "*";
    var sql = "SELECT " + attrs + " FROM bm_user_biz where orderId IN (:orderIds) ";
    var list = await this.customQuery(sql, {orderIds:orderIds});
    if(!list || list.length == 0) {
      return result;
    }

    for(var item of list) {
      result["id_" + item.orderId] = item;
    }
    return result;
  }

  async updateSupplement(params) {
    let sql = `UPDATE ${this.model.tableName} SET front_half_img = :front_half_img, declaration_file = :declaration_file, updated_at = NOW() WHERE idcard = :idcard`;
    let rs = await this.customUpdate(sql, params);
    return rs;
  }

  async countByCondition(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("count(DISTINCT t1.id) as num");
    sql.push("FROM bm_user_biz t1");
    sql.push("INNER JOIN bm_order t2 ON t1.orderId = t2.id");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(sql, params);

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

  async listByCondition(params) {
    params.startRow = Number(params.startRow || 0);
    params.pageSize = Number(params.pageSize || 10);

    let sql = [];
    sql.push("SELECT");
    sql.push("t1.*, ");
    sql.push("t1.merchantId, t2.companyId, t2.orderNo, t2.audit_status, t2.reject_reason");
    sql.push("FROM bm_user_biz t1");
    sql.push("INNER JOIN bm_order t2 ON t1.orderId = t2.id");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(sql, params);

    sql.push("ORDER BY t1.id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
    if (params.hasOwnProperty("audit_status")) {
      sql.push("AND t2.audit_status = :audit_status");
    }
    if (params.merchantId) {
      sql.push("AND t1.merchantId = :merchantId");
    }
    if (params.legalName) {
      params.legalNameLike = `%${params.legalName}%`;
      sql.push("AND t1.legal_name LIKE :legalNameLike");
    }
    if (params.legal_mobile) {
      sql.push("AND t1.legal_mobile = :legal_mobile");
    }
    if (params.idcard) {
      sql.push("AND t1.idcard = :idcard");
    }
  }

  async findExportAll() {
    let sql = [];
    sql.push("SELECT ");
    sql.push("t1.id,");
    sql.push("t1.legal_name,");
    sql.push("t1.legal_mobile,");
    sql.push("t1.idcard,");
    sql.push("t1.idcard_front,");
    sql.push("t1.idcard_back,");
    sql.push("REPLACE(t1.companyNames, '#com#', '，') AS names,");
    sql.push("t1.bank,");
    sql.push("t1.bankno,");
    sql.push("t1.bank_front,");
    sql.push("t1.bank_back,");
    sql.push("t1.bankMobile,");
    sql.push("t1.`front_half_img`,");
    sql.push("t1.`declaration_file`,");
    sql.push("DATE_FORMAT(t1.created_at, '%Y-%m-%d') AS submit_time,");
    sql.push("CASE t2.audit_status WHEN '0' THEN '待审核' WHEN  '1' THEN '审核通过' WHEN '2' THEN '审核失败' ELSE '' END AS audit_status_name,");
    sql.push("t2.reject_reason");
    sql.push("FROM");
    sql.push("`bm_user_biz` t1");
    sql.push("INNER JOIN `bm_order` t2 ON t1.`orderId` = t2.`id`");
    sql.push("WHERE 1 = 1");
    sql.push("ORDER BY id DESC");

    return await this.customQuery(sql.join(" "));
  }
}
module.exports=BmuserbizDao;
