| | |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | 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.TTestFieldSnapshot; |
| | | import com.ruoyi.system.domain.TTestWarningThreshold; |
| | | import com.ruoyi.system.domain.TDetectionChannel; |
| | | 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)); |
| | |
| | | @Override |
| | | public int startTest(Long testId, String updateBy) |
| | | { |
| | | throw new ServiceException("开始接收由后续原子动作处理"); |
| | | throw new ServiceException("旧开始入口已停用,请使用原子开始接收接口并提供操作人"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int startReception(Long testId, Long operatorId, String updateBy) |
| | | { |
| | | if (testId == null) throw new ServiceException("测试ID不能为空"); |
| | | if (operatorId == null) throw new ServiceException("操作人不能为空"); |
| | | TTest current = testMapper.selectTestByIdForUpdate(testId); |
| | | if (current == null) throw new ServiceException("测试不存在"); |
| | | if (!NOT_STARTED.equals(current.getReceptionStatus())) |
| | | throw new ServiceException("当前测试不是未开始接收状态"); |
| | | loadConfiguration(current); |
| | | if (current.getBatteries().isEmpty()) throw new ServiceException("测试未配置电池号"); |
| | | |
| | | Date actionAt = new Date(); |
| | | Set<Long> channelIds = new HashSet<>(); |
| | | Set<String> collectorChannels = new HashSet<>(); |
| | | boolean runtimeAttach = false; |
| | | for (TTestBattery battery : current.getBatteries()) |
| | | { |
| | | TTestChannelBinding binding = battery.getBinding(); |
| | | if (binding == null || binding.getChannelId() == null) |
| | | throw new ServiceException("电池号 " + battery.getBatteryNo() + " 未绑定检测柜通道"); |
| | | if (!channelIds.add(binding.getChannelId())) |
| | | throw new ServiceException("检测柜通道重复绑定,无法开始接收"); |
| | | TDetectionChannel channel = testMapper.selectReceptionChannelForUpdate(binding.getChannelId()); |
| | | if (channel == null || !isEnabled(channel.getStatus()) || !isOnline(channel.getOnlineStatus())) |
| | | throw new ServiceException("检测柜通道 " + binding.getChannelId() + " 当前离线或不可观测"); |
| | | runtimeAttach |= "WORKING".equalsIgnoreCase(channel.getWorkStatus()) |
| | | || "PAUSED".equalsIgnoreCase(channel.getWorkStatus()); |
| | | if (testMapper.selectChannelLeaseForUpdate(binding.getChannelId()) != null) |
| | | throw new ServiceException("检测柜通道 " + binding.getChannelId() + " 已被其他测试占用"); |
| | | if (battery.getCollectorId() == null || testMapper.countCollectorEnabled(battery.getCollectorId()) == 0) |
| | | throw new ServiceException("电池号 " + battery.getBatteryNo() + " 的采集器不存在或未启用"); |
| | | if (!testMapper.collectorOnline(battery.getCollectorId())) |
| | | throw new ServiceException("采集器 " + battery.getCollectorId() + " 当前离线或不可观测"); |
| | | Integer collectorChannelCount = testMapper.selectCollectorChannelCount(battery.getCollectorId()); |
| | | validateCollectorChannels(battery.getCollectorId(), collectorChannelCount == null ? 0 : collectorChannelCount, |
| | | battery.getCollectorChannels(), collectorChannels); |
| | | |
| | | } |
| | | |
| | | try |
| | | { |
| | | for (TTestBattery battery : current.getBatteries()) |
| | | { |
| | | TTestChannelBinding binding = battery.getBinding(); |
| | | testMapper.insertChannelLease(binding.getChannelId(), testId, binding.getBindingId(), operatorId, actionAt); |
| | | for (TTestCollectorChannel mapping : mutable(battery.getCollectorChannels())) |
| | | testMapper.insertCollectorChannelLease(battery.getCollectorId(), mapping.getChannelCode(), testId, |
| | | binding.getBindingId(), operatorId, actionAt); |
| | | } |
| | | if (testMapper.updateReceptionStarted(testId, actionAt, runtimeAttach, updateBy) != 1) |
| | | throw new ServiceException("测试状态已变更,请刷新后重试"); |
| | | testMapper.insertTestAudit(testId, "RECEPTION_STARTED", operatorId, updateBy, null, |
| | | JSON.toJSONString(Map.of("receptionStatus", "RECEIVING", "runtimeAttach", runtimeAttach))); |
| | | return 1; |
| | | } |
| | | catch (DuplicateKeyException exception) |
| | | { |
| | | throw new ServiceException("数据来源已被其他测试占用,请刷新后重试"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int pauseReception(Long testId, Long operatorId, String updateBy) |
| | | { |
| | | if (testId == null) throw new ServiceException("测试ID不能为空"); |
| | | if (operatorId == null) throw new ServiceException("操作人不能为空"); |
| | | TTest current = testMapper.selectTestByIdForUpdate(testId); |
| | | if (current == null) throw new ServiceException("测试不存在"); |
| | | if (!"RECEIVING".equals(current.getReceptionStatus())) |
| | | throw new ServiceException("当前测试不是接收中状态"); |
| | | |
| | | Date actionAt = new Date(); |
| | | if (testMapper.updateReceptionPaused(testId, updateBy, actionAt) != 1) |
| | | throw new ServiceException("测试状态已变更,请刷新后重试"); |
| | | testMapper.insertTestAudit(testId, "RECEPTION_PAUSED", operatorId, updateBy, null, |
| | | JSON.toJSONString(Map.of("receptionStatus", "PAUSED", "polling", "STOPPED"))); |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int resumeReception(Long testId, Long operatorId, String updateBy) |
| | | { |
| | | if (testId == null) throw new ServiceException("测试ID不能为空"); |
| | | if (operatorId == null) throw new ServiceException("操作人不能为空"); |
| | | TTest current = testMapper.selectTestByIdForUpdate(testId); |
| | | if (current == null) throw new ServiceException("测试不存在"); |
| | | if (!"PAUSED".equals(current.getReceptionStatus())) |
| | | throw new ServiceException("当前测试不是暂停接收状态"); |
| | | Date actionAt = new Date(); |
| | | if (testMapper.updateReceptionResumed(testId, updateBy, actionAt) != 1) |
| | | throw new ServiceException("测试状态已变更,请刷新后重试"); |
| | | testMapper.insertTestAudit(testId, "RECEPTION_RESUMED", operatorId, updateBy, null, |
| | | JSON.toJSONString(Map.of("receptionStatus", "RECEIVING", "polling", "RESUMED"))); |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int stopTest(Long testId, Long operatorId, String updateBy) |
| | | { |
| | | if (testId == null) throw new ServiceException("测试ID不能为空"); |
| | | if (operatorId == null) throw new ServiceException("操作人不能为空"); |
| | | TTest current = testMapper.selectTestByIdForUpdate(testId); |
| | | if (current == null) throw new ServiceException("测试不存在"); |
| | | if (NOT_STARTED.equals(current.getReceptionStatus()) || "ENDED".equals(current.getReceptionStatus())) |
| | | throw new ServiceException("当前测试不能停止"); |
| | | Date actionAt = new Date(); |
| | | if (testMapper.updateReceptionStopped(testId, updateBy, actionAt) != 1) |
| | | throw new ServiceException("测试状态已变更,请刷新后重试"); |
| | | testMapper.deleteChannelLeasesByTestId(testId); |
| | | testMapper.deleteCollectorChannelLeasesByTestId(testId); |
| | | testMapper.insertTestAudit(testId, "TEST_STOPPED", operatorId, updateBy, null, |
| | | JSON.toJSONString(Map.of("testStatus", "TERMINATED", "receptionStatus", "ENDED", "polling", "STOPPED"))); |
| | | return 1; |
| | | } |
| | | |
| | | private boolean isOnline(String status) |
| | | { |
| | | return status != null && ("ONLINE".equalsIgnoreCase(status) || "0".equals(status)); |
| | | } |
| | | |
| | | private boolean isEnabled(String status) |
| | | { |
| | | return status == null || "0".equals(status); |
| | | } |
| | | |
| | | @Override |
| | |
| | | private void loadConfiguration(TTest test) |
| | | { |
| | | Long testId = test.getTestId(); |
| | | List<TTestPlanStep> planSteps = mutable(testMapper.selectTestPlanSteps(testId)); |
| | | List<TTestPlanStep> loadedPlanSteps = testMapper.selectTestPlanSteps(testId); |
| | | List<TTestPlanStep> planSteps = loadedPlanSteps == null ? mutable(test.getPlanSteps()) : mutable(loadedPlanSteps); |
| | | for (TTestPlanStep step : planSteps) |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | List<TTestBattery> batteries = mutable(testMapper.selectTestBatteries(testId)); |
| | | List<TTestBatteryField> fields = mutable(testMapper.selectTestBatteryFields(testId)); |
| | | List<TTestCollectorChannel> collectorChannels = mutable(testMapper.selectTestCollectorChannels(testId)); |
| | | List<TTestBattery> loadedBatteries = testMapper.selectTestBatteries(testId); |
| | | List<TTestBattery> batteries = loadedBatteries == null || (loadedBatteries.isEmpty() && test.getBatteries() != null |
| | | && !test.getBatteries().isEmpty()) ? mutable(test.getBatteries()) : mutable(loadedBatteries); |
| | | List<TTestBatteryField> loadedFields = testMapper.selectTestBatteryFields(testId); |
| | | List<TTestBatteryField> fields = loadedFields == null ? List.of() : mutable(loadedFields); |
| | | List<TTestCollectorChannel> loadedCollectorChannels = testMapper.selectTestCollectorChannels(testId); |
| | | List<TTestCollectorChannel> collectorChannels = loadedCollectorChannels == null ? List.of() : mutable(loadedCollectorChannels); |
| | | Map<Long, TTestChannelBinding> bindings = new HashMap<>(); |
| | | for (TTestChannelBinding binding : mutable(testMapper.selectTestChannelBindings(testId))) |
| | | List<TTestChannelBinding> loadedBindings = testMapper.selectTestChannelBindings(testId); |
| | | if (loadedBindings == null || (loadedBindings.isEmpty() && batteries.stream().anyMatch(item -> item.getBinding() != null))) |
| | | for (TTestBattery battery : batteries) |
| | | if (battery.getBinding() != null) bindings.put(battery.getRelationId(), battery.getBinding()); |
| | | for (TTestChannelBinding binding : mutable(loadedBindings)) |
| | | bindings.put(binding.getBatteryRelationId(), binding); |
| | | for (TTestBattery battery : batteries) |
| | | { |
| | | battery.setBinding(bindings.get(battery.getRelationId())); |
| | | battery.setFields(fields.stream() |
| | | .filter(field -> battery.getRelationId().equals(field.getRelationId())) |
| | | .toList()); |
| | | battery.setCollectorChannels(collectorChannels.stream() |
| | | .filter(mapping -> battery.getRelationId().equals(mapping.getBatteryRelationId())) |
| | | .toList()); |
| | | if (loadedFields != null && (!loadedFields.isEmpty() || battery.getFields() == null)) |
| | | battery.setFields(fields.stream() |
| | | .filter(field -> battery.getRelationId().equals(field.getRelationId())) |
| | | .toList()); |
| | | if (loadedCollectorChannels != null && (!loadedCollectorChannels.isEmpty() || battery.getCollectorChannels() == null)) |
| | | battery.setCollectorChannels(collectorChannels.stream() |
| | | .filter(mapping -> battery.getRelationId().equals(mapping.getBatteryRelationId())) |
| | | .toList()); |
| | | } |
| | | test.setPlanSteps(planSteps); |
| | | test.setBatteries(batteries); |
| | | test.setThresholds(mutable(testMapper.selectTestThresholds(testId))); |
| | | test.setFieldSnapshots(mutable(testMapper.selectTestFieldSnapshots(testId))); |
| | | test.setBatteryNo(joinBatteryNos(batteries)); |
| | | List<TTestWarningThreshold> loadedThresholds = testMapper.selectTestThresholds(testId); |
| | | test.setThresholds(loadedThresholds == null ? mutable(test.getThresholds()) : mutable(loadedThresholds)); |
| | | List<TTestFieldSnapshot> loadedSnapshots = sharedFieldSnapshotService.selectByTestId(testId); |
| | | test.setFieldSnapshots(loadedSnapshots == null ? mutable(test.getFieldSnapshots()) : loadedSnapshots); |
| | | } |
| | | |
| | | private void validateBase(TTest test, boolean update) |
| | |
| | | for (TTestCollectorChannel mapping : mappings) |
| | | { |
| | | if (mapping == null) throw new ServiceException("采集器通道映射不完整"); |
| | | mapping.setMeasurementCode(trimToNull(mapping.getMeasurementCode())); |
| | | mapping.setMeasurementCode(normalizeMeasurementCode(mapping.getMeasurementCode())); |
| | | mapping.setMeasurementName(trimToNull(mapping.getMeasurementName())); |
| | | mapping.setChannelCode(normalizeCollectorChannel(mapping.getChannelCode())); |
| | | if (mapping.getMeasurementCode() == null || mapping.getMeasurementName() == null |
| | |
| | | } |
| | | } |
| | | |
| | | 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()); |
| | |
| | | } |
| | | } |
| | | |
| | | /** 测量编码与数据库不区分大小写排序保持一致,避免校验通过后插入冲突。 */ |
| | | private String normalizeMeasurementCode(String value) |
| | | { |
| | | String normalized = trimToNull(value); |
| | | return normalized == null ? null : normalized.toUpperCase(Locale.ROOT); |
| | | } |
| | | |
| | | private <T> List<T> mutable(List<T> values) |
| | | { |
| | | return values == null ? new ArrayList<>() : new ArrayList<>(values); |