双重预防项目-国泰新华二开定制版
huangzhen
2022-10-13 81a6a662b5db806e0824f311a89993d853db9371
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
package com.ruoyi.doublePrevention.service.impl;
 
import com.ruoyi.doublePrevention.entity.DoublePreventDept;
import com.ruoyi.doublePrevention.entity.dto.DataCountDangerLevelRectifiedRespDO;
import com.ruoyi.doublePrevention.entity.dto.DataCountDangerLevelRespDO;
import com.ruoyi.doublePrevention.entity.dto.DataCountDangerResultRespDO;
import com.ruoyi.doublePrevention.entity.dto.req.SPIDataCountReqDTO;
import com.ruoyi.doublePrevention.entity.dto.resp.SPIDataCountRespDTO;
import com.ruoyi.doublePrevention.enums.CountContentEnum;
import com.ruoyi.doublePrevention.repository.param.DataCountStartAndEndTimeParams;
import com.ruoyi.doublePrevention.service.SPIDataCountService;
import com.ruoyi.doublePrevention.service.baseService.DoublePreventionDeptService;
import com.ruoyi.doublePrevention.service.baseService.PreventRiskDangerInfoService;
import com.ruoyi.doublePrevention.service.baseService.TrHiddenDangerCheckPointService;
import com.ruoyi.doublePrevention.vo.ResultVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
@Service("SPIDataCountService")
public class SPIDataCountServiceImpl implements SPIDataCountService {
 
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Autowired
    private DoublePreventionDeptService doublePreventionDeptService;
 
    @Autowired
    private PreventRiskDangerInfoService preventRiskDangerInfoService;
 
