GetRealFreeBerthsJob.java
7.78 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
package com.zteits.job.task.getfreeberths;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.cache.park.ParkFreeBerthsCacheUtil;
import com.clouds.common.utils.ApprntSignUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.zteits.clouds.api.apibase.constants.AppPublicArgsEnum;
import com.zteits.clouds.api.apibase.constants.SourceTypeEnum;
import com.zteits.clouds.api.dto.order.parkorder.param.pushfreeberths.PushFreeBerthsChangeRequest;
import com.zteits.clouds.api.dto.rocketmq.datacollection.freeberths.FreeBerthsChangeMsgVO;
import com.zteits.job.task.getfreeberths.param.GetRealFreeBerthsDO;
import com.zteits.job.dao.park.ParkFreeBerthDao;
import com.zteits.job.dao.park.ParkingLotDao;
import com.zteits.job.domain.ParkingLot;
import com.zteits.job.domain.ParkingLotExample;
import com.zteits.job.util.HttpClientTutorial;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 调用道闸接口,获取真实的空闲车位数
* 青岛,南泽厂家
* .<br/>
*
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: GetRealFreeBerthsJob
* @Description:
* @version: v1.0.0
* @author: zhaowg
* @date: 2018年5月11日 下午5:47:42
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2018年5月11日 zhaowg v1.0.0 创建
*/
@Component
public class GetRealFreeBerthsJob{
private static final Logger logger = LoggerFactory.getLogger(GetRealFreeBerthsJob.class);
/** 私钥key. */
public static final String app_id = "test8f5373ca4866aec2f8e9d93632323";
/** 私钥value. */
public static final String secret_key = "test55dfdf27b13840c2a4af6wewtrer";
@Value("${dzurl.qingdao}")
private String qingdaoUrl;
@Value("${dzurl.nanze}")
private String nanzeUrl;
@Autowired
private ParkingLotDao parkingLotDao;
@Autowired
private ParkFreeBerthDao parkFreeBerthDao;
/**
* 间隔10秒执行一次
*
* 2018年5月4日 zhaowg
*/
@Scheduled(fixedDelay=10000)
public void execute(){
logger.debug("开始查询是南泽和青岛的停车场");
ParkingLotExample example = new ParkingLotExample();
List<Integer> sourceTypes = new ArrayList<>();
sourceTypes.add(4);//南泽
sourceTypes.add(6);//青岛
example.createCriteria()
.andStateEqualTo(1)
.andSourceTypeIn(sourceTypes);
List<ParkingLot> parkingLots = parkingLotDao.selectByExample(example);
//获取空闲车位
this.updateParkFreeBerths(parkingLots);
}
private void updateParkFreeBerths(List<ParkingLot> parkingLots) {
if(CollectionUtil.isEmpty(parkingLots)){
return;
}
List<FreeBerthsChangeMsgVO> berthsChangeMsgVOs = new ArrayList<>();
for (ParkingLot parkingLotDTO : parkingLots) {
String plNo = parkingLotDTO.getPlNo();
GetRealFreeBerthsDO getRealFreeBerthsDO = queryFreeBerths(parkingLotDTO.getPlNo(),parkingLotDTO.getSourceType());
if(getRealFreeBerthsDO == null){
continue;
}
//调用接口返回的真实空闲车位数,可能为负数或者超过总车位数
Integer realFreeBerths = StringUtils.isBlank(getRealFreeBerthsDO.getFreeParkingSpace())?0:Integer.valueOf(getRealFreeBerthsDO.getFreeParkingSpace());
//修改后的空闲车位数,最小0,最大不超过总车位数
Integer freeBerths = realFreeBerths;
//判断数据有效性
if(freeBerths<0){
logger.debug("返回的空闲车位数小于0,修改为0");
freeBerths = 0;
}else if(freeBerths>parkingLotDTO.getPlBerthNum()){
logger.debug("返回的空闲车位数大于总车位数"+parkingLotDTO.getPlBerthNum()+",修改为总车位数");
freeBerths = parkingLotDTO.getPlBerthNum();
}
//判断缓存中空闲车位数是否发生变化了
FreeBerthsChangeMsgVO freeBerthsChangeMsgVO = ParkFreeBerthsCacheUtil.getFreeBerthsObjectByPlNo(plNo);
if(freeBerthsChangeMsgVO==null){
continue;
}
if(freeBerthsChangeMsgVO.getRealFreeBerths() != realFreeBerths){
logger.debug(plNo+"-原来真实空闲车位数:"+freeBerthsChangeMsgVO.getRealFreeBerths()+",新真实空闲车位数:"+realFreeBerths+",新空闲车位数:"+freeBerths);
//更新缓存
freeBerthsChangeMsgVO = ParkFreeBerthsCacheUtil.setFreeBerthsByPlNo(plNo, freeBerths,realFreeBerths);
}
berthsChangeMsgVOs.add(freeBerthsChangeMsgVO);
if(SourceTypeEnum.NAN_ZHE.getValue().equals(parkingLotDTO.getSourceType())){
//南泽的总车位数不正确
continue;
}
//判断总车位数是否一致
Integer plBerthNum = StringUtils.isBlank(getRealFreeBerthsDO.getTotalParkingSpace())?0:Integer.valueOf(getRealFreeBerthsDO.getTotalParkingSpace());
if(!plBerthNum.equals(parkingLotDTO.getPlBerthNum())
&& 0!=plBerthNum.intValue()){
logger.debug(parkingLotDTO.getPlName()+",总车位数:"+parkingLotDTO.getPlBerthNum()+"与接口返回的不一致,更新总车位数为"+plBerthNum);
parkingLotDao.updateTotalBerths(plNo,plBerthNum);
}
}
if(CollectionUtil.isEmpty(berthsChangeMsgVOs)){
return;
}
PushFreeBerthsChangeRequest freeBerthsChangeMsgVO = new PushFreeBerthsChangeRequest();
freeBerthsChangeMsgVO.setBerthsChangeMsgVOs(berthsChangeMsgVOs );
freeBerthsChangeMsgVO.setSysCode("XXL-JOB");
//更新空闲车位数
parkFreeBerthDao.updateFreeBerthsByPlNo(freeBerthsChangeMsgVO);
//推送诱导数据
parkFreeBerthDao.pushFreeBerthsToScreen(freeBerthsChangeMsgVO);
}
/**
* 调用第三方接口查询空闲车位数
* @throws IOException
* 2018年5月11日 zhaowg
*/
private GetRealFreeBerthsDO queryFreeBerths(String plNo,Integer sourceType){
if(StringUtils.isBlank(qingdaoUrl) || StringUtils.isBlank(nanzeUrl)){
logger.error("道闸平台地址为空,不调用空闲车位接口");
return null;
}
String sourceTypeName = "";
String url1 = "";
if(SourceTypeEnum.QING_DAO.getValue().equals(sourceType)){
url1 = qingdaoUrl+"/query/queryParkInfo";
sourceTypeName = "青岛";
}else if(SourceTypeEnum.NAN_ZHE.getValue().equals(sourceType)){
sourceTypeName = "南泽";
url1 = nanzeUrl + "/query/queryParkInfo";
}
Map<String, Object> returnMap = new HashMap<String, Object>();
returnMap.put(AppPublicArgsEnum.APP_ID.getCode(), app_id);
returnMap.put(AppPublicArgsEnum.APP_SALT.getCode(), secret_key);
returnMap.put(AppPublicArgsEnum.APP_SIGN_TYPE.getCode(), "md5");
returnMap.put("parkCode", plNo);
try {
String appSign = ApprntSignUtil.signRequest(returnMap, secret_key, "md5");
returnMap.put("sign", appSign);
String rs = "";
String req = JSON.toJSONString(returnMap,true);
logger.debug(sourceTypeName+" 获取空闲车位请求参数:"+req+"------请求URL:"+url1);
rs = HttpClientTutorial.httpPostRequest(url1, req);
logger.debug(sourceTypeName+"获取空闲车位响应信息"+rs);
//返回示例:{"code":"0","message":"成功","data":{"parkCode":"P32118200D","totalParkingSpace":"55","freeParkingSpace":"18"}}
JSONObject resultJson = JSON.parseObject(rs);
if("0".equals(resultJson.getString("code"))){
//成功
GetRealFreeBerthsDO berthsDO = JSON.parseObject(resultJson.getString("data"),GetRealFreeBerthsDO.class);
return berthsDO;
}else{
logger.warn(sourceTypeName+"获取空闲车位请求参数:"+req+"------请求URL:"+url1);
logger.error(sourceTypeName+"获取空闲车位接口返回失败:"+rs);
}
} catch (Exception e) {
logger.error(sourceTypeName+"获取空闲车位接口失败",e);
}
return null;
}
}