From aa89745fe575f876d474b4bb85e01e7a999ebd22 Mon Sep 17 00:00:00 2001
From: yeg <1347747024@qq.com>
Date: 星期六, 05 九月 2026 01:37:44 +0800
Subject: [PATCH] 接入电池组与测试配置DTO边界

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TTestController.java             |   56 ++++++++++++++++++
 ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TTestControllerContractTest.java |    5 +
 ruoyi-system/src/main/resources/mapper/system/TTestMapper.xml                              |    9 ++-
 battery-business/src/main/java/com/battery/business/battery/dto/BatteryGroupDto.java       |   16 ++++
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TBatteryGroupController.java     |   29 +++++++++
 battery-business/src/main/java/com/battery/business/test/dto/TestBatteryBindingDto.java    |    8 ++
 6 files changed, 112 insertions(+), 11 deletions(-)

diff --git a/battery-business/src/main/java/com/battery/business/battery/dto/BatteryGroupDto.java b/battery-business/src/main/java/com/battery/business/battery/dto/BatteryGroupDto.java
index d2134d1..fa3d93d 100644
--- a/battery-business/src/main/java/com/battery/business/battery/dto/BatteryGroupDto.java
+++ b/battery-business/src/main/java/com/battery/business/battery/dto/BatteryGroupDto.java
@@ -10,6 +10,18 @@
         String purpose,
         String status,
         List<String> batteryNos,
-        List<FieldValueDto> fields) {
-    public record FieldValueDto(Long fieldId, String value) { }
+        List<FieldValueDto> fields,
+        String remark,
+        Long templateId,
+        List<FieldValueDto> templateFieldRelations) {
+    public BatteryGroupDto(Long groupId, String groupName, String model, String purpose, String status,
+            List<String> batteryNos, List<FieldValueDto> fields) {
+        this(groupId, groupName, model, purpose, status, batteryNos, fields, null, null, null);
+    }
+    public List<FieldValueDto> currentFields() { return fields != null ? fields : templateFieldRelations; }
+    public record FieldValueDto(Long fieldId, String value, Long batteryGroupFieldId, String fieldValue) {
+        public FieldValueDto(Long fieldId, String value) { this(fieldId, value, null, null); }
+        public Long resolvedFieldId() { return fieldId != null ? fieldId : batteryGroupFieldId; }
+        public String resolvedValue() { return value != null ? value : fieldValue; }
+    }
 }
diff --git a/battery-business/src/main/java/com/battery/business/test/dto/TestBatteryBindingDto.java b/battery-business/src/main/java/com/battery/business/test/dto/TestBatteryBindingDto.java
index 428bf00..651e416 100644
--- a/battery-business/src/main/java/com/battery/business/test/dto/TestBatteryBindingDto.java
+++ b/battery-business/src/main/java/com/battery/business/test/dto/TestBatteryBindingDto.java
@@ -5,11 +5,19 @@
 /** 单个测试电池及检测柜、采集器通道绑定请求。 */
 public record TestBatteryBindingDto(
         Long testBatteryId,
+        Long relationId,
         Long groupId,
         String batteryNo,
         Long collectorId,
         CabinetChannelDto cabinetChannel,
+        CabinetChannelDto binding,
         List<CollectorChannelDto> collectorChannels) {
+    public TestBatteryBindingDto(Long testBatteryId, Long groupId, String batteryNo, Long collectorId,
+            CabinetChannelDto cabinetChannel, List<CollectorChannelDto> collectorChannels) {
+        this(testBatteryId, null, groupId, batteryNo, collectorId, cabinetChannel, null, collectorChannels);
+    }
+    public Long resolvedTestBatteryId() { return testBatteryId != null ? testBatteryId : relationId; }
+    public CabinetChannelDto resolvedCabinetChannel() { return cabinetChannel != null ? cabinetChannel : binding; }
     public record CabinetChannelDto(Long channelId, String expectedBarcode) { }
     public record CollectorChannelDto(String measurementCode, String measurementName, String channelCode) { }
 }
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TBatteryGroupController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TBatteryGroupController.java
index 94f4208..320984b 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TBatteryGroupController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TBatteryGroupController.java
@@ -18,6 +18,7 @@
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.system.domain.TBatteryGroup;
+import com.battery.business.battery.dto.BatteryGroupDto;
 import com.ruoyi.system.service.ITBatteryGroupService;
 
 /** 电池组管理 Controller。 */
