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 MerchantsignedCtl extends CtlBase {
  constructor() {
    super("merchant", CtlBase.getServiceName(MerchantsignedCtl));

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

    this.merchantSve = system.getObject("service.merchant.merchantSve");
  }

  async merchantSign(pobj, pobj2, req) {
    try {
      var merchantId = Number(pobj.merchant_id);
      var merchant = await this.merchantSve.findById(merchantId);
      if(!merchant) {
        return system.getResult(null, "商户不存在");
      }

      if(merchant.status == 0) {
        return system.getResult(null, "当前状态为【待审核】, 不允许签约");
      }

      var signObj = {
        merchant_id : merchantId,
        nameA : this.trim(pobj.nameA),
        contractNo : this.trim(pobj.contractNo),
        beginDate : this.trim(pobj.beginDate),
        endDate : this.trim(pobj.endDate),
        serviceRate : this.trim(pobj.serviceRate),
      }

      var msign = await this.service.findOne({merchant_id: merchantId});
      if(!msign) {
        msign = {};
      }
      msign.merchant_id = merchantId;
      msign.nameA = this.trim(pobj.nameA);
      msign.contractNo = this.trim(pobj.contractNo);
      msign.beginDate = this.trim(pobj.beginDate);
      msign.endDate = this.trim(pobj.endDate);
      msign.serviceRate = Number(pobj.serviceRate);

      if(msign.id) {
        await msign.save();
      } else {
        msign = this.service.create(msign);
      }

      merchant.status = 2;
      await merchant.save();
      return system.getResultSuccess("");
    } catch (error) {
      return system.getResultFail(500, "接口异常：" + error.message);
    }

  }

  async showSign(pobj, pobj2, req) {
    try {
      var merchantId = Number(pobj.merchant_id);
      var merchant = await this.merchantSve.getById(merchantId);
      var msign = await this.service.getByMerchantId(merchantId);

      return system.getResultSuccess({
        merchant: merchant,
        merchantSign: msign
      });
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }

  async signList(pobj, pobj2, req) {
    try {
      var condition = {
        currentPage: pobj.currentPage,
        pageSize: pobj.pageSize,
        merchantName: this.trim(pobj.merchantName),
        merchantId: pobj.merchantId,
      }
      var page = await this.service.pageByCondition(condition);
      return system.getResultSuccess(page);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }



  // try {
  // } catch (error) {
  //   return system.getResultFail(500, "接口异常：" + error.message);
  // }


}
module.exports = MerchantsignedCtl;