yeg
2026-09-02 a5b6c6d16634f1fa14edf6fe5e7dd6080bef1772
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
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.TBatteryGroup;
import com.ruoyi.system.domain.TBatteryGroupBattery;
import com.ruoyi.system.domain.TBatteryGroupRelationField;
import com.ruoyi.system.mapper.TBatteryGroupMapper;
import com.ruoyi.system.service.ITBatteryGroupService;
 
/** 电池组管理服务实现。 */
@Service
public class TBatteryGroupServiceImpl implements ITBatteryGroupService
{
    private static final String ENABLED = "0";
 
    @Autowired
    private TBatteryGroupMapper batteryGroupMapper;
 
    /** 按查询条件返回未删除的电池组列表,并附带共享字段值。 */
    @Override
    public List<TBatteryGroup> selectBatteryGroupList(TBatteryGroup group)
    {
        List<TBatteryGroup> groups = batteryGroupMapper.selectBatteryGroupList(group);
        if (groups == null)
        {
            return List.of();
        }
        for (TBatteryGroup item : groups)
        {
            item.setTemplateFields(batteryGroupMapper.selectRelationFields(item.getGroupId()));
        }
        return groups;
    }
 
    /** 返回已有电池组的创建人选项。 */
    @Override
    public List<Map<String, Object>> selectBatteryGroupCreatorOptions()
    {
        return batteryGroupMapper.selectBatteryGroupCreatorOptions();
    }
 
    /** 返回电池组及其电池号、模板字段和值。 */
    @Override
    public TBatteryGroup selectBatteryGroupById(Long groupId)
    {
        TBatteryGroup group = batteryGroupMapper.selectBatteryGroupById(groupId);
        if (group == null)
        {
            throw new ServiceException("电池组不存在");
        }
        group.setBatteries(batteryGroupMapper.selectBatteries(groupId));
        group.setTemplateFields(batteryGroupMapper.selectRelationFields(groupId));
        return group;
    }
 
