package org.wcs.business.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * saToken 的缓存
 */
@Component
@RequiredArgsConstructor
public class SaTokenRedis {

    /**
     * token 名称
     */
    @Value("${sa-token.token-name}")
    private String tokenName;

    private final StringRedisTemplate stringRedisTemplate;

    // 用户对应的用户组
    private final String USER_GROUP_KEY = tokenName + ":group";
    // 用户组对应的权限
    private final String USER_PERMISSION_KEY = tokenName + ":permission";


    /* **************** 用户ID 对应的用户组 **************** */

    /**
     * 删除用户ID对应的用户组信息
     * @param loginId 用户ID
     */
    public void deleteUserGroup(String loginId) {
        stringRedisTemplate.delete(USER_GROUP_KEY + ":" + loginId);
    }

    /**
     * 设置用户ID的用户组
     * @param loginId 用户ID
     * @param group 组
     */
    public void setUserGroup(String loginId, String group) {
        stringRedisTemplate.opsForValue().set(USER_GROUP_KEY + ":" + loginId, group);
    }

    /**
     * 获取用户ID的用户组
     * @param loginId 用户ID
     * @return 组
     */
    public String getUserGroup(String loginId) {
        return stringRedisTemplate.opsForValue().get(USER_GROUP_KEY + ":" + loginId);
    }


    /* ***************** 用户组对应的权限 ********************** */

    /**
     * 清空权限缓存
     * @param group 用户组ID
     */
    public void clearPermissionCache(String group) {
        stringRedisTemplate.delete(USER_PERMISSION_KEY + ":" + group);
    }

    /**
     * 获取用户组权限
     * @param group 用户组ID
     * @return 权限列表
     */
    public String[] getUserGroupPermission(String group) {
        List<String> permissionList = stringRedisTemplate.opsForList().range(USER_PERMISSION_KEY + ":" + group, 0, -1);
        if(permissionList == null || permissionList.isEmpty()) {
            return new String[0];
        }
        return permissionList.toArray(new String[0]);
    }

    /**
     * 设置用户组权限
     * @param group 用户组ID
     * @param permissionList 权限
     */
    public void setUserGroupPermission(String group, String[] permissionList) {
        if(permissionList == null || permissionList.length == 0) {
            clearPermissionCache(group);
            return;
        }
        List<String> oldPermissionList = stringRedisTemplate.opsForList().range(USER_PERMISSION_KEY + ":" + group, 0, -1);
        if(oldPermissionList == null || oldPermissionList.isEmpty()) {
            stringRedisTemplate.opsForList().rightPushAll(USER_PERMISSION_KEY + ":" + group, permissionList);
            return;
        }
        for (String permission : permissionList) {
            if(!oldPermissionList.contains(permission)) {
                stringRedisTemplate.opsForList().rightPush(USER_PERMISSION_KEY + ":" + group, permission);
            }
        }
        return;
    }


}
