From b3a8cec76cd3d2d3aa6d470e1c28ec161bc1a16b Mon Sep 17 00:00:00 2001
From: chen.lin <1442464845@qq.com>
Date: 星期二, 10 三月 2026 17:22:44 +0800
Subject: [PATCH] 路径管理-初始化功能优化

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/DeviceSiteServiceImpl.java |  118 ++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 77 insertions(+), 41 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/DeviceSiteServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/DeviceSiteServiceImpl.java
index 73aa5a7..5ddaebe 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/DeviceSiteServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/DeviceSiteServiceImpl.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.vincent.rsf.framework.exception.CoolException;
 import com.vincent.rsf.server.manager.controller.params.DeviceSiteParame;
+import com.vincent.rsf.server.manager.controller.params.DeviceSiteRowParam;
 import com.vincent.rsf.server.manager.entity.BasStation;
 import com.vincent.rsf.server.manager.mapper.DeviceSiteMapper;
 import com.vincent.rsf.server.manager.entity.DeviceSite;
@@ -12,27 +13,30 @@
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
 @Service("deviceSiteService")
 public class DeviceSiteServiceImpl extends ServiceImpl<DeviceSiteMapper, DeviceSite> implements DeviceSiteService {
 
+    /** 涓庤〃 man_device_site.target 鍒楅暱搴︿竴鑷达紝瓒呴暱鎴柇閬垮厤 Data too long */
+    private static final int TARGET_MAX_LENGTH = 255;
+
     @Autowired
     private BasStationService basStationService;
 
 
     /**
-     * 鍒濆鍖栫珯鐐�
-     * @param param
-     * @return
+     * 鍒濆鍖栫珯鐐癸細澶氳锛堣澶囩珯鐐广�佷綔涓氱珯鐐广�佺洰鏍囩珯鐐逛负涓�缁勶級锛屽贩閬撹嫳鏂囬�楀彿鍒嗛殧锛屾瘡缁劽楀贩閬撁椾綔涓氱被鍨嬬敓鎴愬鏉¤褰�
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean initSites(DeviceSiteParame param) {
-        if (param.getFlagInit() == 1) {
+        if (param.getFlagInit() != null && param.getFlagInit() == 1) {
             List<DeviceSite> list = this.list(new LambdaQueryWrapper<DeviceSite>().select(DeviceSite::getId).last("limit 1"));
             if (!list.isEmpty()) {
                 if (!this.remove(new LambdaQueryWrapper<>())) {
@@ -40,47 +44,55 @@
                 }
             }
         }
-        if (Objects.isNull(param.getDeviceSites()) || StringUtils.isBlank(param.getDeviceSites())) {
-            throw new CoolException("鍒濆鍖栧け璐ワ細 璁惧浣滀笟绔欑偣涓嶈兘涓虹┖锛侊紒");
-        }
-        if (Objects.isNull(param.getSite()) || StringUtils.isBlank(param.getSite())) {
-            throw new CoolException("鍒濆鍖栧け璐�: 浣滀笟绔欑偣涓嶈兘涓虹┖锛侊紒");
-        }
         if (Objects.isNull(param.getTypeIds()) || param.getTypeIds().isEmpty()) {
             throw new CoolException("鍒濆鍖栧け璐ワ細 浣滀笟绫诲瀷涓嶈兘涓虹┖锛侊紒");
         }
-        if (Objects.isNull(param.getTarget()) || param.getTarget().isEmpty()) {
-            throw new CoolException("鍒濆鍖栧け璐ワ細 鐩爣绔欑偣涓嶈兘涓虹┖锛侊紒");
+        if (StringUtils.isBlank(param.getChannel())) {
+            throw new CoolException("鍒濆鍖栧け璐ワ細 宸烽亾涓嶈兘涓虹┖锛侊紒");
         }
-        List<String> sites = Arrays.asList(StringUtils.split(param.getSite(), ","));
-        List<String> dvSites = Arrays.asList(StringUtils.split(param.getDeviceSites(), ","));
-        List<String> targets = Arrays.asList(StringUtils.split(param.getTarget(), ","));
-        List<DeviceSite> deviceSites =  new ArrayList<>();
-        for (String site : sites) {
-            BasStation basStation = basStationService.getById(site);
-            if (null == basStation) {
-                throw new CoolException("鍒濆鍖栧け璐ワ細 绔欑偣鏈壘鍒帮紒锛�");
+        List<Integer> channels = parseChannels(param.getChannel());
+        if (channels.isEmpty()) {
+            throw new CoolException("鍒濆鍖栧け璐ワ細 宸烽亾鏍煎紡閿欒锛岃鐢ㄨ嫳鏂囬�楀彿鍒嗛殧锛屽 1,2,3锛侊紒");
+        }
+
+        List<DeviceSiteRowParam> rows = param.getRows();
+        if (Objects.isNull(rows) || rows.isEmpty()) {
+            throw new CoolException("鍒濆鍖栧け璐ワ細 璇疯嚦灏戞坊鍔犱竴琛岋紙璁惧绔欑偣銆佷綔涓氱珯鐐广�佺洰鏍囩珯鐐癸級锛侊紒");
+        }
+
+        List<DeviceSite> deviceSites = new ArrayList<>();
+        for (DeviceSiteRowParam row : rows) {
+            if (StringUtils.isBlank(row.getDeviceSite()) || StringUtils.isBlank(row.getSite()) || StringUtils.isBlank(row.getTarget())) {
+                throw new CoolException("鍒濆鍖栧け璐ワ細 姣忚鐨勮澶囩珯鐐广�佷綔涓氱珯鐐广�佺洰鏍囩珯鐐瑰潎涓嶈兘涓虹┖锛侊紒");
             }
-            for (String deviceSite : dvSites) {
-                BasStation basStation2 = basStationService.getById(deviceSite);
-                if (null == basStation2) {
-                    throw new CoolException("鍒濆鍖栧け璐ワ細 浣滀笟绔欑偣鏈壘鍒帮紒锛�");
-                }
-                for (Long id : param.getTypeIds()) {
-                    for (String target : targets) {
-                        DeviceSite site1 = new DeviceSite();
-                        site1.setType(id + "")
-                                .setSite(basStation.getStationName())
-                                .setDevice(param.getDeviceType())
-                                .setDeviceSite(basStation2.getStationName())
-                                .setTarget(target)
-                                .setDeviceCode(param.getDeviceCode())
-                                .setAreaIdStart(param.getAreaIdStart())
-                                .setAreaIdEnd(param.getAreaIdEnd())
-                                .setChannel(param.getChannel())
-                                ;
-                        deviceSites.add(site1);
-                    }
+            BasStation siteStation = basStationService.getById(Long.parseLong(row.getSite().trim()));
+            if (siteStation == null) {
+                throw new CoolException("鍒濆鍖栧け璐ワ細 浣滀笟绔欑偣鏈壘鍒帮紒锛�");
+            }
+            BasStation deviceStation = basStationService.getById(Long.parseLong(row.getDeviceSite().trim()));
+            if (deviceStation == null) {
+                throw new CoolException("鍒濆鍖栧け璐ワ細 璁惧绔欑偣鏈壘鍒帮紒锛�");
+            }
+            for (Long typeId : param.getTypeIds()) {
+                for (Integer ch : channels) {
+                    DeviceSite ds = new DeviceSite();
+                    String siteName = siteStation.getStationName();
+                    String deviceSiteName = deviceStation.getStationName();
+                    String targetVal = truncate(row.getTarget().trim(), TARGET_MAX_LENGTH);
+                    String commonName = StringUtils.isNotBlank(param.getName()) ? param.getName().trim() : null;
+                    String commonLabel = StringUtils.isNotBlank(param.getLabel()) ? param.getLabel().trim() : null;
+                    ds.setType(String.valueOf(typeId))
+                            .setSite(siteName)
+                            .setDevice(param.getDeviceType())
+                            .setDeviceSite(deviceSiteName)
+                            .setTarget(targetVal)
+                            .setDeviceCode(param.getDeviceCode())
+                            .setAreaIdStart(param.getAreaIdStart())
+                            .setAreaIdEnd(param.getAreaIdEnd())
+                            .setChannel(ch)
+                            .setName(commonName != null ? commonName : (deviceSiteName + "-" + siteName + "-" + targetVal + "-" + ch))
+                            .setLabel(commonLabel);
+                    deviceSites.add(ds);
                 }
             }
         }
@@ -89,4 +101,28 @@
         }
         return true;
     }
+
+    private static String truncate(String s, int maxLen) {
+        if (s == null) return null;
+        return s.length() <= maxLen ? s : s.substring(0, maxLen);
+    }
+
+    /** 宸烽亾鑻辨枃閫楀彿鍒嗗壊锛岃В鏋愪负鏁存暟鍒楄〃 */
+    private List<Integer> parseChannels(String channelStr) {
+        if (StringUtils.isBlank(channelStr)) {
+            return Collections.emptyList();
+        }
+        String[] parts = channelStr.split(",");
+        List<Integer> list = new ArrayList<>();
+        for (String p : parts) {
+            String t = (p == null) ? "" : p.trim();
+            if (t.isEmpty()) continue;
+            try {
+                list.add(Integer.parseInt(t));
+            } catch (NumberFormatException e) {
+                return Collections.emptyList();
+            }
+        }
+        return list;
+    }
 }

--
Gitblit v1.9.1