AsyncTasks.java
7.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
package cn.com.glsx.stdinterface.modules.multithread;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
@Slf4j
@Service
public class AsyncTasks {
/**
* 异步填充设备激活经纬度和激活地址信息
*
* @param snList
* @param countDownLatch
* @param remoteGpsBsService
*/
@Async
public void saveActiveInfo(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
if (CollectionUtils.isEmpty(snList)) {
log.info("countDownLatch查询数据为空关闭资源");
countDownLatch.countDown();
return;
}
try {
log.info("saveActiveInfo 开始分批同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfo lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfo异常信息===" + e.getMessage(), e);
} finally {
countDownLatch.countDown();
log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
}
}
/**
* 异步填充设备激活经纬度和激活地址信息
*
* @param snList
* @param countDownLatch
* @param remoteGpsBsService
*/
@Async
public void saveActiveInfoByTask(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
if (CollectionUtils.isEmpty(snList)) {
log.info("countDownLatch查询数据为空关闭资源");
countDownLatch.countDown();
return;
}
try {
log.info("saveActiveInfoByTask 开始分批同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfoByTask lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info by task gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info by task bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfoByTask异常信息===" + e.getMessage(), e);
} finally {
countDownLatch.countDown();
log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
}
}
}