//package com.fs.app.redis;
//
//import cn.hutool.http.HttpRequest;
//import com.alibaba.fastjson.JSON;
//import com.fs.common.constant.FsConstants;
//import com.fs.common.core.redis.RedisCache;
//import com.fs.his.config.FsSysConfig;
//import com.fs.his.utils.ConfigUtil;
//import com.fs.qw.param.QwLoginParam;
//import com.fs.qw.service.IQwUserService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.redis.connection.Message;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
//import org.springframework.data.redis.listener.RedisMessageListenerContainer;
//import org.springframework.stereotype.Component;
//
//import java.nio.charset.StandardCharsets;
//import java.util.concurrent.TimeUnit;
//
//@Component
//public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
//
//    private static final Logger log = LoggerFactory.getLogger(RedisKeyExpirationListener.class);
//
//    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
//        super(listenerContainer);
//    }
//
//    @Autowired
//    private IQwUserService qwUserService;
//    @Autowired
//    private ConfigUtil configUtil;
//
//    @Value("${hook.path}")
//    private String hookPath;
//
//    @Autowired
//    private StringRedisTemplate redisTemplate;  // 使用 RedisTemplate 进行分布式锁
//
//    /**
//     * 针对redis数据失效事件，进行数据处理
//     * @param message
//     * @param pattern
//     */
//    @Override
//    public void onMessage(Message message, byte[] pattern) {
//        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
//        //过期的key
//        String key = new String(message.getBody(),StandardCharsets.UTF_8);
//
//        //判断是否是appKey失效
//        if(key.contains(FsConstants.REDIS_QW_appKey_Active)) {
//
//            log.info("监听Qw过期appKey-redis过期：pattern={},channel={},key={}",new String(pattern),channel,key);
//
//            String lockKey = "lockAppKey:" + key;  // 分布式锁的 key
//
//            // 尝试获取锁，设置 30 秒自动过期，防止死锁
//            Boolean lockAcquired = redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 30, TimeUnit.SECONDS);
//
//            if (Boolean.TRUE.equals(lockAcquired)) {  // 只有一个实例会成功获取锁
//                try {
//                    String[] parts = key.split(":");
//
//                    if (parts.length == 3) {
//                        String corpId = parts[1].trim();
//                        String qwUserId = parts[2].trim();
//
//                        String appKey = qwUserService.selectQwUserByQwUserIdAndCorId(qwUserId, corpId);
//
//                        // 重新调起登录
//                        QwLoginParam loginParam = new QwLoginParam();
//                        loginParam.setAppKey(appKey);
//
////                        HttpRequest.post(hookPath+"/app/qwSop/qwLogin")
////                                .body(JSON.toJSONString(loginParam), "application/json;charset=UTF-8")
////                                .execute().body();
//
////                        HttpRequest.post("http://127.0.0.1:7771/app/qwSop/qwLogin")
////                                .body(JSON.toJSONString(loginParam), "application/json;charset=UTF-8")
////                                .execute().body();
//                        FsSysConfig config = configUtil.getSysConfig();
//                        String domainName = config.getHookUrl();
//                        HttpRequest.post(domainName+"/app/qwSop/qwLogin")
//                                .body(JSON.toJSONString(loginParam),"application/json;charset=UTF-8")
//                                .execute().body();
//                    } else {
//                        System.out.println("监听appKey失效！");
//                    }
//                } finally {
//                    // 释放锁，避免影响后续任务
//                    redisTemplate.delete(lockKey);
//                }
//            } else {
//                log.info("另一个实例已经处理了 key={}，当前实例跳过", key);
//            }
//
//        }
//    }
//}
