tuqin
2 天以前 ffec90c897e9bd78e88b9fa590a5dd75d26b82c8
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
package cn.iocoder.yudao.module.system.api.dept;
 
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.stereotype.Service;
 
import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
 
/**
 * 部门 API 实现类
 *
 * @author 超级管理员
 */
@Service
public class DeptApiImpl implements DeptApi {
 
    @Resource
    private DeptService deptService;
 
    @Override
    public DeptRespDTO getDept(Long id) {
        DeptDO dept = deptService.getDept(id);
        return BeanUtils.toBean(dept, DeptRespDTO.class);
    }
 
    @Override
    public List<DeptRespDTO> getDeptList(Collection<Long> ids) {
        List<DeptDO> depts = deptService.getDeptList(ids);
        return BeanUtils.toBean(depts, DeptRespDTO.class);
    }
 
    @Override
    public List<DeptRespDTO> getDeptList() {
        List<DeptDO> depts = deptService.getDeptList(new DeptListReqVO());
        return BeanUtils.toBean(depts, DeptRespDTO.class);
    }
 
    @Override
    public void validateDeptList(Collection<Long> ids) {
        deptService.validateDeptList(ids);
    }
 
    @Override
    public List<DeptRespDTO> getChildDeptList(Long id) {
        List<DeptDO> childDeptList = deptService.getChildDeptList(id);
        return BeanUtils.toBean(childDeptList, DeptRespDTO.class);
    }
 
    @Override
    public Long getOrCreateDeptByName(String name) {
        String deptName = StrUtil.trim(name);
        if (StrUtil.isEmpty(deptName)) {
            return null;
        }
        List<DeptRespDTO> depts = getDeptList();
        // 1. 已存在:直接复用
        for (DeptRespDTO dept : depts) {
            if (deptName.equals(StrUtil.trim(dept.getName()))) {
                return dept.getId();
            }
        }
        // 2. 不存在:补建到根部门下
        Long parentId = depts.stream()
                .filter(dept -> dept.getParentId() == null || dept.getParentId() == 0L)
                .map(DeptRespDTO::getId)
                .findFirst()
                .orElse(0L);
        DeptSaveReqVO createReqVO = new DeptSaveReqVO();
        createReqVO.setName(deptName);
        createReqVO.setParentId(parentId);
        createReqVO.setSort(0);
        createReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        return deptService.createDept(createReqVO);
    }
 
}