liusuyi
2026-05-12 59b5859e049628aaeb1b971057a7a0427f92eb16
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
package com.ard.qs.service.impl;
 
import com.google.common.collect.Lists;
import com.ard.common.core.utils.DateUtils;
import com.ard.qs.common.GbCode;
import com.ard.qs.api.domain.QsGroup;
import com.ard.qs.domain.QsGroupTree;
import com.ard.qs.mapper.QsGroupMapper;
import com.ard.qs.service.IQsDeviceService;
import com.ard.qs.service.IQsGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 分组管理服务层处理
 *
 * @FileName QsGroupServiceImpl
 * @Description
 * @Author fengcheng
 * @date 2026-04-14
 **/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class QsGroupServiceImpl implements IQsGroupService {
 
    @Autowired
    private QsGroupMapper qsGroupMapper;
 
    @Autowired
    private IQsDeviceService qsDeviceService;
 
    /**
     * 添加分组
     *
     * @param group 分组信息
     */
    @Override
    public void add(QsGroup group) {
        Assert.notNull(group, "参数不可为NULL");
        Assert.notNull(group.getDeviceId(), "分组编号不可为NULL");
        Assert.isTrue(group.getDeviceId().trim().length() == 20, "分组编号必须为20位");
        Assert.notNull(group.getName(), "分组名称不可为NULL");
 
        GbCode gbCode = GbCode.decode(group.getDeviceId());
        Assert.notNull(gbCode, "分组编号不满足国标定义");
 
        // 查询数据库中已经存在的.
        List<QsGroup> groupListInDb = qsGroupMapper.queryInGroupListByDeviceId(Lists.newArrayList(group));
        if (!ObjectUtils.isEmpty(groupListInDb)) {
            throw new RuntimeException(String.format("该节点编号 %s 已存在", group.getDeviceId()));
        }
 
        if ("215".equals(gbCode.getTypeCode())) {
            // 添加业务分组
            addBusinessGroup(group);
        } else {
            Assert.isTrue("216".equals(gbCode.getTypeCode()), "创建虚拟组织时设备编号11-13位应使用216");
            // 添加虚拟组织
            addGroup(group);
        }
    }
 
    /**
     * 更新分组
     *
     * @param group 分组信息
     */
    @Override
    public void update(QsGroup group) {
        Assert.isTrue(group.getId() > 0, "更新必须携带分组ID");
        Assert.notNull(group.getDeviceId(), "编号不可为NULL");
        Assert.notNull(group.getBusinessGroup(), "业务分组不可为NULL");
        QsGroup groupInDb = qsGroupMapper.queryOne(group.getId());
        Assert.notNull(groupInDb, "分组不存在");
 
        // 查询数据库中已经存在的.
        List<QsGroup> groupListInDb = qsGroupMapper.queryInGroupListByDeviceId(Lists.newArrayList(group));
        if (!ObjectUtils.isEmpty(groupListInDb) && groupListInDb.get(0).getId() != group.getId()) {
            throw new RuntimeException(String.format("该该节点编号 %s 已存在", group.getDeviceId()));
        }
 
        group.setName(group.getName());
        group.setUpdateTime(DateUtils.getNowDate());
        qsGroupMapper.update(group);
 
        // 修改他的子节点
        if (!group.getDeviceId().equals(groupInDb.getDeviceId())
                || !group.getBusinessGroup().equals(groupInDb.getBusinessGroup())) {
            List<QsGroup> groupList = queryAllChildren(groupInDb.getId());
            if (!groupList.isEmpty()) {
                int result = qsGroupMapper.updateChild(groupInDb.getId(), group);
                if (result > 0) {
                    for (QsGroup chjildGroup : groupList) {
                        chjildGroup.setParentDeviceId(group.getDeviceId());
                        chjildGroup.setBusinessGroup(group.getBusinessGroup());
                    }
                }
            }
        }
 
        // 由于编号变化,会需要处理太多内容以及可能发送大量消息,所以目前更新只只支持重命名
        GbCode decode = GbCode.decode(group.getDeviceId());
        if (!groupInDb.getDeviceId().equals(group.getDeviceId())) {
            if (decode.getTypeCode().equals("215")) {
                // 业务分组变化。需要将其下的所有业务分组修改
                qsDeviceService.updateBusinessGroup(groupInDb.getDeviceId(), group.getDeviceId());
            } else {
                // 虚拟组织修改,需要把其下的子节点修改父节点ID
                qsDeviceService.updateParentIdGroup(groupInDb.getDeviceId(), group.getDeviceId());
            }
        }
    }
 
    /**
     * 删除分组
     *
     * @param id 分组id
     */
    @Override
    public boolean delete(Integer id) {
        QsGroup group = qsGroupMapper.queryOne(id);
        Assert.notNull(group, "分组不存在");
        List<QsGroup> groupListForDelete = new ArrayList<>();
        GbCode gbCode = GbCode.decode(group.getDeviceId());
        if (gbCode.getTypeCode().equals("215")) {
            List<QsGroup> groupList = qsGroupMapper.queryByBusinessGroup(group.getDeviceId());
            if (!groupList.isEmpty()) {
                groupListForDelete.addAll(groupList);
            }
            // 业务分组
            qsDeviceService.removeParentIdByBusinessGroup(group.getDeviceId());
        } else {
            List<QsGroup> groupList = queryAllChildren(group.getId());
            if (!groupList.isEmpty()) {
                groupListForDelete.addAll(groupList);
            }
            groupListForDelete.add(group);
            qsDeviceService.removeParentIdByGroupList(groupListForDelete);
        }
        qsGroupMapper.batchDelete(groupListForDelete);
 
        return true;
    }
 
    /**
     * 查询分组树
     *
     * @param query     查询条件
     * @param parentId  父节点
     * @param hasDevice 是否包含设备
     * @return
     */
    @Override
    public List<QsGroupTree> queryForTree(String query, Integer parentId, Boolean hasDevice) {
        List<QsGroupTree> groupTrees = qsGroupMapper.queryForTree(query, parentId);
        if (parentId == null) {
            return groupTrees;
        }
        // 查询含有的通道
        QsGroup parentGroup = qsGroupMapper.queryOne(parentId);
        if (parentGroup != null && hasDevice != null && hasDevice) {
            List<QsGroupTree> groupTreesForChannel = qsDeviceService.queryForGroupTreeByParentId(query, parentGroup.getDeviceId());
            if (!ObjectUtils.isEmpty(groupTreesForChannel)) {
                groupTrees.addAll(groupTreesForChannel);
            }
        }
        return groupTrees;
    }
 
    /**
     * 查询分组节点设备
     *
     * @return
     */
    @Override
    public List<QsGroupTree> queryForDevice() {
        return qsGroupMapper.queryForDevice();
    }
 
    /**
     * 查询分组
     *
     * @param query 要搜索的内容
     * @return
     */
    @Override
    public List<QsGroup> queryList(String query) {
        if (query != null) {
            query = query.replaceAll("/", "//")
                    .replaceAll("%", "/%")
                    .replaceAll("_", "/_");
        }
        List<QsGroup> list = qsGroupMapper.query(query, null, null);
        return list;
    }
 
    private List<QsGroup> queryAllChildren(int id) {
        List<QsGroup> children = qsGroupMapper.getChildren(id);
        if (ObjectUtils.isEmpty(children)) {
            return children;
        }
        for (int i = 0; i < children.size(); i++) {
            children.addAll(queryAllChildren(children.get(i).getId()));
        }
        return children;
    }
 
    private void addBusinessGroup(QsGroup group) {
        group.setBusinessGroup(group.getDeviceId());
        group.setCreateTime(DateUtils.getNowDate());
        qsGroupMapper.addBusinessGroup(group);
    }
 
    private void addGroup(QsGroup group) {
        // 建立虚拟组织
        Assert.notNull(group.getBusinessGroup(), "所属的业务分组分组不存在");
        QsGroup businessGroup = qsGroupMapper.queryBusinessGroup(group.getBusinessGroup());
        Assert.notNull(businessGroup, "所属的业务分组分组不存在");
        if (!ObjectUtils.isEmpty(group.getParentDeviceId())) {
            QsGroup parentGroup = qsGroupMapper.queryOneByDeviceId(group.getParentDeviceId(), group.getBusinessGroup());
            Assert.notNull(parentGroup, "所属的上级分组分组不存在");
        } else {
            group.setParentDeviceId(null);
        }
        group.setCreateTime(DateUtils.getNowDate());
        qsGroupMapper.add(group);
    }
}