const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
const moment = require("moment");

class IborderbaseService extends ServiceBase {
  constructor() {
    super("order", ServiceBase.getDaoName(IborderbaseService));
    this.iborderSve = system.getObject("service.order.iborderSve");
    this.iborderdkSve = system.getObject("service.order.iborderdkSve");
    this.iborderdzSve = system.getObject("service.order.iborderdzSve");
    this.businessmenSve = system.getObject("service.business.businessmenSve");
    this.channelSve = system.getObject("service.order.channelSve");
    this.paymentSve = system.getObject("service.order.paymentSve");
    this.services = {
      "1000": this.iborderSve,   // 注册订单
      "1010": this.iborderdzSve, // 代开订单
      "1030": this.iborderdkSve, // 代账订单
      "1040": this.iborderSve,   // 增值服务
    }
  }


  getService(productType) {
    return this.services[productType];
  }

  /**
   * 创建基类订单
   * @param {*} params
   */
  async apiCreateOrder(params) {
    this.trimObject(params);
    // if(!params.productType){
    //   return system.getResult(null, `参数错误 产品类型错误`);
    // }
    // if(!params.productItems instanceof Array){
    //   return system.getResult(null, `参数错误 产品项目错误`);
    // }
    // if(!params.channelNo){
    //   return system.getResult(null, `参数错误 渠道名称错误`);
    // }
    if (!params.thirdNo) {
      return system.getResult(null, `请传入订单id`);
    }
    // if(!Number(params.price)<0){
    //   return system.getResult(null, `参数错误 订单价格错误`);
    // }
    // if(!params.payType){
    //   return system.getResult(null, `参数错误 支付方式错误`);
    // }
    // if(!params.legalName){
    //   return system.getResult(null, `参数错误 联系人不能为空`);
    // }
    // if(!params.legalMobile){
    //   return system.getResult(null, `参数错误 联系电话不能为空`);
    // }
    // if(!params.invoiceType){
    //   return system.getResult(null, `参数错误 发票类型错误`);
    // }

    let productType = this.trim(params.productTypes);
    let childSve = this.getService(productType);
    if (!childSve) {
      return system.getResult(null, `暂不支持此种产品类型：${productType}`);
    }

    try {
      let channel = this.channelSve.findOne({channelNo: params.channelNo});
      if (!channel) {
        return system.getResult(null, `渠道${params.channelNo}不存在`);
      }

      params.channelNo = this.trim(params.channelNo);
      params.thirdNo = this.trim(params.thirdNo);
      params.notes = this.trim(params.notes);
      params.legalName = this.trim(params.legalName);
      params.legalMobile = this.trim(params.legalMobile);
      params.legalEmail = this.trim(params.legalEmail);
      params.payType = Number(params.payType || 1);
      params.productType = productType;
      params.price = Number(params.price || 0);
      params.status = "1000"; // 待处理
      params.payStatus = "10"; // 支付状态
      params.channelOrderNo = params.channelNo;

      let self = this;
      //1 保存基类的信息
      await this.db.transaction(async t => {
        //创建基类表
        let _baseOrder = await self.create(params, t);

        if (productType == 1010 && params.payType == 1) {
          // 代开订单按次支付的单子, 走异步支付流程
        } else {
          var payment = {
            order_id: _baseOrder.id,
            price: params.price,
            channelName: channel.channelName,
            companyName: "",
            payType: params.payType,
            payStatus: "10",
          };
          payment = await this.paymentSve.create(payment);
          params.orderpay_id = payment.id;
        }
        // 将id赋值
        params.id = _baseOrder.id;

        // 创建订单
        await childSve.createOrder(params, t);
      });
      return system.getResultSuccess();
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 完善信息
   * @param {*} params
   *   legalName
   *   legalMobile
   *   names
   *   creditCode
   *   isBank
   *   businessScope
   */
  async completedOrder(params) {
    try {
      let childSve =this.getService(); 
      return await childSve.completedOrder(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 订单办理
   * @param {} params
   */
  async apiHandling(params) {
    //1 检查个体户参数
    //2 检查是否支付
  }

  /**
   * 更新base表
   * @param {*} params
   */
  async completedOrderInfo(params, t) {
    try {
      return await this.dao.update(params, t);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


  async apiAdd(params) {
    try {
      let sve = this.getService(params.productType);
      if (!sve) {
        return system.getResult(null, `产品类型错误${params.productType}`);
      }
      return sve.addOrder(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, "接口异常");
    }
  }

  async apiSaveInfo() {
    try {
      let sve = this.getService(params.productType);
      if (!sve) {
        return system.getResult(null, `产品类型错误${params.productType}`);
      }
      return sve.saveInfo(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, "接口异常");
    }
  }
}

module.exports = IborderbaseService;