zss
2024-12-27 ebede85283906f52dd45d0755d22140538038ac3
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.dto.DepartmentDto;
import com.yuanchu.mom.mapper.DepartmentMapper;
import com.yuanchu.mom.pojo.Department;
import com.yuanchu.mom.service.DepartmentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 部门明细 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2024-04-15 04:01:48
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
 
    DepartmentMapper departmentMapper;
 
 
 
 
    //添加部门
    @Override
    public int addDepartment(Department department) {
        departmentMapper.insert(department);
        return department.getId();
    }
 
    //获取部门树
    @Override
    public List<DepartmentDto> selectDepartment() {
        List<DepartmentDto> departments = departmentMapper.selectDepartment();
        departments.addAll(departmentMapper.selectCustomList());
        //获取父节点
        List<DepartmentDto> collect = departments.stream().filter(m -> m.getFatherId() == null).peek(
                (m) -> m.setChildren(getChildren(m, departments))
        ).collect(Collectors.toList());
        return collect;
    }
 
    /**
     * 递归查询子节点
     * @param root  根节点
     * @param all   所有节点
     * @return 根节点信息
     */
    private List<DepartmentDto> getChildren(DepartmentDto root, List<DepartmentDto> all) {
        return all.stream().filter(m -> Objects.equals(m.getFatherId(), root.getId())).peek(
                (m) -> m.setChildren(getChildren(m, all))
        ).collect(Collectors.toList());
    }
 
    //删除部门
    @Override
    public boolean delDepartment(Integer id) {
        //判断是否有子类,直到没有为止
        List<Department> department = getDepartment(id);
        return removeBatchByIds(department);
    }
 
    //判断是否有子类,直到没有为止
    public List<Department> getDepartment(Integer id) {
        List<Department> list = new ArrayList<>();
        Department depart = baseMapper.selectById(id);
        list.add(depart);
        List<Department> departments = baseMapper.selectList(Wrappers.<Department>lambdaQuery().eq(Department::getFatherId, id));
        if (ObjectUtils.isNotEmpty(departments)) {
            list.addAll(departments);
            for (Department department : departments) {
                list.addAll(getDepartment(department.getId()));
            }
        }
        return list;
    }
 
    @Override
    public List<Department> selectDepartmentEnum() {
        return departmentMapper.selectList(Wrappers.<Department>lambdaQuery().isNotNull(Department::getFatherId).select(Department::getId,Department::getName));
    }
}