const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class RoleService extends ServiceBase {
  constructor() {
    super("role", ServiceBase.getDaoName(RoleService));
  }

  /**
   * role  添加 
   * @param {*} params 
   */
  async apiAddRole(params) {
    try {
      return await this.addRole(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }

  /**
   * role  更新 
   * @param {*} params 
   */
  async apiUpdRole(params) {
    try {
      return await this.updateRole(params);
    } catch (error) {
      return system.getResult(null, `系统错误 错误信息 ${error}`);
    }
  }
  


/***************************************************************** */


  /**
   * 添加
   * @param {T} params 
   */
  async addRole (params){
    params.code = this.trim(params.code);
    params.name =  this.trim(params.name);
    params.saas_id =  Number(params.saas_id || 0);
    try {
      let res = await this.create(params);
      console.log(`saas 信息插入 ： ${JSON.stringify(res)}`);
      return system.getResult(res);
    } catch (error) {
      return system.getResult(-1,`系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 更新
   * @param {T} params 
   */
  async updateRole (params){
    if(params.id){
      params.id =  Number(this.trim(params.id));
    }
    let _role =  await this.findById({id:params.id});
    if(!_role){
      return system.getResult(-1,`参数错误 角色不存在`);
    }
    if(params.code){
      _role.code = this.trim(params.code);
    }
    params.code = this.trim(params.code);
    params.name =  this.trim(params.name);
    try {
      let res = await this.update(params);
      console.log(`saas 信息插入 ： ${JSON.stringify(res)}`);
      return system.getResult(res);
    } catch (error) {
      return system.getResult(-1,`系统错误 错误信息 ${error}`);
    }
  }
  
}
module.exports = RoleService;
