zhanghua
2025-04-14 1cad14bca191807e18705c3a5526eda8151be439
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.ycl.service.depart.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.dto.UmsDepartDto;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.user.UmsAdmin;
import com.ycl.enums.common.ResultCode;
import com.ycl.exception.ApiException;
import com.ycl.mapper.depart.UmsDepartMapper;
import com.ycl.service.depart.IDepartManagerService;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.user.UmsAdminService;
import com.ycl.service.user.UmsDepartManageService;
import com.ycl.vo.depart.DepartVO;
import com.ycl.vo.depart.UmsDepartVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author lyq
 * @since 2022-09-07
 */
@Service
public class UmsDepartServiceImpl extends ServiceImpl<UmsDepartMapper, UmsDepart> implements UmsDepartService {
    @Resource
    private UmsDepartMapper sccgDepartMapper;
    @Resource
    private UmsDepartManageService umsAdminDepartService;
    @Resource
    private UmsAdminService umsAdminService;
    @Resource
    IDepartManagerService iDepartManagerService;
    @Resource
    UmsDepartService umsDepartService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DepartVO.AddDepartVO addDepartVO) {
        UmsDepart sccgDepart = this.queryByName(addDepartVO.getDepartName());
        if (null != sccgDepart) {
            throw new ApiException(ResultCode.RECORD_ALREADY_EXISTS);
        }
        UmsDepart depart = new UmsDepart();
        BeanUtils.copyProperties(addDepartVO, depart);
        depart.setStatus(Short.valueOf("1"));
        umsDepartService.save(depart);
        addDepartVO.getDepartManagerList()
                .forEach(item -> {
                    item.setCreateTime(LocalDateTime.now());
                    item.setDepartId(depart.getId());
                });
        iDepartManagerService.saveBatch(addDepartVO.getDepartManagerList());
    }
 
    @Override
    public UmsDepart loadDepartById(long id) {
        UmsDepart sccgDepart = sccgDepartMapper.selectById(id);
        if (null == sccgDepart) {
            throw new ApiException(ResultCode.RECORD_NOT_EXISTS);
        }
        return sccgDepart;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DepartVO.UpdateDepartVO updateDepartVO) {
        UmsDepart sccgDepart = this.queryByName(updateDepartVO.getDepartName());
        if (null != sccgDepart && !sccgDepart.getId().equals(updateDepartVO.getId())) {
            throw new ApiException(ResultCode.RECORD_ALREADY_EXISTS);
        }
        UmsDepart depart = new UmsDepart();
        BeanUtils.copyProperties(updateDepartVO, depart);
        umsDepartService.updateById(depart);
        if (updateDepartVO.getDepartManagerList().size() > 0) {
            iDepartManagerService.deleteByDepartId(depart.getId());
            updateDepartVO.getDepartManagerList()
                    .forEach(item -> {
                        item.setCreateTime(LocalDateTime.now());
                        item.setDepartId(depart.getId());
                    });
            iDepartManagerService.saveBatch(updateDepartVO.getDepartManagerList());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(long id) {
        if (sccgDepartMapper.deleteById(id) <= 0) {
            throw new ApiException(ResultCode.RECORD_DELETE_FAIL);
        }
        umsAdminDepartService.deletedByDepartId(id);
    }
 
    @Override
    public List<UmsDepart> tree() {
        // 1.查出所有网格
        List<UmsDepart> list = baseMapper.selectDepartList();
 
        // 2.组装成父子的树型结构
        // 2.1、找到所有的一级网格:使用jdk8的stream流进行过滤
        List<UmsDepart> collect = list.stream().filter(item ->
            //分类父id为0就表示该网格为 一级网格
                item.getParentId() == 0
        ).map(item -> {
            // 找到子网格 set进children属性中
            item.setChildren(getChildrens(item, list));
            return item;
        }).collect(Collectors.toList());
        List<UmsDepart> treeData = getTreeData(collect);
        return treeData;
    }
 
    @Override
    public IPage<UmsDepartVO> pageDepart(String departName, IPage<UmsDepartVO> page) {
 
        List<UmsDepartVO> ls = sccgDepartMapper.selectPageByName(departName, page.offset(), page.getSize());
        LambdaQueryWrapper<UmsDepart> queryWrapper = new LambdaQueryWrapper<UmsDepart>()
                .like(StringUtils.isNotEmpty(departName), UmsDepart::getDepartName, departName);
        long total = this.count(queryWrapper);
 
        page.setRecords(ls);
        page.setTotal(total);
        return page;
    }
 
    @Override
    public List<UmsDepartDto> departExp() {
        List<UmsDepart> umsDeparts = sccgDepartMapper.selectDepartList();
        List<UmsDepartDto> res = umsDeparts
                .stream()
                .map(item -> {
                    UmsDepartDto umsDepartDto = new UmsDepartDto();
                    umsDepartDto.setId(item.getId());
                    umsDepartDto.setDepartTypeName(item.getDepartName());
                    String departType = sccgDepartMapper.getDepartType(item.getDepartType());
                    umsDepartDto.setDepartType(departType);
                    UmsDepart umsDepartFather = sccgDepartMapper.selectOne(new LambdaQueryWrapper<UmsDepart>().eq(UmsDepart::getId, item.getParentId()));
                    if (Objects.isNull(umsDepartFather)){
                        umsDepartDto.setParentDepartName("");
                    }else {
                        umsDepartDto.setParentDepartName(umsDepartFather.getDepartName());
                    }
                    umsDepartDto.setDepartDes(item.getDepartDes());
                    return umsDepartDto;
                }).collect(Collectors.toList());
        return res;
    }
 
    @Override
    public void updateStatus(DepartVO.StatusDepartVO params) {
        UmsDepart sccgDepart = this.loadDepartById(params.getId());
        BeanUtils.copyProperties(params, sccgDepart);
        if (sccgDepartMapper.updateById(sccgDepart) <= 0) {
            throw new ApiException(ResultCode.RECORD_UPDATE_FAIL);
        }
    }
 
    @Override
    public List<UmsAdmin> selectSurplusUser() {
        return sccgDepartMapper.selectSurplusUser();
    }
 
    @Override
    public List<UmsAdmin> selectSurplusUserAndExistUser(Long id) {
        return sccgDepartMapper.selectSurplusUserAndExistUser(id);
    }
 
    /**
     * 利用递归将最后一级空集合变为null,前端联级选择器最后才不会出现 暂无数据的bug
     *
     * @return
     */
    public List<UmsDepart> getTreeData(List<UmsDepart> griddingEntities) {
        for (int i = 0; i < griddingEntities.size(); i++) {
            //如果Children的size小于1就说明为空,设置为null
            if (griddingEntities.get(i).getChildren().size() < 1) {
                griddingEntities.get(i).setChildren(null);
            } else {
                //子网格有数据就递归
                getTreeData(griddingEntities.get(i).getChildren());
            }
        }
        return griddingEntities;
    }
 
    public List<UmsDepart> getChildrens(UmsDepart root, List<UmsDepart> all) {
        List<UmsDepart> collect = all.stream().filter(griddingEntity -> {
            return griddingEntity.getParentId().equals(root.getId());
        }).map(item -> {
            item.setChildren(getChildrens(item, all));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }
 
 
    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    private UmsDepart queryByName(String name) {
        LambdaQueryWrapper<UmsDepart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsDepart::getDepartName, name);
        UmsDepart sccgDepart = this.sccgDepartMapper.selectOne(queryWrapper);
        return sccgDepart;
    }
}