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

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

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

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

  /**
   * fn:订单列表
   * @param params
   * @param pobj2
   * @param req
   * @returns {Promise<{msg: string, data: (*|null), bizmsg: string, status: number}|{msg: string, data, bizmsg: (*|string), status: number}|*>}
   */
  async page(params, pobj2, req) {
    try {
      let listRes = await this.orderSve.page(params);
      if (listRes.status != 0) {
        return system.getResult(null, `系统错误`);
      }
      let source_nos = [];
      for (let item of listRes.data.rows) {
        source_nos.push(item.id);
      }
      if(source_nos.length==0){
        return system.getResult(null, `暂无数据`);
      }
      let res =await this.orderSve.queryOrderStatusBySourceNo({source_nos:source_nos});
      if(res.status!=0){
        return res;
      }
      let StatusMap = res.data;
      for(let item of listRes.data.rows){
        if(StatusMap[item.id]){
          item.handle_name = StatusMap[item.id]['name'] || "处理中";
        }else{
          item.handle_name = "订单待处理";
        }
      }
      return listRes;
    }
     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：查询产品
   * @param pobj
   * @param pobj2
   * @param req
   * @returns {Promise<{msg: *, data: *, status: *}|{msg: *, data, bizmsg: *|string, status: number}>}
   */
  async orderChooseProducts(pobj, pobj2, req) {
    //TODO: 绑定渠道商品信息
    try {
      // return system.getResult([{"id":"10020300","name":"刻章办理"},{"id":"10020400","name":"银行开户"},{"id":"10020500","name":"税务报道"},{"id":"10020600","name":"代理记账"}]);
      return system.getResult([{"id":"10020300","name":"刻章办理"},{"id":"10020400","name":"银行开户"},{"id":"10020500","name":"税务报道"}]);
    } catch (e) {
      console.log(e);
      return system.getResultFail(500, "接口错误");
    }
  }
}

module.exports = ChannelCtl;