var system = require("../../../system")
const CtlBase = require("../../ctlms.base");
class ChannelCtl extends CtlBase {
  constructor() {
    super();
    this.channelSve = system.getObject("service.saas.channelSve");
    this.merchantSve = system.getObject("service.saas.merchantSve");
    this.orderSve = system.getObject("service.saas.orderSve");
  }

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

  async info(params, pobj2, req) {
    try {
      return await this.channelSve.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}|*>}
   */
  async delvierInfo(params, pobj2, req) {
    try {
      if(!params.channel_id){
        system.getResult(null, `登录失效，请重新登录`);
      }
      let user = req.loginUser;
      let rs = {
        contact_man:user.contact_man,
        contact_mobile : user.contact_mobile,
        contact_email : user.contact_email,
        contact_addr : user.contact_addr,
      };
      return system.getResult(rs);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

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


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


  /**
   * fn:当前渠道所有的商户的所有个体户列表
   * @param params
   * @param pobj2
   * @param req
   * @returns {Promise<void>}
   */
  async businessmenBelongChannel (params, pobj2, req){
    if(!params.channel_id){
      return system.getResult(null,`登录失效，请重新登陆`)
    }
    try{
      //获取渠道所有的商户
      let merchantIdsRes = await this.merchantSve.queryMerchantByChannelId({channel_id: params.channel_id});
      console.log(`channerlCtl->businessmenBelongChannel->merchantIdsRes ` + JSON.stringify(merchantIdsRes));
      if(merchantIdsRes.status!=0 ){
        return system.getResult(null,`系统错误`);
      }
      let merchantIds = merchantIdsRes.data;
      let ids=[],merchantMap={};
      if(merchantIds.length==0){
        return system.getResult({rows:[],count:0});
      }else{
        for (let item of merchantIds) {
          merchantMap[item.id] = item;
          ids.push(item.id);
        }
      }
      params.merchant_ids=ids;
      //获取渠道所有的商户的所有个体户
      let res =  await this.orderSve.saasorderbusinessmenPage(params);
      if(res.status!=0){
        return system.getResult({rows:[],count:0});
      }
      for(let element of res.data.rows){
        element.merchant = merchantMap[element.merchant_id];
      }
      return res;
    }catch (e) {
      console.log(e);
      return system.getResult(null, `系统错误`);
    }

  }
}
module.exports = ChannelCtl;