zengy 2 долоо хоног өмнө
parent
commit
d72f24c567

+ 11 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/base/service/impl/EquipmentBaseServiceImpl.java

@@ -329,6 +329,10 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         if (count > 0) {
             throw new ServiceException("设备编码已存在,请更换:" + entity.getEquipmentCode());
         }
+        // 显式记录创建/更新时间,不依赖数据库默认值或自动填充
+        LocalDateTime now = LocalDateTime.now();
+        entity.setCreateTime(now);
+        entity.setUpdateTime(now);
         boolean saved;
         try {
             saved = this.save(entity);
@@ -365,6 +369,11 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
                 throw new ServiceException("设备编码已存在:" + entity.getEquipmentCode());
             }
         }
+        LocalDateTime now = LocalDateTime.now();
+        for (EquipmentBase entity : entityList) {
+            entity.setCreateTime(now);
+            entity.setUpdateTime(now);
+        }
         boolean saved = this.saveBatch(entityList);
         if (saved) {
             for (EquipmentBase entity : entityList) {
@@ -417,6 +426,8 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
                 throw new ServiceException("设备编码已被使用:" + newCode);
             }
         }
+        entity.setCreateTime(null);
+        entity.setUpdateTime(LocalDateTime.now());
         return this.updateById(entity);
     }
 

+ 49 - 37
pipe-network-service/zksy-system/src/main/java/com/zksy/base/service/impl/EquipmentBatchCreateServiceImpl.java

@@ -23,28 +23,24 @@ import org.springframework.stereotype.Service;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
  * 设备批量创建实现。
  *
  * <p>不复用单个新增的 {@code saveWithCheck}:单新增按 {@code equipment_type_id} 校验,
- * 批量创建入参是中文类型名,按 {@code equipment_type.type_name} 查表转换后直接落库。
- * 校验口径与单新增保持一致(类型必须存在、编码唯一、初始化 equipment_status),
- * 且不开启整体事务,单行失败不影响其余行。</p>
+ * 批量导入入参是中文类型名,按 {@code equipment_type.type_name} 查表转换后直接落库。
+ * 校验口径与单新增保持一致(类型必须存在、所属片区合法);设备编码已存在时按本次导入
+ * 数据更新设备信息,不再跳过。新增时初始化 equipment_status,更新时不改状态;
+ * 不开启整体事务,单行失败不影响其余行。</p>
  */
 @Slf4j
 @Service
 public class EquipmentBatchCreateServiceImpl implements EquipmentBatchCreateService {
 
-    /** 已存在编码的统一提示:不做任何修改。 */
-    private static final String DUPLICATE_REASON = "设备编码已存在,未做任何修改";
-
     private static final int CODE_QUERY_BATCH = 500;
 
     @Autowired
@@ -65,31 +61,43 @@ public class EquipmentBatchCreateServiceImpl implements EquipmentBatchCreateServ
         }
 
         Map<String, Integer> typeIndex = loadTypeIndex();
-        Set<String> existingCodes = loadExistingCodes(rows);
-        Set<String> createdCodes = new HashSet<>();
+        Map<String, EquipmentBase> existingByCode = loadExistingByCode(rows);
 
         for (EquipmentBatchCreateDTO row : rows) {
             String code = trim(row == null ? null : row.getEquipmentCode());
-            String reason = validate(row, code, typeIndex, existingCodes, createdCodes);
+            String reason = validate(row, code, typeIndex);
             if (reason != null) {
                 result.addFailure(code, reason);
                 continue;
             }
             try {
-                EquipmentBase entity = buildEntity(row, code, typeIndex);
-                if (!equipmentBaseService.save(entity)) {
-                    result.addFailure(code, "设备保存失败");
-                    continue;
+                LocalDateTime now = LocalDateTime.now();
+                EquipmentBase existing = existingByCode.get(code);
+                if (existing != null) {
+                    // 编码已存在:按本次导入数据更新,保留原主键和创建时间
+                    applyImportData(existing, row, code, typeIndex);
+                    existing.setCreateTime(null);
+                    existing.setUpdateTime(now);
+                    if (!equipmentBaseService.updateById(existing)) {
+                        result.addFailure(code, "设备更新失败");
+                        continue;
+                    }
+                } else {
+                    EquipmentBase entity = buildEntity(row, code, typeIndex, now);
+                    if (!equipmentBaseService.save(entity)) {
+                        result.addFailure(code, "设备保存失败");
+                        continue;
+                    }
+                    initEquipmentStatus(entity.getEquipmentId());
+                    existingByCode.put(code, entity);
                 }
-                initEquipmentStatus(entity.getEquipmentId());
                 result.setSuccessCount(result.getSuccessCount() + 1);
-                createdCodes.add(code);
             } catch (DataIntegrityViolationException e) {
-                result.addFailure(code, DUPLICATE_REASON);
+                result.addFailure(code, "设备编码冲突或数据保存失败");
             } catch (ServiceException e) {
                 result.addFailure(code, e.getMessage() == null ? "设备保存失败" : e.getMessage());
             } catch (Exception e) {
-                log.error("设备批量创建失败,设备编码={}", code, e);
+                log.error("设备批量导入失败,设备编码={}", code, e);
                 result.addFailure(code, e.getMessage() == null ? "设备保存失败" : e.getMessage());
             }
         }
@@ -114,28 +122,30 @@ public class EquipmentBatchCreateServiceImpl implements EquipmentBatchCreateServ
         return index;
     }
 
