forked from gzzfw/backEnd/gz-dyh

liyj
2024-09-04 0271719bce3d6be197eab2e143e478dbf010b8a8
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package cn.huge.module.ctuser.service;
 
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.common.utils.ObjectUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ctuser.dao.mapper.CtUnitMapper;
import cn.huge.module.ctuser.domain.po.CtDept;
import cn.huge.module.ctuser.domain.po.CtUnit;
import cn.huge.module.ctuser.domain.po.CtUser;
import cn.huge.module.ctuser.dto.CtUnitWeChatCountDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @title: 客户组织信息表业务逻辑处理
 * @Description 客户组织信息表业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-08-19 20:04:19
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CtUnitService extends ServiceImpl<CtUnitMapper, CtUnit>{
 
    @Autowired
    private CtUnitMapper mapper;
 
    @Autowired
    private UtilsClientImpl utilsClient;
 
    @Autowired
    private CtDeptService ctDeptService;
 
    @Autowired
    private CtUserService ctUserService;
 
    /**
    * 更新对象
    * @param entity 对象
    */
    public void updateCtUnit(CtUnit entity){
        try{
            mapper.updateCtUnit(entity);
        }catch (Exception e){
            log.error("[CtUnitService.updateCtUnit]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.updateCtUnit", e);
        }
    }
 
    /**
    * 条件更新对象
    * @param entity 对象
    * @param terms 条件
    */
    public void updateCtUnitTerms(CtUnit entity, Map<String, Object> terms){
        try{
            mapper.updateCtUnitTerms(entity, terms);
        }catch (Exception e){
            log.error("[CtUnitService.updateCtUnitTerms]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.updateCtUnitTerms", e);
        }
    }
 
    /**
    * 根据编号物理删除
    * @param id 查询条件集合
    */
    public void deleteCtUnit(String id){
        try{
            mapper.deleteCtUnit(id);
        }catch (Exception e){
            log.error("[CtUnitService.deleteCtUnit]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.deleteCtUnit", e);
        }
    }
 
    /**
    * 按条件查询
    * @param terms 条件
    * @return List
    */
    public List<CtUnit> listTerms(Map<String, Object> terms){
        return mapper.listTerms(terms);
    }
 
    /**
    * 按条件统计
    * @param terms 条件
    * @return long
    */
    public long countTerms(Map<String, Object> terms){
        return mapper.countTerms(terms);
    }
 
    /**
    * 按条件分页查询
    * @param page 分页对象
    * @param terms 条件
    * @return Page
    */
    public Page<CtUnit> pageQuery(PageRequest page, Map<String, Object> terms){
        long total = mapper.countTerms(terms);
        List<CtUnit> content = mapper.pageTerms(page, terms);
        return new PageImpl<CtUnit>(content, page, total);
    }
 
    /**
    * 新增或更新对象
    * @param ctUnit 实体对象
    */
    public void saveCtUnit(CtUnit ctUnit){
        try{
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(ctUnit.getId())){
                ctUnit.setId(utilsClient.getNewTimeId());
                ctUnit.setCreateTime(nowDate);
            }else {
                // 1、更新单位信息时,更新调度规则指标目标组织名称
                mapper.updateDispNormCauseTargetUnitName(ctUnit.getUnitName(), ctUnit.getId());
            }
            ctUnit.setUpdateTime(nowDate);
            this.saveOrUpdate(ctUnit);
        }catch (Exception e){
            log.error("[CtUnitService.saveCtUnit]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.saveCtUnit", e);
        }
    }
 
    /**
     * 获取所有组织(包含所有下级子组织)
     *
     * @param custId  客户编号
     * @param firstId 某一级组织编号
     * @return List
     */
    public List<CtUnit> listUnitTree(String custId, String firstId) {
        QueryWrapper<CtUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_id", custId).orderByDesc("create_time");
        List<CtUnit> ctUnits = mapper.selectList(queryWrapper);
 
        // 获取所有的 CtDept 的数量
        QueryWrapper<CtDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.in("unit_id", ctUnits.stream().map(CtUnit::getId).collect(Collectors.toList()));
        List<CtDept> ctDepts = ctDeptService.list(deptQueryWrapper);
 
        // 将 CtDept 的数量存储在 Map 中
        Map<String, Long> ctDeptCountMap = ctDepts.stream()
                .collect(Collectors.groupingBy(CtDept::getUnitId, Collectors.counting()));
 
        // 为每个 CtUnit 设置 CtDept 的数量
        for (CtUnit ctUnit : ctUnits) {
            Long ctDeptsCount = ctDeptCountMap.getOrDefault(ctUnit.getId(), 0L);
            ctUnit.setCountCtDept(ctDeptsCount.intValue());
        }
        return this.createTree(ctUnits, firstId);
    }
 
    /**
     * 创建某一级组织下树形结构
     *
     * @param ctUnitList 所有组织集合
     * @param firstId    某一级组织编号
     * @return List
     */
    public List<CtUnit> createTree(List<CtUnit> ctUnitList, String firstId) {
        List<CtUnit> firstMapList = new ArrayList<>();
        for (CtUnit ctUnit : ctUnitList) {
            for (CtUnit currentParam : ctUnitList) {
                if (currentParam.getId().equals(ctUnit.getParentId())) {
                    addToMBean(currentParam, ctUnit);
                    break;
                }
            }
        }
        //取第一级节点
        for (CtUnit unit : ctUnitList) {
            if (StringUtils.isNotEmpty(firstId)) {
                if (firstId.equals(unit.getId())) {
                    firstMapList.add(unit);
                }
            } else {
                if (BaseConsts.ROOT.equals(unit.getParentId()) || StringUtils.isEmpty(unit.getParentId())) {
                    firstMapList.add(unit);
                }
            }
        }
        return firstMapList;
    }
 
    /**
     * 获取组织的子级组织
     *
     * @param targetUnit  目标组织
     * @param currentUnit 子级组织
     */
    public void addToMBean(CtUnit targetUnit, CtUnit currentUnit) {
        List<CtUnit> childListObj = targetUnit.getChildren();
        List<CtUnit> childList = null;
        if (CollectionUtils.isEmpty(childListObj)) {
            childList = new ArrayList();
            targetUnit.setChildren(childList);
        } else {
            childList = childListObj;
        }
        childList.add(currentUnit);
    }
 
    /**
     * 获取组织相关数据(包含组织下的部门)
     *
     * @param unitId 组织主键编号
     * @return
     */
    public CtUnit getUnitAndDept(String unitId) {
 
        // 组织详情
        CtUnit ctUnit = mapper.selectById(unitId);
        // 组织人员数量
        QueryWrapper<CtUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("unit_id", unitId);
        Integer countCtUser = ctUserService.count(userQueryWrapper);
        ctUnit.setCountCtUser(countCtUser);
        // 未配岗人员(单位下)
        userQueryWrapper.isNull("dept_id");
        List<CtUser> unMatchUsers = ctUserService.list(userQueryWrapper);
        ctUnit.setUnMatchUsers(unMatchUsers);
        // 部门详情
        QueryWrapper<CtDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("unit_id", unitId);
        deptQueryWrapper.orderByDesc("create_time");
        List<CtDept> ctDepts = ctDeptService.list(deptQueryWrapper);
        for (CtDept ctDept : ctDepts) {
            // 部门
            userQueryWrapper.clear();
            userQueryWrapper.eq("dept_id", ctDept.getId());
            // 部门下成员
            List<CtUser> matchUsers = ctUserService.list(userQueryWrapper);
            ctDept.setMatchUsers(matchUsers);
            // 部门人员数量
            int countDeptUser = ctUserService.count(userQueryWrapper);
            ctDept.setCountCtUser(countDeptUser);
        }
        List<CtDept> ctDeptTrees = ctDeptService.createTree(ctDepts, null);
        ctUnit.setCtDepts(ctDeptTrees);
        // 部门数量
        ctUnit.setCountCtDept(ctDepts.size());
        return ctUnit;
    }
 
    /**
     * 更新对象
     *
     * @param ctUnit 对象
     */
    public void updateTerms(CtUnit ctUnit) {
        mapper.updateTerms(ctUnit);
    }
 
    /**
     * 小程序统计机构数量
     * */
    public CtUnitWeChatCountDTO wechatCountUnit(){
        CtUnitWeChatCountDTO ctUnitWeChatCountDTO = new CtUnitWeChatCountDTO();
        QueryWrapper<CtUnit> unitQueryWrapper = new QueryWrapper<>();
        unitQueryWrapper.eq("unit_type", 1);
        Integer zzUnitNum = mapper.selectCount(unitQueryWrapper);
        if(ObjectUtils.isEmpty(zzUnitNum)){
            zzUnitNum = 0;
        }
        ctUnitWeChatCountDTO.setZzUnitNum(zzUnitNum);
        List<Integer> unitTypeList = Arrays.asList(2,3,4,5);
        QueryWrapper<CtUnit> unitQueryWrapper1 = new QueryWrapper<>();
        unitQueryWrapper1.in("unit_type", unitTypeList);
        Integer tzUnitNum = mapper.selectCount(unitQueryWrapper1);
        if(ObjectUtils.isEmpty(tzUnitNum)){
            tzUnitNum = 0;
        }
        ctUnitWeChatCountDTO.setTzUnitNum(tzUnitNum);
        return ctUnitWeChatCountDTO;
    }
}