package com.lantone.security.facade.base;

import com.google.common.collect.Maps;
import com.lantone.common.constant.AuthConstant;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.enums.StatusEnum;
import com.lantone.common.service.RedisService;
import com.lantone.security.service.impl.ResourceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表名：sys_resource 业务类
 * </p>
 */
@Component
public class ResourceFacade extends ResourceServiceImpl {

    @Autowired
    private RedisService redisService;

    public Map<String, List<String>> initResourceRolesMap() {
        Map<String, List<String>> resourceRolesMap = Maps.newHashMap();
        List<String> resourceRolesList = baseMapper.getResourceRolesMap(IsDeleteEnum.N.getKey(), StatusEnum.Enable.getKey());
        resourceRolesList.forEach(resourceRoles -> {
            String[] resourceRoleArray = resourceRoles.split("=");
            resourceRolesMap.put(resourceRoleArray[0], Arrays.asList(resourceRoleArray[1].split(",")));
        });
        redisService.del(AuthConstant.RESOURCE_ROLES_MAP_KEY);
        redisService.hSetAll(AuthConstant.RESOURCE_ROLES_MAP_KEY, resourceRolesMap);
        return resourceRolesMap;
    }

}