package com.yunfeiyun.agmp.common.framework.manager;

import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.service.ISysResService;
import com.yunfeiyun.agmp.common.annotation.RedisLock;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseRunParam;
import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
import com.yunfeiyun.agmp.common.enums.RedisLockKey;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class ResCacheManager {

    private static final Logger sys_res_logger = LoggerFactory.getLogger("sys-res");

    /**
     * 重置资源数据缓存数据
     */
    @RedisLock(key = RedisLockKey.RES_EXPIRE_BIZ_LOCK)
    //@ConditionalRun(level="after",name="资源缓存", key = {"SYS.RES"},value = {"1"})
    public  void init(IBaseRunParam param)
    {
        sys_res_logger.info("执行={} {}  输出消息=[{}]" , param.getLevel() , param.getName(),param.toString());
        ResCacheManager.clearResCache();
        loadingResCache();
    }

    /**
     * 清理资源数据缓存数据
     */
    //@RedisLock(key = RedisLockKey.RES_EXPIRE_BIZ_LOCK)
    public  void exireResCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisCacheManager.class).keys(RedisCacheKey.SYS_RES.getCacheKey().concat("*"));
        List<String> keysNew = keys.stream().map(key -> {
            String keyNew  ="";
            if(key.indexOf(RedisCacheKey.SYS_RES.getCacheKey()) > -1){
                keyNew = key.substring(RedisCacheKey.SYS_RES.getCacheKey().length() + 1);

            }
            return keyNew;
        }).collect(Collectors.toList());
        if(keysNew.size() > 0 && keys.size() >0 ){
            SpringUtils.getBean(ResManager.class).cleanRes(keysNew);
            clearResKey(keys);
        }else{
            sys_res_logger.info("本次无任何资源文件需要清理");
        }
    }

    /**
     * 加载过期的未绑定的资源表数据到缓存数据
     */
    public void loadingResCache()
    {
        List<SysRes> sysResList =  SpringUtils.getBean(ISysResService.class).selectSysResExpire();
        for(SysRes entry : sysResList ){
            ResCacheManager.setResCache(entry.getResId(), entry);
        }
    }

    /**
     * 设置资源缓存
     *
     * @param key 参数键
     * @param resData 字典数据列表
     */
    public static void setResCache(String key, SysRes resData)
    {
        SpringUtils.getBean(RedisCacheManager.class).setCacheObject(RedisCacheKey.SYS_RES,key, resData);
    }

    /**
     * 清空字典缓存
     */
    public static void clearResCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisCacheManager.class).keys(RedisCacheKey.SYS_RES.getCacheKey() + "*");
        boolean  n = SpringUtils.getBean(RedisCacheManager.class).deleteObject(keys);
        sys_res_logger.info("删除 keys=" + keys.toString() + "删除标记=" + n);
    }

    /**
     * 清空字典缓存
     */
    public static void clearResKey(Collection<String> keys)
    {
        boolean  n = SpringUtils.getBean(RedisCacheManager.class).deleteObject(keys);
        sys_res_logger.info("清理 keys=" + keys.toString() + "清理标记=" + n);
    }

}
