智慧保安后台管理-外网
Administrator
2022-06-28 e290026f1004fca0d8cf43bf986a3dbd8b7974c9
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.SysCache;
import org.springblade.common.vo.DeptVo;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.modules.information.entity.Information;
import org.springblade.modules.information.service.IInformationService;
import org.springblade.modules.jurisdiction.entity.Jurisdiction;
import org.springblade.modules.jurisdiction.service.JurisdictionService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.mapper.DeptMapper;
import org.springblade.modules.system.node.TreeNode;
import org.springblade.modules.system.node.TreeNodes;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DeptVO;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.DeptWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    private static final String TENANT_ID = "tenantId";
    private static final String PARENT_ID = "parentId";
 
    @Autowired
    private IInformationService iInformationService;
 
    @Autowired
    private JurisdictionService jurisdictionService;
 
    @Override
    public List<DeptVO> lazyList(String tenantId, Long parentId, Map<String, Object> param) {
        // 设置租户ID
        if (AuthUtil.isAdministrator()) {
            tenantId = StringPool.EMPTY;
        }
        String paramTenantId = Func.toStr(param.get(TENANT_ID));
        if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
            tenantId = paramTenantId;
        }
        // 判断点击搜索但是没有查询条件的情况
        if (Func.isEmpty(param.get(PARENT_ID)) && param.size() == 1) {
            parentId = 0L;
        }
        // 判断数据权限控制,非超管角色只可看到本级及以下数据
        if (Func.toLong(parentId) == 0L && !AuthUtil.isAdministrator()) {
            Long deptId = Func.firstLong(AuthUtil.getDeptId());
            Dept dept = SysCache.getDept(deptId);
            if (dept.getParentId() != 0) {
                parentId = dept.getParentId();
            }
        }
        // 判断点击搜索带有查询条件的情况
        if (Func.isEmpty(param.get(PARENT_ID)) && param.size() > 1 && Func.toLong(parentId) == 0L) {
            parentId = null;
        }
        return baseMapper.lazyList(tenantId, parentId, param);
    }
 
    @Override
    public List<DeptVO> tree(String tenantId) {
        return ForestNodeMerger.merge(baseMapper.tree(tenantId));
    }
 
    @Override
    public List<DeptVO> lazyTree(String tenantId, Long parentId,String userId) {
//        if (AuthUtil.isAdministrator()) {
//            tenantId = StringPool.EMPTY;
//        }
        //获取用户信息,保安,民警
        if (null!=userId && !userId.equals("")) {
            UserVO user = baseMapper.getUserInfoById(userId);
            if (user.getRoleAlias().equals("公安管理员") || user.getRoleAlias().equals("民警")){
                //公安管理员又分,如果是派出所,只返回当前值
                if (user.getDeptName().contains("派出所")){
                    return ForestNodeMerger.merge(baseMapper.lazyTreeCityOne(tenantId, parentId));
                }else {
                    //市局,区级
                    Dept dept = this.getById(parentId);
                    if (null != dept) {
                        if (dept.getParentId().equals(1123598813738675201L)) {
                            return ForestNodeMerger.merge(baseMapper.lazyTreeCity(tenantId, parentId));
                        }
                        if (dept.getParentId().equals(0L)){
                            //南昌市公安局
                            return ForestNodeMerger.merge(baseMapper.lazyTreeByJurisdiction());
                        }
                    }
                }
            }else {
                //保安公司
                return ForestNodeMerger.merge(baseMapper.securityLazyTree(null, parentId));
            }
        }
        //admin
        return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId,null));
    }
    @Override
    public List<DeptVO> lazyTrees() {
        return  ForestNodeMerger.merge(baseMapper.lazyTrees());
    }
 
    @Override
    public String getDeptIds(String tenantId, String deptNames) {
        List<Dept> deptList = baseMapper.selectList(Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId).in(Dept::getDeptName, Func.toStrList(deptNames)));
        if (deptList != null && deptList.size() > 0) {
            return deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
        }
        return null;
    }
 
    @Override
    public String getDeptIdsByFuzzy(String tenantId, String deptNames) {
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId);
        queryWrapper.and(wrapper -> {
            List<String> names = Func.toStrList(deptNames);
            names.forEach(name -> wrapper.like(Dept::getDeptName, name).or());
        });
        List<Dept> deptList = baseMapper.selectList(queryWrapper);
        if (deptList != null && deptList.size() > 0) {
            return deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
        }
        return null;
    }
 
    @Override
    public List<String> getDeptNames(String deptIds) {
        return baseMapper.getDeptNames(Func.toLongArray(deptIds));
    }
 
    @Override
    public List<Dept> getDeptChild(Long deptId) {
        return baseMapper.selectList(Wrappers.<Dept>query().lambda().like(Dept::getAncestors, deptId));
    }
 
    @Override
    public boolean removeDept(String ids) {
        Integer cnt = baseMapper.selectCount(Wrappers.<Dept>query().lambda().in(Dept::getParentId, Func.toLongList(ids)));
        if (cnt > 0) {
            throw new ServiceException("请先删除子节点!");
        }
        return removeByIds(Func.toLongList(ids));
    }
 
    @Override
    public boolean submit(Dept dept) {
        if (Func.isEmpty(dept.getParentId())) {
            dept.setTenantId(AuthUtil.getTenantId());
            dept.setParentId(BladeConstant.TOP_PARENT_ID);
            dept.setAncestors(String.valueOf(BladeConstant.TOP_PARENT_ID));
        }
        if (dept.getParentId() > 0) {
            Dept parent = getById(dept.getParentId());
            if (Func.toLong(dept.getParentId()) == Func.toLong(dept.getId())) {
                throw new ServiceException("父节点不可选择自身!");
            }
            dept.setTenantId(parent.getTenantId());
            String ancestors = parent.getAncestors() + StringPool.COMMA + dept.getParentId();
            dept.setAncestors(ancestors);
        }
        dept.setIsDeleted(BladeConstant.DB_NOT_DELETED);
        return saveOrUpdate(dept);
    }
 
    @Override
    public List<DeptVO> search(String deptName, Long parentId) {
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>query().lambda();
        if (Func.isNotEmpty(deptName)) {
            queryWrapper.like(Dept::getDeptName, deptName);
        }
        if (Func.isNotEmpty(parentId) && parentId > 0L) {
            queryWrapper.eq(Dept::getParentId, parentId);
        }
        List<Dept> deptList = baseMapper.selectList(queryWrapper);
        return DeptWrapper.build().listNodeVO(deptList);
    }
 
    @Override
    public List<Map<Object,String>> selectCount(String name) {
        return baseMapper.selectCount(name);
    }
 
    @Override
    public List<Map<Object, String>> selectId(String id) {
        return baseMapper.selectId(id);
    }
 
    @Override
    public Map<Object, Integer> selectHold(String deptid) {
        return baseMapper.selectHold(deptid);
    }
 
    /**
     * 懒加载获取部门树形结构,不包含顶级管理员公安局
     */
    @Override
    public List<DeptVO> securityLazyTree(String jurisdiction, Long parentId,Long deptId) {
        //如果传了deptId
        if (null!=deptId){
            //递归查询
//            recursionDeptInfo(deptVOS, list);
            //tree 组装并返回
            return ForestNodeMerger.merge(baseMapper.securityDeptUnitList(null,deptId));
        }
        return ForestNodeMerger.merge(baseMapper.securityLazyTree(jurisdiction, parentId));
    }
 
    /**
     * 递归查询子级菜单
     * @param deptVOS
     * @param list
     * @return
     */
    private List<DeptVO> recursionDeptInfo(List<DeptVO> deptVOS,List<DeptVO> list) {
        //如果有子级
        if (list.size()>0){
            deptVOS.addAll(list);
            //遍历子级
            for (int i = 0; i < list.size(); i++) {
                //查看是否有子级
                List<DeptVO> childList = baseMapper.securityDeptUnitList(list.get(i).getId(), null);
                //如果有
                if (childList.size()>0){
                    //递归
                    recursionDeptInfo(deptVOS,childList);
                }
            }
        }
        return deptVOS;
    }
 
 
    /**
     * 懒加载获取部门树形结构(包含用户数据)
     * @return
     */
    @Override
    public List<DeptVO> lazyTreeUser(String tenantId, Long parentId) {
        if (AuthUtil.isAdministrator()) {
            tenantId = StringPool.EMPTY;
        }
        return ForestNodeMerger.merge(baseMapper.lazyTreeUser(tenantId, parentId));
    }
 
    @Override
    public Long add(Dept dept) {
        return baseMapper.add(dept);
    }
 
    /**
     * 懒加载获取部门树形结构(按分类查询)
     */
    @Override
    public List<DeptVO> lazyTreeType(String deptCategory, Long parentId) {
        return ForestNodeMerger.merge(baseMapper.lazyTreeType("000000",deptCategory,parentId));
    }
 
    /**
     * 懒加载获取部门树形结构(包含用户数据)app
     * @param type 1:保安对保安  2:保安向民警   3:民警向民警
     * @param jurisdiction 辖区id
     * @return
     */
    @Override
    public List<DeptVO> lazyTreeUserApp(Integer type,String jurisdiction) {
        return ForestNodeMerger.merge(baseMapper.lazyTreeUserApp(type,jurisdiction));
    }
 
    /**
     * 懒加载获取部门树形结构(包含用户数据),只查下一级的数据
     * @return
     */
    @Override
    public List<TreeNodes> lazyTreeUsers(Integer type,Long deptId) {
        Map<Long,TreeNodes> map = new HashMap<>();
        //保安向保安汇报
        if(type==1){
            map = baseMapper.lazyTreeUsers(type,deptId,null);
        }
        //保安向民警汇报
        if(type==2){
            //查询当前保安所在的辖区
            Information information = new Information();
            information.setDepartmentid(deptId.toString());
 
            Information one = iInformationService.getOne(Condition.getQueryWrapper(information));
            if (null!=one) {
                //找辖区对应的组织机构id
                Jurisdiction jurisdiction = jurisdictionService.getById(one.getJurisdiction());
                if (null != jurisdiction) {
                    Dept dept = new Dept();
                    dept.setDeptName(jurisdiction.getDeptName());
                    Dept dept1 = this.getOne(Condition.getQueryWrapper(dept));
                    map = baseMapper.lazyTreeUsersPublicSecurity(null, dept1.getId(), one.getJurisdiction());
                }
            }
        }
        //民警向民警汇报,需要南昌市及当前民警所在辖区的部门数据
        if(type==3){
            //当前取所有的民警数据
            map = baseMapper.lazyTreeUsersPublicSecuritys(null,deptId,null);
        }
        List<TreeNodes> tree = new ArrayList<>();
        Map<Long, TreeNodes> finalMap = map;
        map.forEach((id, treeNodes) ->{
            if (finalMap.containsKey(treeNodes.getParentId())){
                finalMap.get(treeNodes.getParentId()).getChildren().add(treeNodes);
            }else {
                tree.add(treeNodes);
            }
        });
        return tree;
    }
 
    @Override
    public List<DeptVO> selectInfo() {
        return ForestNodeMerger.merge(baseMapper.selectInfo());
    }
 
    @Override
    public List<DeptVO> selectInfos() {
        return baseMapper.selectInfos();
    }
 
    @Override
    public List<DeptVo> selDeptList() {
        return baseMapper.selDeptList();
    }
 
 
    /**
     * 懒加载获取部门树形结构,根据辖区查询
     */
    @Override
    public List<DeptVO> lazyTreeJurisdiction(String jurisdiction, Long parentId) {
        return ForestNodeMerger.merge(baseMapper.lazyTreeJurisdiction(jurisdiction,parentId));
    }
 
    /**
     * 懒加载获取单位树,电子围栏用
     * @param type 1:本单位  2:分公司  3:服务单位
     * @param deptId
     * @return
     */
    @Override
    public List<DeptVO> lazyTreeRail(Integer type, Long deptId) {
        //查询本单位
        if(type==1 || type==2){
            return ForestNodeMerger.merge(baseMapper.securityDeptUnitList(null,deptId));
        }
        return ForestNodeMerger.merge(baseMapper.getDispatchUnitList(deptId));
    }
}