郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.specialWork.service.impl;
 
 
import com.gk.hotwork.Domain.Enum.ResultCodes;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Vo.ResultVO;
import com.gk.hotwork.Service.Middle.AccountDepartmentService;
import com.gk.hotwork.specialWork.entity.WorkApplyCountDO;
import com.gk.hotwork.specialWork.enums.WorkTypeEnum;
import com.gk.hotwork.specialWork.model.dto.resp.DepRPCRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.WorkDepStatisticRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.WorkTypeStatisticRespDTO;
import com.gk.hotwork.specialWork.service.WorkStatisticService;
import com.gk.hotwork.specialWork.service.baseService.WorkApplyInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("WorkStatisticService")
public class WorkStatisticServiceImpl implements WorkStatisticService {
    @Autowired
    private WorkApplyInfoService workApplyInfoService;
 
    @Autowired
    private AccountDepartmentService accountDepartmentService;
    @Override
    public ResultVO<List<WorkDepStatisticRespDTO>> getWorkApplyCountByDep() {
        //获取所有部门
        List<DepRPCRespDTO> depList = getDepList();
        //统计各部门每种作业数量
        List<WorkApplyCountDO> workApplyCountDOList = workApplyInfoService.getWorkApplyCountByDep();
        List<WorkDepStatisticRespDTO> respDTOList = new ArrayList<>();
        //循环部门
        for(DepRPCRespDTO dep:depList){
            WorkDepStatisticRespDTO respDTO = new WorkDepStatisticRespDTO();
            respDTO.setDepId(dep.getDepId());
            respDTO.setDepName(dep.getDepName());
            //获取该部门下的作业数量
            List<WorkApplyCountDO> selectDepList = workApplyCountDOList
                    .stream()
                    .filter(coutDo -> coutDo.getDepId().equals(dep.getDepId()))
                    .collect(Collectors.toList());
            List<WorkTypeStatisticRespDTO> typeStatisticRespDTOS = new ArrayList<>();
            //声明变量 部门总数量
            int totalCount = 0;
            //循环作业类型
            for(WorkTypeEnum workTypeEnum:WorkTypeEnum.values()){
                WorkTypeStatisticRespDTO  typeStatisticRespDTO = new WorkTypeStatisticRespDTO();
                typeStatisticRespDTO.setWorkType(workTypeEnum.getType());
                typeStatisticRespDTO.setWorkTypeName(workTypeEnum.getName());
                //获取该类型下的数据
                List<WorkApplyCountDO> selectTypeList = selectDepList
                        .stream()
                        .filter(coutDo -> coutDo.getWorkType().equals(workTypeEnum.getType()))
                        .collect(Collectors.toList());
                if(selectTypeList.size()>0){
                    typeStatisticRespDTO.setCount(selectTypeList.get(0).getCount());
                    totalCount += selectTypeList.get(0).getCount();
                }else{
                    typeStatisticRespDTO.setCount(0);
                }
                typeStatisticRespDTOS.add(typeStatisticRespDTO);
            }
            respDTO.setTotalCount(totalCount);
            respDTO.setTypeList(typeStatisticRespDTOS);
            respDTOList.add(respDTO);
            //如果有子级
            List<DepRPCRespDTO> children = dep.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                traverseChildren(children,workApplyCountDOList,respDTOList);
            }
        }
        return new ResultVO<>(ResultCodes.OK,respDTOList);
    }
 
    private void traverseChildren(List<DepRPCRespDTO> depList, List<WorkApplyCountDO> workApplyCountDOList, List<WorkDepStatisticRespDTO> respDTOList) {
        for(DepRPCRespDTO dep:depList){
            WorkDepStatisticRespDTO respDTO = new WorkDepStatisticRespDTO();
            respDTO.setDepId(dep.getDepId());
            respDTO.setDepName(dep.getDepName());
            //获取该部门下的作业数量
            List<WorkApplyCountDO> selectDepList = workApplyCountDOList
                    .stream()
                    .filter(coutDo -> coutDo.getDepId().equals(dep.getDepId()))
                    .collect(Collectors.toList());
            List<WorkTypeStatisticRespDTO> typeStatisticRespDTOS = new ArrayList<>();
            //声明变量 部门总数量
            int totalCount = 0;
            //循环作业类型
            for(WorkTypeEnum workTypeEnum:WorkTypeEnum.values()){
                WorkTypeStatisticRespDTO  typeStatisticRespDTO = new WorkTypeStatisticRespDTO();
                typeStatisticRespDTO.setWorkType(workTypeEnum.getType());
                typeStatisticRespDTO.setWorkTypeName(workTypeEnum.getName());
                //获取该类型下的数据
                List<WorkApplyCountDO> selectTypeList = selectDepList
                        .stream()
                        .filter(coutDo -> coutDo.getWorkType().equals(workTypeEnum.getType()))
                        .collect(Collectors.toList());
                if(selectTypeList.size()>0){
                    typeStatisticRespDTO.setCount(selectTypeList.get(0).getCount());
                    totalCount += selectTypeList.get(0).getCount();
                }else{
                    typeStatisticRespDTO.setCount(0);
                }
                typeStatisticRespDTOS.add(typeStatisticRespDTO);
            }
            respDTO.setTotalCount(totalCount);
            respDTO.setTypeList(typeStatisticRespDTOS);
            respDTOList.add(respDTO);
            //如果有子级
            List<DepRPCRespDTO> children = dep.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                traverseChildren(children,workApplyCountDOList,respDTOList);
            }
        }
    }
 
    public List<DepRPCRespDTO> getDepList(){
        List<DepRPCRespDTO> depList = new ArrayList<>();
        try{
            ResultVO<List<DepRPCRespDTO>> listResultVO = accountDepartmentService.depList();
            if (listResultVO != null && listResultVO.getCode().equals(ResultCodes.OK.getCode())) {
                if (listResultVO.getData() != null) {
                    depList = (List<DepRPCRespDTO>) listResultVO.getData();
                }else{
                    throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
                }
            } else {
                throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
            }
 
        }catch (Exception e){
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
    return depList;
    }
}