const system = require("../../../system");
const ServiceBase = require("../../svems.base")
const settings = require("../../../../config/settings")
class MerchantService extends ServiceBase {
  constructor() {
    super();
  }

  async nameList() {
    var rs = await this.callms("merchant", "nameList", {});
    return rs;
  }

  async infoPage(params) {
    var rs = await this.callms("merchant", "infoList", params);
    return rs;
  }

  async add(params) {
    var merchant = {};
    var title = {};
    var address = {};

    if(["00", "10"].indexOf(params.taxType) == -1) {
      return system.getResult(null, "纳税人类型类型错误");
    }

    // 商户信息
    merchant.name = this.trim(params.name);
    merchant.taxType = params.taxType;
    merchant.contactName = this.trim(params.contactName);
    merchant.contactMobile = this.trim(params.contactMobile);
    merchant.contactEmail = this.trim(params.contactEmail);
    merchant.invoiceValid = params.invoiceValid ? 1 : 0;
    merchant.mustPay = params.mustPay ? 1 : 0;
    merchant.status = "00";

    merchant.ownerId = this.trim(params.ownerId);
    if(merchant.ownerId) {
      // TODO 去用户平台查询用户信息
      merchant.ownerName = "测试用户名";
      merchant.ownerMobile = "测试用户手机号";
      merchant.ownerPath = "/aaa/bbbb/ccc/ddd/eee/fff/zzz/yyy";
    }

    title.taxNo = this.trim(params.taxNo);
    title.invoiceAddr = this.trim(params.invoiceAddr);
    title.invoiceMobile = this.trim(params.invoiceMobile);
    title.accountName = this.trim(params.accountName);
    title.accountPublic = this.trim(params.accountPublic);

    address.mailAddr = this.trim(params.mailAddr);
    address.mailTo = this.trim(params.mailTo);
    address.mailMobile = this.trim(params.mailMobile);

    merchant.title = title;
    merchant.address = address;

    return await this.callms("merchant", "addMerchant", merchant);
  }

  async upd(params) {
    var merchant = {};
    var title = {};
    var address = {};

    if(["00", "10"].indexOf(params.taxType) == -1) {
      return system.getResult(null, "纳税人类型类型错误");
    }

    // 商户信息
    merchant.id = params.id;
    merchant.name = this.trim(params.name);
    merchant.taxType = params.taxType;
    merchant.contactName = this.trim(params.contactName);
    merchant.contactMobile = this.trim(params.contactMobile);
    merchant.contactEmail = this.trim(params.contactEmail);
    merchant.invoiceValid = params.invoiceValid ? 1 : 0;
    merchant.mustPay = params.mustPay ? 1 : 0;
    merchant.status = "00";

    merchant.ownerId = this.trim(params.ownerId);
    if(merchant.ownerId) {
      // TODO 去用户平台查询用户信息
      merchant.ownerName = "测试用户名";
      merchant.ownerMobile = "测试用户手机号";
      merchant.ownerPath = "/aaa/bbbb/ccc/ddd/eee/fff/zzz/yyy";
    }

    return await this.callms("merchant", "updMerchant", merchant);
  }

  async audit(params) {
    var mobile = this.trim(params.mobile);
    var ucname = this.trim(params.ucname);

    params.status = "10";
    params.ucname = ucname;
    params.ucid = ucname + "_id TODO";
    // TODO 调用平台，创建商户用户
    var auditRs = await this.callms("merchant", "auditMerchant", params);
    return auditRs;
  }
}
module.exports = MerchantService;
