const system = require("../../../system");
const ServiceBase = require("../../sve.base")
/**
 * 订单交付
 */
class OorderdeliverService extends ServiceBase {
  constructor() {
    super("order", ServiceBase.getDaoName(OorderdeliverService));
    this.oorderinforegDao = system.getObject("db.order.oorderinforegDao");
    this.oorderDao = system.getObject("db.order.oorderDao");
    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 
   * @operator_id 业务员ID
   * @id String 订单号
   * @status String 订单状态
   * @operator_id String 交付商业务员
   */
  async orderDelivers(params){
    let where = {};
    if(params.deliver_id){
      where.deliver_id = this.trim(params.deliver_id);
    }else{
      return system.getResult(null,`参数错误 交付商ID不能为空`);
    }
    //交付商订单表条件
    if(params.createdBegin && params.createdEnd){
      where.createdBegin = this.trim(params.createdBegin);
      where.createdEnd = this.trim(params.createdEnd);
    }
    if(params.operator_id){
      where.operator_id = this.trim(params.operator_id);
    }
    
    //下面是订单条件
    if(params.id){
      where.id = this.trim(params.id);
    }
    if(params.status){
      where.status = this.trim(params.status);
    }
    
    where.currentPage = Number(params.currentPage || 1);
    where.pageSize = Number(params.pageSize || 10);

    where.startRow = (where.currentPage-1)*where.pageSize;

    try {
      let ordersCountRes = await this.dao.ordersdeliverCount(where);
      let count = ordersCountRes[0]['orderCount'];
      if(count==0){
        return system.getResult({count:0,rows:[]});
      }
      let rows = await this.dao.findOrders(where);
      //格式化订单状态
      await this.formateStatus(rows);

      await this.formateServiceItems(rows);

      for (var row of rows) {
        this.handleDate(row, ["created_at"], "YYYY-MM-DD HH:mm:ss", -8);
      }

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

  /**
   * 格式化订单交付商
   * @param {*} orderList
   */
  async formateDeliver(orderList) {
    let deliverIdList = [];
    for (let item of orderList) {
      if (item.deliver_id) {
        deliverIdList.push(item.deliver_id);
      }
    }

    let deliverMap = await this.dao.mapByIds(deliverIdList);
    for (let item of orderList) {
      item.odeliver = deliverMap[item.deliver_id] || {}
    }
  }

  /**
   * 格式化订单状态数据
   * @param orderList
   * @returns {Promise<void>}
   */
  async formateStatus(orderList) {
    let ids = [];
    let statuses = [];
    for (let item of orderList) {
      ids.push(item.id);
      statuses.push(item.status);
    }

    let map = await this.oorderprocessDao.mapByOrderIdsAndStatus(ids, statuses);

    for (let item of orderList) {
      let key = item.id + "_" + item.status;
      let v = map[key] || {};
      item.statusName = v.name;
      item.next_status = JSON.parse(v.next_status || "[]");
    }
    console.log(orderList);
  }

  async formateServiceItems(orderList) {
    let ids = [];
    let statuses = [];
    for (let item of orderList) {
      let productIds = this.trim(item.service_items).split(",");
      ids = ids.concat(productIds);
    }

    let productMap = await this.oproductDao.mapByIds(ids);
    for (let item of orderList) {
      let productIds = this.trim(item.service_items).split(",");
      let names = [];
      for (var productId of productIds) {
        let product = productMap[productId];
        if (product) {
          names.push(product.name);
        }
      }
      item.service_items_name = names.join(",");
    }
  }



}

module.exports = OorderdeliverService;