Browse Source

[功能修复]修复循环引用

liuf 9 months atrás
parent
commit
d03ad2b1dd

+ 70 - 3
src/main/java/com/tmzn/devicelinkykc/controller/DeviceController.java

@@ -63,7 +63,12 @@ public class DeviceController {
     @Autowired
     private DeviceControlerService deviceControlerService;
 
-  /*  *//**
+
+    @Autowired
+    private RedisCache redisCache;
+
+
+    /*  *//**
      * 接收到充电桩后台提交的云快充连接设备进行保存
      *
      * @param
@@ -128,8 +133,70 @@ public class DeviceController {
 */
     @DeleteMapping("/{pileCode}")
     public AjaxResult delete( @PathVariable("pileCode") String pileCode) {
-        AjaxResult ajaxResult = deviceService.deleteDevice(pileCode);
-        return ajaxResult;
+//        AjaxResult ajaxResult = deviceService.deleteDevice(pileCode);
+
+        QueryWrapper<DeviceStatus> deviceStatusQueryWrapper = new QueryWrapper<>();
+        deviceStatusQueryWrapper.eq("pile_code", pileCode);
+        List<DeviceStatus> deviceStatusList = deviceStatusService.list(deviceStatusQueryWrapper);
+        List<DeviceStatus> collect = deviceStatusList.stream().filter(deviceStatus ->
+                deviceStatus.getGunStatus() == StatusConstant.CHARGING
+        ).collect(Collectors.toList());
+        if (collect.size() > 0) {
+            //设备有正在充电的端口
+            return AjaxResult.error("设备有正在充电端口,不能进行删除!");
+        } else {
+            //断开socket
+            if (socketHandle.existDeviceConnection(pileCode)) {
+                socketHandle.removeDeviceConnection(pileCode);
+            }
+            //断开socket后,删除状态数据
+            try {
+                boolean b1 = deviceStatusService.removeBatchByIds(deviceStatusList);
+            } catch (Exception e) {
+                logger.info("YKC-device:"+pileCode+">删除状态失败");
+                e.printStackTrace();
+            }
+
+            //和计费模板数据
+            QueryWrapper<BillingModel> billingModelQueryWrapper = new QueryWrapper<>();
+            billingModelQueryWrapper.eq("pile_code", pileCode);
+            BillingModel billingModel = billingModelService.getOne(billingModelQueryWrapper);
+            try {
+                boolean b2 = billingModelService.removeById(billingModel);
+            } catch (Exception e) {
+                logger.info("YKC-device:"+pileCode+">删除计费模板失败");
+                e.printStackTrace();
+            }
+
+            //删除设备
+            QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
+            deviceQueryWrapper.eq("pile_code", pileCode);
+            Device device = deviceService.getOne(deviceQueryWrapper);
+            try {
+                boolean b3 = deviceService.removeById(device);
+            } catch (Exception e) {
+                e.printStackTrace();
+                return AjaxResult.error("删除设备失败");
+            }
+            //通讯密钥也要删除掉
+
+            redisCache.deleteCacheMapValue(RedisConstant.YKC_KEY_MAP,pileCode);
+//            redisCache.deleteObject(RedisConstant.KEYS + pileCode);
+
+            if (redisCache.hasKey(RedisConstant.DEVICE_INFO)) {
+                Set<String> imeis = redisCache.getCacheObject(RedisConstant.DEVICE_INFO);
+                logger.info("delete imeis>>>" + device.toString() + ">>>>>" + imeis.size());
+                imeis.stream().forEach(s -> logger.info("s>>" + s));
+                if (imeis.contains(device.getDeviceImei())) {
+                    imeis.remove(device.getDeviceImei());
+                }
+                //redisCache.setCacheObject(RedisConstant.DEVICE_INFO, imeis, 6 * 1000 * 60, TimeUnit.MILLISECONDS);
+                redisCache.setCacheObject(RedisConstant.DEVICE_INFO,imeis);
+            }
+            redisCache.deleteCacheMapValue(RedisConstant.DEVICE_IMEI_PILE_MAP, device.getDeviceImei());
+        }
+        return AjaxResult.success();
+
     }
 
     @GetMapping("order")

+ 1 - 74
src/main/java/com/tmzn/devicelinkykc/service/impl/DeviceServiceImpl.java

@@ -33,83 +33,10 @@ import java.util.stream.Collectors;
 @Slf4j
 public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
 
-    @Autowired
-    private BillingModelService billingModelService;
-
-    @Autowired
-    private DeviceStatusService deviceStatusService;
-
-    @Autowired
-    private SocketHandle socketHandle;
-    @Autowired
-    private RedisCache redisCache;
 
     @Override
     //@Transactional
     public AjaxResult deleteDevice(String pileCode) {
-        QueryWrapper<DeviceStatus> deviceStatusQueryWrapper = new QueryWrapper<>();
-        deviceStatusQueryWrapper.eq("pile_code", pileCode);
-        List<DeviceStatus> deviceStatusList = deviceStatusService.list(deviceStatusQueryWrapper);
-        List<DeviceStatus> collect = deviceStatusList.stream().filter(deviceStatus ->
-                deviceStatus.getGunStatus() == StatusConstant.CHARGING
-        ).collect(Collectors.toList());
-        if (collect.size() > 0) {
-            //设备有正在充电的端口
-            return AjaxResult.error("设备有正在充电端口,不能进行删除!");
-        } else {
-            //断开socket
-            if (socketHandle.existDeviceConnection(pileCode)) {
-                socketHandle.removeDeviceConnection(pileCode);
-            }
-            //断开socket后,删除状态数据
-            try {
-                boolean b1 = deviceStatusService.removeBatchByIds(deviceStatusList);
-            } catch (Exception e) {
-                log.info("YKC-device:"+pileCode+">删除状态失败");
-                e.printStackTrace();
-            }
-
-            //和计费模板数据
-            QueryWrapper<BillingModel> billingModelQueryWrapper = new QueryWrapper<>();
-            billingModelQueryWrapper.eq("pile_code", pileCode);
-            BillingModel billingModel = billingModelService.getOne(billingModelQueryWrapper);
-            try {
-                boolean b2 = billingModelService.removeById(billingModel);
-            } catch (Exception e) {
-                log.info("YKC-device:"+pileCode+">删除计费模板失败");
-                e.printStackTrace();
-            }
-
-            //删除设备
-            QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
-            deviceQueryWrapper.eq("pile_code", pileCode);
-            Device device = this.getOne(deviceQueryWrapper);
-            try {
-                boolean b3 = this.removeById(device);
-            } catch (Exception e) {
-                e.printStackTrace();
-                return AjaxResult.error("删除设备失败");
-            }
-            //通讯密钥也要删除掉
-
-            redisCache.deleteCacheMapValue(RedisConstant.YKC_KEY_MAP,pileCode);
-//            redisCache.deleteObject(RedisConstant.KEYS + pileCode);
-
-            if (redisCache.hasKey(RedisConstant.DEVICE_INFO)) {
-                Set<String> imeis = redisCache.getCacheObject(RedisConstant.DEVICE_INFO);
-                log.info("delete imeis>>>" + device.toString() + ">>>>>" + imeis.size());
-                imeis.stream().forEach(s -> log.info("s>>" + s));
-                if (imeis.contains(device.getDeviceImei())) {
-                    imeis.remove(device.getDeviceImei());
-                }
-                //redisCache.setCacheObject(RedisConstant.DEVICE_INFO, imeis, 6 * 1000 * 60, TimeUnit.MILLISECONDS);
-                redisCache.setCacheObject(RedisConstant.DEVICE_INFO,imeis);
-
-            }
-
-            redisCache.deleteCacheMapValue(RedisConstant.DEVICE_IMEI_PILE_MAP, device.getDeviceImei());
-
-        }
-        return AjaxResult.success();
+        return null;
     }
 }