Commit fd00fb29546d35d737951bf93945281eaa545389
1 parent
233afd7a
feat:【IoT 物联网】新增产品删除失败错误码,优化产品删除逻辑以防止删除存在设备的产品
Showing
5 changed files
with
17 additions
and
4 deletions
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 | 14 | ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在"); |
| 15 | 15 | ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除"); |
| 16 | 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 | 19 | // ========== 产品物模型 1-050-002-000 ============ |
| 19 | 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
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 | 23 | import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; |
| 24 | 24 | import jakarta.annotation.Resource; |
| 25 | 25 | import lombok.extern.slf4j.Slf4j; |
| 26 | +import org.springframework.context.annotation.Lazy; | |
| 26 | 27 | import org.springframework.scheduling.annotation.Async; |
| 27 | 28 | import org.springframework.stereotype.Service; |
| 28 | 29 | |
| ... | ... | @@ -58,6 +59,7 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { |
| 58 | 59 | @Resource |
| 59 | 60 | private IotThingModelService thingModelService; |
| 60 | 61 | @Resource |
| 62 | + @Lazy // 延迟加载,解决循环依赖 | |
| 61 | 63 | private IotProductService productService; |
| 62 | 64 | |
| 63 | 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 | 10 | import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper; |
| 11 | 11 | import cn.iocoder.yudao.module.iot.dal.redis.RedisKeyConstants; |
| 12 | 12 | import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum; |
| 13 | +import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; | |
| 13 | 14 | import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; |
| 14 | 15 | import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
| 15 | 16 | import jakarta.annotation.Resource; |
| 16 | 17 | import org.springframework.cache.annotation.CacheEvict; |
| 17 | 18 | import org.springframework.cache.annotation.Cacheable; |
| 18 | -import org.springframework.context.annotation.Lazy; | |
| 19 | 19 | import org.springframework.stereotype.Service; |
| 20 | 20 | import org.springframework.validation.annotation.Validated; |
| 21 | 21 | |
| ... | ... | @@ -40,8 +40,9 @@ public class IotProductServiceImpl implements IotProductService { |
| 40 | 40 | private IotProductMapper productMapper; |
| 41 | 41 | |
| 42 | 42 | @Resource |
| 43 | - @Lazy // 延迟加载,解决循环依赖 | |
| 44 | 43 | private IotDevicePropertyService devicePropertyDataService; |
| 44 | + @Resource | |
| 45 | + private IotDeviceService deviceService; | |
| 45 | 46 | |
| 46 | 47 | @Override |
| 47 | 48 | public Long createProduct(IotProductSaveReqVO createReqVO) { |
| ... | ... | @@ -65,6 +66,7 @@ public class IotProductServiceImpl implements IotProductService { |
| 65 | 66 | IotProductDO iotProductDO = validateProductExists(updateReqVO.getId()); |
| 66 | 67 | // 1.2 发布状态不可更新 |
| 67 | 68 | validateProductStatus(iotProductDO); |
| 69 | + | |
| 68 | 70 | // 2. 更新 |
| 69 | 71 | IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class); |
| 70 | 72 | productMapper.updateById(updateObj); |
| ... | ... | @@ -74,9 +76,14 @@ public class IotProductServiceImpl implements IotProductService { |
| 74 | 76 | @CacheEvict(value = RedisKeyConstants.PRODUCT, key = "#id") |
| 75 | 77 | public void deleteProduct(Long id) { |
| 76 | 78 | // 1.1 校验存在 |
| 77 | - IotProductDO iotProductDO = validateProductExists(id); | |
| 79 | + IotProductDO product = validateProductExists(id); | |
| 78 | 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 | 87 | // 2. 删除 |
| 81 | 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 | 20 | import lombok.extern.slf4j.Slf4j; |
| 21 | 21 | import org.springframework.cache.annotation.CacheEvict; |
| 22 | 22 | import org.springframework.cache.annotation.Cacheable; |
| 23 | +import org.springframework.context.annotation.Lazy; | |
| 23 | 24 | import org.springframework.stereotype.Service; |
| 24 | 25 | import org.springframework.transaction.annotation.Transactional; |
| 25 | 26 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -47,6 +48,7 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 47 | 48 | private IotThingModelMapper thingModelMapper; |
| 48 | 49 | |
| 49 | 50 | @Resource |
| 51 | + @Lazy // 延迟加载,解决循环依赖 | |
| 50 | 52 | private IotProductService productService; |
| 51 | 53 | |
| 52 | 54 | @Override | ... | ... |