JourneyService.java
6.06 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
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.stdinterface.dto.DeviceJourneyDTO;
import cn.com.glsx.stdinterface.modules.entity.DeviceJourney;
import cn.com.glsx.stdinterface.modules.entity.DeviceJourneyOverview;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyMapper;
import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyOverviewMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
/**
* 新e盾|里程服务实现类
*
* @author xiangyanlin
* @since 2022-04-07 16:51:41
*/
@Slf4j
@Service
public class JourneyService {
@Resource
private DeviceJourneyMapper deviceJourneyMapper;
@Resource
private DeviceJourneyOverviewMapper deviceJourneyOverviewMapper;
@Resource
private VehicleMapper vehicleMapper;
@Resource
private VehicleService vehicleService;
/**
* 行程数据新增
*/
@Transactional(rollbackFor = Exception.class)
public void saveDeviceJourney(DeviceJourneyDTO deviceJourneyDTO) {
long t1 = System.currentTimeMillis();
if (Objects.isNull(deviceJourneyDTO)) {
return;
}
DeviceJourney journey = deviceJourneyMapper.selectBySnAndBeginTime(deviceJourneyDTO.getSn(), deviceJourneyDTO.getBeginTime());
long t2 = System.currentTimeMillis();
log.debug("重复校验时间{}", t2 - t1);
if(Objects.nonNull(journey)) {
log.warn("该设备同样的开始时间行程数据已经存在" +
"sn: " + deviceJourneyDTO.getSn() + "beginTime:" + DateUtils.formatNormal(deviceJourneyDTO.getBeginTime()));
return;
}
//从车辆信息中获取创建人信息
Vehicle vehicle = getVehicleBySn(deviceJourneyDTO.getSn());
Long createdBy = Optional.ofNullable(vehicle).map(Vehicle::getCreatedBy).orElse(null);
Long updatedBy = Optional.ofNullable(vehicle).map(Vehicle::getUpdatedBy).orElse(null);
long t3 = System.currentTimeMillis();
log.debug("获取创建人时间{}", t3 - t2);
//从关联表从获取关联id
Long vdId = vehicleService.getVdId(deviceJourneyDTO.getSn());
long t4 = System.currentTimeMillis();
log.debug("获取创建人时间{}", t4 - t3);
//行程信息入库
DeviceJourney deviceJourney = convertTo(deviceJourneyDTO, vdId, createdBy, updatedBy);
deviceJourneyMapper.insert(deviceJourney);
//如果行程数据的设备未关联车辆 不计入从次数和总信息
log.info("vdId{}", vdId);
if(Objects.isNull(vdId)) {
log.warn("该设备没有绑定车辆" + deviceJourneyDTO.getSn());
log.debug("同步里程总耗时{}", t4 - t1);
return;
}
//更新总行程次数 总里程
DeviceJourneyOverview overviewDO = buildOverviewWitheJourney(deviceJourneyDTO, vdId, createdBy, updatedBy);
DeviceJourneyOverview overview = deviceJourneyOverviewMapper.selectBySnAndVdId(deviceJourneyDTO.getSn(), vdId);
long t5 = System.currentTimeMillis();
log.debug("行程入库时间{}", t5 - t4);
if (Objects.isNull(overview)) {
//本设备的第一条里程数据
deviceJourneyOverviewMapper.insert(overviewDO);
log.debug("同步里程总耗时{}", t5 - t1);
return;
}
overview.setDistance(overview.getDistance() + overviewDO.getDistance());
overview.setTotalJourneyCount(overview.getTotalJourneyCount() + 1);
overview.setUpdatedDate(new Date());
deviceJourneyOverviewMapper.updateByPrimaryKeySelective(overview);
long t6 = System.currentTimeMillis();
log.debug("同步里程和总里程总耗时{}", t6 - t1);
}
public DeviceJourneyOverview buildOverviewWitheJourney(DeviceJourneyDTO deviceJourneyDTO, Long vnId, Long createdBy, Long updatedBy) {
DeviceJourneyOverview deviceJourneyOverview = new DeviceJourneyOverview();
deviceJourneyOverview.setSn(deviceJourneyDTO.getSn());
deviceJourneyOverview.setVdId(vnId);
//每条数据加一
deviceJourneyOverview.setTotalJourneyCount(1);
deviceJourneyOverview.setDistance(deviceJourneyDTO.getMileage());
deviceJourneyOverview.setCreatedBy(deviceJourneyDTO.getCreatedBy());
deviceJourneyOverview.setUpdatedBy(deviceJourneyDTO.getUpdatedBy());
deviceJourneyOverview.setCreatedDate(new Date());
deviceJourneyOverview.setUpdatedDate(new Date());
deviceJourneyDTO.setCreatedBy(createdBy);
deviceJourneyDTO.setUpdatedBy(updatedBy);
return deviceJourneyOverview;
}
/**
* 转换为数据库对象
*/
public DeviceJourney convertTo(DeviceJourneyDTO deviceJourneyDTO, Long vnId, Long createdBy, Long updatedBy) {
DeviceJourney deviceJourney = new DeviceJourney();
deviceJourney.setSn(deviceJourneyDTO.getSn());
//获取关联id
deviceJourney.setVdId(vnId);
deviceJourney.setBeginTime(deviceJourneyDTO.getBeginTime());
deviceJourney.setEndTime(deviceJourneyDTO.getEndTime());
deviceJourney.setBeginAddress(deviceJourneyDTO.getBeginAddress());
deviceJourney.setEndAddress(deviceJourneyDTO.getEndAddress());
deviceJourney.setMileage(deviceJourneyDTO.getMileage());
deviceJourney.setCreatedBy(createdBy);
deviceJourney.setUpdatedBy(updatedBy);
deviceJourney.setCreatedDate(new Date());
deviceJourney.setUpdatedDate(new Date());
return deviceJourney;
}
/**
* 通过设备号获取绑定车辆的 createBy
*
* @param sn 设备号
* @return 用户Id
*/
private Vehicle getVehicleBySn(String sn) {
return vehicleMapper.getVehicleBySn(sn);
}
}