|
|
@@ -16,7 +16,10 @@ import java.time.LocalDateTime;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashSet;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
+import java.util.Collections;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -58,7 +61,12 @@ public class WarningItemServiceImpl extends ServiceImpl<WarningItemMapper, Warni
|
|
|
public boolean addWarningItem(WarningItem warningItem) {
|
|
|
validateWarningItem(warningItem, false);
|
|
|
if (warningItem.getItemCode() == null || warningItem.getItemCode().isEmpty()) {
|
|
|
- warningItem.setItemCode(generateItemCode(warningItem.getWarningType(), warningItem.getWarningLevel()));
|
|
|
+ if (warningItem.getRegion() != null && !warningItem.getRegion().trim().isEmpty()) {
|
|
|
+ warningItem.setItemCode(generateItemCode(warningItem.getRegion(), warningItem.getWarningType(), warningItem.getDrainageType()));
|
|
|
+ } else {
|
|
|
+ // Existing integrations may still submit only warningType/warningLevel.
|
|
|
+ warningItem.setItemCode(generateItemCode(warningItem.getWarningType(), warningItem.getWarningLevel()));
|
|
|
+ }
|
|
|
}
|
|
|
if (getItemByCode(warningItem.getItemCode()) != null) {
|
|
|
throw new IllegalArgumentException("事项编码已存在:" + warningItem.getItemCode());
|
|
|
@@ -110,6 +118,31 @@ public class WarningItemServiceImpl extends ServiceImpl<WarningItemMapper, Warni
|
|
|
return this.getOne(wrapper);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String generateItemCode(String region, String warningType, String drainageType) {
|
|
|
+ String regionCode = normalizeCode(region, REGION_CODES, "区域", false);
|
|
|
+ String typeCode = normalizeCode(warningType, TYPE_CODES, "预警类型", false);
|
|
|
+ String subtypeCode = "";
|
|
|
+ if ("PS".equals(typeCode)) {
|
|
|
+ subtypeCode = normalizeCode(drainageType, DRAINAGE_CODES, "排水类型", true);
|
|
|
+ }
|
|
|
+
|
|
|
+ String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
+ String prefix = regionCode + typeCode + subtypeCode;
|
|
|
+ String codePrefix = prefix + dateStr;
|
|
|
+ LambdaQueryWrapper<WarningItem> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.likeRight(WarningItem::getItemCode, codePrefix);
|
|
|
+ wrapper.orderByDesc(WarningItem::getItemCode);
|
|
|
+ wrapper.last("LIMIT 1");
|
|
|
+
|
|
|
+ WarningItem lastItem = this.getOne(wrapper);
|
|
|
+ int sequence = nextSequence(lastItem, codePrefix);
|
|
|
+ return codePrefix + String.format("%04d", sequence);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Legacy format retained for existing callers that have not migrated to region/type parameters.
|
|
|
+ */
|
|
|
@Override
|
|
|
public String generateItemCode(String warningType, String warningLevel) {
|
|
|
if (warningType == null || warningType.trim().isEmpty()) {
|
|
|
@@ -146,6 +179,64 @@ public class WarningItemServiceImpl extends ServiceImpl<WarningItemMapper, Warni
|
|
|
return prefix + dateStr + String.format("%04d", sequence);
|
|
|
}
|
|
|
|
|
|
+ private int nextSequence(WarningItem lastItem, String codePrefix) {
|
|
|
+ if (lastItem == null || lastItem.getItemCode() == null || !lastItem.getItemCode().startsWith(codePrefix)) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ String lastSeqStr = lastItem.getItemCode().substring(lastItem.getItemCode().length() - 4);
|
|
|
+ try {
|
|
|
+ int sequence = Integer.parseInt(lastSeqStr) + 1;
|
|
|
+ if (sequence > 9999) {
|
|
|
+ throw new IllegalStateException("当天该区域、类型和排水分类的事项编码已达到流水号上限");
|
|
|
+ }
|
|
|
+ return sequence;
|
|
|
+ } catch (NumberFormatException ex) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final Map<String, String> REGION_CODES;
|
|
|
+ private static final Map<String, String> TYPE_CODES;
|
|
|
+ private static final Map<String, String> DRAINAGE_CODES;
|
|
|
+
|
|
|
+ static {
|
|
|
+ Map<String, String> regions = new java.util.HashMap<>();
|
|
|
+ regions.put("CN", "CN"); regions.put("城南", "CN");
|
|
|
+ regions.put("CB", "CB"); regions.put("城北", "CB");
|
|
|
+ regions.put("TC", "TC"); regions.put("太常", "TC");
|
|
|
+ REGION_CODES = Collections.unmodifiableMap(regions);
|
|
|
+
|
|
|
+ Map<String, String> types = new java.util.HashMap<>();
|
|
|
+ types.put("RQ", "RQ"); types.put("燃气", "RQ"); types.put("GAS_PIPE", "RQ");
|
|
|
+ types.put("GS", "GS"); types.put("供水", "GS"); types.put("WATER_PIPE", "GS");
|
|
|
+ types.put("PS", "PS"); types.put("排水", "PS"); types.put("SEWER_PIPE", "PS");
|
|
|
+ types.put("YJ", "YJ"); types.put("窨井", "YJ"); types.put("MANHOLE", "YJ");
|
|
|
+ TYPE_CODES = Collections.unmodifiableMap(types);
|
|
|
+
|
|
|
+ Map<String, String> drainage = new java.util.HashMap<>();
|
|
|
+ drainage.put("YS", "YS"); drainage.put("雨水", "YS"); drainage.put("RAIN", "YS");
|
|
|
+ drainage.put("WS", "WS"); drainage.put("污水", "WS"); drainage.put("SEWAGE", "WS");
|
|
|
+ DRAINAGE_CODES = Collections.unmodifiableMap(drainage);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String normalizeCode(String value, Map<String, String> aliases, String fieldName, boolean required) {
|
|
|
+ if (value == null || value.trim().isEmpty()) {
|
|
|
+ if (required) {
|
|
|
+ throw new IllegalArgumentException(fieldName + "不能为空");
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException(fieldName + "不能为空");
|
|
|
+ }
|
|
|
+ String normalized = value.trim().toUpperCase(Locale.ROOT);
|
|
|
+ String code = aliases.get(normalized);
|
|
|
+ if (code == null) {
|
|
|
+ code = aliases.get(value.trim());
|
|
|
+ }
|
|
|
+ if (code == null) {
|
|
|
+ throw new IllegalArgumentException("不支持的" + fieldName + ":" + value);
|
|
|
+ }
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
private void validateWarningItem(WarningItem warningItem, boolean update) {
|
|
|
if (warningItem == null) {
|
|
|
throw new IllegalArgumentException("预警事项不能为空");
|