hyingbo
7 天以前 418d29b85d943f57b5600d84acf9cf6ca0ce9173
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
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.dnc.dto.DeviceGroupExt;
import org.jeecg.modules.dnc.entity.DeviceGroup;
import org.jeecg.modules.dnc.entity.DeviceGroupPermission;
import org.jeecg.modules.dnc.entity.DevicePermissionStream;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DeviceGroupMapper;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.response.DeviceGroupCode;
import org.jeecg.modules.dnc.service.IDeviceGroupPermissionService;
import org.jeecg.modules.dnc.service.IDeviceGroupService;
import org.jeecg.modules.dnc.service.IDevicePermissionStreamService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
@Service
public class DeviceGroupServiceImpl extends ServiceImpl<DeviceGroupMapper, DeviceGroup> implements IDeviceGroupService {
    @Autowired
    private IDeviceGroupPermissionService groupPermissionService;
    @Autowired
    private IDevicePermissionStreamService devicePermissionStreamService;
 
 
    //  查询所以父节点
    @Override
    public List<String> findListParentTree(String parentId,List<String> stringList){
        if (StringUtils.isEmpty(parentId)) {
            return null;
        }
        if (stringList == null || stringList.isEmpty()) {
            stringList = new ArrayList<>();
        }
        boolean p = true;
        if (p) {
            DeviceGroup en = super.getById(parentId);
            if (en != null) {
                stringList.add(0,en.getGroupName());
            }
            if (StringUtils.isNotBlank(en.getParentId())) {
                parentId = en.getParentId();
                findListParentTree(parentId,stringList);
            } else {
                p = false;
                return stringList;
            }
        }
        return stringList;
    }
 
    @Override
    public List<DeviceGroupExt> findExtAll() {
        return super.getBaseMapper().findExtAll();
    }
 
    @Override
    public List<DeviceGroupExt> getByUserPerms(String userId) {
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        return super.getBaseMapper().getByUserPerms(userId);
    }
 
    @Override
    public List<DeviceGroupExt> getByUserPermsAs(String userId) {
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        return super.getBaseMapper().getByUserPermsAs(userId);
    }
    @Override
    public List<String> findListParentTreeAll(String groupId) {
        if ( StringUtils.isEmpty(groupId)) {
            return null;
        }
        List<String> strings = new ArrayList<>();
        DeviceGroup en = super.getById(groupId);
        if (en == null) {
            return null;
        }
        strings.add(en.getGroupName());
        if (StringUtils.isEmpty(en.getParentId())) {
            return strings;
        } else {
            return findListParentTree(en.getParentId(),strings);
        }
    }
    @Override
    public List<DeviceGroup> getChildrenByParentId(String groupId) {
        if(!ValidateUtil.validateString(groupId))
            return null;
        List<DeviceGroupExt> extList = super.getBaseMapper().findByParentId(groupId);
        if(extList == null || extList.isEmpty())
            return null;
        List<DeviceGroup> list = new ArrayList<>();
        extList.forEach(item -> {
            item.getAllChildren(list);
        });
        return list;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddUser(DeviceGroup deviceGroup, Collection<SysUser> userList) {
        if(deviceGroup == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupPermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            DeviceGroupPermission en = groupPermissionService.getByUserIdAndGroupId(item.getId(), deviceGroup.getGroupId());
            if(en == null) {
                en = new DeviceGroupPermission();
                en.setUserId(item.getId());
                en.setGroupId(deviceGroup.getGroupId());
                permissionList.add(en);
            }
            DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndUserId(deviceGroup.getGroupId(), item.getId());
            if(stream == null) {
                stream = new DevicePermissionStream();
                stream.setUserId(item.getId());
                stream.setGroupId(deviceGroup.getGroupId());
                permissionStreamList.add(stream);
            }
        });
        if(!permissionList.isEmpty()) {
            boolean b = groupPermissionService.saveBatch(permissionList);
            if(!b) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = devicePermissionStreamService.saveBatch(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignRemoveUser(DeviceGroup deviceGroup, Collection<SysUser> userList) {
        if(deviceGroup == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<DeviceGroupPermission> permissionList = new ArrayList<>();
        List<DevicePermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            DeviceGroupPermission en = groupPermissionService.getByUserIdAndGroupId(item.getId(), deviceGroup.getGroupId());
            if(en != null) {
                permissionList.add(en);
            }
            DevicePermissionStream stream = devicePermissionStreamService.getByGroupIdAndUserId(deviceGroup.getGroupId(), item.getId());
            if(stream != null) {
                permissionStreamList.add(stream);
            }
        });
        //校验清空设备分组权限操作
        List<DeviceGroupPermission> existList = groupPermissionService.getByGroupId(deviceGroup.getGroupId());
        if(existList.size() <= permissionList.size())
            ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_USER_NONE);
        if(!permissionList.isEmpty()) {
            boolean b = groupPermissionService.removeByCollection(permissionList);
            if(!b) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = devicePermissionStreamService.removeByCollection(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(DeviceGroupCode.DEVICE_GROUP_PERM_ERROR);
            }
        }
        return true;
    }
 
    @Override
    public List<DeviceGroupExt> findExtByDeparts(List<String> departIds) {
        return super.getBaseMapper().findExtByDeparts(departIds);
    }
}