@@ -38,14 +39,38 @@
     public AjaxResult getInfo(@PathVariable Long groupId) { return success(batteryGroupService.selectBatteryGroupById(groupId)); }
 
     @PreAuthorize("@ss.hasPermi('system:batteryGroup:add')") @Log(title = "电池组管理", businessType = BusinessType.INSERT) @PostMapping
-    public AjaxResult add(@Validated @RequestBody TBatteryGroup group) { group.setCreateBy(getUsername()); return toAjax(batteryGroupService.insertBatteryGroup(group)); }
+    public AjaxResult add(@Validated @RequestBody BatteryGroupDto request) { TBatteryGroup group = toDomain(request); group.setCreateBy(getUsername()); return toAjax(batteryGroupService.insertBatteryGroup(group)); }
 
     @PreAuthorize("@ss.hasPermi('system:batteryGroup:edit')") @Log(title = "电池组管理", businessType = BusinessType.UPDATE) @PutMapping
-    public AjaxResult edit(@Validated @RequestBody TBatteryGroup group) { group.setUpdateBy(getUsername()); return toAjax(batteryGroupService.updateBatteryGroup(group)); }
+    public AjaxResult edit(@Validated @RequestBody BatteryGroupDto request) { TBatteryGroup group = toDomain(request); group.setUpdateBy(getUsername()); return toAjax(batteryGroupService.updateBatteryGroup(group)); }
 
     @PreAuthorize("@ss.hasPermi('system:batteryGroup:changeStatus')") @Log(title = "电池组管理", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus")
     public AjaxResult changeStatus(@RequestBody TBatteryGroup group) { group.setUpdateBy(getUsername()); return toAjax(batteryGroupService.updateBatteryGroupStatus(group)); }
 
     @PreAuthorize("@ss.hasPermi('system:batteryGroup:remove')") @Log(title = "电池组管理", businessType = BusinessType.DELETE) @DeleteMapping("/{groupIds}")
     public AjaxResult remove(@PathVariable Long[] groupIds) { return toAjax(batteryGroupService.deleteBatteryGroupByIds(groupIds, getUsername())); }
+
+    /** 将公开写入 DTO 转换为现有持久化服务模型,字段值仍写入电池组当前关系。 */
+    private TBatteryGroup toDomain(BatteryGroupDto request)
+    {
+        if (request == null) return null;
+        TBatteryGroup group = new TBatteryGroup();
+        group.setGroupId(request.groupId()); group.setGroupName(request.groupName());
+        group.setModel(request.model()); group.setPurpose(request.purpose()); group.setStatus(request.status());
+        group.setRemark(request.remark()); group.setTemplateId(request.templateId()); group.setBatteryNos(request.batteryNos());
+        java.util.List<BatteryGroupDto.FieldValueDto> values = request.currentFields();
+        if (values != null)
+        {
+            java.util.List<com.ruoyi.system.domain.TBatteryGroupRelationField> fields = new java.util.ArrayList<>();
+            for (BatteryGroupDto.FieldValueDto value : values)
+            {
+                if (value == null) continue;
+                com.ruoyi.system.domain.TBatteryGroupRelationField field = new com.ruoyi.system.domain.TBatteryGroupRelationField();
+                field.setBatteryGroupFieldId(value.resolvedFieldId()); field.setFieldValue(value.resolvedValue()); fields.add(field);
+            }
+            group.setFields(fields);
+            group.setTemplateFieldRelations(fields);
+        }
+        return group;
+    }
 }
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TTestController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TTestController.java
index 746f77d..13751cd 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TTestController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TTestController.java
@@ -19,6 +19,13 @@
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.system.domain.TTest;
+import com.ruoyi.system.domain.TTestBattery;
+import com.ruoyi.system.domain.TTestChannelBinding;
+import com.ruoyi.system.domain.TTestCollectorChannel;
+import com.ruoyi.system.domain.TTestWarningThreshold;
+import com.battery.business.test.dto.TestConfigurationDto;
+import com.battery.business.test.dto.TestBatteryBindingDto;
+import com.battery.business.test.dto.ThresholdDto;
 import com.ruoyi.system.domain.TTestResult;
 import com.ruoyi.system.domain.TTestLogicStepMapping;
 import com.ruoyi.system.domain.TTestConsoleDetail;
