var system = require("../../../system")
const CtlBase = require("../../ctlms.base");
class ChannelCtl extends CtlBase {
  constructor() {
    super();
    this.channelSve = system.getObject("service.saas.channelSve");
    this.saasbusinessSve = system.getObject("service.saas.saasbusinessSve");
    this.businessscopeSve = system.getObject("service.common.businessscopeSve");
    this.platformuserSve = system.getObject("service.uc.platformuserSve");
  }

  async dics(params, pobj2, req) {
    try {
      return await this.channelSve.dics(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async info(params, pobj2, req) {
    try {
      return await this.channelSve.info(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async page(params, pobj2, req) {
    try {
      return await this.channelSve.page(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


  async save(params, pobj2, req) {
    try {
      let check = this.checkSaveParams(params);
      if (check.status !== 0) {
        return check;
      }
      let id = params.id;
      if (!id) {
        // 1. 创建saas记录
        let saasrs = await this.saasbusinessSve.openSaasBusiness({
          "companyName": params.name,
          "domain": "ss-platform"
        });
        let saas_id = saasrs.data.id;
        if (!saas_id) {
          return system.getResult(null, `渠道[${params.name}]saas信息创建失败`);
        }
        // 2. 经营范围创建
        await this.businessscopeSve.setDefaultBusinessScope({saas_id: saas_id});
        params.saas_id = saas_id;
      }
      // 3. 渠道增加
      return await this.channelSve.save(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  checkSaveParams(params) {
    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 (!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 (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, "请填写联系人地址");
    }
    return system.getResultSuccess();
  }

  async userSave(params, pobj2, req) {
    try {
      let channel_id = params.channel_id;
      if (!channel_id) {
        return system.getResult("请选择渠道");
      }
      let channel = await this.channelSve.info({id: params.channel_id}) || {};
      if (channel.status !== 0 || !channel.data || !channel.data.id) {
        return system.getResult(null, `所选渠道不存在`)
      }
      channel = channel.data;
      params.saas_id = channel.saas_id;
      return await this.platformuserSve.save(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async userInfo(params, pobj2, req) {
    try {
      let rs = await this.platformuserSve.info(params);
      await this.channelSve.setChannel([rs.data]);
      return system.getResultSuccess(rs);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async userPage(params, pobj2, req) {
    try {
      let rs = await this.platformuserSve.page(params);
      await this.channelSve.setChannel(rs.data.rows);
      return rs;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

}
module.exports = ChannelCtl;