const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")

class SsmuserService extends ServiceBase {
  constructor() {
    super("user", ServiceBase.getDaoName(SsmuserService));
    this.IS_ENABLED =1;  //SSM_USER 用户是否可用
  }

  /**
   * fn:添加ssmUser 用户
   * @param params
   * @returns {Promise<void>}
   */
  async addSsm(params){
    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, `密码不能为空`);
    }else{
      params.password =  await this.getEncryptStr(params.password);
    }
    try {
      let condition = {
        ucname: this.trim(params.ucname)
      };
      let _ssm = await this.dao.getSingleSsmUser(condition);
      if(_ssm){
        console.log(`xggsve-uc->ssmuserSve->addSsm（查询用户信息）${JSON.stringify(_ssm)}`);
        return system.getResult(null, `用户【${params.ucname}】已存在`);
      }
      let property = {isEnabled:this.IS_ENABLED};
      property.ucname = this.trim(params.ucname);
      property.mobile = this.trim(params.mobile);
      property.realName = this.trim(params.realName);
      property.password = this.trim(params.password);
      console.log(`xggsve-uc->ssmuserSve->addSsm（添加用户信息）${JSON.stringify(property)}`);
      let res = await this.dao.create(property);
      return system.getResult(res);
    }catch (e) {
      console.log(e);
      return system.getResult(null,`系统错误 错误信息${e.stack}`)
    }
  }

  /**
   * fn:添加ssmUser 用户
   * @param params
   * @returns {Promise<void>}
   */
  async updSsm(params){
    let property = {};
    if (params.id) {
      property.id = this.trim(params.id);
    }
    if (params.ucname) {
      property.ucname = this.trim(params.ucname);
    }
    if (params.mobile) {
      property.mobile = this.trim(params.mobile);
    }
    if (params.realName) {
      property.realName = this.trim(params.realName);
    }
    if (params.password) {
      property.password =  await this.getEncryptStr(params.password);
    }
    if(params.hasOwnProperty("isEnabled") && (params.isEnabled==1 || params.isEnabled==0)){
      property.isEnabled = params.isEnabled;
    }

    try {
      let condition = {
        id: this.trim(params.id)
      };
      let _ssm = await this.dao.getSingleSsmUser(condition);
      if(!_ssm){
        return system.getResult(null, `用户【${params.ucname}】不存已存在`);
      }
      console.log(`xggsve-uc->ssmuserSve->updSsm（查询用户信息）${JSON.stringify(_ssm)}`);
      console.log(`xggsve-uc->ssmuserSve->updSsm（跟新用户信息）${JSON.stringify(property)}`);
      let res = await this.dao.update(property);
      return system.getResult(res);
    }catch (e) {
      console.log(e);
      return system.getResult(null,`系统错误 错误信息${e.stack}`)
    }
  }

  /**
   * fn：查询用户信息
   * @param params
   * @returns {Promise<void>}
   */
  async querySsmUser(params){
    if(!params || JSON.stringify(params) === '{}'){
      return system.getResult(null, `参数错误 参数：【${JSON.stringify(params)}】`);
    }
    try{
      let _ssm = await this.dao.getSingleSsmUser(params);
      if(!_ssm){
        return system.getResult(null, `用户【${params.ucname}】不存存在`);
      }
      console.log(`xggsve-uc->ssmuserSve->updSsm（查询用户信息）${JSON.stringify(_ssm)}`);
      this.handleDate(_ssm, ["created_at"], null);
      _ssm.isEnabledName = _ssm.isEnabled == 1 ? "已启用" : "未启用";
      return system.getResult(_ssm);
    }catch (e) {
      console.log(e);
      return system.getResult(null, `系统错误 错误信息${e.stack}`);
    }

  }

  /**
   * fn:ssmuser用户列表
   * @param params
   * @returns {Promise<void>}
   */
  async querySsmUsers(params) {
    var result = {
      count: 0,
      rows: []
    };
    try{
      params.currentPage = Number(params.currentPage || 1);
      params.pageSize = Number(params.pageSize || 10);
      params.startRow = (params.currentPage - 1) * params.pageSize;

      var total = await this.dao.countByCondition(params);
      if (total == 0) {
        return result;
      }else{
        result.count = total;
      }
      result.rows = await this.dao.getMultipleSsmUser(params) || [];
      if (result.rows) {
        for (let item of result.rows) {
          this.handleDate(item, ["created_at"], null);
          item.isEnabledName = item.isEnabled == 1 ? "已启用" : "未启用";
        }
      }
      return system.getResultSuccess(result);
    }catch (e) {
      console.log(e);
      return system.getResult(null, `系统错误 错误信息${e.stack}`);
    }
  }

  /**
   * fn：ssm用户登录
   * @param params
   * @returns {Promise<void>}
   */
  async ssmUserlogin(params){
    let ucname = this.trim(params.ucname);
    let password = this.trim(params.password);

    if(!ucname){
      return system.getResult(null, `参数错误 用户名不能为空`);
    }
    if(!password){
      return system.getResult(null, `参数错误 密码不能为空`);
    }
    let pwd = await this.getEncryptStr(password);

    let user = await this.dao.getSingleSsmUser({ucname});
    if (!user || user.length == 0) {
      return system.getResult(null, "用户名或密码错误");
    }
    user = user[0] || null;
    if(!user || pwd != user.password) {
      return system.getResult(null, "用户名或密码错误");
    }

    if(!user.isEnabled) {
      return system.getResult(null, "用户已禁用");
    }
    return system.getResultSuccess(user);
  }

}

module.exports = SsmuserService;