From 56af34af67c95414eb81e56e0704899a6d6b5a27 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=95=B8=E9=9D=9E?= <8153694@qq.com>
Date: Sat, 16 Dec 2023 16:44:22 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=BB=84=E6=9B=B4=E6=96=B0=E8=AE=BE?=
 =?UTF-8?q?=E5=A4=87?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../CustomerKeyExpirationListener.java        | 240 +++++++++---------
 1 file changed, 122 insertions(+), 118 deletions(-)

diff --git a/device-manager/src/main/java/com/mortals/xhx/base/framework/listener/CustomerKeyExpirationListener.java b/device-manager/src/main/java/com/mortals/xhx/base/framework/listener/CustomerKeyExpirationListener.java
index 688c5afe..bbde0697 100644
--- a/device-manager/src/main/java/com/mortals/xhx/base/framework/listener/CustomerKeyExpirationListener.java
+++ b/device-manager/src/main/java/com/mortals/xhx/base/framework/listener/CustomerKeyExpirationListener.java
@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSON;
 import com.mortals.framework.common.Rest;
 import com.mortals.framework.model.BizLogPdu;
 import com.mortals.framework.model.ErrorLogPdu;
+import com.mortals.framework.service.ICacheService;
 import com.mortals.framework.service.IMessageProduceService;
 import com.mortals.xhx.common.code.*;
 import com.mortals.xhx.common.key.RedisKey;
