package com.yuchen.saas.park.service.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuchen.saas.park.api.entity.park.PmgBatHandlerWarning;
import com.yuchen.saas.park.api.entity.park.PmgBatWarningInfo;
import com.yuchen.saas.park.service.service.IHandlerWarningService;
import com.yuchen.saas.park.service.service.IWarningInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName KeyExpiredListener
 * @Description
 * @author 吕海泉
 * @Date 2021/4/8 15:55
 * @Version 1.0
 **/
@Deprecated // modify by qianjiang 2022.9.24 功能不明确代码
@Component
public class KeyExpiredListener implements MessageListener {

    private static Logger logger = LoggerFactory.getLogger(KeyExpiredListener.class);

   // @Autowired
    private IHandlerWarningService iHandlerWarningService;

    //@Autowired
    private IWarningInfoService iWarningInfoService;

    //这里是回调函数失效的时候回调用这个函数
    @Override
    public void onMessage(Message message, byte[] pattern) {

        String expiredKey = message.toString();
        if (expiredKey.startsWith("event:")) {
            String id = expiredKey.split(":")[1];
            LambdaQueryWrapper<PmgBatHandlerWarning> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmgBatHandlerWarning::getId, Long.valueOf(id));
            PmgBatHandlerWarning pmgBatHandlerWarning = iHandlerWarningService.getOne(queryWrapper);

            // 根据pk、dn、eventTime查询该时间正负两分钟的数据
            LambdaQueryWrapper<PmgBatWarningInfo> warningInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            warningInfoLambdaQueryWrapper.eq(PmgBatWarningInfo::getProductKey, pmgBatHandlerWarning.getProductKey())
                    .eq(PmgBatWarningInfo::getDeviceName, pmgBatHandlerWarning.getProductName())
                    .between(PmgBatWarningInfo::getEventTime, pmgBatHandlerWarning.getEventTime() - 120,
                            pmgBatHandlerWarning.getEventTime() + 120);
            List<PmgBatWarningInfo> pmgBatWarningInfoList = iWarningInfoService.list(warningInfoLambdaQueryWrapper);

            for (PmgBatWarningInfo pmgBatWarningInfo : pmgBatWarningInfoList) {
                pmgBatWarningInfo.setIsHandling("2");
                iWarningInfoService.updateById(pmgBatWarningInfo);
            }
        }

    }
}