-    /** 一次性预查库中已存在的设备编码,避免逐行查询。 */
-    private Set<String> loadExistingCodes(List<EquipmentBatchCreateDTO> rows) {
+    /** 一次性预查库中已存在的设备,避免逐行查询;命中后直接按本次导入数据更新。 */
+    private Map<String, EquipmentBase> loadExistingByCode(List<EquipmentBatchCreateDTO> rows) {
         List<String> codes = rows.stream()
                 .filter(Objects::nonNull)
                 .map(row -> trim(row.getEquipmentCode()))
                 .filter(code -> !code.isEmpty())
                 .distinct()
                 .collect(Collectors.toList());
-        Set<String> existing = new HashSet<>();
+        Map<String, EquipmentBase> existing = new HashMap<>();
         for (int i = 0; i < codes.size(); i += CODE_QUERY_BATCH) {
             List<String> batch = codes.subList(i, Math.min(i + CODE_QUERY_BATCH, codes.size()));
             equipmentBaseService.list(new LambdaQueryWrapper<EquipmentBase>()
-                            .select(EquipmentBase::getEquipmentCode)
                             .in(EquipmentBase::getEquipmentCode, batch))
-                    .forEach(item -> existing.add(item.getEquipmentCode()));
+                    .forEach(item -> {
+                        if (item != null && item.getEquipmentCode() != null) {
+                            existing.put(item.getEquipmentCode(), item);
+                        }
+                    });
         }
         return existing;
     }
 
     private String validate(EquipmentBatchCreateDTO row, String code,
-                            Map<String, Integer> typeIndex,
-                            Set<String> existingCodes, Set<String> createdCodes) {
+                            Map<String, Integer> typeIndex) {
         if (row == null) {
             return "数据行不能为空";
         }
@@ -167,29 +177,31 @@ public class EquipmentBatchCreateServiceImpl implements EquipmentBatchCreateServ
         if (!latitude.isEmpty() && !isNumber(latitude)) {
             return "纬度格式不正确,应为数字";
         }
-        if (existingCodes.contains(code) || createdCodes.contains(code)) {
-            return DUPLICATE_REASON;
-        }
         return null;
     }
 
     private EquipmentBase buildEntity(EquipmentBatchCreateDTO row, String code,
-                                      Map<String, Integer> typeIndex) {
+                                      Map<String, Integer> typeIndex, LocalDateTime now) {
         EquipmentBase entity = new EquipmentBase();
         entity.setEquipmentCode(code);
+        applyImportData(entity, row, code, typeIndex);
+        entity.setCreateTime(now);
+        entity.setUpdateTime(now);
+        return entity;
+    }
+
+    /** 把本次导入行的可编辑字段覆盖到设备实体;空坐标按清空处理。 */
+    private void applyImportData(EquipmentBase entity, EquipmentBatchCreateDTO row,
+                                 String code, Map<String, Integer> typeIndex) {
+        entity.setEquipmentCode(code);
         entity.setEquipmentName(trim(row.getEquipmentName()));
         entity.setEquipmentTypeId(resolveTypeId(trim(row.getEquipmentTypeName()), typeIndex));
         entity.setDistrict(resolveDistrict(trim(row.getDistrictName())));
         entity.setEquipmentLocation(nullIfBlank(row.getEquipmentLocation()));
         String longitude = trim(row.getLongitude());
-        if (!longitude.isEmpty()) {
-            entity.setLongitude(new BigDecimal(longitude));
-        }
+        entity.setLongitude(longitude.isEmpty() ? null : new BigDecimal(longitude));
         String latitude = trim(row.getLatitude());
-        if (!latitude.isEmpty()) {
-            entity.setLatitude(new BigDecimal(latitude));
-        }
-        return entity;
+        entity.setLatitude(latitude.isEmpty() ? null : new BigDecimal(latitude));
     }
 
     private Integer resolveTypeId(String typeName, Map<String, Integer> typeIndex) {