|
|
@@ -0,0 +1,245 @@
|
|
|
+package com.zksy.base.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
+import com.zksy.base.domain.EquipmentStatus;
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
+import com.zksy.base.domain.dto.EquipmentBatchCreateDTO;
|
|
|
+import com.zksy.base.domain.dto.EquipmentBatchCreateResult;
|
|
|
+import com.zksy.base.enums.AlarmStatusEnum;
|
|
|
+import com.zksy.base.enums.CurrentStatusEnum;
|
|
|
+import com.zksy.base.enums.DistrictEnum;
|
|
|
+import com.zksy.base.enums.OnlineStatusEnum;
|
|
|
+import com.zksy.base.mapper.EquipmentStatusMapper;
|
|
|
+import com.zksy.base.service.EquipmentBaseService;
|
|
|
+import com.zksy.base.service.EquipmentBatchCreateService;
|
|
|
+import com.zksy.base.service.EquipmentTypeService;
|
|
|
+import com.zksy.common.exception.ServiceException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.dao.DataIntegrityViolationException;
|
|
|
+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>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class EquipmentBatchCreateServiceImpl implements EquipmentBatchCreateService {
|
|
|
+
|
|
|
+ /** 已存在编码的统一提示:不做任何修改。 */
|
|
|
+ private static final String DUPLICATE_REASON = "设备编码已存在,未做任何修改";
|
|
|
+
|
|
|
+ private static final int CODE_QUERY_BATCH = 500;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private EquipmentBaseService equipmentBaseService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private EquipmentTypeService equipmentTypeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private EquipmentStatusMapper equipmentStatusMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public EquipmentBatchCreateResult batchCreate(List<EquipmentBatchCreateDTO> rows) {
|
|
|
+ EquipmentBatchCreateResult result = new EquipmentBatchCreateResult();
|
|
|
+ if (rows == null || rows.isEmpty()) {
|
|
|
+ result.finish();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Integer> typeIndex = loadTypeIndex();
|
|
|
+ Set<String> existingCodes = loadExistingCodes(rows);
|
|
|
+ Set<String> createdCodes = new HashSet<>();
|
|
|
+
|
|
|
+ for (EquipmentBatchCreateDTO row : rows) {
|
|
|
+ String code = trim(row == null ? null : row.getEquipmentCode());
|
|
|
+ String reason = validate(row, code, typeIndex, existingCodes, createdCodes);
|
|
|
+ if (reason != null) {
|
|
|
+ result.addFailure(code, reason);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ EquipmentBase entity = buildEntity(row, code, typeIndex);
|
|
|
+ if (!equipmentBaseService.save(entity)) {
|
|
|
+ result.addFailure(code, "设备保存失败");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ initEquipmentStatus(entity.getEquipmentId());
|
|
|
+ result.setSuccessCount(result.getSuccessCount() + 1);
|
|
|
+ createdCodes.add(code);
|
|
|
+ } catch (DataIntegrityViolationException e) {
|
|
|
+ result.addFailure(code, DUPLICATE_REASON);
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ result.addFailure(code, e.getMessage() == null ? "设备保存失败" : e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("设备批量创建失败,设备编码={}", code, e);
|
|
|
+ result.addFailure(code, e.getMessage() == null ? "设备保存失败" : e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ result.finish();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 以 equipment_type.type_name 为唯一权威建立「名称 -> 主键 id」索引。 */
|
|
|
+ private Map<String, Integer> loadTypeIndex() {
|
|
|
+ Map<String, Integer> index = new HashMap<>();
|
|
|
+ List<EquipmentType> types = equipmentTypeService.list();
|
|
|
+ if (types == null) {
|
|
|
+ return index;
|
|
|
+ }
|
|
|
+ for (EquipmentType type : types) {
|
|
|
+ if (type == null || type.getId() == null || isBlank(type.getTypeName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ index.putIfAbsent(normalize(type.getTypeName()), type.getId());
|
|
|
+ }
|
|
|
+ return index;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 一次性预查库中已存在的设备编码,避免逐行查询。 */
|
|
|
+ private Set<String> loadExistingCodes(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<>();
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String validate(EquipmentBatchCreateDTO row, String code,
|
|
|
+ Map<String, Integer> typeIndex,
|
|
|
+ Set<String> existingCodes, Set<String> createdCodes) {
|
|
|
+ if (row == null) {
|
|
|
+ return "数据行不能为空";
|
|
|
+ }
|
|
|
+ if (code.isEmpty()) {
|
|
|
+ return "设备编码不能为空";
|
|
|
+ }
|
|
|
+ if (isBlank(row.getEquipmentName())) {
|
|
|
+ return "设备名称不能为空";
|
|
|
+ }
|
|
|
+ if (isBlank(row.getEquipmentTypeName())) {
|
|
|
+ return "设备类型不能为空";
|
|
|
+ }
|
|
|
+ String typeName = trim(row.getEquipmentTypeName());
|
|
|
+ if (resolveTypeId(typeName, typeIndex) == null) {
|
|
|
+ return "设备类型「" + typeName + "」在 equipment_type.type_name 中不存在";
|
|
|
+ }
|
|
|
+ if (isBlank(row.getDistrictName())) {
|
|
|
+ return "所属片区不能为空";
|
|
|
+ }
|
|
|
+ String districtName = trim(row.getDistrictName());
|
|
|
+ if (resolveDistrict(districtName) == null) {
|
|
|
+ return "所属片区「" + districtName + "」无效,仅支持城南、城北、太常";
|
|
|
+ }
|
|
|
+ String longitude = trim(row.getLongitude());
|
|
|
+ if (!longitude.isEmpty() && !isNumber(longitude)) {
|
|
|
+ return "经度格式不正确,应为数字";
|
|
|
+ }
|
|
|
+ String latitude = trim(row.getLatitude());
|
|
|
+ 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) {
|
|
|
+ EquipmentBase entity = new EquipmentBase();
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ String latitude = trim(row.getLatitude());
|
|
|
+ if (!latitude.isEmpty()) {
|
|
|
+ entity.setLatitude(new BigDecimal(latitude));
|
|
|
+ }
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer resolveTypeId(String typeName, Map<String, Integer> typeIndex) {
|
|
|
+ return typeIndex.get(normalize(typeName));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer resolveDistrict(String districtName) {
|
|
|
+ for (DistrictEnum item : DistrictEnum.values()) {
|
|
|
+ if (item.getDesc().equals(districtName)) {
|
|
|
+ return item.getCode();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 与单个新增一致:新增设备后初始化一条「在用 / 正常 / 离线」状态记录。 */
|
|
|
+ private void initEquipmentStatus(String equipmentId) {
|
|
|
+ EquipmentStatus status = new EquipmentStatus();
|
|
|
+ status.setEquipmentId(equipmentId);
|
|
|
+ status.setCurrentStatus(CurrentStatusEnum.IN_USE.getCode());
|
|
|
+ status.setAlarmStatus(AlarmStatusEnum.NORMAL.getCode());
|
|
|
+ status.setOnlineStatus(OnlineStatusEnum.OFFLINE.getCode());
|
|
|
+ status.setStatusUpdateTime(LocalDateTime.now());
|
|
|
+ equipmentStatusMapper.insert(status);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNumber(String value) {
|
|
|
+ try {
|
|
|
+ new BigDecimal(value);
|
|
|
+ return true;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 仅归一化全角括号、加号与空白,兼容填写写法差异,不做名称猜测映射。 */
|
|
|
+ private static String normalize(String value) {
|
|
|
+ return trim(value).replace("(", "(").replace(")", ")")
|
|
|
+ .replace("+", "+").replaceAll("\\s+", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String trim(String value) {
|
|
|
+ return value == null ? "" : value.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isBlank(String value) {
|
|
|
+ return value == null || value.trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String nullIfBlank(String value) {
|
|
|
+ return isBlank(value) ? null : value.trim();
|
|
|
+ }
|
|
|
+}
|