@@ -68,8 +75,9 @@
     @PreAuthorize("@ss.hasPermi('system:test:add')")
     @Log(title = "测试管理", businessType = BusinessType.INSERT)
     @PostMapping
-    public AjaxResult add(@Validated @RequestBody TTest test)
+    public AjaxResult add(@Validated @RequestBody TestConfigurationDto request)
     {
+        TTest test = toDomain(request);
         test.setOperatorId(getUserId());
         test.setCreatorId(getUserId());
         test.setCreateBy(getUsername());
@@ -80,8 +88,9 @@
     @PreAuthorize("@ss.hasPermi('system:test:edit')")
     @Log(title = "测试管理", businessType = BusinessType.UPDATE)
     @PutMapping
-    public AjaxResult edit(@Validated @RequestBody TTest test)
+    public AjaxResult edit(@Validated @RequestBody TestConfigurationDto request)
     {
+        TTest test = toDomain(request);
         test.setOperatorId(getUserId());
         test.setUpdateBy(getUsername());
         testService.updateTest(test);
@@ -238,4 +247,47 @@
         return getDataTable(testService.selectTestResults(result));
     }
 
+    /** 将测试配置 DTO 转换为现有测试服务模型;计划工步等废止字段不再进入服务。 */
+    private TTest toDomain(TestConfigurationDto request)
+    {
+        if (request == null) return null;
+        TTest test = new TTest();
+        test.setTestId(request.testId()); test.setTestName(request.testName()); test.setPrincipalId(request.principalId());
+        test.setTestTypeId(request.testTypeId()); test.setTestPurpose(request.testPurpose());
+        test.setMonitorTimeoutMinutes(request.monitorTimeoutMinutes()); test.setRemark(request.remark());
+        java.util.List<TTestBattery> batteries = new java.util.ArrayList<>();
+        if (request.batteries() != null) for (TestBatteryBindingDto item : request.batteries())
+        {
+            if (item == null) continue;
+            TTestBattery battery = new TTestBattery(); battery.setTestBatteryId(item.resolvedTestBatteryId());
+            battery.setGroupId(item.groupId()); battery.setBatteryNo(item.batteryNo()); battery.setCollectorId(item.collectorId());
+            if (item.resolvedCabinetChannel() != null)
+            {
+                TTestChannelBinding binding = new TTestChannelBinding();
+                binding.setTestBatteryId(item.resolvedTestBatteryId()); binding.setChannelId(item.resolvedCabinetChannel().channelId());
+                binding.setExpectedBarcode(item.resolvedCabinetChannel().expectedBarcode()); battery.setBinding(binding);
+            }
+            java.util.List<TTestCollectorChannel> channels = new java.util.ArrayList<>();
+            if (item.collectorChannels() != null) for (TestBatteryBindingDto.CollectorChannelDto channel : item.collectorChannels())
+            {
+                if (channel == null) continue;
+                TTestCollectorChannel mapping = new TTestCollectorChannel(); mapping.setTestBatteryId(item.resolvedTestBatteryId());
+                mapping.setCollectorId(item.collectorId()); mapping.setMeasurementCode(channel.measurementCode());
+                mapping.setMeasurementName(channel.measurementName()); mapping.setChannelCode(channel.channelCode()); channels.add(mapping);
+            }
+            battery.setCollectorChannels(channels); batteries.add(battery);
+        }
+        test.setBatteries(batteries);
+        java.util.List<TTestWarningThreshold> thresholds = new java.util.ArrayList<>();
+        if (request.thresholds() != null) for (ThresholdDto item : request.thresholds())
+        {
+            if (item == null) continue;
+            TTestWarningThreshold threshold = new TTestWarningThreshold(); threshold.setMetricCode(item.metricCode());
+            threshold.setMinValue(item.minValue()); threshold.setMaxValue(item.maxValue()); threshold.setUnit(item.unit());
+            threshold.setSeverity(item.severity()); thresholds.add(threshold);
+        }
+        test.setThresholds(thresholds);
+        return test;
+    }
+
 }
