ParkLotStatusStatisticJob.java
10.8 KB
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
package com.zteits.park.job;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.cache.park.ParkingLotCacheUtil;
import com.clouds.common.utils.ExceptionUtil;
import com.clouds.common.utils.ResultUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zteits.clouds.api.apibase.bean.BizResult;
import com.zteits.clouds.api.apibase.constants.ErrorType;
import com.zteits.clouds.api.apibase.exception.BizException;
import com.zteits.clouds.api.dto.order.parkorder.param.ParkingLotUseStatisticOfParkOrderReq;
import com.zteits.clouds.api.dto.park.dto.ParkingLotDTO;
import com.zteits.clouds.api.dto.park.dto.ParkingLotUseStatisticDTO;
import com.zteits.clouds.api.service.order.parkorder.query.ParkingRunningStatisticPakOrderQueryService;
import com.zteits.park.dao.ParkingLotUseStatisticDao;
import com.zteits.park.domain.ParkingLotUseStatistic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
/**
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: com.zteits.park.job
* @Description: 停车场状态统计 统计进场数量、出场数量、空置率、周转率、
* @version: v1.0.0
* @author: atao
* @date: 2017/9/20 下午2:36
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2017/9/20 atao v1.0.0 创建
*/
@Component
public class ParkLotStatusStatisticJob{
private static final Logger logger = LoggerFactory.getLogger(ParkLotStatusStatisticJob.class);
private String errMsg = null;
@Autowired
private ParkingLotUseStatisticDao parkingLotUseStatisticDao;
@Autowired
private ParkingRunningStatisticPakOrderQueryService parkingRunningStatisticPakOrderQueryService;
@XxlJob("parkLotStatusStatisticJob")
public ReturnT<String> execute(String... params) throws Exception {
String param = XxlJobHelper.getJobParam();
if (StringUtils.isNotEmpty(param) && params == null) {
params = param.split(",");
}
ReturnT<String> returnT = new ReturnT<>("成功");
boolean flag = false;
if (null != params && params.length > 0) {
Integer type = Integer.valueOf(params[0]);
//states:停车场状态:1-使用中,2-演示停车场
List<Integer> states = new ArrayList<>();
//如果第二个参数为空
if (params.length < 2) {
//停车场状态1-使用中
states.add(1);
} else if (StringUtils.isEmpty(params[1])) {
//停车场状态1-使用中
states.add(1);
} else {
String statesStr = params[1];
String state[] = statesStr.split("@");
if (state == null || state.length < 1) {
//停车场状态1-使用中
states.add(1);
} else {
for (String obj : state) {
states.add(Integer.valueOf(obj.toString()));
}
}
}
try {
flag = startStatisticData(type, states);
if (!flag) {
returnT.setCode(ReturnT.FAIL_CODE);
returnT.setMsg(errMsg);
}
} catch (Exception e) {
e.printStackTrace();
returnT.setCode(ReturnT.FAIL_CODE);
returnT.setMsg("捕获异常");
returnT.setContent(ExceptionUtil.getTrace(e));
}
}
return returnT;
}
/**
* 开始统计 数据
*
* @param type 1-20分钟; 2-1小时; 3-1天;
*/
private boolean startStatisticData(final Integer type, final List<Integer> states) throws Exception {
if (type == 1) {
//统计前20分钟的数据
//获取要统计的时间段
Calendar begin = Calendar.getInstance();
Calendar end = Calendar.getInstance();
int curMinute = begin.get(Calendar.MINUTE);
if (curMinute >= 0 && curMinute < 20) {
//取上一小时的40-当前时间的0分钟时间段
begin.add(Calendar.HOUR_OF_DAY, -1);
begin.set(Calendar.MINUTE, 40);
end.set(Calendar.MINUTE, 0);
} else if (curMinute >= 20 && curMinute < 40) {
//取当前小时的0-20分钟时间段
begin.set(Calendar.MINUTE, 0);
end.set(Calendar.MINUTE, 20);
} else if (curMinute >= 40 && curMinute <= 59) {
//取当前时间的20-40分钟时间段
begin.set(Calendar.MINUTE, 20);
end.set(Calendar.MINUTE, 40);
}
begin.set(Calendar.SECOND, 0);
end.set(Calendar.SECOND, 0);
//parkingLotUseStatisticDao.statistic20MinData(begin.getTime(), end.getTime(),states);
//查询-分页插入每页100条
parkingLotUseStatisticDao.statistic20MinDataForSelectByLCPark(begin.getTime(), end.getTime(), states);
// parkingLotUseStatisticDao.statistic20MinDataForSelectByFBPark(begin.getTime(), end.getTime(), states);
//从订单中查询封闭停车场的数据
statistic20MinDataForSelectByFBPark(begin.getTime(), end.getTime(), states);
} else if (type == 2) {
//统计前1小时数据
//获取要统计的时间段,统计上一个1小时
Calendar begin = Calendar.getInstance();
Calendar end = Calendar.getInstance();
begin.add(Calendar.HOUR_OF_DAY, -1);
begin.set(Calendar.MINUTE, 0);
end.set(Calendar.MINUTE, 0);
begin.set(Calendar.SECOND, 0);
end.set(Calendar.SECOND, 0);
parkingLotUseStatisticDao.static1HourAnd1DayData(begin.getTime(), end.getTime(), type);
} else if (type == 3) {
//统计前1天的数据
Calendar begin = Calendar.getInstance();
Calendar end = Calendar.getInstance();
begin.add(Calendar.DAY_OF_YEAR, -1);
begin.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.HOUR_OF_DAY, 0);
begin.set(Calendar.MINUTE, 0);
end.set(Calendar.MINUTE, 0);
begin.set(Calendar.SECOND, 0);
end.set(Calendar.SECOND, 0);
parkingLotUseStatisticDao.static1HourAnd1DayData(begin.getTime(), end.getTime(), type);
} else {
//未知的统计状态
errMsg = "未知的统计状态";
return false;
}
return true;
}
/**
* 获取封闭停车场 20分钟统计数据,从订单中获取
*
* @param
* @return
* @Author: atao
* @Date: 2018/3/14 下午8:22
*/
private void statistic20MinDataForSelectByFBPark(Date startDate, Date endDate, List<Integer> states) {
List<ParkingLotUseStatistic> parkingLotUseStatistics = parkingLotUseStatisticDao
.query20MinNewDataGroupByPlNO();
if (parkingLotUseStatistics == null) {
throw new BizException(ErrorType.BIZ_ERROR, "查询停车场业务统计数据异常");
}
parkingLotUseStatistics.stream().forEach(parkingLotUseStatistic -> {
ParkingLotDTO parkingLotDTO = ParkingLotCacheUtil.getParkLotByPlNo(parkingLotUseStatistic.getPlNo());
//初始化处理一下从统计表中整理出来的数据
parkingLotUseStatistic.setId(null);
parkingLotUseStatistic.setPlName(parkingLotDTO == null ? null : parkingLotDTO.getPlName());
parkingLotUseStatistic.setPlType(parkingLotDTO == null ? 0 : parkingLotDTO.getPlType());
parkingLotUseStatistic.setStatisticBeginTime(startDate);
parkingLotUseStatistic.setStatisticEndTime(endDate);
parkingLotUseStatistic.setTmpVehicleFlow(0);
parkingLotUseStatistic.setVehicleFlow(0);
parkingLotUseStatistic.setFixVehicleFlow(0);
parkingLotUseStatistic.setCreateTime(new Date());
parkingLotUseStatistic.setOutParkCount(0);
parkingLotUseStatistic.setTotalFreeBerths(0);
parkingLotUseStatistic.setTotalBerths(parkingLotDTO == null ? 0 : parkingLotDTO.getPlBerthNum());
System.out.println("数据处理中....");
});
System.out.println("数据为:"+ JSONObject.toJSONString(parkingLotUseStatistics));
//从订单域中查询出最新的汇总数据
ParkingLotUseStatisticOfParkOrderReq req = new ParkingLotUseStatisticOfParkOrderReq();
req.setStartDate(startDate);
req.setEndDate(endDate);
req.setSysCode("sysCode");
BizResult<List<ParkingLotUseStatisticDTO>> orerDataBizResult = parkingRunningStatisticPakOrderQueryService
.queryParkingLotUseStatisticOfParkOrder(req);
List<ParkingLotUseStatisticDTO> dtoList = ResultUtils.getBizResultData(orerDataBizResult);
Map<String, ParkingLotUseStatistic> parkingLotUseStatisticMap = parkingLotUseStatistics.stream().collect(
Collectors.toMap(ParkingLotUseStatistic::getPlNo, dto -> dto));
//遍历处理订单域的数据
dtoList.stream().forEach(dto -> {
ParkingLotDTO parkingLotDTO = ParkingLotCacheUtil.getParkLotByPlNo(dto.getPlNo());
ParkingLotUseStatistic temp = parkingLotUseStatisticMap.get(dto.getPlNo());
if (null == temp) {
temp = new ParkingLotUseStatistic();
BeanUtils.copyProperties(dto, temp);
temp.setTotalBerths(parkingLotDTO == null ? 0 : parkingLotDTO.getPlBerthNum());
parkingLotUseStatisticMap.put(temp.getPlNo(), temp);
}
if (null != dto.getFreeRatio() && null != parkingLotDTO && null != parkingLotDTO.getPlBerthNum()
&& 0 != parkingLotDTO.getPlBerthNum()) {
if (null != dto.getFreeRatio()) {
//设置空置率
temp.setFreeRatio(dto.getFreeRatio() / parkingLotDTO.getPlBerthNum());
}
if (null != dto.getTmpVehicleFlow()) {
//设置周转率
temp.setTurnoverRatio(dto.getTmpVehicleFlow() * 1.0 / parkingLotDTO.getPlBerthNum());
}
}
});
List<ParkingLotUseStatistic> saveData = new ArrayList<>(parkingLotUseStatisticMap.values());
Integer num = parkingLotUseStatisticDao.batchSave(saveData);
}
}