危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.constant.UserConstant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.common.utils.StringUtils;
import com.gkhy.hazmat.system.domain.SysDept;
import com.gkhy.hazmat.system.mapper.SysDeptMapper;
import com.gkhy.hazmat.system.service.SysDeptService;
import org.springframework.stereotype.Service;
 
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-05 14:41:40
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    @Override
    public List<SysDept> selectDeptList(SysDept dept) {
        return baseMapper.selectDeptList(dept);
    }
 
    @Override
    public List<SysDept> selectDeptTreeList(SysDept dept) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            dept.setCompanyId(currentUser.getCompanyId());
        }
        List<SysDept>  deptList=selectDeptList(dept);
        List<SysDept> deptTrees= getChildDept(deptList,0L);
        return deptTrees;
    }
 
    public List<SysDept> getChildDept(List<SysDept> depts, Long parentId){
        List<SysDept> childList=depts.stream().filter(m -> Objects.equals(m.getParentId(),parentId)).map(m -> {
                    m.setChildren(getChildDept(depts, m.getId()));
                    return m;
                }).sorted(Comparator.comparingInt(m -> (m.getSort()==null?0:m.getSort())))
                .collect(Collectors.toList());
        return childList;
    }
 
 
    @Override
    public SysDept selectDeptById(Long deptId) {
        if(deptId==0L){
            return null;
        }
        SysDept dept = baseMapper.selectById(deptId);
        if(dept==null){
            return dept;
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            return dept;
        } else if (!dept.getCompanyId().equals(currentUser.getCompanyId())) {
            throw new ApiException("无权限查看其它企业数据");
        }
        return dept;
    }
 
    @Override
    public int selectNormalChildrenDeptById(Long deptId) {
        return baseMapper.selectNormalChildrenDeptById(deptId);
    }
 
    @Override
    public boolean hasChildByDeptId(Long deptId) {
        int result = baseMapper.hasChildByDeptId(deptId);
        return result > 0;
    }
 
    @Override
    public boolean checkDeptExistUser(Long deptId) {
        int result = baseMapper.checkDeptExistUser(deptId);
        return result > 0;
    }
 
 
    @Override
    public boolean checkDeptNameUnique(SysDept dept) {
        Long deptId =dept.getId()==null ? -1L : dept.getId();
        SysDept info = baseMapper.checkNameUnique(dept.getName(), dept.getParentId()==null?0:dept.getParentId(),dept.getCompanyId());
        if (!Objects.isNull(info) && info.getId().longValue() != deptId.longValue())
        {
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
 
    @Override
    public int insertDept(SysDept dept) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        dept.setCompanyId(currentUser.getCompanyId());
        if(!checkDeptNameUnique(dept)){
            throw new ApiException("部门已存在");
        }
        checkUserAllowed(null,currentUser);
        dept.setCreateBy(currentUser.getUsername());
        if(dept.getParentId()!=null&&dept.getParentId()!=0L) {
            SysDept info = selectDeptById(dept.getParentId());
            // 如果父节点不为正常状态,则不允许新增子节点
            if (!UserConstant.DEPT_NORMAL.equals(info.getStatus())) {
                throw new ApiException("部门停用,不允许新增");
            }
            dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        }
 
        return baseMapper.insert(dept);
    }
 
    @Override
    public int updateDept(SysDept dept) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(dept,currentUser);
        if(!checkDeptNameUnique(dept)){
            throw new ApiException("部门名称已存在");
        }
        if(dept.getParentId().equals(dept.getId())){
            throw new ApiException("上级部门不能是自己");
        }
        if(UserConstant.DEPT_DISABLE.equals(dept.getStatus()) &&selectNormalChildrenDeptById(dept.getId())>0){
            throw new ApiException("该部门包含未停用的子部门");
        }
        SysDept newParentDept = selectDeptById(dept.getParentId());
        SysDept oldDept = selectDeptById(dept.getId());
        if (!Objects.isNull(newParentDept) &&!Objects.isNull(oldDept))
        {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getId();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getId(), newAncestors, oldAncestors);
        }
        dept.setUpdateBy(SecurityUtils.getUsername());
        int result = baseMapper.updateById(dept);
        if (UserConstant.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty((dept.getAncestors()))
                && !StringUtils.equals("0", dept.getAncestors()))
        {
            // 如果该部门是启用状态,则启用该部门的所有上级部门
            updateParentDeptStatusNormal(dept);
        }
        return result;
    }
 
    public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
    {
        List<SysDept> children = baseMapper.selectChildrenDeptById(deptId);
        for (SysDept child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseMapper.updateDeptChildren(children);
        }
    }
 
    private void updateParentDeptStatusNormal(SysDept dept)
    {
        String ancestors = dept.getAncestors();
        Long[] deptIds = Convert.toLongArray(ancestors);
        baseMapper.updateDeptStatusNormal(deptIds);
    }
 
    @Override
    public int deleteDeptById(Long deptId) {
        if(hasChildByDeptId(deptId)){
            throw new ApiException("存在下级部门,不能删除");
        }
        if(checkDeptExistUser(deptId)){
            throw new ApiException("部门存在用户,不能删除");
        }
        return baseMapper.deleteById(deptId);
    }
 
    public void checkUserAllowed(SysDept dept, SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(dept!=null){
            if(!Objects.equals(user.getCompanyId(), dept.getCompanyId())){
                throw new ApiException("无权限操作其他企业数据");
            }
        }
    }
 
}