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

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

  async countByCondition(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("COUNT(1) AS num");
    sql.push("FROM `o_businessmen` t1");
    sql.push("WHERE 1 = 1");

    this.setCondition(sql, params);

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

  async listByCondition(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM `o_businessmen` t1");
    sql.push("WHERE 1 = 1");

    this.setCondition(sql, params);

    sql.push("ORDER BY t1.created_at DESC");
    sql.push("LIMIT :startRow, :pageSize");

    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(sql, params) {
    if (params.saas_id) {
      sql.push("AND t1.saas_id = :saas_id");
    }
    if (params.order_id) {
      sql.push("AND t1.order_id = :order_id");
    }
    if (params.orderIds && params.orderIds.length > 0) {
      sql.push("AND t1.order_id IN (:orderIds)");
    }
    if (params.name) {
      params.name_like = "%" + params.name + "%";
      sql.push("AND t1.name LIKE :name_like");
    }
    if (params.bd_id) {
      sql.push("AND t1.bd_id = :bd_id");
    }
    if (params.bd_path) {
      params.bd_path_like = params.bd_path + "%";
      sql.push("AND t1.bd_path LIKE :bd_path_like");
    }
    if (params.order_id) {
      sql.push("AND t1.order_id = :order_id");
    }
    if (params.legal_name) {
      sql.push("AND t1.legal_name = :legal_name");
    }
    if (params.legal_mobile) {
      sql.push("AND t1.legal_mobile = :legal_mobile");
    }
    if (params.legal_idcard) {
      sql.push("AND t1.legal_idcard = :legal_idcard");
    }
    if (params.credit_code) {
      sql.push("AND t1.credit_code = :credit_code");
    }

    if (params.hasOwnProperty("isSign")) {
      if (params.isSign) {
        sql.push("AND t1.sign_time IS NOT NULL");
      } else {
        sql.push("AND t1.sign_time IS NULL");
      }
    }
  }

  /**
   * 通过统一社会信用代码
   * @param {*} CreditCode 
   */
  async findByCreditCode(credit_code,saas_id){
    let sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM `o_businessmen` t1");
    sql.push("WHERE t1.credit_code = :credit_code");
    if(saas_id){
      sql.push("WHERE t1.saas_id = :saas_id")
    }
    return await this.customQuery(sql.join(" "), {credit_code:credit_code});
  }

  async mapByCreditCodes(creditCodes, attrs) {
    let result = {};
    if (!creditCodes || creditCodes.length == 0) {
      return result;
    }
    var sql = [];
    sql.push("SELECT");
    sql.push(attrs || "*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE credit_code IN (:creditCodes)");
    let list = await this.customQuery(sql.join(" "), {creditCodes: creditCodes}) || [];
    if (list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.credit_code] = item;
    }
    return result;
  }

  /**
   * 提送发票
   * @param {*} id 
   */
  async queryObusinessmenInfo(params){
    let sql = [`SELECT t1.source_no,t1.saas_deliver_api ,t1.product_id,t1.merchant_id,t2.* FROM o_order t1 INNER JOIN o_businessmen t2 ON t1.id = t2.order_id WHERE 1=1 `];
    if(params.id){
      sql.push(` and t1.id = :id`);
    }
    if(params.saas_id){
      sql.push(` and t2.saas_id = :saas_id`);
      sql.push(` and t1.saas_id = :saas_id`);
    }
    let list = await this.customQuery(sql.join(" "), params) || [];
    if (list.length == 0) {
      return {};
    }
    return list[0];
  }


  /**
   * 根据order_id 查个体户信息
   * @param ids
   * @returns {Promise<void>}
   */
  async getByOrderIds(ids){
    if(!ids || ids.length == 0){
      return {};
    }
    let sql = `select * from  o_businessmen where 1=1 and order_id in (:ids)`;
    let list = await this.customQuery(sql, {ids:ids});
    let map = {};
    for(let item of list) {
      map[item.order_id] = item;
    }
    return map;
  }
}

module.exports = ObusinessmenDao;