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

/**
 * 订单产品表
 */
class OorderService extends ServiceBase {
  constructor() {
    super("order", ServiceBase.getDaoName(OorderService));

    this.oorderinforegDao = system.getObject("db.order.oorderinforegDao");
    this.oorderdeliverDao = system.getObject("db.order.oorderdeliverDao");
    this.oorderprocessDao = system.getObject("db.order.oorderprocessDao");

    this.osourceDao = system.getObject("db.common.osourceDao");

    this.oproductDao = system.getObject("db.product.oproductDao");
    this.oprocessDao = system.getObject("db.product.oprocessDao");
    this.oproductprocessDao = system.getObject("db.product.oproductprocessDao");
  }

  /**
   *  新增渠道订单
   * @returns {Promise<void>}
   */
  async addSourceOrder(params) {
    // 整理参数
    let order = {
      merchantId: "",
      channelOrderNo: "",
      busi_type: 1,
      busi_id: "",
      price: 0,
    };
    order.productId = Number(params.productId);
    order.sourceId = Number(params.sourceId);
    order.sourceNo = this.trim(params.sourceNo);
    order.notes = this.trim(params.notes);
    order.contactMobile = this.trim(params.contactMobile);

    // 验证来源
    let source = await this.osourceDao.findById(order.sourceId);
    if (!source) {
      return system.getResult(null, "来源错误");
    }

    // 验证产品
    let product = await this.oproductDao.findById(order.productId);
    if (!product) {
      return system.getResult(null, "产品错误");
    }

    // 获取产品状态
    await this.getProductProcess();

    // 查询产品订单状态

    // 合并订单状态

    // 开启事务

    // 插入订单数据

    // 插入订单状态数据
  }

  /**
   * 新建进度
   * @param params
   * @returns {Promise<void>}
   */
  async createProcess(params) {
    // 整理参数 变成这个格式，id, status, params

    // 查询订单

    // 找出订单下个状态，于参数订单状态匹配

    // 调用状态func(order, nextObj, params)
  }

  async getProductProcessList(params) {
    var options = {};
    options.where = params;
    options.raw = true;
    options.order = [['sort', 'ASC']];

    var oplist = await this.oproductprocessDao.model.findAll(options);
    if (!oplist) {
      return [];
    }

    let processIds = [];
    for (var item of oplist) {
      processIds.push(item.processId);
    }

    var optionsProcess = {};
    optionsProcess.where = {

    };
    optionsProcess.raw = true;
    optionsProcess.order = [['sort', 'ASC']];

    await this.oprocessDao.model.findAll();
  }


}

module.exports = OorderService;