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

class IborderdkService extends ServiceBase {
  constructor() {
    super("order", ServiceBase.getDaoName(IborderdkService));

    this.baseDao = system.getObject("db.order.iborderbaseDao");
    this.businessmenDao = system.getObject("db.business.businessmenDao");
    this.iborderbaseDao = system.getObject("db.order.iborderbaseDao");
  }

  async addOrder(params) {
  }

  /**
   * 创建订单
   * @param {*} params
   */
  async createOrder(params, t) {
    try {
      if (!params.merchantId) {
        return system.getResult(null, `参数错误 商户ID不能为空`);
      }
      if (!params.businessmentId) {
        return system.getResult(null, `参数错误 个体户ID不能为空`);
      }
      if (!params.orderpayId) {
        return system.getResult(null, `参数错误 支付ID不能为空`);
      }
      if (!Number(params.price) < 0) {
        return system.getResult(null, `参数错误 订单金额格式错误`);
      }
      this.trimObject(params);
      let _ibOrderDk = await this.dao.create(params, t);
      return system.getResult(_ibOrderDk);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 更新数据
   * @param {*} params 
   * @param {*} t 
   */
  async updateOrder(params,t){
    try {
      if(!params.id){
        return system.getResult(null, `参数错误 ID 不能为空`);
      }
      this.trimObject(params);
      return await this.dao.update(params,t);
    } catch (error) {
      return system.getResult(null,`系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 完善信息
   * @param {*} params
   *   legalName
   *   legalMobile
   *   names
   *   creditCode
   *   isBank
   *   businessScope
   */
  async completedOrder(params) {
    try {
      //1 检查个体户是否存在 （根据统一社会信用代码查个体工商户是否存在）
      this.trimObject(params);
      if (!params.id) { return system.getResult(null, `参数错误 ID 不能为空`); }
      if (!params.legalName) { return system.getResult(null, `参数错误 法人姓名不能为空`); }
      if (!params.legalMobile) { return system.getResult(null, `参数错误 法人电话不能为空`); }
      if (!params.creditCode) { return system.getResult(null, `参数错误 法人统一社会信用代码不能为空`); }
      if (!params.names) { return system.getResult(null, `参数错误 个体户名称不能为空`); }
      if (!params.businessScope) { return system.getResult(null, `参数错误 经营范围不能为空`); }

      //2 如果不存在添加保存个体户  完善信息
      let _businessmenRes = await this.businessmenDao.findBusinessmenByCreditCode(this.trim(params.creditCode));
      if (_businessmenRes.hasOwnProperty("status")) {
        return _businessmenRes;
      }
      if (_businessmenRes.id) {
        //如果用户存在id 直接更新  
        let _iborderbase = await this.iborderbaseDao.findById(this.trim(params.id));
        if (!_iborderbase) {
          return system.getResult(null, `参数错误 订单不存在`);
        }
        let self = this;
        await this.db.transaction(async t =>{
          //更新主表
          await self.iborderbaseDao.update({businessmen_id:self.trim(_businessmenRes.id),id:self.trim(params.id)},t);
          //更新子表
          await this.dao.update(params, t);
        });
      } else {
        let self = this;
        await this.db.transaction(async t =>{
          //如果不存在id 创建后更新
          let _businessment = await this.businessmenDao.create(params,t);
          //更新主表
          await self.iborderbaseDao.update({businessmen_id:self.trim(_businessment.id),id:self.trim(params.id)},t);
          //更新子表
          await this.dao.update(params, t);
        });
      }
      return system.getResultSuccess();
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }
}

module.exports = IborderdkService;
