VehicleService.java
11 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
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.devicemonitor.api.dto.response.DeviceDTO;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.devicemonitor.api.enums.GeoLocationEnum;
import cn.com.glsx.devicemonitor.api.feign.DeviceFeignService;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.common.enums.ResultEnum;
import cn.com.glsx.stdinterface.common.exception.StdInterfaceException;
import cn.com.glsx.stdinterface.dto.DevicesComposeVerifyBO;
import cn.com.glsx.stdinterface.dto.VehicleDto;
import cn.com.glsx.stdinterface.modules.converter.VehicleConverter;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.entity.VehicleDeviceOperator;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.model.*;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.com.glsx.stdinterface.modules.util.DeviceLocationUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 车辆服务
*/
@Slf4j
@Service
public class VehicleService {
@Autowired
private VehicleMapper vehicleMapper;
@Autowired
private VehicleDeviceMapper vehicleDeviceMapper;
@Autowired
private WorkOrderService workOrderService;
@Autowired
private RemoteGpsBsService remoteGpsBsService;
@Autowired
private DeviceFeignService deviceFeignService;
public Vehicle getVehicleInfoBySn(String sn) {
Vehicle vehicle = vehicleMapper.getVehicleBySn(sn);
return vehicle;
}
public List<VehicleDeviceOperator> getVehicleListBySn(Collection<String> sn) {
return vehicleMapper.listVehicleBySn(sn);
}
/**
* key:sn value:VehicleDeviceOperator
*/
public Map<String, VehicleDeviceOperator> getVehicleDeviceOperatorMap(List<String> snList) {
List<VehicleDeviceOperator> vehicleList = getVehicleListBySn(snList);
if (CollectionUtils.isEmpty(vehicleList)) {
return Maps.newHashMap();
}
return vehicleList.stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(VehicleDeviceOperator::getSn, Function.identity(), (o1, o2) -> o2));
}
public Long getVdId(String sn) {
VehicleDevice vehicleDevice = vehicleDeviceMapper.selectVehicleDevcieBySn(sn);
return Optional.ofNullable(vehicleDevice)
.map(VehicleDevice::getId)
.orElse(null);
}
/**
* key:sn value:vdId
*/
public Map<String, Long> getSnVdIdMap(Collection<String> sn) {
List<VehicleDevice> vehicleDevice = vehicleDeviceMapper.selectBindingDevices(sn);
if (CollectionUtils.isEmpty(vehicleDevice)) {
return Maps.newHashMap();
}
return vehicleDevice.stream()
.collect(Collectors.toMap(VehicleDevice::getSn, VehicleDevice::getId, (o1, o2) -> o2));
}
public Vehicle getVehicleInfoByWorkOrderIdAndVin(Long workOrderId, String vin) {
if (workOrderId == null || StringUtils.isBlank(vin)) {
return null;
}
return vehicleMapper.selectByWorkOrderIdAndVin(workOrderId, vin);
}
public boolean verifyDevicesCompose(DevicesComposeVerifyBO verifyBO) {
//获取工单id
if (verifyBO.getWorkOrderId() == null) {
Vehicle vehicle = vehicleMapper.selectByPrimaryKey(verifyBO.getVehicleId());
//没有工单,无需判断设备组合,直接返回验证通过
if (vehicle.getWorkOrderId() == null) {
return true;
} else {
verifyBO.setWorkOrderId(vehicle.getWorkOrderId());
}
}
//验证设备存在性
workOrderService.checkDevicesExist(verifyBO.getDevices());
//验证设备组合正确性
workOrderService.checkDevicesCompose(verifyBO.getWorkOrderId(), verifyBO.getDevices());
return true;
}
public List<VehicleDto> getVehicleByVinAndName(String vin, String name) {
List<Vehicle> vehicleList = vehicleMapper.getVehicleByVinAndName(vin, name);
return VehicleConverter.INSTANCE.syncEntity2DtoList(vehicleList);
}
/**
* 根据设备号批量查询车架号信息
*
* @param snList 设备号集合
* @return sn和vin的映射列表
*/
public List<VehicleSnVinDTO> getVinBySnList(List<String> snList) {
if (CollectionUtils.isEmpty(snList)) {
return Lists.newArrayList();
}
List<VehicleSnVinDTO> list = vehicleMapper.getVinBySnList(snList);
if (CollectionUtils.isEmpty(list)) {
return Lists.newArrayList();
}
return list;
}
/**
* 查询襄本设备定位信息
*
* @param verifyBO 襄本请求体
* @return
*/
public XBAddrVerifyDTO addrVerifyByXB(XBAddrVerifyBo verifyBO) {
if (StringUtils.isEmpty(verifyBO.getSn())) {
throw new StdInterfaceException(ResultEnum.PARAM_ERROR);
}
XBAddrVerifyDTO verifyDTO = vehicleMapper.getVehicleBySnAndTenantName(verifyBO.getSn(), verifyBO.getOrderSource());
if (Objects.isNull(verifyDTO)) {
log.warn("没有查询到该设备相关信息==" + JSONUtil.toJsonStr(verifyBO));
throw new StdInterfaceException(ResultEnum.DEVICE_NOT_EXIST);
}
List<XBDeviceAddressItem> list = Lists.newArrayList();
//查位置点
Map<String, GpsBsData> gpsBsDataMap = remoteGpsBsService.getLastGpsBsBatchBySns(Lists.newArrayList(verifyBO.getSn()));
XBDeviceAddressItem addressItem = new XBDeviceAddressItem();
R<DeviceDTO> deviceDTOR = deviceFeignService.getDeviceBySn(verifyBO.getSn());
DeviceDTO deviceDTO = deviceDTOR.getData(DeviceDTO.class);
addressItem.setSn(verifyBO.getSn());
addressItem.setSim(deviceDTO.getSim());
addressItem.setDeviceType((deviceDTO.getSource() != null && deviceDTO.getSource() == Constants.DeviceSource.WIRED) ? "有线设备" : "无线设备");
GpsBsData gpsBsData = gpsBsDataMap.get(verifyBO.getSn());
if (gpsBsData != null) {
String speedValue = "";
// 获取 Gps 定位数据
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
if (gpsLocation != null) {
addressItem.setGpsLat(gpsLocation.getLat());
addressItem.setGpsLng(gpsLocation.getLng());
addressItem.setGpsPosition(gpsLocation.getAddress());
addressItem.setGpsTime(gpsLocation.getDateTime());
speedValue = gpsLocation.getSpeed();
}
// 获取基站定位数据
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (bsLocation != null) {
addressItem.setBsLat(bsLocation.getBlat());
addressItem.setBsLng(bsLocation.getBlng());
addressItem.setBsPosition(bsLocation.getAddress());
addressItem.setBsTime(bsLocation.getDateTime());
}
// 如果基站时间为空,则将GPS时间填充到基站时间中
if (StringUtils.isBlank(addressItem.getBsTime())) {
addressItem.setBsTime(addressItem.getGpsTime());
}
// 设置设备位置有效状态
addressItem.setType(DeviceLocationUtil.resolutionGpsType(addressItem.getGpsTime(), addressItem.getBsTime(), deviceDTO.getSource()));
// 设置设备位置行驶状态
addressItem.setStatus(DeviceLocationUtil.resolutionDeviceOnlineStatus(addressItem, deviceDTO.getSource(), speedValue));
list.add(addressItem);
verifyDTO.setList(list);
}
return verifyDTO;
}
/**
* 批量查询设备GPS设备定位数据
*
* @param verifyBatchBo 查询gps设备参数
* @return
*/
public List<AddrVerifyBatchDTO> addrVerifyBatch(AddrVerifyBatchBo verifyBatchBo) {
StopWatch sw = new StopWatch();
sw.start("checkSnBySnListAndTenantName");
if (CollectionUtils.isEmpty(verifyBatchBo.getSnList())) {
throw new StdInterfaceException(ResultEnum.PARAM_ERROR);
}
List<String> snList = vehicleMapper.checkSnBySnListAndTenantName(verifyBatchBo.getSnList(), verifyBatchBo.getOrderSource());
if (CollectionUtils.isEmpty(snList)) {
log.warn("没有查询到该设备相关信息==" + JSONUtil.toJsonStr(verifyBatchBo));
throw new StdInterfaceException(ResultEnum.DEVICE_ARR_NOT_EXIST);
}
sw.stop();
log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");
List<AddrVerifyBatchDTO> list = Lists.newArrayList();
//查位置点
sw.start("getLastGpsBsSimpleBatchBySns");
Map<String, GpsBsData> gpsBsDataMap = remoteGpsBsService.getLastGpsBsSimpleBatchBySns(snList);
sw.stop();
log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");
sw.start("merge");
snList.forEach(sn -> {
AddrVerifyBatchDTO addressItem = new AddrVerifyBatchDTO();
GpsBsData gpsBsData = gpsBsDataMap.get(sn);
if (gpsBsData != null) {
addressItem.setSn(sn);
// 获取 Gps 定位数据
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
if (gpsLocation != null) {
addressItem.setLat(gpsLocation.getLat());
addressItem.setLng(gpsLocation.getLng());
addressItem.setTime(gpsLocation.getDateTime());
addressItem.setLocationType(GeoLocationEnum.GPS.getLocationType());
}
// 获取基站定位数据
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (bsLocation != null) {
addressItem.setLat(bsLocation.getBlat());
addressItem.setLng(bsLocation.getBlng());
addressItem.setTime(bsLocation.getDateTime());
addressItem.setLocationType(GeoLocationEnum.BS.getLocationType());
}
list.add(addressItem);
}
});
sw.stop();
log.info(sw.getLastTaskName() + "耗时:" + sw.getLastTaskTimeMillis() + "ms");
log.info("addrVerifyBatch总耗时:" + sw.getTotalTimeSeconds() + "s");
return list;
}
}