zss
2024-08-23 8a9505bd7845d50e83fae7adf4846931979c1419
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
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.DepartmentLimsMapper;
import com.yuanchu.mom.pojo.Department;
import com.yuanchu.mom.pojo.DepartmentLims;
import com.yuanchu.mom.service.DepartmentLimsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
* @author z1292
* @description 针对表【department_lims(部门明细)】的数据库操作Service实现
* @createDate 2024-05-22 14:08:17
*/
@Service
@AllArgsConstructor
public class DepartmentLimsServiceImpl extends ServiceImpl<DepartmentLimsMapper, DepartmentLims>
    implements DepartmentLimsService{
 
    DepartmentLimsMapper departmentMapper;
 
    @Override
    public int addDepartment(DepartmentLims department) {
        departmentMapper.insert(department);
        return department.getId();
    }
 
    //获取部门树
    @Override
    public List<DepartmentDto> selectDepartment() {
        List<DepartmentDto> departments = departmentMapper.selectDepartment();
        //获取父节点
        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<DepartmentLims> department = getDepartment(id);
        return removeBatchByIds(department);
    }
 
    @Override
    public List<DepartmentLims> selectDepartmentLimsEnum() {
        return baseMapper.selectList(Wrappers.<DepartmentLims>lambdaQuery().isNotNull(DepartmentLims::getFatherId).select(DepartmentLims::getId,DepartmentLims::getName));
    }
 
    //判断是否有子类,直到没有为止
    public List<DepartmentLims> getDepartment(Integer id) {
        List<DepartmentLims> list = new ArrayList<>();
        DepartmentLims depart = baseMapper.selectById(id);
        list.add(depart);
        List<DepartmentLims> departments = baseMapper.selectList(Wrappers.<DepartmentLims>lambdaQuery().eq(DepartmentLims::getFatherId, id));
        if (ObjectUtils.isNotEmpty(departments)) {
            list.addAll(departments);
            for (DepartmentLims department : departments) {
                list.addAll(getDepartment(department.getId()));
            }
        }
        return list;
    }
 
}