const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasChannelService extends ServiceBase {
  constructor() {
    super("channel", ServiceBase.getDaoName(SaasChannelService));
  }
  // -----------------------以此间隔，上面为API，下面为service---------------------------------

  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 main = await this.dao.getById(params.id);
    if (!main) {
      return system.getResult(null, "渠道不存在");
    }

    this.handleDate(main, ["created_at"], null, -8);
    return system.getResultSuccess(main);
  }

  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 channel;
    if (id) {
      channel = await this.dao.findById(id);
    } else {
      channel = {};
      channel.saas_id = saas_id;
    }
    channel.name = this.trim(params.name);
    channel.short_name = this.trim(params.short_name);
    channel.credit_code = this.trim(params.credit_code);
    channel.business_lincense_img = this.trim(params.business_lincense_img);
    channel.residence = this.trim(params.residence);
    channel.tax_type = this.trim(params.tax_type);
    channel.business_scope = this.trim(params.business_scope);
    channel.idcard_front = this.trim(params.idcard_front);
    channel.idcard_back = this.trim(params.idcard_back);
    channel.legal_idno = this.trim(params.legal_idno);
    channel.legal_name = this.trim(params.legal_name);
    channel.account_name = this.trim(params.account_name);
    channel.account_bank_name = this.trim(params.account_bank_name);
    channel.account_bank_no = this.trim(params.account_bank_no);
    channel.account_mobile = this.trim(params.account_mobile);
    channel.contact_man = this.trim(params.contact_man);
    channel.contact_mobile = this.trim(params.contact_mobile);
    channel.contact_email = this.trim(params.contact_email);
    channel.contact_addr = this.trim(params.contact_addr);

    channel.term = term;
    if(term_end) {
      channel.term_end = term_end;
    }
    if(term == 1) {
      channel.term_end = null;
    }

    channel.validity = validity;
    if(validity_end) {
      channel.validity_end = validity_end;
    }
    if(validity == 1) {
      channel.validity_end = null;
    }


    if (channel.id) {
      await channel.save();
    } else {
      channel = await this.dao.create(channel);
    }
    return system.getResultSuccess(channel);
  }

  async pageByCondition(params) {
    var currentPage = Number(params.currentPage || 0);
    var pageSize = Number(params.pageSize || 10);

    var where = {
      saas_id: params.saas_id,
    };

    if (params.name) {
      where.name = {
        [this.db.Op.like]: "%" + params.name + "%"
      };
    }

    // this.addWhereTime(where, 'created_at', params.createdBegin, params.createdEnd, true);

    var orderby = [
      ["id", 'desc']
    ];
    var page = await this.getPageList(currentPage, pageSize, where, orderby, null);
    if (page && page.rows) {
      for (var row of page.rows) {
        this.handleDate(row, ["created_at"], null, -8);
      }
    }
    return system.getResultSuccess(page);
  }

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