BatteryService.java
7.47 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
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.alarm.services.model.DevicePowerDto;
import cn.com.glsx.stdinterface.common.constants.DeviceConstants;
import cn.com.glsx.stdinterface.dto.DevicePowerDTO;
import cn.com.glsx.stdinterface.modules.converter.DevicePowerConverter;
import cn.com.glsx.stdinterface.modules.entity.DevicePower;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.entity.VehicleDeviceOperator;
import cn.com.glsx.stdinterface.modules.mapper.DeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.DevicePowerMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteFeignDevicePowerService;
import com.glsx.plat.common.multithread.ListSplitIterator;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 电量
* @author xiangyanlin
* @date 2022/4/12
*/
@Slf4j
@Service
public class BatteryService {
@Resource
private DevicePowerMapper devicePowerMapper;
@Resource
private VehicleMapper vehicleMapper;
@Resource
private VehicleService vehicleService;
@Resource
private DeviceMapper deviceMapper;
@Resource
private RemoteFeignDevicePowerService remoteFeignDevicePowerService;
/**
* 保存电量信息
*/
public void saveBatteryInfo(DevicePowerDTO devicePowerDTO) {
//从车辆信息中获取创建人信息
Vehicle vehicle = vehicleMapper.getVehicleBySn(devicePowerDTO.getSn());
Long createdBy = Optional.ofNullable(vehicle).map(Vehicle::getCreatedBy).orElse(null);
Long updatedBy = Optional.ofNullable(vehicle).map(Vehicle::getUpdatedBy).orElse(null);
if(Objects.isNull(vehicle)) {
log.warn("该设备没有绑定车辆" + devicePowerDTO.getSn());
}
//从关联表从获取关联id
Long vdIdQuery = vehicleService.getVdId(devicePowerDTO.getSn());
Long vdId = Optional.ofNullable(vdIdQuery).orElse(0L);
DevicePower devicePower = convert(devicePowerDTO, createdBy, updatedBy, vdId);
DevicePower devicePowerOld = devicePowerMapper.selectBySn(devicePowerDTO.getSn(), vdId);
if (Objects.isNull(devicePowerOld)) {
devicePowerMapper.insert(devicePower);
return;
}
devicePower.setId(devicePowerOld.getId());
devicePowerMapper.updateByPrimaryKeySelective(devicePower);
}
public DevicePower convert(DevicePowerDTO devicePowerDTO, Long createBy, Long updateBy, Long vnId) {
DevicePower devicePower = new DevicePower();
devicePower.setSn(devicePowerDTO.getSn());
devicePower.setVdId(vnId);
devicePower.setAlarmTime(devicePowerDTO.getAlarmTime());
devicePower.setSoc(devicePowerDTO.getSoc());
devicePower.setCreatedBy(createBy);
devicePower.setUpdatedBy(updateBy);
devicePower.setCreatedDate(devicePowerDTO.getCreatedDate());
devicePower.setUpdatedDate(devicePowerDTO.getUpdatedDate());
return devicePower;
}
public DevicePower convert(DevicePowerDto devicePowerDTO, Long createBy, Long updateBy, Long vnId) {
DevicePower devicePower = new DevicePower();
devicePower.setSn(devicePowerDTO.getSn());
devicePower.setVdId(vnId);
devicePower.setAlarmTime(devicePowerDTO.getAlarmtime());
devicePower.setSoc(devicePowerDTO.getSoc());
devicePower.setCreatedBy(createBy);
devicePower.setUpdatedBy(updateBy);
devicePower.setCreatedDate(devicePowerDTO.getCreatetime());
devicePower.setUpdatedDate(devicePowerDTO.getUpdatetime());
return devicePower;
}
/**
* 迁移老表数据
*/
@Async("threadPoolTaskExecutor")
public void initDataFromOld() {
long startTime = System.currentTimeMillis();
//获取所有无源设备
List<String> snList = deviceMapper.listSnBySource(DeviceConstants.NO_SOURCE);
if (CollectionUtils.isEmpty(snList)) {
log.info("新e盾平台的无线设备数量:{}", 0);
return;
}
log.info("新e盾平台的无线设备数量:{}", snList.size());
ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(snList, 1000);
int times = 1;
while (listSplitIterator.hasNext()) {
List<String> subSnList = listSplitIterator.next();
log.info("分批同步老电量表数据--批次:{}", times);
initDataTask(subSnList);
times ++;
}
long endTime = System.currentTimeMillis();
log.info("分批同步老电量表数据完成 总耗时(毫秒):{}", endTime - startTime);
}
/**
* 组装数据并写库
*/
public void initDataTask(List<String> snList) {
List<DevicePowerDto> devicePowerDtoList = getOldDevicePowerList(snList);
if (CollectionUtils.isEmpty(devicePowerDtoList)) {
log.info("分批同步老电量表数据--数量:{}", 0);
return;
}
//从车辆里面获取创建人信息
Map<String, VehicleDeviceOperator> vehicleDeviceOperatorMap = vehicleService.getVehicleDeviceOperatorMap(snList);
//从设备车辆关联获取 关联id
Map<String, Long> snVdIdMap = vehicleService.getSnVdIdMap(snList);
//构建DO
List<DevicePower> devicePowers = devicePowerDtoList.stream()
.map(e -> convert(e,
Optional.ofNullable(vehicleDeviceOperatorMap.get(e.getSn()))
.map(VehicleDeviceOperator::getCreatedBy)
.orElse(null),
Optional.ofNullable(vehicleDeviceOperatorMap
.get(e.getSn())).map(VehicleDeviceOperator::getUpdatedBy)
.orElse(null),
snVdIdMap.get(e.getSn())))
.collect(Collectors.toList());
//写入新的数据库
log.info("分批同步老电量表数据--数量:{}", devicePowers.size());
devicePowerMapper.insertOrUpdateBatch(devicePowers);
}
private List<DevicePowerDto> getOldDevicePowerList(List<String> snList) {
//从旧警情中心获取设备电量数据
List<DevicePowerDto> devicePowerDTOList = remoteFeignDevicePowerService.getDevicePowerListBySn(snList);
if(CollectionUtils.isEmpty(devicePowerDTOList)) {
return Lists.newArrayList();
}
//去重
return new ArrayList<>(devicePowerDTOList.stream()
.collect(Collectors.toMap(DevicePowerDto::getSn, Function.identity(), (o1, o2) -> o1)).values());
}
/**
* 根据sn获取
*/
public DevicePowerDTO getDevicePowerBySn(String sn) {
if (StringUtils.isEmpty(sn)) {
return null;
}
Long vdId = vehicleService.getVdId(sn);
DevicePower devicePower = devicePowerMapper.selectBySn(sn, vdId);
if (Objects.isNull(devicePower)) {
return null;
}
return DevicePowerConverter.INSTANCE.convertTo(devicePower);
}
}