jiangping
2023-10-08 150a19b58d53ecc56fda26cb6568bc2d8bfc6be6
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
package com.doumee.service.system.impl;
 
import com.doumee.dao.system.SystemDepartmentMapper;
import com.doumee.dao.system.dto.QuerySystemDepartmentDTO;
import com.doumee.dao.system.model.SystemDepartment;
import com.doumee.dao.system.vo.SystemDepartmentListVO;
import com.doumee.service.system.SystemDepartmentService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import com.doumee.core.constants.Constants;
/**
 * Service实现
 * @author Eva.Caesar Liu
 * @date 2022/03/15 09:54
 */
@Service
public class SystemDepartmentServiceImpl implements SystemDepartmentService {
 
    @Autowired
    private SystemDepartmentMapper systemDepartmentMapper;
 
    @Override
    public Integer create(SystemDepartment systemDepartment) {
        systemDepartmentMapper.insert(systemDepartment);
        return systemDepartment.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        SystemDepartment systemDepartment = new SystemDepartment();
        systemDepartment.setId(id);
        systemDepartment.setDeleted(Constants.ZERO);
        this.updateById(systemDepartment);
    }
 
    @Override
    @Transactional
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }
 
    @Override
    public void updateById(SystemDepartment systemDepartment) {
        systemDepartmentMapper.updateById(systemDepartment);
    }
 
    @Override
    @Transactional
    public void updateByIdInBatch(List<SystemDepartment> systemDepartments) {
        if (CollectionUtils.isEmpty(systemDepartments)) return;
        for (SystemDepartment systemDepartment: systemDepartments) {
            this.updateById(systemDepartment);
        }
    }
 
    @Override
    public SystemDepartment findById(Integer id) {
        return systemDepartmentMapper.selectById(id);
    }
 
    @Override
    public List<SystemDepartment> findByIds(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        QueryWrapper<SystemDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SystemDepartment::getId, ids).eq(SystemDepartment::getDeleted, Constants.ZERO);
        return systemDepartmentMapper.selectList(queryWrapper);
    }
 
    @Override
    public SystemDepartment findOne(SystemDepartment systemDepartment) {
        Wrapper<SystemDepartment> wrapper = new QueryWrapper<>(systemDepartment);
        return systemDepartmentMapper.selectOne(wrapper );
    }
 
    @Override
    public List<SystemDepartmentListVO> findList(QuerySystemDepartmentDTO dto) {
        return systemDepartmentMapper.selectManageList(dto);
    }
 
    @Override
    public long count(SystemDepartment systemDepartment) {
        Wrapper<SystemDepartment> wrapper = new QueryWrapper<>(systemDepartment);
        return systemDepartmentMapper.selectCount(wrapper);
    }
 
    @Override
    public List<Integer> findChildren(Integer departmentId) {
        List<Integer> pool = new ArrayList<>();
        this.fillChildren(pool, Arrays.asList(departmentId));
        return pool;
    }
 
    /**
     * 获取子部门ID
     * @author Eva.Caesar Liu
     * @date 2022/03/15 09:54
     */
    private void fillChildren(List<Integer> pool, List<Integer> parentIds) {
        QueryWrapper<SystemDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SystemDepartment::getDeleted,Constants.ZERO)
                .in(SystemDepartment::getParentId, parentIds);
        List<SystemDepartment> departments = systemDepartmentMapper.selectList(queryWrapper);
        List<Integer> ids = departments.stream().map(SystemDepartment::getId).collect(Collectors.toList());
        if (ids.size() > 0) {
            pool.addAll(ids);
            this.fillChildren(pool, ids);
        }
    }
}