郑永安
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
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
package com.gkhy.safePlatform.specialWork.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.enums.DepartmentLevelEnum;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.utils.TimeUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.specialWork.enums.WorkApplyStatusEnum;
import com.gkhy.safePlatform.specialWork.enums.WorkTypeEnum;
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkCountRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkDepCountRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkTypeCountRespDTO;
import com.gkhy.safePlatform.specialWork.model.query.db.WorkCountDbQuery;
import com.gkhy.safePlatform.specialWork.service.WorkCountService;
import com.gkhy.safePlatform.specialWork.service.baseService.WorkInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class WorkCountServiceImpl implements WorkCountService {
 
    @Autowired
    private WorkInfoService workInfoService;
 
    @DubboReference(check = false)
    private AccountDepartmentService departmentService;
 
 
    @Override
    public List<WorkTypeCountRespDTO> countEveryTypeByOptions(LocalDateTime startTimne, LocalDateTime endTime, Long depId) {
        List<WorkTypeCountRespDTO> countList = new ArrayList<>();
        //1、校验部门是否存在,无效部门返回全零数据
        ResultVO<DepInfoRPCRespDTO> depSearchVo = departmentService.getDepInfoByDepId(depId);
        if(depSearchVo == null || !depSearchVo.getCode().equals(ResultCodes.OK.getCode()) || depSearchVo.getData() == null){
            countList = initEveryTypeZeroCountResult();
            return countList;
        }
        //2、解析该部门需要查询的深度,车间级只统计本车间,往上级别需要统计全部子部门数据
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depSearchVo.getData();
        if(depInfo == null || depInfo.getDepLevel() == null){
            countList = initEveryTypeZeroCountResult();
            return countList;
        }
        //3、查询条件 - 部门ID列表
        List<Long> queryDepIdList = new ArrayList<>();
        if(depInfo.getDepLevel().equals(DepartmentLevelEnum.WORKSHOP.code)){
            //部门为车间
            queryDepIdList.add(depId);
        }else if(depInfo.getDepLevel().equals(DepartmentLevelEnum.GROUP.code) || depInfo.getDepLevel().equals(DepartmentLevelEnum.OTHER.code)){
            //部门为班组或其他
            //获取本部门隶属的车间
            DepInfoRPCRespDTO chejianDep = praseDepWorkShop(depInfo);
            //获取该车间的所有子部门
            if(chejianDep != null){
                ResultVO subDepIdListVo = departmentService.listDepAndSubDepIds(chejianDep.getDepId());
                if(subDepIdListVo != null && subDepIdListVo.getCode().equals(ResultCodes.OK.getCode())){
                    queryDepIdList = (List<Long>) subDepIdListVo.getData();
                }
            }
        }else {
            //部门为公司或事业部
            //获取其下面的所有部门
            ResultVO subDepIdListVo = departmentService.listDepAndSubDepIds(depInfo.getDepId());
            if(subDepIdListVo != null && subDepIdListVo.getCode().equals(ResultCodes.OK.getCode())){
                queryDepIdList = (List<Long>) subDepIdListVo.getData();
            }
        }
        //如果查询条件部门id列表为空,就不查询了,返回全零数据
        if(queryDepIdList == null || queryDepIdList.isEmpty()){
            countList = initEveryTypeZeroCountResult();
            return countList;
        }
        //5、为每种作业分别统计数量
        for(WorkTypeEnum workType : WorkTypeEnum.values()){
            WorkCountDbQuery dbQuery = new WorkCountDbQuery();
            dbQuery.setDepIdList(queryDepIdList);
            dbQuery.setType(workType.getType());
            dbQuery.setStartTime(startTimne);
            dbQuery.setEndTime(endTime);
            Integer count = workInfoService.countEvertWorkByOptions(dbQuery);
            WorkTypeCountRespDTO workTypeCountRespDTO = new WorkTypeCountRespDTO();
            workTypeCountRespDTO.setName(workType.getName());
            workTypeCountRespDTO.setValue(count);
            countList.add(workTypeCountRespDTO);
        }
        return countList;
    }
 
    /**
     * 统计子部门的作业数量
     * @param startTimne
     * @param endTime
     * @param depId
     * @return
     */
    @Override
    public List<WorkDepCountRespDTO> countWorkBySubDeps(LocalDateTime startTimne, LocalDateTime endTime, Long depId,Byte type) {
        if(depId == null || depId < 1)
            return null;
        //1、查询部门信息
        ResultVO<DepInfoRPCRespDTO> depSearchVo = departmentService.getDepInfoByDepId(depId);
        if(depSearchVo == null || !depSearchVo.getCode().equals(ResultCodes.OK.getCode()) || depSearchVo.getData() == null){
            return null;
        }
        //2、查询直属子部门列表
        ResultVO<List<DepInfoRPCRespDTO>> subDepSearchVO = departmentService.listSubDepsByDepId(depId);
        if(subDepSearchVO == null || !subDepSearchVO.getCode().equals(ResultCodes.OK.getCode()) || subDepSearchVO.getData() == null)
            return null;
        List<DepInfoRPCRespDTO> subDepList = (List<DepInfoRPCRespDTO>) subDepSearchVO.getData();
        if(subDepList.isEmpty())
            return null;
        //3、统计子部门作业数据
        //4、结构化数据
        List<WorkDepCountRespDTO> resultList = new ArrayList<>();
        subDepList.forEach(dep -> {
            //获取该部门的全部子部门
            ResultVO<List<Long>> subDepIdsVO = departmentService.listDepAndSubDepIds(dep.getDepId());
            if(subDepIdsVO != null && subDepIdsVO.getCode().equals(ResultCodes.OK.getCode()) && subDepIdsVO.getData() != null){
                List<Long> subDepIds = (List<Long>) subDepIdsVO.getData();
                WorkDepCountRespDTO depCountDTO = new WorkDepCountRespDTO();
                WorkCountDbQuery query = new WorkCountDbQuery();
                query.setDepIdList(subDepIds);
                query.setType(type);
                query.setStartTime(startTimne);
                query.setEndTime(endTime);
//                Integer workCount = workInfoService.countAllWorkByDep(dep.getDepId(),startTimne,endTime);
                Integer workCount = workInfoService.countEvertWorkByOptions(query);
                depCountDTO.setName(dep.getDepName());
                if(workCount == null)
                    depCountDTO.setValue(0);
                else
                    depCountDTO.setValue(workCount);
                resultList.add(depCountDTO);
            }
        });
        return resultList;
    }
 
 
    /**
     * 统计指定部门最近12个月的作业数量,按月份统计
     * @param depId
     * @return
     */
    @Override
    public List<WorkCountRespDTO> count12MonthWorkByDep(Long depId) {
        if(depId == null || depId < 1)
            return null;
        //查询部门信息
        ResultVO<DepInfoRPCRespDTO> depSearchVO = departmentService.getDepInfoByDepId(depId);
        if(depSearchVO == null || !depSearchVO.getCode().equals(ResultCodes.OK.getCode()) || depSearchVO.getData() == null)
            return null;
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depSearchVO.getData();
        //获取全部子部门数据
        List<Long> subDepIds = null;
        ResultVO<List<Long>> subDepIdsVO = departmentService.listDepAndSubDepIds(depInfo.getDepId());
        if(subDepIdsVO != null && subDepIdsVO.getCode().equals(ResultCodes.OK.getCode()) && subDepIdsVO.getData() != null){
            subDepIds = (List<Long>) subDepIdsVO.getData();
        }
        List<WorkCountRespDTO> respDTOList = new ArrayList<>();
        LocalDateTime time = LocalDateTime.now();
        //重置时间,从一年前开始统计
        time = time.minusYears(1);
        for(long i=0;i<12;i++){
            time = time.plusMonths(1);
            LocalDateTime monthBeginTime = calBeginOrEndOfMonth(time,true);
            LocalDateTime monthEndTime = calBeginOrEndOfMonth(time,false);
            WorkCountDbQuery query = new WorkCountDbQuery();
            query.setDepIdList(subDepIds);
            query.setStartTime(monthBeginTime);
            query.setEndTime(monthEndTime);
            Integer workCount = workInfoService.countEvertWorkByOptions(query);
            WorkCountRespDTO workCountRespDTO = new WorkCountRespDTO();
            workCountRespDTO.setName(sayMonthName(time));
            workCountRespDTO.setValue(workCount);
            respDTOList.add(workCountRespDTO);
        }
        return respDTOList;
    }
 
    private String sayMonthName(LocalDateTime time){
        if(time == null)
            return null;
        int month = time.getMonthValue();
        if(month == 1){
            return "一月";
        }else if(month == 2){
            return "二月";
        }else if(month == 3){
            return "三月";
        }else if(month == 4){
            return "四月";
        }else if(month == 5){
            return "五月";
        }else if(month == 6){
            return "六月";
        }else if(month == 7){
            return "七月";
        }else if(month == 8){
            return "八月";
        }else if(month == 9){
            return "九月";
        }else if(month == 10){
            return "十月";
        }else if(month == 11){
            return "十一月";
        }else if(month == 12){
            return "十二月";
        }else {
            return null;
        }
    }
 
    /**
     * 给定时间,计算月初或月末时间
     * @param time
     * @param isBegin
     * @return
     */
    private LocalDateTime calBeginOrEndOfMonth(LocalDateTime time,boolean isBegin){
        if(time == null)
            return null;
        String str = DateUtil.formatLocalDateTime(time);
        Date date = DateUtil.parseDate(str);
        if(isBegin){
            //计算月开始时间
            return DateUtil.beginOfMonth(date).toLocalDateTime();
        }else {
            //计算月结束时间
            return DateUtil.endOfMonth(date).toLocalDateTime();
        }
    }
 
 
    private List<WorkTypeCountRespDTO> initEveryTypeZeroCountResult(){
        List<WorkTypeCountRespDTO> countList = new ArrayList<>();
        for(WorkTypeEnum workType : WorkTypeEnum.values()){
            WorkTypeCountRespDTO dto = new WorkTypeCountRespDTO();
            dto.setName(workType.getName());
            dto.setValue(0);
            countList.add(dto);
        }
        return countList;
    }
 
    /**
     * 解析部门所属的车间
     * @param dep
     * @return
     */
    private DepInfoRPCRespDTO praseDepWorkShop(DepInfoRPCRespDTO dep){
        if(dep == null || dep.getDepLevel() == null)
            dep = null;
        if(dep.getDepLevel().equals(DepartmentLevelEnum.GROUP.code) || dep.getDepLevel().equals(DepartmentLevelEnum.OTHER.code)){
            if(dep.getParentDepId() == null)
                dep = null;
            ResultVO<DepInfoRPCRespDTO> depResult = departmentService.getDepInfoByDepId(dep.getParentDepId());
            if(depResult == null || !depResult.getCode().equals(ResultCodes.OK.getCode()) || depResult.getData() == null)
                dep = null;
            DepInfoRPCRespDTO prevDepInfo = (DepInfoRPCRespDTO) depResult.getData();
            dep = praseDepWorkShop(prevDepInfo);
        }
        return dep;
    }
}