Commit fd00fb29546d35d737951bf93945281eaa545389

Authored by YunaiV
1 parent 233afd7a

feat:【IoT 物联网】新增产品删除失败错误码,优化产品删除逻辑以防止删除存在设备的产品

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
@@ -14,6 +14,7 @@ public interface ErrorCodeConstants { @@ -14,6 +14,7 @@ public interface ErrorCodeConstants {
14 ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在"); 14 ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在");
15 ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除"); 15 ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除");
16 ErrorCode PRODUCT_STATUS_NOT_ALLOW_THING_MODEL = new ErrorCode(1_050_001_003, "产品状是发布状态,不允许操作物模型"); 16 ErrorCode PRODUCT_STATUS_NOT_ALLOW_THING_MODEL = new ErrorCode(1_050_001_003, "产品状是发布状态,不允许操作物模型");
  17 + ErrorCode PRODUCT_DELETE_FAIL_HAS_DEVICE = new ErrorCode(1_050_001_004, "产品下存在设备,不允许删除");
17 18
18 // ========== 产品物模型 1-050-002-000 ============ 19 // ========== 产品物模型 1-050-002-000 ============
19 ErrorCode THING_MODEL_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在"); 20 ErrorCode THING_MODEL_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在");
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
@@ -56,6 +56,7 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -56,6 +56,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
56 private IotDeviceMapper deviceMapper; 56 private IotDeviceMapper deviceMapper;
57 57
58 @Resource 58 @Resource
  59 + @Lazy // 延迟加载,解决循环依赖
59 private IotProductService productService; 60 private IotProductService productService;
60 @Resource 61 @Resource
61 @Lazy // 延迟加载,解决循环依赖 62 @Lazy // 延迟加载,解决循环依赖
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/property/IotDevicePropertyServiceImpl.java
@@ -23,6 +23,7 @@ import cn.iocoder.yudao.module.iot.service.product.IotProductService; @@ -23,6 +23,7 @@ import cn.iocoder.yudao.module.iot.service.product.IotProductService;
23 import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; 23 import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
24 import jakarta.annotation.Resource; 24 import jakarta.annotation.Resource;
25 import lombok.extern.slf4j.Slf4j; 25 import lombok.extern.slf4j.Slf4j;
  26 +import org.springframework.context.annotation.Lazy;
