yeg
2026-08-29 8ba28ba5fbee0582f742f6eb1e824544869abc05
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TTestServiceImpl.java
@@ -15,20 +15,17 @@
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TTest;
import com.ruoyi.system.domain.TBatteryGroup;
import com.ruoyi.system.domain.TBatteryGroupRelationField;
import com.ruoyi.system.domain.TTestBattery;
import com.ruoyi.system.domain.TTestBatteryField;
import com.ruoyi.system.domain.TTestChannelBinding;
import com.ruoyi.system.domain.TTestCollectorChannel;
import com.ruoyi.system.domain.TTestFieldSnapshot;
import com.ruoyi.system.domain.TTestPlanStep;
import com.ruoyi.system.domain.TTestResult;
import com.ruoyi.system.domain.TTestWarningThreshold;
import com.ruoyi.system.domain.vo.TestConfigurationOptionsVo;
import com.ruoyi.system.mapper.TTestMapper;
import com.ruoyi.system.service.ITTestService;
import com.ruoyi.system.service.ITBatteryGroupService;
import com.ruoyi.system.service.ISharedFieldSnapshotService;
/** 测试配置服务,负责状态边界、关系校验、事务写入和只追加审计。 */
@Service
@@ -47,7 +44,7 @@
    private TTestMapper testMapper;
    @Autowired
    private ITBatteryGroupService batteryGroupService;
    private ISharedFieldSnapshotService sharedFieldSnapshotService;
    @Override
    public TTest selectTestById(Long testId)
@@ -67,7 +64,9 @@
        test.setBatteryNo(trimToNull(test.getBatteryNo()));
        validateQueryStatus(test.getTestStatus(), TEST_STATUSES, "测试状态无效");
        validateQueryStatus(test.getReceptionStatus(), RECEPTION_STATUSES, "接收状态无效");
        return testMapper.selectTestList(test);
        List<TTest> tests = mutable(testMapper.selectTestList(test));
        populateBatteryNos(tests);
        return tests;
    }
    @Override
@@ -93,7 +92,8 @@
        validateConfiguration(test);
        test.setTestStatus(WAITING);
        test.setReceptionStatus(NOT_STARTED);
        test.setFieldSnapshots(snapshotGroupFields(test.getBatteries().get(0).getGroupId()));
        test.setFieldSnapshots(sharedFieldSnapshotService.createForBatteryGroup(
                test.getBatteries().get(0).getGroupId()));
        test.setDelFlag("0");
        test.setCreateTime(new Date());
        testMapper.insertTest(test);
@@ -121,7 +121,10 @@
            test.setMonitorTimeoutMinutes(current.getMonitorTimeoutMinutes());
        validateBase(test, true);
        validateConfiguration(test);
        test.setFieldSnapshots(resolveUpdatedFieldSnapshots(test, current));
        Long currentGroupId = current.getBatteries().isEmpty() ? null : current.getBatteries().get(0).getGroupId();
        Long nextGroupId = test.getBatteries().get(0).getGroupId();
        test.setFieldSnapshots(sharedFieldSnapshotService.resolveForUpdate(
                currentGroupId, nextGroupId, current.getFieldSnapshots()));
        test.setUpdateTime(new Date());
        int rows = testMapper.updateTest(test);
        if (rows != 1) throw new ServiceException("测试配置已变更,请刷新后重试");
@@ -133,7 +136,7 @@
        testMapper.deleteTestFieldsByTestId(test.getTestId());
        testMapper.deleteTestBatteriesByTestId(test.getTestId());
        testMapper.deleteTestThresholdsByTestId(test.getTestId());
        testMapper.deleteTestFieldSnapshotsByTestId(test.getTestId());
        sharedFieldSnapshotService.deleteByTestId(test.getTestId());
        saveConfiguration(test);
        testMapper.insertTestAudit(test.getTestId(), "TEST_CONFIGURATION_UPDATED", test.getOperatorId(),
                test.getUpdateBy(), JSON.toJSONString(current), JSON.toJSONString(test));
@@ -209,8 +212,9 @@
        }
        test.setPlanSteps(planSteps);
        test.setBatteries(batteries);
        test.setBatteryNo(joinBatteryNos(batteries));
        test.setThresholds(mutable(testMapper.selectTestThresholds(testId)));
        test.setFieldSnapshots(mutable(testMapper.selectTestFieldSnapshots(testId)));
        test.setFieldSnapshots(sharedFieldSnapshotService.selectByTestId(testId));
    }
    private void validateBase(TTest test, boolean update)
@@ -326,39 +330,20 @@
        }
    }
    private List<TTestFieldSnapshot> resolveUpdatedFieldSnapshots(TTest test, TTest current)
    private void populateBatteryNos(List<TTest> tests)
    {
        Long nextGroupId = test.getBatteries().get(0).getGroupId();
        Long currentGroupId = current.getBatteries().isEmpty() ? null : current.getBatteries().get(0).getGroupId();
        if (nextGroupId.equals(currentGroupId))
            return mutable(current.getFieldSnapshots()).stream().map(this::copySnapshot).toList();
        return snapshotGroupFields(nextGroupId);
        if (tests.isEmpty()) return;
        Map<Long, List<TTestBattery>> batteriesByTest = new HashMap<>();
        List<Long> testIds = tests.stream().map(TTest::getTestId).toList();
        for (TTestBattery battery : mutable(testMapper.selectBatteryNosByTestIds(testIds)))
            batteriesByTest.computeIfAbsent(battery.getTestId(), ignored -> new ArrayList<>()).add(battery);
        for (TTest item : tests)
            item.setBatteryNo(joinBatteryNos(batteriesByTest.get(item.getTestId())));
    }
    private List<TTestFieldSnapshot> snapshotGroupFields(Long groupId)
    private String joinBatteryNos(List<TTestBattery> batteries)
    {
        TBatteryGroup group = batteryGroupService.selectBatteryGroupById(groupId);
        List<TTestFieldSnapshot> snapshots = new ArrayList<>();
        for (TBatteryGroupRelationField field : mutable(group.getTemplateFields()))
        {
            TTestFieldSnapshot snapshot = new TTestFieldSnapshot();
            snapshot.setFieldDefinitionId(field.getBatteryGroupFieldId());
            snapshot.setFieldName(field.getFieldName());
            snapshot.setFieldType(field.getFieldType());
            snapshot.setFieldValue(field.getFieldValue());
            snapshots.add(snapshot);
        }
        return snapshots;
    }
    private TTestFieldSnapshot copySnapshot(TTestFieldSnapshot source)
    {
        TTestFieldSnapshot copy = new TTestFieldSnapshot();
        copy.setFieldDefinitionId(source.getFieldDefinitionId());
        copy.setFieldName(source.getFieldName());
        copy.setFieldType(source.getFieldType());
        copy.setFieldValue(source.getFieldValue());
        return copy;
        return mutable(batteries).stream().map(TTestBattery::getBatteryNo).collect(java.util.stream.Collectors.joining("、"));
    }
    private void validatePlanSteps(List<TTestPlanStep> planSteps)
@@ -436,11 +421,7 @@
                testMapper.insertTestCollectorChannel(mapping);
            }
        }
        for (TTestFieldSnapshot snapshot : mutable(test.getFieldSnapshots()))
        {
            snapshot.setTestId(test.getTestId());
            testMapper.insertTestFieldSnapshot(snapshot);
        }
        sharedFieldSnapshotService.saveForTest(test.getTestId(), test.getFieldSnapshots());
        for (TTestWarningThreshold threshold : test.getThresholds())
        {
            threshold.setTestId(test.getTestId());