<?php
namespace app\model;

use think\facade\Cache;

class AuthGroup extends Base
{
    public static function updateRules($groupId, $rules)
    {
        $rules = implode(',', $rules);
        $data = ['rules' => $rules];
        self::updateById($groupId, $data);
        Member::where('group_id', $groupId)
        ->update($data);
    }
    
    //根据ID获取角色列表,ID为1是超级管理员 
    public static function getListById($groupId = 1)
    {
        if($groupId < 1){
            return [];
        }
        $group = self::getById($groupId);
        if(empty($group)){
            return [];
        }
        if($groupId == 1){
            return self::select()
            ->toArray();
        }else{
            return self::where('id','<>','1')
            ->select()
            ->toArray();
        }
    }

    /**
     * 重置角色权限缓存
     * @param int $groupId 指定重置的角色ID,若不指定则重置所有角色
     */
    public static function resetGroupRulesCache($groupId = 0)
    {
        if(is_numeric($groupId) && $groupId > 0) {
            Cache::set('group_rules_'.$groupId, null);
            Cache::set('rule_names_'.$groupId, null);
        } else {
            $groupIds = self::column('id');
            foreach ($groupIds as $id){
                Cache::set('group_rules_'.$id, null);
                Cache::set('rule_names_'.$id, null);
            }
        }
    }
}