    /** 新增电池组并原子写入电池号和可选模板字段值。 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBatteryGroup(TBatteryGroup group)
    {
        if (StringUtils.isEmpty(group.getStatus()))
        {
            group.setStatus(ENABLED);
        }
        validate(group, false);
        group.setGroupName(StringUtils.trim(group.getGroupName()));
        group.setCreateTime(new Date());
        int rows = batteryGroupMapper.insertBatteryGroup(group);
        replaceRelations(group);
        return rows;
    }
 
    /** 更新电池组并原子替换电池号和可选模板字段值。 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBatteryGroup(TBatteryGroup group)
    {
        if (group == null || group.getGroupId() == null)
        {
            throw new ServiceException("电池组ID不能为空");
        }
        TBatteryGroup current = batteryGroupMapper.selectBatteryGroupById(group.getGroupId());
        if (current == null)
        {
            throw new ServiceException("电池组不存在");
        }
        if (StringUtils.isEmpty(group.getStatus()))
        {
            group.setStatus(current.getStatus());
        }
        validateStatusChange(group, current.getStatus());
        validate(group, true);
        group.setGroupName(StringUtils.trim(group.getGroupName()));
        group.setUpdateTime(new Date());
        int rows = batteryGroupMapper.updateBatteryGroup(group);
        if (rows == 0)
        {
            throw new ServiceException("电池组不存在或已删除");
        }
        replaceRelations(group);
        return rows;
    }
 
    /** 启用或禁用电池组,存在进行中测试时禁止禁用。 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBatteryGroupStatus(TBatteryGroup group)
    {
        if (group == null || group.getGroupId() == null || StringUtils.isEmpty(group.getStatus()))
        {
            throw new ServiceException("电池组ID和目标状态不能为空");
        }
        if (!ENABLED.equals(group.getStatus()) && !"1".equals(group.getStatus()))
        {
            throw new ServiceException("电池组状态不正确");
        }
        if ("1".equals(group.getStatus()) && batteryGroupMapper.countTestingByGroupId(group.getGroupId()) > 0)
        {
            throw new ServiceException("检测中的电池组不能禁用");
        }
        return batteryGroupMapper.updateBatteryGroupStatus(group);
    }
 
    /** 逻辑删除电池组及关系,存在进行中测试时拒绝删除。 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBatteryGroupByIds(Long[] groupIds, String updateBy)
    {
        if (groupIds == null || groupIds.length == 0)
        {
            throw new ServiceException("电池组ID不能为空");
        }
        int rows = 0;
        for (Long groupId : groupIds)
        {
            if (groupId == null)
            {
                continue;
            }
            if (batteryGroupMapper.countTestingByGroupId(groupId) > 0)
            {
                throw new ServiceException("检测中的电池组不能删除");
            }
            rows += batteryGroupMapper.deleteBatteryGroup(groupId, updateBy);
            batteryGroupMapper.deleteBatteries(groupId);
            batteryGroupMapper.deleteRelationFields(groupId);
        }
        return rows;
    }
 
    private void validate(TBatteryGroup group, boolean update)
    {
        if (group == null)
        {
            throw new ServiceException("电池组信息不能为空");
        }
        if (update && group.getGroupId() == null)
        {
            throw new ServiceException("电池组ID不能为空");
        }
        if (StringUtils.isEmpty(StringUtils.trim(group.getGroupName())))
        {
            throw new ServiceException("电池组名称不能为空");
        }
        if (StringUtils.isEmpty(StringUtils.trim(group.getModel())))
        {
            throw new ServiceException("电池组型号不能为空");
        }
        if (!ENABLED.equals(group.getStatus()) && !"1".equals(group.getStatus()))
        {
            throw new ServiceException("电池组状态不正确");
        }
        TBatteryGroup existing = batteryGroupMapper.selectBatteryGroupByName(StringUtils.trim(group.getGroupName()));
        if (existing != null && (group.getGroupId() == null || !group.getGroupId().equals(existing.getGroupId())))
        {
            throw new ServiceException("电池组名称已存在");
        }
        if (group.getBatteryNos() == null || group.getBatteryNos().isEmpty())
        {
            throw new ServiceException("电池号不能为空");
        }
        Set<String> uniqueNos = new HashSet<>();
        for (String batteryNo : group.getBatteryNos())
        {
            String normalized = StringUtils.trim(batteryNo);
            if (StringUtils.isEmpty(normalized) || !uniqueNos.add(normalized))
            {
                throw new ServiceException("电池号不能重复或为空");
            }
            TBatteryGroupBattery existingBattery = batteryGroupMapper.selectBatteryByNo(normalized, group.getGroupId());
            if (existingBattery != null && (group.getGroupId() == null || !group.getGroupId().equals(existingBattery.getGroupId())))
            {
                throw new ServiceException("电池号已被其他电池组使用");
            }
        }
        if (group.getTemplateId() == null)
        {
            if (group.getTemplateFieldRelations() != null && !group.getTemplateFieldRelations().isEmpty())
            {
                throw new ServiceException("未选择模板时不能配置模板字段");
            }
            return;
        }
        if (batteryGroupMapper.selectTemplate(group.getTemplateId()) == null)
        {
            throw new ServiceException("模板不存在");
        }
        List<Long> fieldIds = new ArrayList<>();
        Map<Long, String> fieldTypes = new HashMap<>();
        if (group.getTemplateFieldRelations() != null)
        {
            for (TBatteryGroupRelationField relation : group.getTemplateFieldRelations())
            {
                if (relation == null || relation.getBatteryGroupFieldId() == null || !fieldIds.add(relation.getBatteryGroupFieldId()))
                {
                    throw new ServiceException("模板字段关系不正确");
                }
                if (relation.getFieldValue() != null && relation.getFieldValue().length() > 500)
                {
                    throw new ServiceException("模板字段值长度不能超过500个字符");
                }
            }
        }
        List<Long> templateFieldIds = batteryGroupMapper.selectTemplateFieldIds(group.getTemplateId());
        if (templateFieldIds == null || templateFieldIds.isEmpty())
        {
            throw new ServiceException("模板未配置有效字段");
        }
        if (fieldIds.isEmpty())
        {
            fieldIds.addAll(templateFieldIds);
        }
        if (!new HashSet<>(templateFieldIds).equals(new HashSet<>(fieldIds)))
        {
            throw new ServiceException("电池组字段必须与模板字段一致");
        }
        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());
            }
        }
    }
 
    private void replaceRelations(TBatteryGroup group)
    {
        batteryGroupMapper.deleteBatteries(group.getGroupId());
        batteryGroupMapper.deleteRelationFields(group.getGroupId());
        List<TBatteryGroupBattery> batteries = new ArrayList<>();
        for (int i = 0; i < group.getBatteryNos().size(); i++)
        {
            TBatteryGroupBattery item = new TBatteryGroupBattery();
            item.setGroupId(group.getGroupId());
            item.setBatteryNo(StringUtils.trim(group.getBatteryNos().get(i)));
            item.setSortOrder(i);
            item.setDelFlag("0");
            batteries.add(item);
        }
        batteryGroupMapper.batchInsertBatteries(batteries);
        if (group.getTemplateId() == null)
        {
            return;
        }
        List<TBatteryGroupRelationField> relations = group.getTemplateFieldRelations();
        if (relations == null || relations.isEmpty())
        {
            relations = new ArrayList<>();
            for (Long fieldId : batteryGroupMapper.selectTemplateFieldIds(group.getTemplateId()))
            {
                TBatteryGroupRelationField relation = new TBatteryGroupRelationField();
                relation.setBatteryGroupFieldId(fieldId);
                relations.add(relation);
            }
        }
        for (TBatteryGroupRelationField relation : relations)
        {
            relation.setGroupId(group.getGroupId());
            relation.setTemplateId(group.getTemplateId());
            relation.setDelFlag("0");
        }
        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)
                && batteryGroupMapper.countTestingByGroupId(group.getGroupId()) > 0)
        {
            throw new ServiceException("测试中的电池组不能禁用");
        }
    }
 
    private String trimToNull(String value)
    {
        String trimmed = StringUtils.trim(value);
        return StringUtils.isEmpty(trimmed) ? null : trimmed;
    }
}