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