SyncData.java
12.9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.bsth.data.commonData;
import com.bsth.data.BasicData;
import com.bsth.data.commonData.entity.*;
import com.bsth.entity.*;
import com.bsth.repository.*;
import com.bsth.service.*;
import com.bsth.service.schedule.CarsService;
import com.bsth.util.db.DBUtils_jiaDingBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
@EnableTransactionManagement
public class SyncData extends Thread{
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private BusinessService businessService;
@Autowired
private BusinessRepository businessRepository;
@Autowired
private CarParkRepository carParkRepository;
@Autowired
private CarParkService carParkService;
@Autowired
private LineService lineService;
@Autowired
private LineVersionsService lineVersionsService;
@Autowired
private LineInformationRepository informationRepository;
@Autowired
private LineInformationService lineInformationService;
@Autowired
private CarsService carsService;
@Autowired
private CarsRepository carsRepository;
@Autowired
private PersonnelRepository personnelRepository;
@Autowired
private PersonnelService personnelService;
@Autowired
private StationService stationService;
@Autowired
private StationRepository stationRepository;
@Autowired
private StationRouteRepository stationRouteRepository;
@Autowired
private LsStationRouteRepository lsStationRouteRepository;
@Autowired
SectionService sectionService;
@Autowired
SectionRepository sectionRepository;
@Autowired
SectionRouteRepository sectionRouteRepository;
@Autowired
LsSectionRouteRepository lsSectionRouteRepository;
@Autowired
BasicData.BasicDataLoader basicDataLoader;
//@Scheduled(cron = "0 1 * * * ?")
//@Transactional()
public void SyncData(){
syncBusiness();
syncCarPark();
syncLine();
syncLineInformation();
syncCar();
syncPersonnel();
syncStation();
syncStationRoute();
syncSection();
syncSectionRoute();
basicDataLoader.loadAllData();
}
public void syncBusiness(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_bus_company";
List<BusCompany> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusCompany.class));
List<Business> businesses=BusCompany.convert(list);
for (Business business : businesses) {
if(businessRepository.findByBusinessCode(business.getBusinessCode()).size()>0){
businessService.update(business);
}else {
businessService.save(business);
}
}
logger.info(">>>>>>>>>>>>>公司数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>公司数据同步异常",e);
}
}
public void syncCarPark(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql2 = "select *,ST_AsText(b_park_point) b_park_point_wkt,ST_AsText(g_park_point) g_park_point_wkt from common_bus_park";
List<BusPark> list2 = jdbcTemp.query(sql2, new BeanPropertyRowMapper(BusPark.class));
List<CarPark> carParks=BusPark.convert(list2);
for (CarPark carPark : carParks) {
if(carParkRepository.selectTccInfoByCode(carPark.getParkCode()).size()>0){
carParkService.update(carPark);
}else {
carParkService.save(carPark);
}
}
logger.info(">>>>>>>>>>>>>停车场数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>停车场数据同步异常",e);
}
}
public void syncLine(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_bus_line_base";
List<BusLineBase> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusLineBase.class));
List<Line> lines=BusLineBase.convert(list);
for (Line line : lines) {
if(line.getId()==null) {
line.setId(Integer.valueOf(line.getLineCode()));
}
if(line.getId().toString().length()>6) {
logger.info(">>>>>>>>>>>>>线路编码不符合条件"+line.getId().toString());
}
if(lineService.lineCodeVerification(line.getLineCode()).equals("false")){//线路存在 修改
lineService.update(line);
}else {//新增
saveLine(line);
}
}
logger.info(">>>>>>>>>>>>>线路数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>线路数据同步异常",e);
}
}
public void saveLine(Line t){
// 添加线路版本
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date endDate = simpleDateFormat.parse("2088-08-08 00:00:00");
LineVersions lineVersions = new LineVersions();
lineVersions.setName("原始版本");
lineVersions.setLine(t);
lineVersions.setLineCode(t.getLineCode());
lineVersions.setStartDate(new java.sql.Date(new Date().getTime()));
lineVersions.setEndDate(new java.sql.Date(endDate.getTime()));// 2088-8-8 00:00:00
lineVersions.setCreateDate(new java.sql.Date(new Date().getTime()));
lineVersions.setUpdateDate(new java.sql.Date(new Date().getTime()));
lineVersions.setVersions(1);
lineVersions.setStatus(1);
// 先添加线路再添加版本
lineService.save(t);
lineVersionsService.save(lineVersions);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
public void syncLineInformation(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_bus_line_operations";
List<BusLineOperations> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusLineOperations.class));
List<LineInformation> lineInformations=BusLineOperations.convert(list);
for (LineInformation lineInformation : lineInformations) {
if(informationRepository.findLineInformationByLineCode(String.valueOf(lineInformation.getLine().getId())).size()>0){//线路存在 修改
lineInformationService.update(lineInformation);
}else {//新增
lineInformationService.save(lineInformation);
}
}
logger.info(">>>>>>>>>>>>>线路配置数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>线路配置数据同步异常",e);
}
}
public void syncCar(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_bus_veh";
List<BusVeh> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusVeh.class));
List<Cars> cars=BusVeh.convert(list);
for (Cars car : cars) {
if(carsRepository.findCarsByCode(car.getInsideCode()).size()>0){//存在
carsService.update(car);
}else {
carsService.save(car);
}
}
logger.info(">>>>>>>>>>>>>车辆数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>车辆数据同步异常",e);
}
}
public void syncPersonnel(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_bus_staff";
List<BusStaff> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStaff.class));
List<Personnel> personnels=BusStaff.convert(list);
for (Personnel personnel : personnels) {
if(personnelRepository.findPersonnelByCode(personnel.getJobCodeori()).size()>0){//存在
personnelService.update(personnel);
}else {
personnelService.save(personnel);
}
}
logger.info(">>>>>>>>>>>>>人员数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>人员数据同步异常",e);
}
}
public void syncStation(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select *,ST_AsText(center_point) center_point_wkt from common_bus_stop";
List<BusStop> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStop.class));
List<Station> stations=BusStop.convert(list);
for (Station station : stations) {
if(stationRepository.findStationByStationCode(station.getStationCode()).size()>0){//存在
stationService.update(station);
}else {
stationService.add(station);
}
}
logger.info(">>>>>>>>>>>>>站点数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>站点数据同步异常",e);
}
}
public void syncStationRoute(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select *,ST_AsText(center_point) center_point_wkt,ST_AsText(buffer_polygon) buffer_polygon_wkt from common_bus_stoplevel";
List<BusStopLevel> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(BusStopLevel.class));
List<StationRoute> stationRoutes=BusStopLevel.convert(list);
List<LsStationRoute> lsStationRoutes=BusStopLevel.convertLS(list);
if(stationRoutes.size()>0){
stationRouteRepository.deleteAll();
lsStationRouteRepository.deleteAll();
}
stationRouteRepository.saveAll(stationRoutes);
lsStationRouteRepository.saveAll(lsStationRoutes);
logger.info(">>>>>>>>>>>>>站点路由数据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>站点路由数据同步异常",e);
}
}
private void syncSection(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select *,ST_AsText(bsection_vector) bsectionVectorWkt from common_road_section";
List<RoadSection> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(RoadSection.class));
List<Section> sections=RoadSection.convert(list);
for (Section section : sections) {
if(sectionRepository.findSectionByCode(section.getSectionCode()).size()>0){
sectionService.modify(section);
}else {
sectionService.add(section);
}
}
logger.info(">>>>>>>>>>>>>路段据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>路段数据同步异常",e);
}
}
private void syncSectionRoute(){
try {
JdbcTemplate jdbcTemp = new JdbcTemplate(DBUtils_jiaDingBus.getDataSource());
String sql = "select * from common_road_sectionlevel where destroy=0";
List<RoadSectionLevel> list = jdbcTemp.query(sql, new BeanPropertyRowMapper(RoadSectionLevel.class));
List<SectionRoute> sectionRoutes=RoadSectionLevel.convert(list);
List<LsSectionRoute> lsSectionRoutes=RoadSectionLevel.convertLS(list);
if(sectionRoutes.size()>0){
sectionRouteRepository.deleteAll();
lsSectionRouteRepository.deleteAll();
}
sectionRouteRepository.saveAll(sectionRoutes);
lsSectionRouteRepository.saveAll(lsSectionRoutes);
logger.info(">>>>>>>>>>>>>路段路由据同步完成");
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>路段路由数据同步异常",e);
}
}
}