var system = require("../../../system")
const CtlBase = require("../../ctlms.base");

class ChannelCtl extends CtlBase {
  constructor() {
    super();
    this.orderSve = system.getObject("service.saas.orderSve");
    this.channelSve = system.getObject("service.saas.channelSve");
  }


  async platformInfo(params, pobj2, req) {
    try {
      return await this.orderSve.platformInfo(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async info(params, pobj2, req) {
    try {
      return await this.orderSve.info(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async page(params, pobj2, req) {
    try {
      return await this.orderSve.page(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async audit(params, pobj2, req) {
    if (params.audit_status != "20" && params.audit_status != "30") {
      return system.getResult(null, "审核状态错误");
    }
    try {
      return await this.orderSve.audit(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async deliver(params, pobj2, req) {
    try {
      return await this.orderSve.deliver(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * fn:查看所有订单
   * condition1:需要将channel_name显示出来
   * condition2:需要将对应的businessmen详情带出来
   * @param params
   * @param pobj2
   * @param req
   * @returns {Promise<void>}
   */
  async queryOrderGroupByChannel(params, pobj2, req) {
    // if(!params.channel_id){
    //   return system.getResult(null, `参数错误 渠道ID不能为空`);
    // }
    this.doTimeCondition(params, ["createBegin", "createEnd"]);
    try {
      let channels = await this.channelSve.dics({});
      if(channels.status!=0){
        return system.getResult(null, `渠道列表获取失败，请稍后再试`);
      }
      let channelMap = {};
      for(let channel_item of channels.data) {
        channelMap[channel_item.id] = channel_item.name;
      }
      let orders= await this.orderSve.page(params);
      let order_ids = [];
      if(orders.status!=0 || !orders.data){
        return system.getResult({count:0,rows:[]});
      }
      for (let item of orders.data.rows) {
        item.channel_name = channelMap[item.channel_id] || "";
        order_ids.push(item.id)
      }
      let obusinessmenMap = await this.orderSve.businessmenByOrderIds({order_ids});
      if(obusinessmenMap.status!=0 ){
        return system.getResult(null, `系统错误 个体户不存在`);
      }
      for (let element of orders.data.rows) {
        element.obusinessmen = obusinessmenMap.data[element.id] ?obusinessmenMap.data[element.id]:{} ;
      }
      return orders;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  async info(params, pobj2, req) {
    try {
      let order =  await this.orderSve.saasOrderMerchantOrderInfo(params);
      return order;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

}

module.exports = ChannelCtl;