yeg
2026-08-27 2b64fd747005b44b685e616da71b63c92c536fe3
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
package com.ruoyi.system.service.impl;
 
import java.util.ArrayList;
import java.util.Date;
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";
    private static final String TESTING = "1";
 
    @Autowired
    private TBatteryGroupMapper batteryGroupMapper;
 
    @Override
    public List<TBatteryGroup> selectBatteryGroupList(TBatteryGroup group)
    {
        return batteryGroupMapper.selectBatteryGroupList(group);
    }
 
    @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)
    {
        validate(group, false);
        group.setGroupName(StringUtils.trim(group.getGroupName()));
        group.setCreateTime(new Date());
        if (StringUtils.isEmpty(group.getStatus()))
        {
            group.setStatus(ENABLED);
        }
        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不能为空");
        }
        if (batteryGroupMapper.selectBatteryGroupById(group.getGroupId()) == null)
        {
            throw new ServiceException("电池组不存在");
        }
        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 (TESTING.equals(group.getStatus()) || !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("电池组型号不能为空");
        }
        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<>();
        if (group.getTemplateFieldRelations() != null)
        {
            for (TBatteryGroupRelationField relation : group.getTemplateFieldRelations())
            {
                if (relation == null || relation.getBatteryGroupFieldId() == null || !fieldIds.add(relation.getBatteryGroupFieldId()))
                {
                    throw new ServiceException("模板字段关系不正确");
                }
            }
        }
        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("电池组字段必须与模板字段一致");
        }
        if (batteryGroupMapper.selectValidTemplateFields(group.getTemplateId(), fieldIds).size() != fieldIds.size())
        {
            throw new ServiceException("电池组字段不属于指定模板");
        }
    }
 
    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);
    }
}