const system = require("../../../system");
const ServiceBase = require("../../svems.base")
const settings = require("../../../../config/settings")
class OrderService extends ServiceBase {
  constructor() {
    super();
    this.pushapiSve = system.getObject("service.push.pushapiSve");
    this.userSve = system.getObject("service.uc.userSve");
  }

  async allProcess(params) {
    try {
      return await this.callms("order", "allProcess", params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async orders(params) {
    var rs = await this.callms("order", "orderDelivers", params);
    await this.setUcUser(rs.data.rows);
    await this.transfields(rs.data.rows);
    return rs;
  }

  async handleStatus(params) {
    return await this.callms("order", "handleStatus", params);
  }

  async orderInfo(params) {
    var rs = await this.callms("order", "orderInfo", params);
    return rs;
  }

  async orderInfoAll(params) {
    var rs = await this.callms("order", "orderInfoAll", params);
    // await this.setUcUser([rs.data.order]);
    return rs;
  }

  async productDics(params) {
    var rs = await this.callms("order", "productDics", params);
    return rs;
  }

  async transfields(rows) {
    if (!rows || rows.length == 0) {
      return;
    }

    for (let row of rows) {
      if(row.deliver_divide) {
        row.deliver_divide = system.f2y(row.deliver_divide);
      }
    }
  }

  async setUcUser(rows) {
    if (!rows || rows.length == 0) {
      return;
    }

    let ids = [];
    for (let row of rows) {
      if (row.operator_id) {
        ids.push(row.operator_id);
      }
    }

    let userRs = await this.userSve.mapByIds({ ids: ids });
    let userMap = userRs.data || {};

    for (let row of rows) {
      let user = userMap[row.operator_id] || {};
      row.operator_name = user.realName || "";
    }
  }

}
module.exports = OrderService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });