const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class AuthService extends ServiceBase {
  constructor() {
    super("auth", ServiceBase.getDaoName(AuthService));
    this.pmap = {};
  }

  async add(obj) {
    var saas_id = Number(obj.saas_id || 0);
    var pid = Number(obj.pid || 0);

    if (!saas_id) {
      return system.getResult(null, "saas_id不存在");
    }

    if (pid === 0) {
      // 验证是否存在其他权限
      var exist = await this.findCount({
        where: {
          saas_id: saas_id
        }
      });
      if (exist) {
        return system.getResult(null, "菜单根目录已经存在");
      }
    }

    obj.pid = pid;
    obj.saas_id = saas_id;
    obj.menuType = Number(obj.menuType || 1);
    obj.name = this.trim(obj.name);
    obj.icon = this.trim(obj.icon);
    obj.path = this.trim(obj.path);
    obj.sort = Number(obj.sort || 99);
    var obj = await this.dao.create(obj);
    return system.getResultSuccess();
  }

  async upd(obj) {
    var saas_id = Number(obj.saas_id || 0);
    var pid = Number(obj.pid || 0);
    var id = Number(obj.id || 0);
    if (!id) {
      return system.getResult(null, "菜单不存在");
    }
    var auth = await this.findById(id);
    if (!saas_id) {
      return system.getResult(null, "saas_id不存在");
    }

    if (saas_id != auth.saas_id) {
      return system.getResult(null, "修改失败，权限不足");
    }

    auth.menuType = Number(obj.menuType || 1);
    auth.name = this.trim(obj.name);
    auth.icon = this.trim(obj.icon);
    auth.path = this.trim(obj.path);
    auth.sort = Number(obj.sort || 99);

    await auth.save();
    return system.getResultSuccess();
  }

  async byPid(obj) {
    var list = await this.dao.findAll({
      pid: obj.pid || 0,
    });
    return system.getResultSuccess(list);
  }

  async tree(params) {
    try {
      var all = await this.dao.all(params.saas_id);

      var pmap = {};
      for (var item of all) {
        item.label =  item.name;
        var list = pmap[item.pid];
        if (!list) {
          list = [];
        }
        list.push(item);
        pmap[item.pid] = list;
      }
  
      for(var item of all) {
        item.children = pmap[item.id] || [];
      }
      return system.getResultSuccess(pmap[0]);
    } catch (error) {
      return system.getResult(error);
    }

  }

  async byRoleIds(params) {
    return await this.dao.byRoleIds(params);
  }

  async byIds(params) {
    return await this.dao.byIds(params);
  }
  async menuByRoleIds(params) {
    params.menuType = 1;
    let all = await this.byRoleIds(params);
    if(!all || all.length == 0) {
      return [];
    }

    let pids = [];
    for (let item of all) {
      if (item.pid === 0) {
        pids.push(item.id);
      } else {
        pids.push(item.pid);
      }
    }

    let pmenuMap = {};
    let pmenus = await this.byIds(pids);
    for(let p of pmenus) {
      pmenuMap[p.id] = p;
    }

    for (let item of all) {
      let pid = item.pid;
      if (pid === 0) {
        continue;
      }
      let pmenu = pmenuMap[pid];
      if (!pmenu) {
        continue;
      }
      let childs = pmenu.childs || [];
      let childIds = pmenu.childIds || [];
      if (childIds.indexOf(item.id) != -1) {
        continue;
      }
      childIds.push(item.id);
      childs.push(item);
      pmenu.childs = childs;
      pmenu.childIds = childIds;
    }

    return pmenus;
  }

  async authByRoleIds(params) {
    params.menuType = 2;
    return await this.byRoleIds(params);
  }
  
  async info(obj) {
    var auth = await this.dao.getById(obj.id);
    return system.getResultSuccess(auth);
  }

  async del(obj) {
    await this.delete({id: obj.id});
    return system.getResultSuccess();
  }
}
module.exports = AuthService;