var system = require("../../../system")
const settings = require("../../../../config/settings")
const CtlBase = require("../../ctlms.base");
const validation = system.getObject("util.validation");

class MerchantUserCtl extends CtlBase {
  constructor() {
    super();
    this.userSve = system.getObject("service.uc.userSve");
    this.merchantSve = system.getObject("service.merchant.merchantSve");
  }

  async save(params, pobj2, req) {
    try {
      params.uctype_id = this.trim(params.uctype_id);
      params.real_name = this.trim(params.real_name);
      params.password = this.trim(params.password);
      params.ucname = this.trim(params.ucname);
      params.mobile = this.trim(params.mobile);

      if (!params.uctype_id) {
        return system.getResult(null, "请选择商户");
      }
      if (!params.real_name) {
        return system.getResult(null, "请选择商户");
      }
      if (!params.ucname) {
        return system.getResult(null, "请选择商户");
      }
      if (!params.mobile) {
        return system.getResult(null, "请选择商户");
      }

      let merchantBean = await this.merchantSve.merchantOfInfo({id: this.trim(params.uctype_id)});
      if(merchantBean.status!=0 || !merchantBean.data || merchantBean.data.audit_status!="1"){
        return system.getResult(null, `商户【${params.uctype_id}】不存在`);
      }
      validation.check(params, "real_name", {name: "姓名", is_require: true});
      validation.check(params, "mobile", {name: "联系电话", is_require: true});
      validation.check(params, "ucname", {name: "用户名", is_require: true});

      return await this.userSve.saveMerchantUser(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error.name}`);
    }
  }

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

  async page(params, pobj2, req) {
    try {
      params.utype = 2;
      params.uctype_id = this.trim(params.merchant_id);
      let res = await this.userSve.page(params);
      let merchant_ids = new Set();
      if (res.status === 0 && res.data && res.data.rows) {
        for (let item of res.data.rows) {
          // item.merchant = {id:"todo", name: "todo"};
          merchant_ids.add(item.uctype_id);
        }
      }
      if(merchant_ids.size==0){
        return res;
      }

      let merchantDist = await  this.merchantSve.merchantMapByIds({ids:Array.from(merchant_ids)});
      if(merchantDist.status!=0 || !merchantDist.data){
        return res;
      }
      merchantDist = merchantDist.data;
      for (let item of res.data.rows) {
        item.merchant_name = merchantDist[item.uctype_id] || "";
      }
      return res;
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


  /**
   * 启用禁用
   * @param {*} params
   * @param {*} pobj2
   * @param {*} req
   */
  async enabled(params, pobj2, req) {
    try {
      params.id = Number(params.id || 0);
      params.is_enabled = Number(params.is_enabled || 0);
      return await this.userSve.enabled(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


}

module.exports = MerchantUserCtl;