var system = require("../../../system")
const settings = require("../../../../config/settings")
const CtlBase = require("../../ctlms.base");
const md5 = require("MD5");
const uuidv4 = require('uuid/v4');
const logCtl = system.getObject("web.common.oplogCtl");

class SsmUserCtl extends CtlBase {
  constructor() {
    super();
    this.ssmuserSve = system.getObject("service.uc.ssmuserSve");
    this.redisClient = system.getObject("util.redisClient");
  }

  /**
   * fn:ssm用户登录
   * @param pobj
   * @param pobj2
   * @param req
   * @param res
   * @returns {Promise<{msg: *, data: *, status: *}|{msg: *, data: (*|null), status: number}|*>}
   */
  async login(pobj, pobj2, req, res) {
    var loginName = this.trim(pobj.loginName);
    var password = this.trim(pobj.password);
    try {
      var loginUser = await this.ssmuserSve.ssmUserlogin({
        ucname: loginName,
        password: password,
      });
      if (loginUser.status != 0 && !loginUser.data) {
        return loginUser;
      }
      loginUser = loginUser.data;
      var loginsid = await this.setLogin(loginUser);
      let rs = {
        key: loginsid,
        loginname: loginUser.ucname,
        menus: await this.getMenu(loginUser)
      };

      return system.getResultSuccess(rs);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.message);
    }
  }

  /**
   * fn:设置登录属性
   * @param user
   * @returns {Promise<string>}
   */
  async setLogin(user) {
    let loginsid = "saasa_" + uuidv4();
    // if(settings.env == 'dev') {
    //   user.loginsid = "jfs_" + "3cb49932-fa02-44f0-90db-9f06fe02e5c7";
    //   user.subsid = "sub_" +  "3cb49932-fa02-44f0-90db-9f06fe02e5c7";
    // }
    // loginsid = "saasp_" + "2cb49932-fa02-44f0-90db-9f06fe02e5c7";
    await this.redisClient.setWithEx(loginsid, JSON.stringify(user), 60 * 60 * 5);
    return loginsid;
  }

  /**
   * fn:返回用户功能菜单
   * @param loginUser
   * @returns {*[]|({name: string, icon: string, team: [{path: string, name: string}]}|{name: string, icon: string, team: [{path: string, name: string}]}|{name: string, icon: string, team: [{path: string, name: string}]})[]}
   */
  getMenu(loginUser) {
    if (!loginUser) {
      return [];
    }
    return [{
        "name": "数据概览",
        "icon": "iconfont icon-gth-gsshujugailan",
        "team": [{
          "name": "业务数据汇总",
          "path": "/home"
        }]
      },
      {
        "name": "订单中心",
        "icon": "iconfont icon-gth-gsdingdanzhongxin",
        "team": [{
          "name": "订单管理",
          "path": "/trading/ordersAll"
        }, ]
      },
      {
        "name": "个体户中心",
        "icon": "iconfont icon-gth-gsgetihuzhongxin",
        "team": [{
          "name": "个体户管理",
          "path": "/trading/userInformation"
        }, ]
      },
    ];
  }

  /**
   * fn:添加ssmUser 用户
   * @param params
   * @returns {Promise<void>}
   */
  async addSsm(params, pobj2, req){
    if (!params.ucname) {
      return system.getResult(null, `用户名不能为空`);
    }
    if (!params.mobile) {
      return system.getResult(null, `手机号不能为空`);
    }
    if (!params.realName) {
      return system.getResult(null, `姓名不能为空`);
    }
    if (!params.password) {
      return system.getResult(null, `密码不能为空`);
    }
    return await this.ssmuserSve.addSsm(params);
  }

  /**
   * fn:更新ssmUser 用户
   * @param params
   * @returns {Promise<void>}
   */
  async updSsm(params, pobj2, req){
    if (!params.id) {
      return system.getResult(null, `参数错误 ID不能为空`);
    }
    return await this.ssmuserSve.updSsm(params);
  }

  /**
   * fn：查询用户信息
   * @param params
   * @returns {Promise<void>}
   */
  async querySsmUser(params, pobj2, req){
    if(!params.id){
      return system.getResult(null, `参数错误 ID不能为空`);
    }
    return await this.ssmuserSve.querySsmUser(params);
  }

  /**
   * fn:ssmuser用户列表
   * @param params
   * @returns {Promise<void>}
   */
  async querySsmUsers(params, pobj2, req) {
    if(params.createdBegin){
      params.createdBegin = this.trim(params.createdBegin);
    }
    if(params.createdEnd){
      params.createdEnd = this.trim(params.createdEnd);
    }
    return await this.ssmuserSve.querySsmUsers(params);
  }

}

module.exports = SsmUserCtl;