lyh
2025-01-16 1d84a3c62eeee429f7d7d6339bcf9b504a9d7277
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
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.activiti.entity.ActivitiDefinition;
import org.jeecg.modules.activiti.service.IActivitiDefinitionService;
import org.jeecg.modules.dnc.dto.DepartApproveUser;
import org.jeecg.modules.dnc.dto.DepartmentExt;
import org.jeecg.modules.dnc.dto.DepartmentUser;
import org.jeecg.modules.dnc.entity.DevicePermissionStream;
import org.jeecg.modules.dnc.entity.PermissionStream;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DepartmentMapper;
import org.jeecg.modules.dnc.request.DepartmentRequest;
import org.jeecg.modules.dnc.response.*;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.service.support.DepartmentTreeWrapper;
import org.jeecg.modules.dnc.service.support.FindsDepartmentChildrenUtil;
import org.jeecg.modules.dnc.ucenter.Department;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDepartApproveUserService departApproveUserService;
    @Autowired
    private IDepartmentUserService departmentUserService;
    @Autowired
    private IPermissionStreamService permissionStreamService;
    @Autowired
    private IDevicePermissionStreamService devicePermissionStreamService;
    @Autowired
    private IActivitiDefinitionService activitiDefinitionService;
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDepartment(Department department) {
        if(department == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(department.getDepartCode()))
            ExceptionCast.cast(DepartmentCode.DEPART_CODE_NONE);
        if(!ValidateUtil.validateString(department.getDepartName()))
            ExceptionCast.cast(DepartmentCode.DEPART_NAME_NONE);
        Department en = this.getByDepartCode(department.getDepartCode(), null);
        if(en != null)
            ExceptionCast.cast(DepartmentCode.DEPART_IS_EXIST);
        if(ValidateUtil.validateString(department.getParentId())) {
            Department parent = super.getById(department.getParentId());
            if(parent == null)
                ExceptionCast.cast(DepartmentCode.DEPART_PARENT_NOT_EXIST);
            department.setRankLevel(parent.getRankLevel() + 1);
        }else {
            department.setParentId(null);
            department.setRankLevel(1);
        }
        return super.save(department);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean editDepartment(String id, Department department) {
        if(!ValidateUtil.validateString(id) || department == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        Department en = super.getById(id);
        if (en == null)
            ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
        department.setDepartId(id);
        department.setDepartCode(null);
        if(ValidateUtil.validateString(department.getParentId())) {
            if (!department.getParentId().equals(en.getParentId())) {
                Department parent = super.getById(department.getParentId());
                if(parent == null)
                    ExceptionCast.cast(DepartmentCode.DEPART_PARENT_NOT_EXIST);
                department.setRankLevel(parent.getRankLevel() + 1);
            }
        }else {
            department.setRankLevel(1);
        }
        return super.updateById(department);
    }
 
    @Override
    public Department getByDepartCode(String departCode, String departId) {
        if(!ValidateUtil.validateString(departCode))
            return null;
        LambdaQueryChainWrapper<Department> lambdaQueryChain = super.lambdaQuery();
        lambdaQueryChain.eq(Department::getDepartCode, departCode);
        if(ValidateUtil.validateString(departId)) {
            lambdaQueryChain.ne(Department::getDepartId, departCode);
        }
        List<Department> departmentList = lambdaQueryChain.list();
        if(!ValidateUtil.validateString(departCode))
            return null;
        if(departmentList == null || departmentList.isEmpty())
            return null;
        return departmentList.get(0);
    }
 
    @Override
    public List<Department> getUserNonPermDepart(String userId) {
        return super.getBaseMapper().getUserNonPermDepart(userId);
    }
 
    @Override
    public List<Department> getUserPermDepart(String userId) {
        return super.getBaseMapper().getUserPermDepart(userId);
    }
 
    @Override
    public QueryPageResponseResult<Department> findPageList(int page, int size, DepartmentRequest requestParams) {
        if(page < 1 || size < 1) {
            ExceptionCast.cast(CommonCode.INVALID_PAGE);
        }
        IPage<Department> pageData = new Page<>(page, size);
        LambdaQueryChainWrapper<Department> lambdaQuery = super.lambdaQuery();
        if(requestParams != null) {
            if(ValidateUtil.validateString(requestParams.getDepartName())) {
                lambdaQuery.like(Department::getDepartName, requestParams.getDepartName());
            }
            if(ValidateUtil.validateString(requestParams.getDepartCode())) {
                lambdaQuery.like(Department::getDepartCode, requestParams.getDepartCode());
            }
            if(ValidateUtil.validateString(requestParams.getId())) {
                lambdaQuery.eq(Department::getParentId,requestParams.getId());
            }
            if(ValidateUtil.validateString(requestParams.getAscStr())) {
                String[] ascArr = requestParams.getAscStr().split(",");
//                ((Page<Department>) pageData).setAsc(ascArr);
            }
            if(ValidateUtil.validateString(requestParams.getDescStr())) {
                String[] descStr = requestParams.getDescStr().split(",");
//                ((Page<Department>) pageData).setDesc(descStr);
            }
        }
        IPage<Department> userIPage = lambdaQuery.page(pageData);
        return new QueryPageResponseResult<>(CommonCode.SUCCESS, userIPage);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteDepartmentById(String id) {
        if(!ValidateUtil.validateString(id)) {
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        Department en = super.getById(id);
        if (en == null)
            ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
        List<Department> list =  findSunLint(id);
        if (list != null && list.size() > 0) {
            ExceptionCast.cast(DepartmentCode.DEPART_SUN_EXIST);
        }
        List<DepartmentUser> departmentUserList = departmentUserService.findByDepartId(id);
        if(departmentUserList != null && !departmentUserList.isEmpty())
            ExceptionCast.cast(DepartmentCode.DEPART_USER_EXIST);
        List<PermissionStream> permissionStreamList = permissionStreamService.findByDepartId(id);
        if(permissionStreamList != null && !permissionStreamList.isEmpty())
            ExceptionCast.cast(DepartmentCode.DEPART_PRODUCT_EXIST);
        List<DevicePermissionStream> devicePermissionStreams = devicePermissionStreamService.findByDepartId(id);
        if(devicePermissionStreams != null && !devicePermissionStreams.isEmpty())
            ExceptionCast.cast(DepartmentCode.DEPART_DEVICE_EXIST);
        ActivitiDefinition activitiDefinition = activitiDefinitionService.getByDepartId(id);
        if(activitiDefinition != null)
            ExceptionCast.cast(DepartmentCode.DEPART_ACT_EXIST);
        return super.removeById(id);
    }
 
    @Override
    public List<Department> findSunLint(String parentId) {
        if(!ValidateUtil.validateString(parentId))
            return null;
        List<Department> list = super.lambdaQuery().eq(Department::getParentId, parentId).list();
        if(list == null || list.isEmpty())
            return null;
        return list;
    }
 
    @Override
    public List<CommonJsonTree> loadTree() {
        List<DepartmentExt> multilevelExt = this.getBaseMapper().findExtAll();
        return DepartmentTreeWrapper.loadTree(multilevelExt);
    }
 
    @Override
    public Map<String, Department> getMapByUserId(String userId) {
        if(!ValidateUtil.validateString(userId))
            return null;
        List<Department> userPermDepart = getUserPermDepart(userId);
        if(userPermDepart == null || userPermDepart.isEmpty())
            return null;
        Map<String, Department> map = new HashMap<>();
        userPermDepart.forEach(item -> {
            map.put(item.getDepartId(), item);
        });
        return map;
    }
 
    @Override
    public List<SysUser> getUserNonApproveDepart(String departId) {
        return super.getBaseMapper().getUserNonApproveDepart(departId);
    }
 
    @Override
    public List<SysUser> getUserApproveDepart(String departId) {
        return super.getBaseMapper().getUserApproveDepart(departId);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddApproveUser(String departId, Integer relativeFlag, String[] userIds) {
        if(!ValidateUtil.validateString(departId) ||! ValidateUtil.validateInteger(relativeFlag))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(userIds == null || userIds.length < 1)
            ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
        Department en = super.getById(departId);
        if(en == null)
            ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
        List<String> userIdList = new ArrayList<>(userIds.length);
        Collections.addAll(userIdList, userIds);
        Collection<SysUser> userCollection = userService.listByIds(userIdList);
        if(userCollection == null || userCollection.size() != userIds.length)
            ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
        List<DepartApproveUser> permissionList = new ArrayList<>();
        userCollection.forEach(item -> {
            DepartApproveUser approveUser = departApproveUserService.getByDepartIdAndUserId(en.getDepartId(), item.getId());
            if(approveUser == null) {
                approveUser = new DepartApproveUser();
                approveUser.setUserId(item.getId());
                approveUser.setDepartId(en.getDepartId());
                permissionList.add(approveUser);
            }
        });
        if(!permissionList.isEmpty()) {
            return departApproveUserService.saveBatch(permissionList);
        }
        return false;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignRemoveApproveUser(String departId, Integer relativeFlag, String[] userIds) {
        if(!ValidateUtil.validateString(departId) ||! ValidateUtil.validateInteger(relativeFlag))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(userIds == null || userIds.length < 1)
            ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
        Department en = super.getById(departId);
        if(en == null)
            ExceptionCast.cast(DepartmentCode.DEPART_NOT_EXIST);
        List<String> userIdList = new ArrayList<>(userIds.length);
        Collections.addAll(userIdList, userIds);
        Collection<SysUser> userCollection = userService.listByIds(userIdList);
        if(userCollection == null || userCollection.size() != userIds.length)
            ExceptionCast.cast(DepartmentCode.DEPART_APPROVE_USER_ERROR);
        List<DepartApproveUser> permissionList = new ArrayList<>();
        userCollection.forEach(item -> {
            DepartApproveUser approveUser = departApproveUserService.getByDepartIdAndUserId(en.getDepartId(), item.getId());
            if(approveUser != null) {
                permissionList.add(approveUser);
            }
        });
        if(!permissionList.isEmpty()) {
            return departApproveUserService.removeByCollection(permissionList);
        }
        return false;
    }
 
    /**
     * 查询所有部门信息,并分节点进行显示
     * @return
     */
    @Override
    public List<DepartmentTreeModel> queryTreeList(){
        LambdaQueryWrapper<Department> query = new LambdaQueryWrapper<Department>();
        query.eq(Department::getDeleteFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(Department::getPriority);
        List<Department> list = this.list(query);
        List<DepartmentTreeModel> listResult = FindsDepartmentChildrenUtil.wrapTreeDataToTreeList(list);
        return listResult;
    }
 
 
    /**
     * 查询所有部门信息,并分节点进行显示
     * @param ids 多个部门id
     * @return
     */
    @Override
    public List<DepartmentTreeModel> queryTreeList(String ids){
        List<DepartmentTreeModel> listResult=new ArrayList<>();
        LambdaQueryWrapper<Department> query = new LambdaQueryWrapper<Department>();
        query.eq(Department::getDeleteFlag, CommonConstant.DEL_FLAG_0.toString());
        if(oConvertUtils.isNotEmpty(ids)){
            query.in(true,Department::getDepartId,ids.split(","));
        }
        query.orderByAsc(Department::getPriority);
        List<Department> list= this.list(query);
        for (Department department : list) {
            listResult.add(new DepartmentTreeModel(department));
        }
        return  listResult;
    }
 
    /**
     * <p>
     * 根据关键字搜索相关的部门数据
     * </p>
     */
    @Override
    public List<DepartmentTreeModel> searchByKeyWord(String keyWord,String myDeptSearch,String departIds) {
        LambdaQueryWrapper<Department> query = new LambdaQueryWrapper<Department>();
        List<DepartmentTreeModel> newList = new ArrayList<>();
        query.like(Department::getDepartName, keyWord);
        //update-begin--Author:huangzhilin  Date:20140417 for:[bugfree号]组织机构搜索回显优化--------------------
        DepartmentTreeModel model = new DepartmentTreeModel();
        List<Department> departList = this.list(query);
        if(departList.size() > 0) {
            for(Department depart : departList) {
                model = new DepartmentTreeModel(depart);
                model.setChildren(null);
                //update-end--Author:huangzhilin  Date:20140417 for:[bugfree号]组织机构搜索功回显优化----------------------
                newList.add(model);
            }
            return newList;
        }
        return null;
    }
}