    @Autowired
    private TrHiddenDangerCheckPointService checkPointService;
 
 
    /**
     * 数据统计-隐患数据统计-根据月或者年
     */
    @Override
    public ResultVO<SPIDataCountRespDTO> listDangerResultCountByMonthOrYear(SPIDataCountReqDTO spiDataCountReqDTO) {
        ResultVO<SPIDataCountRespDTO> parameterVerificationResult = parameterVerification(spiDataCountReqDTO);
        if ("400".equals(parameterVerificationResult.getCode())){
            return parameterVerificationResult;
        }
        Integer month = spiDataCountReqDTO.getMonth();
        Integer year = spiDataCountReqDTO.getYear();
 
        Long depId = spiDataCountReqDTO.getDepId();
        //获取部门信息
        DoublePreventDept depInfoByDepId = doublePreventionDeptService.getDepInfoByDepId(depId);
        //获取部门及所有子部门信息
        //祖级列表
        String ancestors = depInfoByDepId.getAncestors();
        ancestors = ancestors + ",";
        List<Long> depIds = doublePreventionDeptService.listDepAndSubDepIds(ancestors);
        depIds.add(depId);
        if (CollectionUtils.isEmpty(depIds)){
            throw new RuntimeException("部门不存在");
        }
 
        DataCountDangerResultRespDO resultCountByTime = null;
        DataCountDangerLevelRespDO levelCountByTime = null;
        DataCountDangerLevelRectifiedRespDO levelRectifiedCountByTime = null;
 
        String SPIDataCountKey = "SPI_PREVENT_RISK_";
        //判断month是否null
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int currentDay = calendar.get(Calendar.DATE);
 
        if (month == null){
            if (currentYear != year){
                SPIDataCountKey = SPIDataCountKey + year+"_"+"【1-12】_"+depId;
            }else {
                SPIDataCountKey = SPIDataCountKey + year + "_" + "【1-" + currentMonth + "/" + currentDay + "】_" + depId;
            }
        }else {
            if (currentYear != year){
                SPIDataCountKey = SPIDataCountKey + year+"_"+month+"_"+depId;
            }else {
                SPIDataCountKey = SPIDataCountKey + year + "_" + currentMonth +"_【1-" + currentDay + "】_" + depId;
            }
        }
 
        if (ObjectUtils.isNotEmpty(month) && ObjectUtils.isNotEmpty(year)){
            DataCountStartAndEndTimeParams params = timeInterval(month, year);
 
            //    月-统计时间段内所有、死亡、重伤、轻伤的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name())){
                resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name(),resultCountByTime);
            }
            resultCountByTime = (DataCountDangerResultRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name());
 
 
            //    月-统计时间段内一般、重大的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name())){
                levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name(),levelCountByTime);
            }
            levelCountByTime = (DataCountDangerLevelRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name());
 
            //    月-统计时间段内一般已整改、重大已整改的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name())){
                levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name(),levelRectifiedCountByTime);
            }
            levelRectifiedCountByTime = (DataCountDangerLevelRectifiedRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name());
 
        }else {
            DataCountStartAndEndTimeParams params = timeInterval(null, year);
            //    年-统计时间段内所有、死亡、重伤、轻伤的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name())){
                resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name(),resultCountByTime);
            }
            resultCountByTime = (DataCountDangerResultRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name());
 
 
            //    年-统计时间段内一般、重大的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name())){
                levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name(),levelCountByTime);
            }
            levelCountByTime = (DataCountDangerLevelRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name());
 
 
            //    年-统计时间段内一般已整改、重大已整改的隐患数量
            if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name())){
                levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds);
                redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name(),levelRectifiedCountByTime);
            }
            levelRectifiedCountByTime = (DataCountDangerLevelRectifiedRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name());
 
        }
        if (resultCountByTime == null || levelCountByTime == null || levelRectifiedCountByTime == null){
            parameterVerificationResult.setCode("400");
            parameterVerificationResult.setMsg("数据统计异常");
            return parameterVerificationResult;
        }
        SPIDataCountRespDTO spiDataCountRespDTO = new SPIDataCountRespDTO();
        BeanUtils.copyProperties(resultCountByTime,spiDataCountRespDTO);
        BeanUtils.copyProperties(levelCountByTime,spiDataCountRespDTO);
        BeanUtils.copyProperties(levelRectifiedCountByTime,spiDataCountRespDTO);
        BeanUtils.copyProperties(spiDataCountReqDTO,spiDataCountRespDTO);
        spiDataCountRespDTO.setDep(depInfoByDepId.getDeptName());
        parameterVerificationResult.setData(spiDataCountRespDTO);
        return parameterVerificationResult;
    }
 
 
    /**
     * @description 参数校验
     */
    private ResultVO<SPIDataCountRespDTO> parameterVerification(SPIDataCountReqDTO spiDataCountReqDTO){
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
        if (ObjectUtils.isEmpty(spiDataCountReqDTO)){
            resultVO.setCode("400");
            resultVO.setMsg("参数为空,查询失败");
            return resultVO;
        }
 
        if (ObjectUtils.isEmpty(spiDataCountReqDTO.getMonth())){
            if (ObjectUtils.isEmpty(spiDataCountReqDTO.getYear())){
                resultVO.setCode("400");
                resultVO.setMsg("查询年份不能为空");
                return resultVO;
            }
        }else {
            if (ObjectUtils.isEmpty(spiDataCountReqDTO.getYear())){
                resultVO.setCode("400");
                resultVO.setMsg("查询年份不能为空");
                return resultVO;
            }
        }
 
        if (ObjectUtils.isNotEmpty(spiDataCountReqDTO.getDepId())){
            DoublePreventDept depInfoByDepId = doublePreventionDeptService.getDepInfoByDepId(spiDataCountReqDTO.getDepId());
            if (depInfoByDepId == null){
                resultVO.setCode("400");
                resultVO.setMsg("查询的部门不存在");
                return resultVO;
            }
        }
        return resultVO;
    }
 
 
 
    /**
     * 数据统计-统计时间处理:根据传参判断是否为统计月还是统计年,月份为空表明是统计年 月份不为空表明是统计月
     */
    private DataCountStartAndEndTimeParams timeInterval(Integer month, Integer year){
 
        //确定结束时间
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);
        Date startTime = null;
        Date endTime = null;
        if (month == null){
            //说明统计的是当前年份
            if (nowYear == year){
                endTime =calendar.getTime();
 
                calendar.clear();
                calendar.set(Calendar.YEAR,nowYear);
                startTime = calendar.getTime();
 
            }else {
                calendar.clear();
                calendar.set(Calendar.YEAR,year);
                startTime = calendar.getTime();
 
                calendar.set(Calendar.YEAR,year+1);
                calendar.set(Calendar.DAY_OF_YEAR,-1);
                calendar.set(Calendar.SECOND,59);
                calendar.set(Calendar.MINUTE,59);
                calendar.set(Calendar.HOUR_OF_DAY,23);
                endTime = calendar.getTime();
 
            }
        //统计的是指定月份
        }else{
            calendar.set(year, month - 1, 1);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            startTime = calendar.getTime();
 
            calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DATE)); //指定月份的最后一天
            calendar.set(Calendar.SECOND,59);
            calendar.set(Calendar.MINUTE,59);
            calendar.set(Calendar.HOUR_OF_DAY,23);
            endTime = calendar.getTime();
 
        }
        DataCountStartAndEndTimeParams params = new DataCountStartAndEndTimeParams();
        params.setStartTime(startTime);
        params.setEndTime(endTime);
        return params;
    }
}