郑永安
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package com.gkhy.safePlatform.doublePrevention.service.impl;
 
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.DataCountI1RespDO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.DataCountI2RespDO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.req.DataCountI1ReqDTO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.req.DataCountI2ReqDTO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.resp.DataCountI1RespDTO;
import com.gkhy.safePlatform.doublePrevention.entity.dto.resp.DataCountI2RespDTO;
import com.gkhy.safePlatform.doublePrevention.repository.param.DataCountIMonthParams;
import com.gkhy.safePlatform.doublePrevention.service.DataCountService;
import com.gkhy.safePlatform.doublePrevention.service.baseService.PreventDangerManageService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public class DataCountServiceImpl implements DataCountService {
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @Autowired
    private PreventDangerManageService preventDangerManageService;
 
    /**
     * 数据统计-隐患数据统计-I1计算
     */
    @Override
    public DataCountI1RespDTO listMonthDataCountI1(DataCountI1ReqDTO dataCountI1ReqDTO){
 
 
        //获取时间格式化工具
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        StringBuffer time = new StringBuffer();
        String year = dataCountI1ReqDTO.getYear().toString();
        String month;
        if (dataCountI1ReqDTO.getMonth() < 10) {
            month = "0" + dataCountI1ReqDTO.getMonth().toString();
        }else {
            month = dataCountI1ReqDTO.getMonth().toString();
        }
        time.append(year + "-" + month + "-" + "01");
        //确定开始时间
        Date startTime = null;
 
        try {
            startTime = monthFormat.parse(String.valueOf(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
 
        //确定结束时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);//设置起时间
        calendar.add(Calendar.MONTH, +1);//增加1个月
        Date endTime = calendar.getTime();
 
        //封装查询参数
        DataCountIMonthParams countParams = new DataCountIMonthParams();
        countParams.setStartTime(startTime);
        countParams.setEndTime(endTime);
        DepInfoRPCRespDTO depInfo = new DepInfoRPCRespDTO();
        //如果depId不为空
        if (ObjectUtils.isNotEmpty(dataCountI1ReqDTO.getDepId())){
            //获取部门信息
            ResultVO<DepInfoRPCRespDTO> depInfoByDepId = accountDepartmentService.getDepInfoByDepId(dataCountI1ReqDTO.getDepId());
            depInfo = (DepInfoRPCRespDTO) depInfoByDepId.getData();
            if (ObjectUtils.isEmpty(depInfo)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门已经被删除");
            }
            //获取部门及所有子部门信息
            ResultVO<List<Long>> listResultVO = accountDepartmentService.listDepAndSubDepIds(dataCountI1ReqDTO.getDepId());
            if (ObjectUtils.isEmpty(listResultVO)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在");
            }
            List<Long> ids = (List<Long>) listResultVO.getData();
            countParams.setIds(ids);
        }
 
        DataCountI1RespDO dataCountI1RespDO = preventDangerManageService.listMonthDataCountI1(countParams);
 
        DataCountI1RespDTO respDTO = new DataCountI1RespDTO();
        //封装返回值
        respDTO.setRiskCount(dataCountI1RespDO.getA());
        respDTO.setDeathCount(dataCountI1RespDO.getA1());
        respDTO.setHeavyInjureCount(dataCountI1RespDO.getA2());
        respDTO.setLightInjureCount(dataCountI1RespDO.getA3());
        respDTO.setYear(dataCountI1ReqDTO.getYear());
        respDTO.setMonth(dataCountI1ReqDTO.getMonth());
        respDTO.setDepId(dataCountI1ReqDTO.getDepId());
        respDTO.setDep(depInfo.getDepName());
 
 
        return respDTO;
    }
    /**
     * 数据统计-隐患数据统计-I1计算 - 年
     */
    @Override
    public List<DataCountI1RespDTO> listYearDataCountI1(DataCountI1ReqDTO dataCountI1ReqDTO) {
 
        //获取时间格式化工具
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer time = new StringBuffer();
        String year = dataCountI1ReqDTO.getYear().toString();
        Date date = new Date();
 
        time.append(year + "-" + "01" + "-" + "01");
        //确定开始时间
        Date originTime = null;
        try {
            originTime = monthFormat.parse(String.valueOf(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //获得本月第一天
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDay = calendar.getTime();
 
        Date startTime = originTime;
        Date endTime = null;
        //获取list
        List<DataCountI1RespDTO> list = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        DepInfoRPCRespDTO depInfo = new DepInfoRPCRespDTO();
 
        //如果depId不为空
        if (ObjectUtils.isNotEmpty(dataCountI1ReqDTO.getDepId())){
            ResultVO<DepInfoRPCRespDTO> depInfoByDepId = accountDepartmentService.getDepInfoByDepId(dataCountI1ReqDTO.getDepId());
            depInfo = (DepInfoRPCRespDTO) depInfoByDepId.getData();
            if (ObjectUtils.isEmpty(depInfo)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门已经被删除");
            }
            ResultVO<List<Long>> listResultVO = accountDepartmentService.listDepAndSubDepIds(dataCountI1ReqDTO.getDepId());
            if (ObjectUtils.isEmpty(listResultVO)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在");
            }
            ids = (List<Long>) listResultVO.getData();
        }
 
        //循环统计本年度数据
        int i = 1;
        while (originTime.getTime() < firstDay.getTime()){
            calendar.setTime(startTime);//设置起时间
            calendar.add(Calendar.MONTH, +1);//增加1个月
            endTime = calendar.getTime();
            //封装查询参数
            DataCountIMonthParams countParams = new DataCountIMonthParams();
            countParams.setStartTime(startTime);
            countParams.setEndTime(endTime);
            countParams.setIds(ids);
            //查询
            DataCountI1RespDO dataCountI1RespDO = preventDangerManageService.listMonthDataCountI1(countParams);
            DataCountI1RespDTO respDTO = new DataCountI1RespDTO();
            //封装返回值
            respDTO.setRiskCount(dataCountI1RespDO.getA());
            respDTO.setDeathCount(dataCountI1RespDO.getA1());
            respDTO.setHeavyInjureCount(dataCountI1RespDO.getA2());
            respDTO.setLightInjureCount(dataCountI1RespDO.getA3());
            respDTO.setYear(dataCountI1ReqDTO.getYear());
            respDTO.setMonth(i);
            //todo 部门相关信息
            respDTO.setDepId(dataCountI1ReqDTO.getDepId());
            respDTO.setDep(depInfo.getDepName());
 
            list.add(respDTO);
            originTime = endTime;
            startTime = endTime;
            i ++;
        }
 
        return list;
    }
 
    /**
     * 数据统计-隐患等级-I2计算 - 月
     */
    @Override
    public DataCountI2RespDTO listMonthDataCountI2(DataCountI2ReqDTO dataCountI2ReqDTO){
 
        //获取时间格式化工具
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        StringBuffer time = new StringBuffer();
        String year = dataCountI2ReqDTO.getYear().toString();
        String month;
        if (dataCountI2ReqDTO.getMonth() < 10) {
            month = "0" + dataCountI2ReqDTO.getMonth().toString();
        }else {
            month = dataCountI2ReqDTO.getMonth().toString();
        }
        time.append(year + "-" + month + "-" + "01");
        //确定开始时间
        Date startTime = null;
        try {
            startTime = monthFormat.parse(String.valueOf(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //确定结束时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);//设置起时间
        calendar.add(Calendar.MONTH, +1);//增加1个月
        Date endTime = calendar.getTime();
        DepInfoRPCRespDTO depInfo = new DepInfoRPCRespDTO();
 
        //封装查询参数
        DataCountIMonthParams countParams = new DataCountIMonthParams();
        countParams.setStartTime(startTime);
        countParams.setEndTime(endTime);
        //如果depId不为空
        if (ObjectUtils.isNotEmpty(dataCountI2ReqDTO.getDepId())){
            //todo 部门相关信息
            ResultVO<DepInfoRPCRespDTO> depInfoByDepId = accountDepartmentService.getDepInfoByDepId(dataCountI2ReqDTO.getDepId());
            depInfo = (DepInfoRPCRespDTO) depInfoByDepId.getData();
            if (ObjectUtils.isEmpty(depInfo)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门已经被删除");
            }
            ResultVO<List<Long>> listResultVO = accountDepartmentService.listDepAndSubDepIds(dataCountI2ReqDTO.getDepId());
            if (ObjectUtils.isEmpty(listResultVO)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在");
            }
            List<Long> ids = (List<Long>) listResultVO.getData();
            countParams.setIds(ids);
        }
 
        DataCountI2RespDO dataCountI2RespDO = preventDangerManageService.listMonthDataCountI2(countParams);
        DataCountI2RespDTO respDTO = new DataCountI2RespDTO();
        //封装返回值
        respDTO.setHeavyRiskCount(dataCountI2RespDO.getB1());
        respDTO.setHeavyRiskRectifyCount(dataCountI2RespDO.getB1Rectify());
        respDTO.setLightRiskCount(dataCountI2RespDO.getB2());
        respDTO.setLightRiskRectifyCount(dataCountI2RespDO.getB2Rectify());
        respDTO.setMonth(dataCountI2ReqDTO.getYear());
        respDTO.setMonth(dataCountI2ReqDTO.getMonth());
        respDTO.setDepId(dataCountI2ReqDTO.getDepId());
        respDTO.setDep(depInfo.getDepName());
 
        return respDTO;
    }
    /**
     * 数据统计-隐患数据统计-I2计算 - 年
     */
    @Override
    public List<DataCountI2RespDTO> listYearDataCountI2(DataCountI2ReqDTO dataCountI2ReqDTO){
 
        //获取时间格式化工具
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer time = new StringBuffer();
        String year = dataCountI2ReqDTO.getYear().toString();
        Date date = new Date();
 
        time.append(year + "-" + "01" + "-" + "01");
        //确定开始时间
        Date originTime = null;
        try {
            originTime = monthFormat.parse(String.valueOf(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //获得本月第一天
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDay = calendar.getTime();
 
        Date startTime = originTime;
        Date endTime = null;
        //获取list
        List<DataCountI2RespDTO> list = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        DepInfoRPCRespDTO depInfo = new DepInfoRPCRespDTO();
 
        //如果depId不为空
        if (ObjectUtils.isNotEmpty(dataCountI2ReqDTO.getDepId())){
            ResultVO<DepInfoRPCRespDTO> depInfoByDepId = accountDepartmentService.getDepInfoByDepId(dataCountI2ReqDTO.getDepId());
            depInfo = (DepInfoRPCRespDTO) depInfoByDepId.getData();
            if (ObjectUtils.isEmpty(depInfo)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门已经被删除");
            }
            ResultVO<List<Long>> listResultVO = accountDepartmentService.listDepAndSubDepIds(dataCountI2ReqDTO.getDepId());
            if (ObjectUtils.isEmpty(listResultVO)){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在");
            }
            ids = (List<Long>) listResultVO.getData();
        }
 
        //循环统计年度数据
        int i = 1;
        while (originTime.getTime() < firstDay.getTime()){
            calendar.setTime(startTime);//设置起时间
            calendar.add(Calendar.MONTH, +1);//增加1个月
            endTime = calendar.getTime();
            //封装查询参数
            DataCountIMonthParams countParams = new DataCountIMonthParams();
            countParams.setStartTime(startTime);
            countParams.setEndTime(endTime);
            countParams.setIds(ids);
            //查询
            DataCountI2RespDO dataCountI2RespDO = preventDangerManageService.listMonthDataCountI2(countParams);
            DataCountI2RespDTO respDTO = new DataCountI2RespDTO();
            //封装返回值
            respDTO.setHeavyRiskCount(dataCountI2RespDO.getB1());
            respDTO.setHeavyRiskRectifyCount(dataCountI2RespDO.getB1Rectify());
            respDTO.setLightRiskCount(dataCountI2RespDO.getB2());
            respDTO.setLightRiskRectifyCount(dataCountI2RespDO.getB2Rectify());
            respDTO.setMonth(dataCountI2ReqDTO.getMonth());
            respDTO.setYear(dataCountI2ReqDTO.getYear());
            respDTO.setMonth(i);
            respDTO.setDepId(dataCountI2ReqDTO.getDepId());
            respDTO.setDep(depInfo.getDepName());
 
            list.add(respDTO);
            originTime = endTime;
            startTime = endTime;
            i++;
        }
 
        return list;
    }
}