Oliver
2022-09-14 91a9dd80b51af93c9305bb39b3757e6750f88008
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
package com.ycl.service.depart.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.dto.user.AdminDepartDTO;
import com.ycl.entity.depart.SccgDepart;
import com.ycl.entity.user.UmsDepartManage;
import com.ycl.enums.common.ResultCode;
import com.ycl.exception.ApiException;
import com.ycl.mapper.depart.SccgDepartMapper;
import com.ycl.service.depart.SccgDepartService;
import com.ycl.service.user.UmsDepartManageService;
import com.ycl.service.user.UmsAdminService;
import com.ycl.utils.common.PojoUtils;
import com.ycl.vo.depart.DepartVO;
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.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author lyq
 * @since 2022-09-07
 */
@Service
public class SccgDepartServiceImpl extends ServiceImpl<SccgDepartMapper, SccgDepart> implements SccgDepartService {
    @Resource
    private SccgDepartMapper sccgDepartMapper;
    @Resource
    private UmsDepartManageService umsAdminDepartService;
    @Resource
    private UmsAdminService umsAdminService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DepartVO.AddDepartVO addDepartVO) {
        SccgDepart sccgDepart = this.queryByName(addDepartVO.getDepartName());
        if (null != sccgDepart) {
            throw new ApiException(ResultCode.RECORD_ALREADY_EXISTS);
        }
        SccgDepart depart = new SccgDepart();
        BeanUtils.copyProperties(addDepartVO, depart);
        if (sccgDepartMapper.insert(depart) <= 0) {
            throw new ApiException(ResultCode.RECORD_SAVE_FAIL);
        }
    }
 
    @Override
    public SccgDepart loadDepartById(long id) {
        SccgDepart 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) {
        SccgDepart sccgDepart = this.queryByName(updateDepartVO.getDepartName());
        if (null != sccgDepart && sccgDepart.getId() != updateDepartVO.getId()) {
            throw new ApiException(ResultCode.RECORD_ALREADY_EXISTS);
        }
        SccgDepart depart = this.getById(updateDepartVO.getId());
        BeanUtils.copyProperties(updateDepartVO, depart);
        List<Long> userIds = updateDepartVO.getUserIds();
        if (CollUtil.isNotEmpty(userIds)) {
            Long departId = updateDepartVO.getId();
            List<UmsDepartManage> departList = umsAdminDepartService.queryByDepartId(departId);
            if (CollUtil.isNotEmpty(departList)) {
                umsAdminDepartService.deletedByDepartId(departId);
            }
            userIds.forEach(e -> {
                UmsDepartManage adminDepart = new UmsDepartManage();
                adminDepart.setDepartId(departId);
                adminDepart.setUserId(e);
                umsAdminDepartService.save(adminDepart);
            });
        }
        if (sccgDepartMapper.updateById(depart) <= 0) {
            throw new ApiException(ResultCode.RECORD_UPDATE_FAIL);
        }
    }
 
    @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<SccgDepart> tree() {
        // 1.查出所有网格
        List<SccgDepart> list = list();
 
        // 2.组装成父子的树型结构
        // 2.1、找到所有的一级网格:使用jdk8的stream流进行过滤
        List<SccgDepart> collect = list.stream().filter(griddingEntity -> {
            //分类父id为0就表示该网格为 一级网格
            return griddingEntity.getParentId() == 0;
        }).map(item -> {
            // 找到子网格 set进children属性中
            item.setChildren(getChildrens(item, list));
            return item;
        }).collect(Collectors.toList());
        List<SccgDepart> treeData = getTreeData(collect);
        return treeData;
    }
 
    @Override
    public IPage<SccgDepart> pageDepart(DepartVO.PageDepartVO params) {
        Page<SccgDepart> page = new Page<>(params.getCurrent(), params.getPageSize());
        LambdaQueryWrapper<SccgDepart> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(params.getDepartName())) {
            query.like(SccgDepart::getDepartName, params.getDepartName());
        }
        if (PojoUtils.Vo.isUsefulSearchParam(params.getDepartType())) {
            query.like(SccgDepart::getDepartType, params.getDepartType());
        }
        query.orderByDesc(SccgDepart::getCreateTime);
        Page<SccgDepart> departPage = sccgDepartMapper.selectPage(page, query);
        List<SccgDepart> records = departPage.getRecords();
        //负责人
        if (CollUtil.isNotEmpty(records)) {
            for (SccgDepart record : records) {
                List<UmsDepartManage> umsAdminDeparts = umsAdminDepartService.queryByDepartId(record.getId());
                if (CollUtil.isNotEmpty(umsAdminDeparts)) {
                    List<AdminDepartDTO.UserInfoDTO> userInfoDTOS = umsAdminDeparts.stream().map(a -> {
                        AdminDepartDTO.UserInfoDTO userInfoDTO = new AdminDepartDTO.UserInfoDTO();
                        userInfoDTO.setUserId(a.getUserId());
                        userInfoDTO.setUsername(umsAdminService.getById(a.getUserId()).getUsername());
                        return userInfoDTO;
                    }).collect(Collectors.toList());
                    record.setUserInfoDTOS(userInfoDTOS);
                }
            }
        }
        return departPage;
    }
 
    @Override
    public void updateStatus(DepartVO.StatusDepartVO params) {
        SccgDepart sccgDepart = this.loadDepartById(params.getId());
        BeanUtils.copyProperties(params, sccgDepart);
        if (sccgDepartMapper.updateById(sccgDepart) <= 0) {
            throw new ApiException(ResultCode.RECORD_UPDATE_FAIL);
        }
    }
 
 
    /**
     * 利用递归将最后一级空集合变为null,前端联级选择器最后才不会出现 暂无数据的bug
     *
     * @return
     */
    public List<SccgDepart> getTreeData(List<SccgDepart> 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;
    }
 
    /**
     * 递归查找所有网格的子网格
     *
     * @param root 当前网格
     * @param all  所有网格
     * @return
     */
    public List<SccgDepart> getChildrens(SccgDepart root, List<SccgDepart> all) {
        List<SccgDepart> 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 SccgDepart queryByName(String name) {
        LambdaQueryWrapper<SccgDepart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SccgDepart::getDepartName, name);
        SccgDepart sccgDepart = this.sccgDepartMapper.selectOne(queryWrapper);
        return sccgDepart;
    }
 
}