فهرست منبع

设备导入、保存增加sn校验

xucaiqin 2 سال پیش
والد
کامیت
1498e030f8

+ 17 - 0
iot-module/iot-module-manage/iot-module-manage-biz/src/main/java/com/middle/platform/manage/biz/service/IotDeviceService.java

@@ -76,7 +76,10 @@ public class IotDeviceService {
      * @param iotDevicePara
      * @return
      */
+    @Transactional(rollbackFor = Exception.class)
     public Object save(IotDevicePara iotDevicePara) {
+        //校验sn
+        checkDeviceSn(iotDevicePara.getSn());
         IotDevice iotDevice = new IotDevice();
         iotDevice.setProductId(iotDevicePara.getProductId());
         iotDevice.setName(iotDevicePara.getName());
@@ -192,6 +195,7 @@ public class IotDeviceService {
             if (Objects.isNull(iotProduct)) {
                 throw new BusinessException("产品编码[" + deviceImportExcelVo.getCode() + "]产品不存在");
             }
+            checkDeviceSn(deviceImportExcelVo.getSn());
             deviceImportExcelVo.setProductId(iotProduct.getId());
         }
         //入库
@@ -227,4 +231,17 @@ public class IotDeviceService {
         Optional.ofNullable(deviceVoC).ifPresent(product -> cacheService.setKey(String.format(CacheConstant.DEVICE_CACHE, guid), product));
         return deviceVoC;
     }
+
+    /**
+     * 校验设备sn是否唯一
+     *
+     * @param sn
+     */
+    private void checkDeviceSn(String sn) {
+        IotDevice iotDevice = iotDeviceMapper.selectOne(new LambdaQueryWrapper<IotDevice>()
+                .eq(IotDevice::getSn, sn).eq(IotDevice::getDelFlag, Global.UN_DEL).last("limit 1"));
+        if (Objects.nonNull(iotDevice)) {
+            throw new BusinessException("当前设备【" + iotDevice.getSn() + "】已存在");
+        }
+    }
 }