zhangf
2024-07-26 698995469a3fcdc3164fc486d18bdbe059b6c92e
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
package com.gkhy.fourierSpecialGasMonitor.domain.account.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.fourierSpecialGasMonitor.domain.account.converter.SysDeparmentConverter;
import com.gkhy.fourierSpecialGasMonitor.domain.account.entity.SysDepartment;
import com.gkhy.fourierSpecialGasMonitor.domain.account.model.bo.SysDepartmentBO;
import com.gkhy.fourierSpecialGasMonitor.domain.account.model.dto.SysDepartmentDomainDTO;
import com.gkhy.fourierSpecialGasMonitor.domain.account.repository.jpa.SysDeparmentRepository;
import com.gkhy.fourierSpecialGasMonitor.domain.account.service.SysDepartmentDomainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @email 1603559716@qq.com
 * @author: zf
 * @date: 2023/3/7
 * @time: 17:18
 */
@Service
public class SysDepartmentDomainServiceImpl implements SysDepartmentDomainService {
 
    @Autowired
    private SysDeparmentRepository repository;
 
    @Autowired
    private SysDeparmentConverter converter;
 
    @Override
    public SysDepartmentDomainDTO save(SysDepartmentBO sysDepartmentBO,Long currentUserId) {
        if (ObjectUtil.isEmpty(sysDepartmentBO.getDepName())) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"请填写部门名称");
        }
        byte depLevel = 0;
        if(ObjectUtil.isEmpty(sysDepartmentBO.getParentId())){
            depLevel = 1;
        }
        if (!ObjectUtil.isEmpty(sysDepartmentBO.getParentId())) {
            //验证父级部门
            SysDepartment parentDep = repository.findByIdAndDeleteStatus(sysDepartmentBO.getParentId());
            if(ObjectUtil.isEmpty(parentDep)){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"父级部门不存在!");
            }
            depLevel = (byte)(parentDep.getLevel()+1);
        }
 
        //复制
        SysDepartment sysDepartment = new SysDepartment();
        BeanUtils.copyProperties(sysDepartmentBO,sysDepartment);
        sysDepartment.setDeleteStatus((byte)0);
        sysDepartment.setLevel(depLevel);
        sysDepartment.setCreateByUserId(currentUserId);
        sysDepartment.setUpdateByUserId(currentUserId);
        SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment));
 
        return sysDepartmentDomainDTO;
    }
 
    @Override
    public SysDepartmentDomainDTO update(SysDepartmentBO sysDepartmentBO,Long currentUserId) {
        if (ObjectUtil.isEmpty(sysDepartmentBO.getId())) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"主键不可为空!");
        }
        if (ObjectUtil.isEmpty(sysDepartmentBO.getDepName())) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"请填写部门名称");
        }
        SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(sysDepartmentBO.getId());
        if(sysDepartment == null){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"该部门不存在,不可修改!");
        }
 
        //复制
        sysDepartment.setDepName(sysDepartmentBO.getDepName());
        sysDepartment.setUpdateByUserId(currentUserId);
        sysDepartment.setInfo(sysDepartmentBO.getInfo());
        SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment));
        return sysDepartmentDomainDTO;
    }
 
    @Override
    public SysDepartmentDomainDTO delete(Long id, Long currentUserId) {
        SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(id);
        if(sysDepartment == null){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"该部门不存在,不可修改!");
        }
        //验证该部门下子部门
        if (!checkDepChildren(id)) {
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"该部门存在子部门,请先删除子部门!");
        }
        sysDepartment.setDeleteStatus((byte)1);
        SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment));
        return sysDepartmentDomainDTO;
    }
    @Override
    public SysDepartmentDomainDTO findById(Long id){
        if(ObjectUtil.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"主键不可为空!");
        }
        SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(id);
        SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment);
        return sysDepartmentDomainDTO;
    }
 
 
    @Override
    public List<SysDepartmentDomainDTO> list() {
        List<SysDepartment> list = repository.findByDeleteStatus();
        List<SysDepartment> parentList = list.stream().filter(item -> item.getParentId() == null).collect(Collectors.toList());
        List<SysDepartment> childList = list.stream().filter(item -> item.getParentId() != null).collect(Collectors.toList());
        List<SysDepartmentDomainDTO> departmentDomainDTOList = new ArrayList<>();
        if(parentList.size() > 0){
            for(SysDepartment sysDepartment : parentList){
                SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment);
                sysDepartmentDomainDTO.setChildren(recursion(sysDepartmentDomainDTO.getId(), childList));
                departmentDomainDTOList.add(sysDepartmentDomainDTO);
            }
        }
        return departmentDomainDTOList;
    }
    private List<SysDepartmentDomainDTO> recursion(Long parentId,List<SysDepartment> list){
        List<SysDepartment> childList = list.stream().filter(item -> item.getParentId().equals(parentId)).collect(Collectors.toList());
        List<SysDepartmentDomainDTO> departmentDomainDTOList = new ArrayList<>();
        if(childList.size() > 0){
            for(SysDepartment sysDepartment : childList){
                SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment);
                sysDepartmentDomainDTO.setChildren(recursion(sysDepartmentDomainDTO.getId(), list));
                departmentDomainDTOList.add(sysDepartmentDomainDTO);
            }
        }
        return departmentDomainDTOList;
    }
 
 
 
    //验证部门
    private boolean checkDep(Long id){
        if(ObjectUtil.isEmpty(id)){
            return false;
        }
        SysDepartment dep = repository.findByIdAndDeleteStatus(id);
        if(ObjectUtil.isEmpty(dep)){
            return false;
        }
        return true;
    }
    //验证部门是否存在子部门
    private boolean checkDepChildren(Long parentId){
        if (ObjectUtil.isEmpty(parentId)) {
            return false;
        }
        List<SysDepartment> list = repository.findByParentIdAndDeleteStatus(parentId);
        if(!CollectionUtils.isEmpty(list)){
            return false;
        }
        return true;
    }
 
 
}