| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | throw new ServiceException("模板不存在"); |
| | | } |
| | | List<Long> fieldIds = new ArrayList<>(); |
| | | Map<Long, String> fieldTypes = new HashMap<>(); |
| | | if (group.getTemplateFieldRelations() != null) |
| | | { |
| | | for (TBatteryGroupRelationField relation : group.getTemplateFieldRelations()) |
| | |
| | | { |
| | | throw new ServiceException("电池组字段必须与模板字段一致"); |
| | | } |
| | | if (batteryGroupMapper.selectValidTemplateFields(group.getTemplateId(), fieldIds).size() != fieldIds.size()) |
| | | List<TBatteryGroupRelationField> validFields = batteryGroupMapper.selectValidTemplateFields(group.getTemplateId(), fieldIds); |
| | | if (validFields == null || validFields.size() != fieldIds.size()) |
| | | { |
| | | throw new ServiceException("电池组字段不属于指定模板"); |
| | | } |
| | | for (TBatteryGroupRelationField field : validFields) |
| | | { |
| | | fieldTypes.put(field.getBatteryGroupFieldId(), field.getFieldType()); |
| | | } |
| | | if (group.getTemplateFieldRelations() != null) |
| | | { |
| | | for (TBatteryGroupRelationField relation : group.getTemplateFieldRelations()) |
| | | { |
| | | relation.setFieldValue(trimToNull(relation.getFieldValue())); |
| | | validateFieldValue(fieldTypes.get(relation.getBatteryGroupFieldId()), relation.getFieldValue()); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | batteryGroupMapper.batchInsertRelationFields(relations); |
| | | } |
| | | |
| | | /** 按字段定义校验业务值,空值表示该字段暂未填写。 */ |
| | | private void validateFieldValue(String fieldType, String value) |
| | | { |
| | | if (value == null || fieldType == null) return; |
| | | try |
| | | { |
| | | if ("number".equals(fieldType)) |
| | | { |
| | | new BigDecimal(value); |
| | | } |
| | | else if ("time".equals(fieldType)) |
| | | { |
| | | try |
| | | { |
| | | LocalDate.parse(value); |
| | | } |
| | | catch (DateTimeParseException exception) |
| | | { |
| | | try |
| | | { |
| | | LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | } |
| | | catch (DateTimeParseException nested) |
| | | { |
| | | LocalDateTime.parse(value); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (NumberFormatException | DateTimeParseException exception) |
| | | { |
| | | throw new ServiceException("模板字段值格式不正确"); |
| | | } |
| | | } |
| | | |
| | | private void validateStatusChange(TBatteryGroup group, String currentStatus) |
| | | { |
| | | if ("1".equals(group.getStatus()) && !"1".equals(currentStatus) |
| | |
| | | throw new ServiceException("测试中的电池组不能禁用"); |
| | | } |
| | | } |
| | | |
| | | private String trimToNull(String value) |
| | | { |
| | | String trimmed = StringUtils.trim(value); |
| | | return StringUtils.isEmpty(trimmed) ? null : trimmed; |
| | | } |
| | | } |