26 import org.springframework.scheduling.annotation.Async; 27 import org.springframework.scheduling.annotation.Async;
27 import org.springframework.stereotype.Service; 28 import org.springframework.stereotype.Service;
28 29
@@ -58,6 +59,7 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { @@ -58,6 +59,7 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
58 @Resource 59 @Resource
59 private IotThingModelService thingModelService; 60 private IotThingModelService thingModelService;
60 @Resource 61 @Resource
  62 + @Lazy // 延迟加载,解决循环依赖
61 private IotProductService productService; 63 private IotProductService productService;
62 64
63 @Resource 65 @Resource
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java
@@ -10,12 +10,12 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; @@ -10,12 +10,12 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
10 import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper; 10 import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper;
11 import cn.iocoder.yudao.module.iot.dal.redis.RedisKeyConstants; 11 import cn.iocoder.yudao.module.iot.dal.redis.RedisKeyConstants;
12 import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum; 12 import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
  13 +import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
13 import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; 14 import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService;
14 import com.baomidou.dynamic.datasource.annotation.DSTransactional; 15 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
15 import jakarta.annotation.Resource; 16 import jakarta.annotation.Resource;
16 import org.springframework.cache.annotation.CacheEvict; 17 import org.springframework.cache.annotation.CacheEvict;
17 import org.springframework.cache.annotation.Cacheable; 18 import org.springframework.cache.annotation.Cacheable;
18 -import org.springframework.context.annotation.Lazy;  
19 import org.springframework.stereotype.Service; 19 import org.springframework.stereotype.Service;
20 import org.springframework.validation.annotation.Validated; 20 import org.springframework.validation.annotation.Validated;
21 21
@@ -40,8 +40,9 @@ public class IotProductServiceImpl implements IotProductService { @@ -40,8 +40,9 @@ public class IotProductServiceImpl implements IotProductService {
40 private IotProductMapper productMapper; 40 private IotProductMapper productMapper;
41 41
42 @Resource 42 @Resource
43 - @Lazy // 延迟加载,解决循环依赖  
44 private IotDevicePropertyService devicePropertyDataService; 43 private IotDevicePropertyService devicePropertyDataService;
  44 + @Resource
  45 + private IotDeviceService deviceService;
45 46
46 @Override 47 @Override
47 public Long createProduct(IotProductSaveReqVO createReqVO) { 48 public Long createProduct(IotProductSaveReqVO createReqVO) {
@@ -65,6 +66,7 @@ public class IotProductServiceImpl implements IotProductService { @@ -65,6 +66,7 @@ public class IotProductServiceImpl implements IotProductService {
65 IotProductDO iotProductDO = validateProductExists(updateReqVO.getId()); 66 IotProductDO iotProductDO = validateProductExists(updateReqVO.getId());
66 // 1.2 发布状态不可更新 67 // 1.2 发布状态不可更新
67 validateProductStatus(iotProductDO); 68 validateProductStatus(iotProductDO);
  69 +
68 // 2. 更新 70 // 2. 更新
69 IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class); 71 IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class);
70 productMapper.updateById(updateObj); 72 productMapper.updateById(updateObj);
@@ -74,9 +76,14 @@ public class IotProductServiceImpl implements IotProductService { @@ -74,9 +76,14 @@ public class IotProductServiceImpl implements IotProductService {
74 @CacheEvict(value = RedisKeyConstants.PRODUCT, key = "#id") 76 @CacheEvict(value = RedisKeyConstants.PRODUCT, key = "#id")
75 public void deleteProduct(Long id) { 77 public void deleteProduct(Long id) {
76 // 1.1 校验存在 78 // 1.1 校验存在
77 - IotProductDO iotProductDO = validateProductExists(id); 79 + IotProductDO product = validateProductExists(id);
78 // 1.2 发布状态不可删除 80 // 1.2 发布状态不可删除
79 - validateProductStatus(iotProductDO); 81 + validateProductStatus(product);
  82 + // 1.3 校验是否有设备
  83 + if (deviceService.getDeviceCountByProductId(id) > 0) {
  84 + throw exception(PRODUCT_DELETE_FAIL_HAS_DEVICE);
  85 + }
  86 +
80 // 2. 删除 87 // 2. 删除
81 productMapper.deleteById(id); 88 productMapper.deleteById(id);
82 } 89 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thingmodel/IotThingModelServiceImpl.java
@@ -20,6 +20,7 @@ import jakarta.annotation.Resource; @@ -20,6 +20,7 @@ import jakarta.annotation.Resource;
20 import lombok.extern.slf4j.Slf4j; 20 import lombok.extern.slf4j.Slf4j;
21 import org.springframework.cache.annotation.CacheEvict; 21 import org.springframework.cache.annotation.CacheEvict;
22 import org.springframework.cache.annotation.Cacheable; 22 import org.springframework.cache.annotation.Cacheable;
  23 +import org.springframework.context.annotation.Lazy;
23 import org.springframework.stereotype.Service; 24 import org.springframework.stereotype.Service;
24 import org.springframework.transaction.annotation.Transactional; 25 import org.springframework.transaction.annotation.Transactional;
25 import org.springframework.validation.annotation.Validated; 26 import org.springframework.validation.annotation.Validated;
@@ -47,6 +48,7 @@ public class IotThingModelServiceImpl implements IotThingModelService { @@ -47,6 +48,7 @@ public class IotThingModelServiceImpl implements IotThingModelService {
47 private IotThingModelMapper thingModelMapper; 48 private IotThingModelMapper thingModelMapper;
48 49
49 @Resource 50 @Resource
  51 + @Lazy // 延迟加载,解决循环依赖
50 private IotProductService productService; 52 private IotProductService productService;
51 53
52 @Override 54 @Override