var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctlms.base");

var cacheBaseComp = null;
class OrderCtl extends CtlBase {
  constructor() {
    super();
    this.redisClient = system.getObject("util.redisClient");

    this.orderSve = system.getObject("service.order.orderSve");
  }

  async add(pobj, pobj2, req) {
    var loginUser = req.loginUser;
    var order = {};

    order.merchantId = "";
    order.channelOrderNo = await this.redisClient.genrateId("channelOrderNo");
    order.names = this.trim(pobj.names);

    order.domicile_id = this.trim(pobj.domicile_id);
    order.domicileName = this.trim(pobj.domicileName);

    order.businessScope_id = this.trim(pobj.businessScope_id);
    order.businessType = this.trim(pobj.businessType);
    order.businessScope = this.trim(pobj.businessScope);

    order.legalName = this.trim(pobj.legalName);
    order.legalMobile = this.trim(pobj.legalMobile);
    order.legalImg = this.trim(pobj.legalImg);
    order.idcard = this.trim(pobj.idcard);
    order.idcardFront = this.trim(pobj.idcardFront);
    order.idcardBack = this.trim(pobj.idcardBack);

    order.userId = this.trim(loginUser.ucid);

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

    // order.bankNo = this.trim(pobj.bankNo);
    // order.bank = this.trim(pobj.bank);
    // order.bankImg = this.trim(pobj.bankImg);
    // order.capital = this.trim(pobj.capital);

    order.price = system.y2f(Number(pobj.price || 2300));
    try {
      return await this.orderSve.add(order);
    } catch (e) {
      console.log(e);
      return system.getResultFail(500, "接口错误");
    }
  }

  async myPage(pobj, pobj2, req) {
    try {
      var condition = {
        currentPage: pobj.currentPage,
        pageSize: pobj.pageSize,
        userId: req.loginUser.ucid,
        createdBegin: pobj.createdBegin,
        createdEnd: pobj.createdEnd,
      }
      this.doTimeCondition(condition, ["createdBegin", "createdEnd"]);
      return await this.orderSve.myPage(condition);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }

  async info(pobj, pobj2, req) {
    try {
      var condition = {
        id: pobj.id,
      }
      return await this.orderSve.info(condition);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }

  async createStep(pobj, pobj2, req) {
    var id = Number(pobj.orderId);
    var status = pobj.status;
    try {
      switch (status) {
        case "05": //"执照办理中"
        case "15": //"刻章办理中" 
        case "25": //"银行开户中" 
        case "35": //"税务报到中" 
          return await this.service.changeStatus(id, status);
        case "10": { //"已出执照" 
          var params = {
            orderId: pobj.orderId,
            name: this.trim(pobj.name), // 个体工商户名称
            creditCode: this.trim(pobj.creditCode), // 统一社会信用代码
            businessPlace: this.trim(pobj.businessPlace), // 经营场所
            businessScope: this.trim(pobj.businessScope), // 经营范围
            regDate: this.trim(pobj.regDate), // 注册日期
            businessImg: this.trim(pobj.businessImg), // 执照照片
          };
          return await this.service.createLicense(pobj);
        }
        case "20": { //"已刻章" 
          var params = {
            orderId: pobj.orderId,
            gongzhang: this.trim(pobj.gongzhang),
            caiwuzhang: this.trim(pobj.caiwuzhang),
            businessImg: this.trim(pobj.businessImg),
            fapiaozhang: this.trim(pobj.fapiaozhang),
            hetongzhang: this.trim(pobj.hetongzhang),
            farenzhang: this.trim(pobj.farenzhang),
          };
          return await this.businessmenSve.putSealInfo(params);
        }
        case "30": { //"银行已开户"
          var params = {
            orderId: pobj.orderId,
            isBank: Number(pobj.isBank) == 1,
            bankName: this.trim(pobj.bankName),
            bankNo: this.trim(pobj.bankNo),
            bank: this.trim(pobj.bank),
            bankImg: this.trim(pobj.bankImg),
          }
          return await this.businessmenSve.putBankInfo(params);
        }
        case "40": { //"已税务报到" 
          var params = {
            orderId: pobj.orderId,
            caImg: this.trim(pobj.caImg),
            taxRegDay: this.trim(pobj.taxRegDay),
            taxOrg: this.trim(pobj.taxOrg),
          }
          return await this.businessmenSve.putTaxInfo(params);
        }
        case "50": { //"已邮寄" 
          var params = {
            orderId: pobj.orderId,
            courierNo: this.trim(pobj.courierNo),
            courierImg: this.trim(pobj.courierImg),
          }
          return await this.businessmenSve.putExpressInfo(params);
        }
        default: {
          return system.getResult(null, "状态错误");
        }
      }
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }
}
module.exports = OrderCtl;