const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasMerchantService extends ServiceBase {
  constructor() {
    super("merchant", ServiceBase.getDaoName(SaasMerchantService));

    this.saasmerchantsignDao = system.getObject("db.merchant.saasmerchantsignDao");
  }
  // -----------------------以此间隔，上面为API，下面为service---------------------------------

  async mapByIds(params) {
    if(!params.ids || params.ids.length == 0) {
      return system.getResultSuccess({});
    }
    let rs = this.dao.mapByIds(params.ids);
    return system.getResultSuccess(rs);
  }

  async dics(params) {
    let saas_id = Number(this.trim(params.saas_id) || 0);
    if (!saas_id) {
      return system.getResultSuccess([]);
    }
    let list = await this.dao.bySaasId(Number(params.saas_id), "id, name, short_name");
    return system.getResultSuccess(list);
  }

  async info(params) {
    if (!params.id) {
      return system.getResult(null, "id不存在");
    }

    let merchant = await this.dao.getById(params.id);
    if (!merchant) {
      return system.getResult(null, "商户不存在");
    }

    this.handleDate(merchant, ["created_at"], null, -8);
    let sign = await this.saasmerchantsignDao.getById(params.id);
    this.handleDate(sign, ["created_at"], null, -8);
    this.handleDate(sign, ["begin_date", "end_date"], "YYYY-MM-DD", -8);
    merchant.sign = sign;
    return system.getResultSuccess(merchant);
  }

  async signInfo(params) {
    if (!params.id) {
      return system.getResult(null, "id不存在");
    }

    let sign = await this.saasmerchantsignDao.getById(params.id);
    if (!sign) {
      return system.getResult(null, "商户不存在");
    }

    this.handleDate(sign, ["created_at"], null, -8);
    this.handleDate(sign, ["begin_date", "end_date"], "YYYY-MM-DD", -8);
    return system.getResultSuccess(sign);
  }

  async save(params) {
    let id = Number(params.id || 0);
    let saas_id = Number(params.saas_id || 0);

    let term = Number(params.term || 1);
    let term_end = this.trim(params.term_end);

    let validity = Number(params.validity || 1);
    let validity_end = this.trim(params.validity_end);

    if (!saas_id) {
      return getResult(null, "saas不存在");
    }

    if (!this.trim(params.name)) {
      return system.getResult(null, "请填写渠道名称");
    }
    if (!this.trim(params.short_name)) {
      return system.getResult(null, "请填写渠道简称");
    }
    if (!this.trim(params.credit_code)) {
      return system.getResult(null, "请填写统一社会信用代码");
    }
    // if(!this.trim(params.business_lincense_img)) {return system.getResult(null, ""); }   // 营业执照暂不验证
    if (!this.trim(params.tax_type)) {
      return system.getResult(null, "请选择纳税人类型");
    }
    if (!this.trim(params.residence)) {
      return system.getResult(null, "请填写住所");
    }
    if (!this.trim(params.business_scope)) {
      return system.getResult(null, "请填写经营范围");
    }
    if (term == 2 && !term_end) {
      return system.getResult(null, "请填写营业期限固定日期");
    }

    // if(!this.trim(params.idcard_front)) {return system.getResult(null, "idcard_front为空"); } 
    // if(!this.trim(params.idcard_back)) {return system.getResult(null, "idcard_back为空"); } 
    if (!this.trim(params.legal_name)) {
      return system.getResult(null, "请填写法人姓名");
    }
    if (!this.trim(params.legal_idno)) {
      return system.getResult(null, "请填写法人身份证");
    }
    if (validity == 2 && !validity_end) {
      return system.getResult(null, "请填写有效期限固定日期");
    }

    if (!this.trim(params.account_name)) {
      return system.getResult(null, "请填写账户名称");
    }
    if (!this.trim(params.account_bank_name)) {
      return system.getResult(null, "请填写开户行");
    }
    if (!this.trim(params.account_bank_no)) {
      return system.getResult(null, "请填写开户账号");
    }
    if (!this.trim(params.account_mobile)) {
      return system.getResult(null, "请填写联系电话");
    }

    if (!this.trim(params.contact_man)) {
      return system.getResult(null, "请填写联系人姓名");
    }
    if (!this.trim(params.contact_mobile)) {
      return system.getResult(null, "请填写联系人电话");
    }
    if (!this.trim(params.contact_email)) {
      return system.getResult(null, "请填写联系人邮箱");
    }
    if (!this.trim(params.contact_addr)) {
      return system.getResult(null, "请填写联系人地址");
    }

    let merchant;
    if (id) {
      merchant = await this.dao.findById(id);
    } else {
      merchant = {};
      merchant.saas_id = saas_id;
      merchant.channel_id = this.trim(params.channel_id);
    }
    merchant.name = this.trim(params.name);
    merchant.short_name = this.trim(params.short_name);
    merchant.credit_code = this.trim(params.credit_code);
    merchant.business_lincense_img = this.trim(params.business_lincense_img);
    merchant.residence = this.trim(params.residence);
    merchant.tax_type = this.trim(params.tax_type);
    merchant.business_scope = this.trim(params.business_scope);
    merchant.idcard_front = this.trim(params.idcard_front);
    merchant.idcard_back = this.trim(params.idcard_back);
    merchant.legal_idno = this.trim(params.legal_idno);
    merchant.legal_name = this.trim(params.legal_name);
    merchant.account_name = this.trim(params.account_name);
    merchant.account_bank_name = this.trim(params.account_bank_name);
    merchant.account_bank_no = this.trim(params.account_bank_no);
    merchant.account_mobile = this.trim(params.account_mobile);
    merchant.contact_man = this.trim(params.contact_man);
    merchant.contact_mobile = this.trim(params.contact_mobile);
    merchant.contact_email = this.trim(params.contact_email);
    merchant.contact_addr = this.trim(params.contact_addr);

    merchant.term = term;
    merchant.term_end = term_end;

    merchant.validity = validity;
    merchant.validity_end = validity_end;

    let self = this;
    if (merchant.id) {
      await merchant.save();
    } else {
      merchant = await self.db.transaction(async function (t) {
        // 创建商户
        merchant = await self.dao.create(merchant, t);
        await self.saasmerchantsignDao.create({
          id: merchant.id
        }, t);
        return merchant;
      });
    }
    return system.getResultSuccess(merchant);
  }

  async pageByCondition(params) {
    let currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);
    params.startRow = (currentPage - 1) * params.pageSize;

    var page = { count: 0, rows: [] };

    page.count = await this.dao.countByCondition(params);
    if (page.count == 0) {
      return page;
    }

    page.rows = await this.dao.listByCondition(params) || [];
    if (page.rows) {
      for (let item of page.rows) {
        this.handleDate(item, ["created_at"], null, -8);
        this.handleDate(item, ["begin_date", "end_date"], "YYYY-MM-DD", -8);
      }
    }
    return system.getResultSuccess(page);
  }

  async sign(params) {
    if (!params.id) {
      return system.getResult(null, "id不存在");
    }

    if (!this.trim(params.main_id)) {
      return system.getResult(null, "请选择签约主体");
    }
    if (!this.trim(params.begin_date)) {
      return system.getResult(null, "请选择签约开始时间");
    }
    if (!this.trim(params.end_date)) {
      return system.getResult(null, "请选择签约失效时间");
    }
    let merchant = await this.dao.findById(params.id);
    let sign = await this.saasmerchantsignDao.findById(params.id);
    sign.main_id = Number(this.trim(params.main_id) || 0);
    sign.begin_date = this.trim(params.begin_date);
    sign.end_date = this.trim(params.end_date);
    sign.bm_reg_price = Number(params.bm_reg_price || 0);
    sign.invoice_service_rate = Number(params.invoice_service_rate || 0);
    sign.trans_service_rate = Number(params.trans_service_rate || 0);

    await sign.save();
    merchant.is_sign = true;
    await merchant.save();
    return system.getResultSuccess();
  }
}
module.exports = SaasMerchantService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });