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

  async saveByMerchantId(params) {
    let id = this.trim(params.id); // 暂时没鸡巴用，以后可能有用
    let saas_merchant_id = this.trim(params.saas_merchant_id);
    let mail_addr = this.trim(params.mail_addr);
    let mail_mobile = this.trim(params.mail_mobile);
    let mail_to = this.trim(params.mail_to);
    if (!saas_merchant_id) {
      return system.getResult(null, "商户不存在");
    }
    if(!mail_to) {
      return system.getResult(null, `联系人不能为空`);
    }
    if(!mail_mobile) {
      return system.getResult(null, `联系电话不能为空`);
    }
    if(!mail_addr) {
      return system.getResult(null, `邮寄地址不能为空`);
    }
    let addr = await this.dao.findOne({saas_merchant_id: params.saas_merchant_id});
    if(addr) {
      addr.mail_addr = mail_addr;
      addr.mail_mobile = mail_mobile;
      addr.mail_to = mail_to;
      await addr.save();
    } else {
      addr = await this.dao.create({
        saas_merchant_id: saas_merchant_id,
        mail_addr: mail_addr,
        mail_mobile: mail_mobile,
        mail_to: mail_to,
      });
    }
    return system.getResultSuccess(addr);
  }

  async byMerchantId(params) {
    let list = await this.dao.getAllByMerchantId(params.saas_merchant_id) || [];

    for (let row of list) {
      this.handleDate(row, ["created_at"], null, -8);
    }
    return system.getResultSuccess(list);
  }

  /**
   * 发票信息管理列表
   * @param {*} params 
   */
  async invoiceInfoPage(params){
    try {
      var currentPage = Number(params.currentPage || 1);
      var pageSize = Number(params.pageSize || 10);
      let where={};
      if(params.merchant_name){
        where = {
          merchant_name:this.trim(params.merchant_name)
        };
      }
      var orderby = [
        ["id", 'desc']
      ];
      var page = await this.getPageList(currentPage, pageSize, where, orderby);
      if (page && page.rows) {
        for (var row of page.rows) {
          this.handleDate(row, ["created_at"], null, -8);
        }
      }
      return system.getResultSuccess(page);
    } catch (error) {
      console.log(error);
      return system.getResult(null,`系统错误`);
    }
  }
  
  /**
   * 发票信息管理修改
   * @param {*} params 
   */
  async updateinvoiceInfo(params){
    if(!params.id){
      return system.getResult(null,`参数错误 ID不能为空`);
    }
    let _saasInvoice = this.dao.getById(this.trim(params.id));
    if(!_saasInvoice){
      return system.getResult(null, `信息不存在，请核对信息ID`);
    }

    // merchant_name: { type: DataTypes.STRING(45), allowNull: false, COMMENT: '商户名称' },
    // merchant_credit_code: { type: DataTypes.STRING(45), allowNull: false, COMMENT: '商户纳税人识别号' },
    // merchant_tax_type: { type: DataTypes.STRING(4), allowNull: false, COMMENT: '纳税人类型 00一般纳税人 10小规模纳税人' },
    // merchant_addr: { type: DataTypes.STRING(45), allowNull: false, COMMENT: '商户地址' },
    // merchant_mobile: { type: DataTypes.STRING(45), allowNull: false, COMMENT: '商户电话' },
    // merchant_bank: { type: DataTypes.STRING(4), allowNull: true, defaultValue: "", COMMENT: '商户开户行' },
    // merchant_account: { type: DataTypes.STRING(32), allowNull: false, COMMENT: '商户银行账号' },
    let _invoiceInfoPorperty={};
    if(params.merchant_name){
      _invoiceInfoPorperty.merchant_name = this.trim(params._invoiceInfoPorperty);
    }
    if(params.merchant_credit_code){
      _invoiceInfoPorperty.merchant_credit_code = this.trim(params.merchant_credit_code);
    }
    if(params.merchant_tax_type){
      _invoiceInfoPorperty.merchant_tax_type = this.trim(params.merchant_tax_type);
    }
    if(params.merchant_addr){
      _invoiceInfoPorperty.merchant_addr = this.trim(params.merchant_addr);
    }
    if(params.merchant_mobile){
      _invoiceInfoPorperty.merchant_mobile = this.trim(params.merchant_mobile);
    }
    if(params.merchant_bank){
      _invoiceInfoPorperty.merchant_bank = this.trim(params.merchant_bank);
    }
    if(params.merchant_account){
      _invoiceInfoPorperty.merchant_account = this.trim(params.merchant_account);
    }
    



  }
  





}
module.exports = SaasmerchantAddrService;