const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")

class SaasorderbusinessmenService extends ServiceBase {
  constructor() {
    super("saas", ServiceBase.getDaoName(SaasorderbusinessmenService));

    this.saasorderbminfoDao = system.getObject("db.saas.saasorderbminfoDao");
    this.saasorderpayDao = system.getObject("db.saas.saasorderpayDao");
    this.saasorderdeliverinfoDao = system.getObject("db.saas.saasorderdeliverinfoDao");
    this.redisClient = system.getObject("util.redisClient");
  }

  /**
   * 查询个体户列表
   * @param params
   * @returns {Promise<{msg: *, data, bizmsg: *|string, status: number}>}
   */
  async saasorderbusinessmenPage(params) {
    let where = {};
    if (params.credit_code) {
      where.credit_code = this.trim(params.credit_code);
    }
    if (params.name) {
      where.name = this.trim(params.name);
    }
    if (params.merchant_app_user_id) {
      where.merchant_app_user_id = this.trim(params.merchant_app_user_id);
    }
    if (params.legal_idcard) {
      where.legal_idcard = this.trim(params.legal_idcard);
    }
    params.currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);
    params.startRow = (params.currentPage - 1) * params.pageSize;

    try {
      let countRes = await this.dao.saasorderbusinessmenCount(params);
      if (countRes.count == 0) {
        return system.getResult({count: 0, rows: []});
      }
      let res = await this.dao.saasorderbusinessmenPage(params);

      for (let item of res) {
        this.handleDate(item, ["created_at", "reg_date", "service_begin_time", "service_end_time", "sign_time","tax_reg_day"], "YYYY-MM-DD HH:mm:ss");
        item.is_bank_name = item.is_bank ? "已开户" : "未开户";
        item.common_tax_ladder = JSON.parse(item.common_tax_ladder);
        item.common_other_ladder = JSON.parse(item.common_other_ladder);
        item.special_tax_ladder = JSON.parse(item.special_tax_ladder);
        item.special_other_ladder = JSON.parse(item.special_other_ladder);
      }
      return system.getResult({count: countRes.count, rows: res});
    } catch (e) {
      console.log(e);
      return system.getResult(nul, `系统错误`);
    }
  }

  /**
   * 获取个体户信息
   * @param params
   * @returns {Promise<{msg: string, data: (*|null), bizmsg: string, status: number}>}
   */
  async assorderBusinessmenInfo(params){
    try {
      let where = {};
      if(params.merchant_app_user_id){
        where.merchant_app_user_id = params.merchant_app_user_id;
      }
      if(params.merchant_id){
        where.merchant_id = params.merchant_id;
      }
      if(params.name){
        where.name = params.name;
      }

      let res = await this.dao.model.findAll({
        where:where,
        attributes:params.attrs
      });
      return system.getResult(res || []);
    }catch (e) {
      console.log(e);
      return system.getResult(null, `系统错误`);
    }
  }
}



module.exports = SaasorderbusinessmenService;