ludc
2024-07-10 4d571ecaabae01dc825f01ce92ff4a5023f56fb0
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
/*
 *      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 com.vci.ubcs.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.system.vo.RoleVO;
import org.springblade.core.log.exception.ServiceException;
import com.vci.ubcs.system.cache.SysCache;
import com.vci.ubcs.system.entity.Dept;
import com.vci.ubcs.system.mapper.DeptMapper;
import com.vci.ubcs.system.service.IDeptService;
import com.vci.ubcs.system.vo.DeptVO;
import com.vci.ubcs.system.wrapper.DeptWrapper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
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";
    /**
     * nacos配置的管理组租户id
     */
    @Value("${user-info.tenant-id}")
    private String tenantId;
 
    @Override
    public IPage<DeptVO> lazyList(String tenantId, Long parentId, Map<String, Object> param, Query query) {
        // 设置租户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, Condition.getPage(query));
    }
 
 
 
    @Override
    public List<DeptVO> deptList(String tenantId) {
        // 这里判断我们自己配置的管理组租户,可以查询所有
        List<DeptVO> tree = baseMapper.deptList(tenantId.equals(this.tenantId) ? "":tenantId);
        return tree;
    }
 
    @Override
    public List<DeptVO> tree(String tenantId) {
        // 这里判断我们自己配置的管理组租户,可以查询所有
        List<DeptVO> tree = baseMapper.tree(tenantId.equals(this.tenantId) ? "":tenantId);
        return ForestNodeMerger.merge(tree);
    }
 
    @Override
    public List<DeptVO> lazyTree(String tenantId, Long parentId) {
        if (AuthUtil.isAdministrator()) {
            tenantId = StringPool.EMPTY;
        }
        return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId));
    }
 
    @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) {
        Long cnt = baseMapper.selectCount(Wrappers.<Dept>query().lambda().in(Dept::getParentId, Func.toLongList(ids)));
        if (cnt > 0L) {
            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) {
        String tenantId = AuthUtil.getTenantId();
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>query().lambda();
        if (Func.isNotEmpty(tenantId)) {
            queryWrapper.eq(Dept::getTenantId, tenantId);
        }
        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);
    }
 
}