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

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

    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");
  }


  /**
   * 验证订单状态
   * @param params
   * @returns {Promise<{msg: string, data: (*|null), bizmsg: string, status: number}>}
   */
  async handleStatus(params) {
    try {
      let _order = await this.dao.findById(params.id);
      if (!_order) {
        return system.getResult(null, `订单不存在`);
      }
      //todo ...  验证状态
      let _orderStatus = await this.getOrderProcessStatus(_order.id, _order.status);
      let nextStatus = JSON.parse(_orderStatus.next_status);
      var statuses = [];
      for(var ns of nextStatus) {
        statuses.push(ns.next_status);
      }
      if (statuses.indexOf(params.status) == -1) {
        return system.getResult(null, `订单状态错误，下一订单状态应该是${nextStatus.next_name}`);
      }
      params._order = _order;

    } catch (error) {
      console.log(error);
      return system.getResult(null,`系统错误 错误信息 ${error}`);
    }
    
  }

  /**
   * 获取订单状态对象
   * @param order_id
   * @param status
   * @returns {Promise<*|{}>}
   */
  async getOrderProcessStatus(order_id, status) {
    let map = await this.oorderprocessDao.mapByOrderIdsAndStatus([order_id], [status]);
    return map[order_id + "_" + status] || {};
  }

  /**
   * 分配业务员
   * @param {*} params
   * @id     String  订单ID
   * @bd_id  String  业务员ID
   * @status String 下一个状态码
   * @bd_path String 业务员权限
   */
  async assignSalesman(params) {
    if (!params.bd_id) {
      return system.getResult(null, `参数错误 业务员ID不能为空`);
    }
    try {
      //更新订单业务员
      let _order = await this.findById(params.id);
      if (!_order) {
        return system.getResult(null, `订单不存在`);
      }
      //to do ... 验证下一个状态
      await this.statusAction(params);

      _order.bd_id = this.trim(params.bd_id);
      _order.status = this.trim(params.status);
      _order.bd_path = this.trim(params.bd_path);

      let res = await _order.save();
      return system.getResult(res);
    } catch (error) {
      console.log(error);
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 分配交付商
   * @param {*} params
   * @id  String 订单ID
   * @deliver_id String 交付商ID
   * @deliver_name String 交付商名称
   * @deliver_deliver String 交付商分成
   * @status String 下一个状态码 
   */
  async assignDeliver(params) {
    //参数验证
    if (!params.id) {
      return system.getResult(null, `参数错误 订单ID不能为空`);
    }
    if (!params.deliver_id) {
      return system.getResult(null, `参数错误 交付商ID不能为空`);
    }
    if (!params.hasOwnProperty(`deliver_divide`)) {
      return system.getResult(null, `参数错误 交付商分成不能为空`);
    }

    try {
      let _order = params._order;
      let self = this;
      let res = await this.db.transaction(async t => {
        //创建orderdeliver记录
        await self.oorderdeliverDao.create({
          order_id: self.trim(params.id),
          deliver_id: self.trim(params.deliver_id),
          deliver_name: self.trim(params.deliver_name),
          deliver_divide: self.trim(params.deliver_divide),
        }, t);

        //更新oorder订单记录
        await self.dao.update({
          deliver_id: self.trim(params.deliver_id),
          id: self.trim(params.id),
          status: self.trim(params.status)
        }, t);

      });
      return system.getResult(res);
    } catch (error) {
      console.log(error);
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

}

module.exports = OorderstatusService;