doum
2025-09-28 9ab109b9ee96e7ff2bf2b935a044aee5842a3ddc
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
package com.doumee.biz.system.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doumee.biz.system.SystemDepartmentBiz;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.dao.system.SystemUserJoinMapper;
import com.doumee.dao.system.model.SystemDepartment;
import com.doumee.dao.system.model.SystemDepartmentUser;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.dao.system.vo.SystemDepartmentListVO;
import com.doumee.service.aware.DepartmentDataPermissionAware;
import com.doumee.service.system.SystemDepartmentService;
import com.doumee.service.system.SystemDepartmentUserService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
 
@Service
public class SystemDepartmentBizImpl implements SystemDepartmentBiz {
 
    @Autowired
    private SystemDepartmentService systemDepartmentService;
 
    @Autowired
    private SystemDepartmentUserService systemDepartmentUserService;
 
    @Autowired
    private DepartmentDataPermissionAware departmentDataPermissionAware;
 
    @Autowired
    private SystemUserJoinMapper systemUserJoinMapper;
 
    @Override
    public Integer create(SystemDepartment department) {
        // 验证部门编码
        if (Objects.nonNull(department.getParentId())){
            SystemDepartment systemDepartment = systemDepartmentService.findById(department.getParentId());
            if (systemDepartment == null) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "上级部门不存在");
            }
        }
        SystemDepartment query = new SystemDepartment();
        query.setName(department.getName());
        query.setDeleted(false);
        SystemDepartment one = systemDepartmentService.findOne(query);
        if (one != null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "部门名称已经存在");
        }
 
        SystemDepartment insert = new SystemDepartment();
        insert.setParentId(department.getParentId());
        insert.setCode(department.getCode());
        insert.setName(department.getName());
        insert.setPhone(department.getPhone());
        insert.setEmail(department.getEmail());
        insert.setCreateUser(department.getCreateUser());
        insert.setUpdateUser(department.getCreateUser());
        return systemDepartmentService.create(insert);
    }
 
    @Override
    public void updateById(SystemDepartment department) {
 
        if (Objects.isNull(department.getParentId())){
            SystemDepartment update = new SystemDepartment();
            update.setId(department.getId());
            update.setName(department.getName());
            update.setUpdateUser(department.getUpdateUser());
            update.setUpdateTime(new Date());
            systemDepartmentService.updateById(update);
            return;
        }
 
        SystemDepartment systemDepartment = Optional.ofNullable(department)
                                                    .map(s -> systemDepartmentService.findById(s.getParentId()))
                                                    .orElseThrow(()->new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"上级部门不存在"));
        SystemDepartment update = new SystemDepartment();
        update.setId(department.getId());
        update.setParentId(department.getParentId());
        update.setName(department.getName());
        update.setUpdateUser(department.getUpdateUser());
        update.setUpdateTime(new Date());
        systemDepartmentService.updateById(update);
    }
 
    @Override
    public List<SystemDepartmentListVO> findTree() {
        return departmentDataPermissionAware.execute();
    }
 
    @Override
    public SystemDepartmentListVO findTreeUser(Integer parentId) {
 
        SystemDepartment parent = null;
        if (Objects.nonNull(parentId)){
            parent = systemDepartmentService.findById(parentId);
        }else {
            QueryWrapper<SystemDepartment> wrapper = new QueryWrapper<>();
            wrapper.lambda().isNull(SystemDepartment::getParentId).last("limit 1");
            parent = systemDepartmentService.findOne(wrapper);
        }
        if (Objects.isNull(parent)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"部门不存在");
        }
        QueryWrapper<SystemDepartment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SystemDepartment::getParentId,parent.getId());
        List<SystemDepartment> list = systemDepartmentService.findList(wrapper);
 
        MPJLambdaWrapper<SystemUser> userWrapper = new MPJLambdaWrapper<>();
        userWrapper.selectAll(SystemUser.class)
                .rightJoin(SystemDepartmentUser.class,SystemDepartmentUser::getUserId,SystemUser::getId)
                .eq(SystemDepartmentUser::getDepartmentId,parentId);
        List<SystemUser> systemUsers = systemUserJoinMapper.selectJoinList(SystemUser.class, userWrapper);
 
        SystemDepartmentListVO systemDepartmentListVO = new SystemDepartmentListVO();
        BeanUtils.copyProperties(parent,systemDepartmentListVO);
        systemDepartmentListVO.setUserList(systemUsers);
        if (!CollectionUtils.isEmpty(list)){
            List<SystemDepartmentListVO> collect = list.stream().map(s -> {
                SystemDepartmentListVO children = new SystemDepartmentListVO();
                BeanUtils.copyProperties(s, children);
                return children;
            }).collect(Collectors.toList());
            systemDepartmentListVO.setChildren(collect);
        }
 
 
        return systemDepartmentListVO;
    }
 
    @Override
    public void deleteById(Integer id) {
        List<Integer> ids = systemDepartmentService.findChildren(id);
        if (!CollectionUtils.isEmpty(ids)){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "存在子节点。请删除子节点");
        }
        SystemDepartmentUser systemDepartmentUser = new SystemDepartmentUser();
        systemDepartmentUser.setDepartmentId(id);
        systemDepartmentUser.setDeleted(false);
        List<SystemDepartmentUser> list = systemDepartmentUserService.findList(systemDepartmentUser);
 
        if (!CollectionUtils.isEmpty(list)){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "部门存在人员,请处理人员");
        }
 
        ids.add(id);
        systemDepartmentService.deleteByIdInBatch(ids);
    }
 
    @Override
    @Transactional
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }
}