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

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

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

  /**
   * 订单管理列表查询条件参数设置
   * @param {*} params
   */
  async setsaasorderbusinessmenCountParams(sql,params){
    if(params.id){
      sql.push(`AND id = :id `);
    }
    if(params.credit_code){
      sql.push(`AND credit_code = :credit_code `);
    }
    if(params.name){
      sql.push(`AND name = :name `);
    }
    if (params.legal_idcard) {
      sql.push(`AND legal_idcard = :legal_idcard `);
    }
    if (params.merchant_id) {
      sql.push(`AND merchant_id = :merchant_id `);
    }
    if (params.merchant_app_user_id) {
      sql.push(`AND merchant_app_user_id = :merchant_app_user_id `);
    }
    if (params.merchant_id) {
      sql.push(`AND merchant_id = :merchant_id `);
    }
    if (params.merchant_id) {
      sql.push(`AND merchant_id = :merchant_id `);
    }
    if (params.merchant_ids) {
      sql.push(`AND merchant_id in (:merchant_ids) `);
    }
    if (params.saas_id) {
      sql.push(`AND saas_id = :saas_id `);
    }

  }


  /**
   * saas商户平台查询个体户列表
   * @param params
   * @returns {Promise<void>}
   */
  async saasorderbusinessmenPage(params){
    let sql = [];
    sql.push('SELECT t1.* FROM saas_order_businessmen t1 WHERE 1 = 1 ');
    this.setsaasorderbusinessmenCountParams(sql,params);
    sql.push("ORDER BY t1.id DESC");
    sql.push(`limit ${params.startRow} ,${params.pageSize}`);
    return await this.customQuery(sql.join(" "), params);
  }

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


}
module.exports = SaasorderbusinessmenDao;