diff --git a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TTestControllerContractTest.java b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TTestControllerContractTest.java
index 05845a2..527d812 100644
--- a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TTestControllerContractTest.java
+++ b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TTestControllerContractTest.java
@@ -6,6 +6,7 @@
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.security.access.prepost.PreAuthorize;
+import com.battery.business.test.dto.TestConfigurationDto;
 
 class TTestControllerContractTest
 {
@@ -14,8 +15,8 @@
     {
         assertPermission("options", "@ss.hasAnyPermi('system:test:add,system:test:edit')");
         assertPermission("getInfo", "@ss.hasPermi('system:test:query')", Long.class);
-        assertPermission("add", "@ss.hasPermi('system:test:add')", com.ruoyi.system.domain.TTest.class);
-        assertPermission("edit", "@ss.hasPermi('system:test:edit')", com.ruoyi.system.domain.TTest.class);
+        assertPermission("add", "@ss.hasPermi('system:test:add')", TestConfigurationDto.class);
+        assertPermission("edit", "@ss.hasPermi('system:test:edit')", TestConfigurationDto.class);
         assertPermission("startReception", "@ss.hasPermi('system:test:start')", Long.class);
         Method startReception = TTestController.class.getMethod("startReception", Long.class);
         assertEquals("/{testId}/reception/start", startReception.getAnnotation(PostMapping.class).value()[0]);
diff --git a/ruoyi-system/src/main/resources/mapper/system/TTestMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TTestMapper.xml
index 11e314d..115e96e 100644
--- a/ruoyi-system/src/main/resources/mapper/system/TTestMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/TTestMapper.xml
@@ -403,7 +403,8 @@
                sample.energy_wh energy, channel.adapter_id adapterId, channel.adapter_code adapterCode,
                channel.devtype devtype, channel.devid deviceId, channel.subdevid subdevid, channel.chlid channelId
         from t_test_sample sample
-        join t_test_battery battery on battery.test_battery_id = sample.binding_id and battery.del_flag = 0
+        join t_test_channel_binding binding on binding.binding_id = sample.binding_id
+        join t_test_battery battery on battery.test_battery_id = binding.test_battery_id and battery.del_flag = 0
         join t_detection_channel channel on channel.channel_id = sample.channel_id and channel.del_flag = 0
         where sample.test_id = #{testId}
         order by sample.device_time_utc desc, sample.seq_id desc, sample.sample_id desc
@@ -458,7 +459,8 @@
                channel.adapter_id adapterId, channel.adapter_code adapterCode, channel.devtype devtype,
                channel.devid deviceId, channel.subdevid subdevid, channel.chlid channelId
         from t_test_sample sample
-        join t_test_battery battery on battery.test_battery_id = sample.binding_id and battery.del_flag = 0
+        join t_test_channel_binding binding on binding.binding_id = sample.binding_id
+        join t_test_battery battery on battery.test_battery_id = binding.test_battery_id and battery.del_flag = 0
         join t_detection_channel channel on channel.channel_id = sample.channel_id and channel.del_flag = 0
         where sample.test_id = #{testId}
         group by sample.binding_id, battery.battery_no, channel.adapter_id, channel.adapter_code, channel.devtype,
@@ -483,7 +485,8 @@
                channel.adapter_id adapterId, channel.adapter_code adapterCode, channel.devtype devtype,
                channel.devid deviceId, channel.subdevid subdevid, channel.chlid channelId
         from t_test_sample sample
-        join t_test_battery battery on battery.test_battery_id = sample.binding_id and battery.del_flag = 0
+        join t_test_channel_binding binding on binding.binding_id = sample.binding_id
+        join t_test_battery battery on battery.test_battery_id = binding.test_battery_id and battery.del_flag = 0
         join t_detection_channel channel on channel.channel_id = sample.channel_id and channel.del_flag = 0
         where sample.test_id = #{testId}
         group by sample.binding_id, battery.battery_no, channel.adapter_id, channel.adapter_code, channel.devtype,

--
Gitblit v1.7.1