郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.account.service.baseService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfo;
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO;
import com.gkhy.safePlatform.account.enums.DepartmentStatusEnum;
import com.gkhy.safePlatform.account.repository.DepartmentInfoRepository;
import com.gkhy.safePlatform.account.service.baseService.DepartmentInfoService;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service("departmentInfoService")
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoRepository, DepartmentInfo> implements DepartmentInfoService {
 
    @Autowired
    private DepartmentInfoRepository departmentInfoRepository;
 
    @Override
    public List<DepartmentInfoDO> getDepartmentByStatus(DepartmentStatusEnum status) {
        return departmentInfoRepository.getDepartmentListByStatus(status.getCode());
    }
 
    @Override
    public boolean departmentExist(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        long count = this.count(new LambdaQueryWrapper<DepartmentInfo>().eq(DepartmentInfo::getId, id));
        return count > 0;
    }
 
 
    @Override
    public void saveDepartmentInfo(DepartmentInfo departmentInfo) {
        int i = departmentInfoRepository.insertDepartmentInfo(departmentInfo);
        if (i != 1) {
            throw new BusinessException(ResultCodes.SERVER_ADD_ERROR);
        }
    }
 
    @Override
    public DepartmentInfo getDepartmentInfoById(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return departmentInfoRepository.getDepartmentInfoById(id);
    }
 
    @Override
    public DepartmentInfoDO getDepartmentInfoDOById(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return departmentInfoRepository.getDepartmentInfoDOById(id);
    }
 
    @Override
    public List<DepartmentInfoDO> getDepartmentByParentId(Long parentId) {
        // id = null 有意义 查询根目录所有菜单
        return departmentInfoRepository.listDepartmentByParentId(parentId);
    }
 
    @Override
    public void updateDepartmentInfo(DepartmentInfo departmentInfo) {
        if (departmentInfo == null || departmentInfo.getId() == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        int i = departmentInfoRepository.updateDepartmentInfo(departmentInfo);
        if (i == 0) {
            throw new BusinessException(ResultCodes.SERVER_UPDATE_DATA_NO_CHANGE);
        }
        if (i > 1) {
            throw new BusinessException(ResultCodes.SERVER_UPDATE_ERROR);
        }
    }
 
    @Override
    public long countByParentDepId(Long depId) {
        if (depId == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return departmentInfoRepository.selectCount(new LambdaQueryWrapper<DepartmentInfo>()
                .eq(DepartmentInfo::getParentId,depId)
                .eq(DepartmentInfo::getStatus,DepartmentStatusEnum.ENABLED.getCode()));
    }
 
    @Override
    public DepartmentInfoDO getParentDepartmentInfoDOByDepId(Long depId) {
        if (depId == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return departmentInfoRepository.getParentDepartmentInfoDOByDepId(depId);
    }
 
    @Override
    public DepartmentInfo getDepartmentInfoByName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<DepartmentInfo> departmentInfos = departmentInfoRepository.selectList(new LambdaQueryWrapper<DepartmentInfo>()
                .eq(DepartmentInfo::getName, name)
                .eq(DepartmentInfo::getStatus, DepartmentStatusEnum.ENABLED.getCode()));
        if (departmentInfos.size() == 0) {
            return null;
        }
        if (departmentInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        return departmentInfos.get(0);
    }
 
 
    @Override
    public List<Long> listSubDepIdsByParentId(Long depId) {
        if (depId == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return departmentInfoRepository.listSubDepIdsByParentId(depId);
    }
 
 
    @Override
    public DepartmentInfo getDepartmentInfoByCode(String depCode) {
        if (StringUtils.isBlank(depCode)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<DepartmentInfo> departmentInfos = departmentInfoRepository.selectList(new LambdaQueryWrapper<DepartmentInfo>()
                .eq(DepartmentInfo::getCode, depCode)
                .eq(DepartmentInfo::getStatus, DepartmentStatusEnum.ENABLED.getCode()));
        if (departmentInfos.size() == 0) {
            return null;
        }
        if (departmentInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        return departmentInfos.get(0);
    }
 
 
 
 
}