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

  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, "请选择商户");
      }
      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) {
      let msg = error.message;
      if (msg.startsWith("bpo-validation-error:")) {
        return system.getResult(null, msg.replace("bpo-validation-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.enabled = Number(params.enabled || 0);
      return await this.userSve.enabled(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }


  /**
   * 根据pid查出子目录
   * @param {*} params
   */
  async page(params, pobj2, req) {
    try {
      params.uctypeId = params.deliver_id;
      return await this.userSve.page(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

}

module.exports = MerchantUserCtl;