var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const logCtl = system.getObject("web.common.oplogCtl");
const uuidv4 = require('uuid/v4');
var svgCaptcha = require('svg-captcha');

var cacheBaseComp = null;
class MerchantCtl extends CtlBase {
  constructor() {
    super("merchant", CtlBase.getServiceName(MerchantCtl));

    this.redisClient = system.getObject("util.redisClient");

    this.merchantsignedSve = system.getObject("service.merchant.merchantsignedSve");
    this.merchantaccountSve = system.getObject("service.merchant.merchantaccountSve");
    this.merchantrechargeSve = system.getObject("service.merchant.merchantrechargeSve");

  }

  async testId(pobj, pobj2, req) {
    this.service.create({
      name: "tesxtid"
    });
  }

  async nameList(pobj, pobj2, req) {
    try {
      var nameList = await this.service.nameList();
      return system.getResultSuccess(nameList);
    } catch (e) {
      console.log(e);
      return system.getResultFail(500, "接口错误");
    }
  }

  async allList(pobj, pobj2, req) {
    return await this.queryList(pobj || {});
  }

  async queryList(condition) {
    var condition = condition || {};
    this.doTimeCondition(condition, ["createdBegin", "createdEnd"]);
    try {
      var page = await this.service.pageByCondition(condition);
      return system.getResultSuccess(page);
    } catch (e) {
      console.log(e);
      return system.getResultFail(500, "接口错误");
    }
  }

  async saveMerchant(pobj, pobj2, req) {
    var merchant;
    var merchantName = this.trim(pobj.name);
    if(!merchantName) {
      return system.getResult(null, "商户名称不能为空");
    }
    var id = Number(pobj.id);
    if (id) {
      merchant = await this.service.findById(id)
      if (merchant.name != merchantName) {
        var nameMerchant = await this.service.findOne({
          name: merchantName
        });
        if (nameMerchant) {
          return system.getResult(null, "商户名称【" + merchantName + "】已存在");
        }
      }
    } else {
      var nameMerchant = await this.service.findOne({
        name: merchantName
      });
      if (nameMerchant) {
        return system.getResult(null, "商户名称【" + merchantName + "】已存在");
      }
      merchant = {};
      merchant.status = 0;
      merchant.appSecret = system.getUid(32);
    }

    merchant.name = merchantName;
    merchant.taxType = Number(pobj.taxType || 0);
    merchant.contactName = this.trim(pobj.contactName);
    merchant.contactMobile = this.trim(pobj.contactMobile);
    merchant.contactEmail = this.trim(pobj.contactEmail);

    merchant.taxNo = this.trim(pobj.taxNo);
    merchant.invoiceAddr = this.trim(pobj.invoiceAddr);
    merchant.invoiceMobile = this.trim(pobj.invoiceMobile);
    merchant.accountName = this.trim(pobj.accountName);
    merchant.accountPublic = this.trim(pobj.accountPublic);

    merchant.mailAddr = this.trim(pobj.mailAddr);
    merchant.mailTo = this.trim(pobj.mailTo);
    merchant.mailMobile = this.trim(pobj.mailMobile);

    merchant.ownerName = this.trim(pobj.ownerName);
    merchant.ownerMobile = this.trim(pobj.ownerMobile);

    try {
      if (merchant.id) {
        // 判断名称是否重复
        await merchant.save();
      } else {
        merchant = await this.service.create(merchant);
      }

      this.merchantaccountSve.saveNew(merchant.id);

      return system.getResultSuccess(merchant);
    } catch (error) {
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }
  async merchantAudit(pobj, pobj2, req) {
    
    try {
      var id = Number(pobj.id);
      var ucname = this.trim(pobj.ucname);
      if(!ucname) {
        return system.getResult(null, "请输入系统登录帐号");
      }
      var merchant = await this.service.findById(id);
      if(!merchant) {
        return system.getResult(null, "商户不存在");
      }
      merchant.status = 1;
      merchant.ucname = ucname;

      // TODO 创建ucname账户
      await merchant.save();
      return system.getResultSuccess("");

    } catch (error) {
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }
}
module.exports = MerchantCtl;