| | |
| | | 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 |
| | |
| | | private TTestMapper testMapper; |
| | | |
| | | @Autowired |
| | | private ITBatteryGroupService batteryGroupService; |
| | | private ISharedFieldSnapshotService sharedFieldSnapshotService; |
| | | |
| | | @Override |
| | | public TTest selectTestById(Long testId) |
| | |
| | | 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 |
| | |
| | | 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); |
| | |
| | | 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("测试配置已变更,请刷新后重试"); |
| | |
| | | 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)); |
| | |
| | | } |
| | | 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) |
| | |
| | | } |
| | | } |
| | | |
| | | 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) |
| | |
| | | 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()); |