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

This commit is contained in:
YunaiV
2025-06-29 10:50:40 +08:00
parent 233afd7a59
commit fd00fb2954
5 changed files with 17 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ public interface ErrorCodeConstants {
ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在"); ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在");
ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除"); ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除");
ErrorCode PRODUCT_STATUS_NOT_ALLOW_THING_MODEL = new ErrorCode(1_050_001_003, "产品状是发布状态,不允许操作物模型"); ErrorCode PRODUCT_STATUS_NOT_ALLOW_THING_MODEL = new ErrorCode(1_050_001_003, "产品状是发布状态,不允许操作物模型");
ErrorCode PRODUCT_DELETE_FAIL_HAS_DEVICE = new ErrorCode(1_050_001_004, "产品下存在设备,不允许删除");
// ========== 产品物模型 1-050-002-000 ============ // ========== 产品物模型 1-050-002-000 ============
ErrorCode THING_MODEL_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在"); ErrorCode THING_MODEL_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在");

View File

@@ -56,6 +56,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
private IotDeviceMapper deviceMapper; private IotDeviceMapper deviceMapper;
@Resource @Resource
@Lazy // 延迟加载,解决循环依赖
private IotProductService productService; private IotProductService productService;
@Resource @Resource
@Lazy // 延迟加载,解决循环依赖 @Lazy // 延迟加载,解决循环依赖

View File

@@ -23,6 +23,7 @@ import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -58,6 +59,7 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
@Resource @Resource
private IotThingModelService thingModelService; private IotThingModelService thingModelService;
@Resource @Resource
@Lazy // 延迟加载,解决循环依赖
private IotProductService productService; private IotProductService productService;
@Resource @Resource

View File

@@ -10,12 +10,12 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper; import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper;
import cn.iocoder.yudao.module.iot.dal.redis.RedisKeyConstants; import cn.iocoder.yudao.module.iot.dal.redis.RedisKeyConstants;
import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum; import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService; import cn.iocoder.yudao.module.iot.service.device.property.IotDevicePropertyService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@@ -40,8 +40,9 @@ public class IotProductServiceImpl implements IotProductService {
private IotProductMapper productMapper; private IotProductMapper productMapper;
@Resource @Resource
@Lazy // 延迟加载,解决循环依赖
private IotDevicePropertyService devicePropertyDataService; private IotDevicePropertyService devicePropertyDataService;
@Resource
private IotDeviceService deviceService;
@Override @Override
public Long createProduct(IotProductSaveReqVO createReqVO) { public Long createProduct(IotProductSaveReqVO createReqVO) {
@@ -65,6 +66,7 @@ public class IotProductServiceImpl implements IotProductService {
IotProductDO iotProductDO = validateProductExists(updateReqVO.getId()); IotProductDO iotProductDO = validateProductExists(updateReqVO.getId());
// 1.2 发布状态不可更新 // 1.2 发布状态不可更新
validateProductStatus(iotProductDO); validateProductStatus(iotProductDO);
// 2. 更新 // 2. 更新
IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class); IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class);
productMapper.updateById(updateObj); productMapper.updateById(updateObj);
@@ -74,9 +76,14 @@ public class IotProductServiceImpl implements IotProductService {
@CacheEvict(value = RedisKeyConstants.PRODUCT, key = "#id") @CacheEvict(value = RedisKeyConstants.PRODUCT, key = "#id")
public void deleteProduct(Long id) { public void deleteProduct(Long id) {
// 1.1 校验存在 // 1.1 校验存在
IotProductDO iotProductDO = validateProductExists(id); IotProductDO product = validateProductExists(id);
// 1.2 发布状态不可删除 // 1.2 发布状态不可删除
validateProductStatus(iotProductDO); validateProductStatus(product);
// 1.3 校验是否有设备
if (deviceService.getDeviceCountByProductId(id) > 0) {
throw exception(PRODUCT_DELETE_FAIL_HAS_DEVICE);
}
// 2. 删除 // 2. 删除
productMapper.deleteById(id); productMapper.deleteById(id);
} }

View File

@@ -20,6 +20,7 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@@ -47,6 +48,7 @@ public class IotThingModelServiceImpl implements IotThingModelService {
private IotThingModelMapper thingModelMapper; private IotThingModelMapper thingModelMapper;
@Resource @Resource
@Lazy // 延迟加载,解决循环依赖
private IotProductService productService; private IotProductService productService;
@Override @Override