ParkingLotHeatMapHourJob.java
12.5 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
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
package com.zteits.park.job;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.cache.park.ParkingLotGeoCacheUtil;
import com.clouds.common.utils.ListPageHelper;
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.constants.LonLatTypeEnum;
import com.zteits.park.dao.InParkLogDao;
import com.zteits.park.dao.ParkingLotDao;
import com.zteits.park.dao.ParkinglotHeatmapHourDao;
import com.zteits.park.domain.InparkLogCountDO;
import com.zteits.park.domain.ParkingLotAndGeoDO;
import com.zteits.park.domain.ParkinglotHeatmapHour;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
/**
* 企业云平台-->停车热力分布图-->按照小时.<br/>
*
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: ParkingLotHeatMapHourJob.java
* @Description:
* @version: v1.0.0
* @author: wangfs
* @date: 2017年7月10日 下午5:24:36
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2017年7月10日 wangfs v1.0.0 创建
*/
@Component
public class ParkingLotHeatMapHourJob{
private static final Logger logger = LoggerFactory.getLogger(ParkingLotHeatMapHourJob.class);
@Autowired
private ParkingLotDao parkingLotDao;
@Autowired
private InParkLogDao inParkLogDao;
@Autowired
private ParkinglotHeatmapHourDao parkinglotHeatmapHourDao;
/**停车场热力分布查询范围 200米.*/
private static final String DISTANCE = "200";
/**
* 企业云平台-->停车热力分布图-->按照小时.<br/>
* 描述:1.每间隔2小时跑一次(0,2,4,6,8...24点跑job)即:0 0,2,4,6,8,10,12,14,16,18,20,22 * * * ? *。
* 2.统计2小时中200米内各个停车场进场车辆的和。
*
*/
@XxlJob("ParkingLotHeatMapHourJob")
public ReturnT<String> execute(String... params) throws Exception {
String param = XxlJobHelper.getJobParam();
if (StringUtils.isNotEmpty(param) && params == null) {
params = param.split(",");
}
ReturnT<List<ParkinglotHeatmapHour>> result = new ReturnT<List<ParkinglotHeatmapHour>>();
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.log("--begin---企业云平台-->停车热力分布图-->按照小时..."+formart.format(new Date()));
if(params == null || params.length == 0){
return new ReturnT<>(ReturnT.FAIL_CODE, "请求参数不能为空,格式:5,91,1 参数说明:第一个参数='省份Id'"
+ " ,第二个参数='地市ID', 第三个参数='停车场经纬度'" );
}
this.log("获取到的入参="+params.toString());
ParkingLotAndGeoDO parkingLotAndGeoDO = new ParkingLotAndGeoDO();
try{
parkingLotAndGeoDO.setPlProvinceId(Long.valueOf(params[0]));
parkingLotAndGeoDO.setPlCityId(Long.valueOf(params[1]));
parkingLotAndGeoDO.setGeoType(Integer.valueOf(params[2]));
/**停车热力分布图-->按小时-查询停车场基本信息及停车数量..*/
result= this.queryParkingLotHeatMapHour(parkingLotAndGeoDO);
/**通过给定的停车场编号和给定的范围,查询附近的停车场编号,将查询到的范围停车场 进场车辆.*/
this.queryParkLotNosByRadius(result);
System.out.println(JSONObject.toJSON(result.getContent()));
if(result != null && !CollectionUtils.isEmpty(result.getContent())){
SimpleDateFormat formartDate = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
Date heatmapTime = formartDate.parse(formartDate.format(new Date()));
this.log("begin删除数据,入参="+formartDate.format(new Date()));
Integer deleteCount = parkinglotHeatmapHourDao.deleteParkinglotHeatmapHourByheatmapTime(heatmapTime,parkingLotAndGeoDO.getPlCityId());
this.log("end 删除数据,结果="+deleteCount+" 条数据。");
this.log("begin保存数据");
Integer insertCount = parkinglotHeatmapHourDao.insertParkinglotHeatmapHourList(result.getContent());
this.log("end保存结果="+insertCount+" 条数据。");
}
this.log("--end---企业云平台-->停车热力分布图-->按照小时..."+formart.format(new Date()));
}catch (Exception e) {
if (e instanceof InterruptedException) {
throw e;
}
logger.warn("{}", e);
}
return ReturnT.SUCCESS;
}
/**
* 通过给定的停车场编号和给定的范围,查询附近的停车场编号,将查询到的范围停车场 进场车辆sum.<br/>
* @return
* 2017年7月10日 zhaowg
*/
private void queryParkLotNosByRadius(ReturnT<List<ParkinglotHeatmapHour>> result){
this.log("begin查询200米范围内的停车场编码...");
Map<String,List<String>> map = new HashMap<String,List<String>>();
List<String> ParkLotNosList = new ArrayList<String>();
if(result != null && !CollectionUtils.isEmpty(result.getContent())){
List<ParkinglotHeatmapHour> list = result.getContent();
for(ParkinglotHeatmapHour parkinglotHeatmapHour :list){
if(parkinglotHeatmapHour != null){
ParkLotNosList = ParkingLotGeoCacheUtil.queryParkLotNosByRadius(LonLatTypeEnum.LON_LAT_TYPE_1,Double.valueOf(parkinglotHeatmapHour.getLon().toString()),Double.valueOf(parkinglotHeatmapHour.getLat().toString()),Double.valueOf(DISTANCE));
if(!CollectionUtils.isEmpty(ParkLotNosList)){
map.put(parkinglotHeatmapHour.getPlNo(), ParkLotNosList);
}
}
}
this.log("end查询200米范围内的停车场编码...,结果="+(map != null && map.size()>0 ? map.size() :0));
//计算范围
if(map != null && map.size() >0){
Set<Entry<String, List<String>>> set = map.entrySet();
for(Iterator<Entry<String, List<String>>> iterator = set.iterator();iterator.hasNext();){
Entry<String, List<String>> entry = iterator.next();
List<ParkinglotHeatmapHour> ll = result.getContent();
for(ParkinglotHeatmapHour parkinglotHeatmapHour :ll){
if(parkinglotHeatmapHour != null && entry.getKey().equals(parkinglotHeatmapHour.getPlNo())){
this.log("停车场编码=["+entry.getKey()+"],对应的200米范围内的停车场编码="+JSONObject.toJSONString(entry.getValue()));
parkinglotHeatmapHour.setCurrentParkCount(this.sumInParkCount(entry.getValue(), ll));
}
}
}
}
}
}
/**
* 计算对应范围内多个停车场停车数量求和.<br/>
* @param list
* @param ll
* @return
*/
private Integer sumInParkCount(List<String> list,List<ParkinglotHeatmapHour> ll){
this.log("begin计算200米范围内,各个停车场进场车辆和..");
int sum = 0;
if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(ll)){
for(String plNo : list){
for(ParkinglotHeatmapHour parkinglotHeatmapHour :ll){
if(plNo.equals(parkinglotHeatmapHour.getPlNo())){
this.log(plNo+"=对应入场车辆数量="+parkinglotHeatmapHour.getCurrentParkCount());
sum += parkinglotHeatmapHour.getCurrentParkCount();
}
}
}
}
this.log("end计算200米范围内,各个停车场进场车辆和..,结果sum="+sum);
return sum;
}
/**
* 停车热力分布图-->按小时-查询停车场基本信息及停车数量.<br/>
* @param parkingLotAndGeoDO
* @return
*/
private ReturnT<List<ParkinglotHeatmapHour>> queryParkingLotHeatMapHour(ParkingLotAndGeoDO parkingLotAndGeoDO ){
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
List<String> plNos = new ArrayList<String>();
String endTime = formart.format(new Date());
List<InparkLogCountDO> inparkList = new ArrayList<InparkLogCountDO>();
List<ParkinglotHeatmapHour> list = new ArrayList<ParkinglotHeatmapHour>();
try{
this.log("begin--查询停车场基本信息list..,入参="+JSONObject.toJSONString(parkingLotAndGeoDO));
/**查询停车场及停车场经纬度信息.*/
parkingLotAndGeoDO.setLonLatType(LonLatTypeEnum.LON_LAT_TYPE_1.value());
parkingLotAndGeoDO.setGeoType(1);
list = parkingLotDao.queryParkingLotAndGeoInfo(parkingLotAndGeoDO);
this.log("end--查询基本信息list..,结果list.size()="+(list != null && list.size() >0 ? list.size() : 0));
if(!CollectionUtils.isEmpty(list)){
for(ParkinglotHeatmapHour parklot:list){
if(parklot != null){
plNos.add(parklot.getPlNo());
}
}
Date beginTime = headDate(formart.parse(endTime),-2);
/**查询进场车辆数.*/
inparkList = this.queryParklotInCarCount(beginTime, formart.parse(endTime), plNos);
}
/**合并list.*/
list = mergeList(formart.parse(endTime),inparkList,list);
}catch (Exception e) {
e.printStackTrace();
return new ReturnT<List<ParkinglotHeatmapHour>>(ReturnT.FAIL_CODE,"查询信息异常!");
}
return new ReturnT<List<ParkinglotHeatmapHour>>(list);
}
/**
* 合并list<br/>
* @param endTime
* @param inparkList
* @param list
* @return
* @throws ParseException
*/
private List<ParkinglotHeatmapHour> mergeList(Date endTime,List<InparkLogCountDO> inparkList,List<ParkinglotHeatmapHour> list) throws ParseException{
List<ParkinglotHeatmapHour> rsultList = new ArrayList<ParkinglotHeatmapHour>();
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
Calendar col = Calendar.getInstance();
col.setTime(formart.parse(formart.format(endTime)));
int heatmapTime = col.get(Calendar.HOUR_OF_DAY);
this.log("begin合并进场车辆数及停车场及停车场经纬度信息信息");
this.log("转换后的小时="+heatmapTime);
if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(inparkList)){
for(ParkinglotHeatmapHour parkinglotHeatmapHour:list){
parkinglotHeatmapHour.setHeatmapTime(endTime);
parkinglotHeatmapHour.setHeatmapHour(heatmapTime+"");
parkinglotHeatmapHour.setCreateDate(new Date());
parkinglotHeatmapHour.setRanges(Integer.valueOf(DISTANCE));
parkinglotHeatmapHour.setDataStatus(1);
parkinglotHeatmapHour.setCurrentParkCount(1);
for(InparkLogCountDO inparkLogCountDO:inparkList){
if(parkinglotHeatmapHour.getPlNo().equals(inparkLogCountDO.getPlNo())){
System.out.println("存在在停车辆,车场编码="+parkinglotHeatmapHour.getPlNo()+"="+inparkLogCountDO.getCurrentParkCount());
parkinglotHeatmapHour.setCurrentParkCount(inparkLogCountDO.getCurrentParkCount());
}
}
}
rsultList.addAll(list);
}
this.log("end合并进场车辆数及停车场及停车场经纬度信息信息,结果="+JSONObject.toJSON(rsultList));
return rsultList;
}
/**
* 查询进场车辆数.<br/>
* @param beginTime
* @param endTime
* @param plNos
* @return
*/
private List<InparkLogCountDO> queryParklotInCarCount(Date beginTime, Date endTime,List<String> plNos){
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
this.log("---begin查询进场车辆数,入参={beginTime="+formart.format(beginTime)+",endTime="+formart.format(endTime)+",plNos="+plNos+"}");
List<InparkLogCountDO> inparkList = new ArrayList<InparkLogCountDO>();
if(!CollectionUtils.isEmpty(plNos)){
ListPageHelper<String> pageInfo = new ListPageHelper<String>(plNos,500);
for(int i=1;i<=pageInfo.getTotalPages();i++){
System.out.println(JSONObject.toJSON(pageInfo.getPagedList(i)));
List<InparkLogCountDO> list = inParkLogDao.queryParklotInCarCount(beginTime, endTime, pageInfo.getPagedList(i));
if(!CollectionUtils.isEmpty(list)){
inparkList.addAll(list);
}
}
}
this.log("---end查询进场车辆数,结果 inparkList.size="+((inparkList !=null && inparkList.size() >0) ? inparkList.size() :0 ));
return inparkList;
}
/**
* 计算前2小时的时间.<br/>
* @param date
* @param hours
* @return
* @throws ParseException
*/
private static Date headDate(Date date ,int hours) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hours);
return cal.getTime();
}
private void log(String msg){
XxlJobHelper.log(msg);
logger.info(msg);
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
System.out.println(formart.format(new Date()));
Calendar col = Calendar.getInstance();
col.setTime(formart.parse(formart.format(new Date())));
System.out.println(col.get(Calendar.HOUR_OF_DAY));
System.out.println(formart.format(new Date()).substring(10, 13));
}
}