@@ -66,139 +67,142 @@ public class CustomerKeyExpirationListener implements MessageListener {
     private ISmsSetFeign smsSetFeign;
     @Autowired
     private IMessageProduceService messageProducer;
+    @Autowired
+    private ICacheService cacheService;
 
     @Override
     public void onMessage(Message message, byte[] bytes) {
         String key = message.toString();
-        String subStr = StrUtil.removePrefix(key, RedisKey.KEY_DEVICE_ONLINE_CACHE);
-        if (!subStr.equals(key)) {
-            DeviceEntity deviceEntity = deviceService.getExtCache(subStr);
-            if (!ObjectUtils.isEmpty(deviceEntity)) {
-                if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.鍦ㄧ嚎.getValue()) {
-                    deviceEntity.setOfflineTime(new Date());
-                    deviceEntity.setDeviceStatus(DeviceStatusEnum.绂荤嚎.getValue());
-
-                    DeviceEntity entity = new DeviceEntity();
-                    entity.setOfflineTime(new Date());
-                    entity.setUpdateTime(new Date());
-                    entity.setDeviceStatus(DeviceStatusEnum.绂荤嚎.getValue());
-                    DeviceEntity condition = new DeviceEntity();
-                    condition.setId(deviceEntity.getId());
-                    deviceService.getDeviceDao().update(entity, condition);
-                    deviceService.putCache(deviceEntity.getId().toString(), deviceEntity);
-                    log.info("deviceCode:{}锛宒eviceName:{}==>绂荤嚎", deviceEntity.getDeviceCode(), deviceEntity.getDeviceName());
-                    // deviceService.update(deviceEntity);
-                    PlatformEntity platformEntity = platformService.getCache(deviceEntity.getPlatformId().toString());
-                    ProductEntity productEntity = productService.getCache(deviceEntity.getProductId().toString());
-                    if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) {
-                        //閫氱煡绗笁鏂瑰钩鍙�
-                        deviceService.sendThirdParty(deviceEntity, productEntity, platformEntity, DeviceMethodEnum.OFFLINE);
-                        //淇濆瓨涓氬姟娑堟伅涓虹绾�
-                        BizLogPdu bizLogPdu = new BizLogPdu();
-                        bizLogPdu.initAttrValue();
-                        //淇濆瓨涓轰骇鍝佸悕绉�
-                        bizLogPdu.setAppName(productEntity.getProductCode());
-                        bizLogPdu.setTraceID(IdUtil.objectId());
-                        bizLogPdu.setUserCode("system");
-                        bizLogPdu.setDeviceCode(deviceEntity.getDeviceCode());
-                        bizLogPdu.setEventTopic("offline");
-                        bizLogPdu.setEventTopicName(LogTypeEnum.涓婃姤浜嬩欢.name());
-                        bizLogPdu.setMsg(Base64.encode("璁惧涓嬬嚎"));
-                        bizLogPdu.setLogLevel("INFO");
-                        bizLogPdu.setLogTime(new Date());
-                        messageProducer.syncBizSend(bizLogPdu);
-
-                        ErrorLogPdu errorLogPdu = new ErrorLogPdu();
-                        errorLogPdu.initAttrValue();
-                        errorLogPdu.setTraceID(IdUtil.objectId());
-                        errorLogPdu.setAppName(productEntity.getProductCode());
-                        errorLogPdu.setMessage(deviceEntity.getDeviceName()+deviceEntity.getDeviceCode()+"璁惧涓嬬嚎!");
-                        errorLogPdu.setPlatform("webos");
-                        errorLogPdu.setCulprit("");
-                        errorLogPdu.setTags("");
-                        errorLogPdu.setLevel("INFO");
-                        errorLogPdu.setEnvironment("");
-                        errorLogPdu.setReleaseVersion("");
-                        errorLogPdu.setFingerprint(LogTypeEnum.涓婃姤浜嬩欢.name());
-                        errorLogPdu.setThreadNo(Thread.currentThread().toString());
-                        errorLogPdu.setErrorStack(Thread.currentThread()+deviceEntity.getDeviceName()+deviceEntity.getDeviceCode()+"璁惧涓嬬嚎!");
-                        errorLogPdu.setContext("");
-                        errorLogPdu.setExtra("");
-                        errorLogPdu.setLogTime(new Date());
-                        messageProducer.syncErrorSend(errorLogPdu);
-
-                        //鐢熸垚寮傚父鏃ュ織
-                    }
+        //瑙e喅鍒嗗竷寮忕郴缁� 閲嶅鑾峰彇骞挎挱娑堟伅
+        boolean setnx = cacheService.setnx(key, 1, 10);
+
+        if (setnx) {
+            String subStr = StrUtil.removePrefix(key, RedisKey.KEY_DEVICE_ONLINE_CACHE);
+            if (!subStr.equals(key)) {
+                DeviceEntity deviceEntity = deviceService.getExtCache(subStr);
+                if (!ObjectUtils.isEmpty(deviceEntity)) {
+                    if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.鍦ㄧ嚎.getValue()) {
+                        deviceEntity.setOfflineTime(new Date());
+                        deviceEntity.setDeviceStatus(DeviceStatusEnum.绂荤嚎.getValue());
+
+                        DeviceEntity entity = new DeviceEntity();
+                        entity.setOfflineTime(new Date());
+                        entity.setUpdateTime(new Date());
+                        entity.setDeviceStatus(DeviceStatusEnum.绂荤嚎.getValue());
+                        DeviceEntity condition = new DeviceEntity();
+                        condition.setId(deviceEntity.getId());
+                        deviceService.getDeviceDao().update(entity, condition);
+                        deviceService.putCache(deviceEntity.getId().toString(), deviceEntity);
+                        log.info("deviceCode:{}锛宒eviceName:{}==>绂荤嚎", deviceEntity.getDeviceCode(), deviceEntity.getDeviceName());
+                        // deviceService.update(deviceEntity);
+                        PlatformEntity platformEntity = platformService.getCache(deviceEntity.getPlatformId().toString());
+                        ProductEntity productEntity = productService.getCache(deviceEntity.getProductId().toString());
+                        if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) {
+                            //閫氱煡绗笁鏂瑰钩鍙�
+                            deviceService.sendThirdParty(deviceEntity, productEntity, platformEntity, DeviceMethodEnum.OFFLINE);
+                            //淇濆瓨涓氬姟娑堟伅涓虹绾�
+                            BizLogPdu bizLogPdu = new BizLogPdu();
+                            bizLogPdu.initAttrValue();
+                            //淇濆瓨涓轰骇鍝佸悕绉�
+                            bizLogPdu.setAppName(productEntity.getProductCode());
+                            bizLogPdu.setTraceID(IdUtil.objectId());
+                            bizLogPdu.setUserCode("system");
+                            bizLogPdu.setDeviceCode(deviceEntity.getDeviceCode());
+                            bizLogPdu.setEventTopic("offline");
+                            bizLogPdu.setEventTopicName(LogTypeEnum.涓婃姤浜嬩欢.name());
+                            bizLogPdu.setMsg(Base64.encode("璁惧涓嬬嚎"));
+                            bizLogPdu.setLogLevel("INFO");
+                            bizLogPdu.setLogTime(new Date());
+                            messageProducer.syncBizSend(bizLogPdu);
+
+                            ErrorLogPdu errorLogPdu = new ErrorLogPdu();
+                            errorLogPdu.initAttrValue();
+                            errorLogPdu.setTraceID(IdUtil.objectId());
+                            errorLogPdu.setAppName(productEntity.getProductCode());
+                            errorLogPdu.setMessage(deviceEntity.getDeviceName() + deviceEntity.getDeviceCode() + "璁惧涓嬬嚎!");
+                            errorLogPdu.setPlatform("webos");
+                            errorLogPdu.setCulprit("");
+                            errorLogPdu.setTags("");
+                            errorLogPdu.setLevel("INFO");
+                            errorLogPdu.setEnvironment("");
+                            errorLogPdu.setReleaseVersion("");
+                            errorLogPdu.setFingerprint(LogTypeEnum.涓婃姤浜嬩欢.name());
+                            errorLogPdu.setThreadNo(Thread.currentThread().toString());
+                            errorLogPdu.setErrorStack(Thread.currentThread() + deviceEntity.getDeviceName() + deviceEntity.getDeviceCode() + "璁惧涓嬬嚎!");
+                            errorLogPdu.setContext("");
+                            errorLogPdu.setExtra("");
+                            errorLogPdu.setLogTime(new Date());
+                            messageProducer.syncErrorSend(errorLogPdu);
+
+                            //鐢熸垚寮傚父鏃ュ織
+                        }
 
-                    //淇濆瓨绂荤嚎鍛婅娑堟伅
-                    DeviceAlarmInfoEntity alarmInfoEntity = new DeviceAlarmInfoEntity();
-                    alarmInfoEntity.initAttrValue();
-                    alarmInfoEntity.setAlarmDevice(deviceEntity.getId());
-                    alarmInfoEntity.setSiteId(deviceEntity.getSiteId());
-                    alarmInfoEntity.setAlarmTime(new Date());
-                    alarmInfoEntity.setDeviceName(deviceEntity.getDeviceName());
-                    alarmInfoEntity.setDeviceCode(deviceEntity.getDeviceCode());
-                    alarmInfoEntity.setAlarmContent(String.format("璁惧鍛婅锛�%s璁惧锛�%s宸茬绾匡紝璇锋敞鎰忔鏌ワ紒", productEntity.getProductName(), deviceEntity.getDeviceName() + ":" + deviceEntity.getDeviceName()));
-                    alarmInfoEntity.setAlarmType(AlarmTypeEnum.绂荤嚎.getValue());
-                    alarmInfoEntity.setAlarmLevel(AlarmLevelEnum.涓€鑸�.getValue());
-                    alarmInfoEntity.setAlarmStatus(AlarmStatusEnum.鏈竻闄�.getValue());
-                    alarmInfoEntity.setAlarmReceivePersonnel(deviceEntity.getLeadingOfficial());
-                    alarmInfoEntity.setReceivePersonnelTelephone(deviceEntity.getLeadingOfficialTelephone());
-                    alarmInfoEntity.setCreateTime(new Date());
-                    alarmInfoEntity.setCreateUserId(1L);
-                    // TODO: 2022/6/23 鍛婅淇℃伅淇濆瓨涓庡彂閫�
-                    AlarmConfigEntity alarmConfigEntity = alarmConfigService.selectOne(new AlarmConfigQuery().productId(productEntity.getId()));
-                    if (!ObjectUtils.isEmpty(alarmConfigEntity)) {
+                        //淇濆瓨绂荤嚎鍛婅娑堟伅
+                        DeviceAlarmInfoEntity alarmInfoEntity = new DeviceAlarmInfoEntity();
+                        alarmInfoEntity.initAttrValue();
+                        alarmInfoEntity.setAlarmDevice(deviceEntity.getId());
+                        alarmInfoEntity.setSiteId(deviceEntity.getSiteId());
+                        alarmInfoEntity.setAlarmTime(new Date());
+                        alarmInfoEntity.setDeviceName(deviceEntity.getDeviceName());
+                        alarmInfoEntity.setDeviceCode(deviceEntity.getDeviceCode());
+                        alarmInfoEntity.setAlarmContent(String.format("璁惧鍛婅锛�%s璁惧锛�%s宸茬绾匡紝璇锋敞鎰忔鏌ワ紒", productEntity.getProductName(), deviceEntity.getDeviceName() + ":" + deviceEntity.getDeviceName()));
+                        alarmInfoEntity.setAlarmType(AlarmTypeEnum.绂荤嚎.getValue());
+                        alarmInfoEntity.setAlarmLevel(AlarmLevelEnum.涓€鑸�.getValue());
+                        alarmInfoEntity.setAlarmStatus(AlarmStatusEnum.鏈竻闄�.getValue());
+                        alarmInfoEntity.setAlarmReceivePersonnel(deviceEntity.getLeadingOfficial());
+                        alarmInfoEntity.setReceivePersonnelTelephone(deviceEntity.getLeadingOfficialTelephone());
+                        alarmInfoEntity.setCreateTime(new Date());
+                        alarmInfoEntity.setCreateUserId(1L);
+                        // TODO: 2022/6/23 鍛婅淇℃伅淇濆瓨涓庡彂閫�
+                        AlarmConfigEntity alarmConfigEntity = alarmConfigService.selectOne(new AlarmConfigQuery().productId(productEntity.getId()));
+                        if (!ObjectUtils.isEmpty(alarmConfigEntity)) {
 /*                        if (alarmConfigEntity.getAlarmPusW1ay() == AlarmPusW1ayEnum.绔欏唴淇℃伅.getValue()) {
                         }*/
-                        if(EnabledEnum.鍚敤.getValue()==alarmConfigEntity.getEnabled()){
-                            alarmInfoEntity.setProductId(productEntity.getId());
-                            alarmInfoEntity.setProductCode(productEntity.getProductCode());
-                            alarmInfoEntity.setProductName(productEntity.getProductName());
-                            alarmInfoEntity.setPush(YesNoEnum.YES.getValue());
-                            deviceAlarmInfoService.save(alarmInfoEntity);
-                        }
-
-                        if (alarmConfigEntity.getAlarmPusW1ay() == AlarmPusW1ayEnum.鐭俊.getValue()) {
-                            // TODO: 2022/7/4 鍙戦€佺煭淇�
-                            Rest<RespData<List<SmsSetPdu>>> respDataRest = smsSetFeign.list(new SmsSetPdu().siteId(deviceEntity.getSiteId()));
-                            if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
-                                List<SmsSetPdu> data = respDataRest.getData().getData();
-                                if (!ObjectUtils.isEmpty(data)) {
-                                    Integer messageoff = data.get(0).getMessageoff();
-                                    if (messageoff == YesNoEnum.YES.getValue()) {
-                                        // todo 鍙戦€佺煭淇�
-                                        //鐭俊浣跨敤妯℃澘锛屽 璁惧鍛婅锛歿浜у搧}璁惧锛歿璁惧鍚嶇О鍔犵紪鐮亇宸茬绾匡紝璇锋敞鎰忔鏌ワ紒
-                                        int count = deviceAlarmInfoService.count(new DeviceAlarmInfoQuery().alarmDevice(deviceEntity.getId()), null);
-                                        if (count > 0) {
-                                            AlarmSmsSendEntity alarmSmsSendEntity = new AlarmSmsSendEntity();
-                                            alarmSmsSendEntity.initAttrValue();
-                                            alarmSmsSendEntity.setId(IdUtil.getSnowflake().nextId());
-                                            alarmSmsSendEntity.setSiteId(deviceEntity.getSiteId());
-                                            alarmSmsSendEntity.setMobile(deviceEntity.getLeadingOfficial());
-                                            alarmSmsSendEntity.setReceiver(deviceEntity.getLeadingOfficialTelephone());
-                                            Map<String, String> map = new HashMap<>();
-                                            map.put("1", productEntity.getProductName());
-                                            map.put("2", deviceEntity.getDeviceName() + ":" + deviceEntity.getDeviceName());
-                                            alarmSmsSendEntity.setSendMess(JSON.toJSONString(map));
-                                            alarmSmsSendEntity.setSendStatus(SendStatusEnum.鏈彂閫�.getValue());
-                                            alarmSmsSendService.save(alarmSmsSendEntity, null);
-                                        }
-                                    } else {
-                                        //鎺ㄩ€佺珯绫�
-
+                            if (EnabledEnum.鍚敤.getValue() == alarmConfigEntity.getEnabled()) {
+                                alarmInfoEntity.setProductId(productEntity.getId());
+                                alarmInfoEntity.setProductCode(productEntity.getProductCode());
+                                alarmInfoEntity.setProductName(productEntity.getProductName());
+                                alarmInfoEntity.setPush(YesNoEnum.YES.getValue());
+                                deviceAlarmInfoService.save(alarmInfoEntity);
+                            }
 
+                            if (alarmConfigEntity.getAlarmPusW1ay() == AlarmPusW1ayEnum.鐭俊.getValue()) {
+                                // TODO: 2022/7/4 鍙戦€佺煭淇�
+                                Rest<RespData<List<SmsSetPdu>>> respDataRest = smsSetFeign.list(new SmsSetPdu().siteId(deviceEntity.getSiteId()));
+                                if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
+                                    List<SmsSetPdu> data = respDataRest.getData().getData();
+                                    if (!ObjectUtils.isEmpty(data)) {
+                                        Integer messageoff = data.get(0).getMessageoff();
+                                        if (messageoff == YesNoEnum.YES.getValue()) {
+                                            // todo 鍙戦€佺煭淇�
+                                            //鐭俊浣跨敤妯℃澘锛屽 璁惧鍛婅锛歿浜у搧}璁惧锛歿璁惧鍚嶇О鍔犵紪鐮亇宸茬绾匡紝璇锋敞鎰忔鏌ワ紒
+                                            int count = deviceAlarmInfoService.count(new DeviceAlarmInfoQuery().alarmDevice(deviceEntity.getId()), null);
+                                            if (count > 0) {
+                                                AlarmSmsSendEntity alarmSmsSendEntity = new AlarmSmsSendEntity();
+                                                alarmSmsSendEntity.initAttrValue();
+                                                alarmSmsSendEntity.setId(IdUtil.getSnowflake().nextId());
+                                                alarmSmsSendEntity.setSiteId(deviceEntity.getSiteId());
+                                                alarmSmsSendEntity.setMobile(deviceEntity.getLeadingOfficial());
+                                                alarmSmsSendEntity.setReceiver(deviceEntity.getLeadingOfficialTelephone());
+                                                Map<String, String> map = new HashMap<>();
+                                                map.put("1", productEntity.getProductName());
+                                                map.put("2", deviceEntity.getDeviceName() + ":" + deviceEntity.getDeviceName());
+                                                alarmSmsSendEntity.setSendMess(JSON.toJSONString(map));
+                                                alarmSmsSendEntity.setSendStatus(SendStatusEnum.鏈彂閫�.getValue());
+                                                alarmSmsSendService.save(alarmSmsSendEntity, null);
+                                            }
+                                        } else {
+                                            //鎺ㄩ€佺珯绫�
 
 
+                                        }
                                     }
                                 }
                             }
                         }
-                    }
-
 
-
-                   // deviceAlarmInfoService.save(alarmInfoEntity);
+                        // deviceAlarmInfoService.save(alarmInfoEntity);
+                    }
                 }
             }
         }
-- 
2.24.3