EnableRouteVersionHandler.java
5.08 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
package com.bsth.data.line_version;
import com.bsth.data.line_version.dto.EvSectionRoute;
import com.bsth.data.line_version.dto.EvStationRoute;
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.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.util.ArrayList;
import java.util.List;
/**
* 启用线路路由版本
* Created by panzhao on 2017/12/28.
*/
@Component
public class EnableRouteVersionHandler {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
NamedParameterJdbcTemplate namedParameterJdbcTemplate;
Logger logger = LoggerFactory.getLogger(this.getClass());
public void enable(String lineCode, int version){
//编程式事务
DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = tran.getTransaction(def);
try{
enableStation(lineCode, version);
enableRoad(lineCode, version);
//将当前版本变为历史版本
String sql = "update bsth_c_line_versions set status=0, end_date=SYSDATE(),update_date=SYSDATE() where line_code=" + lineCode + " and status=1";
jdbcTemplate.update(sql);
//将待更新版本变为启用版本
sql = "update bsth_c_line_versions set status=1,update_date=SYSDATE() where line_code=" + lineCode + " and versions="+version;
jdbcTemplate.update(sql);
tran.commit(status);
}catch (Exception e){
tran.rollback(status);
logger.error("", e);
}
}
/**
* 启用新版本站点
* @param lineCode
* @param version
*/
public void enableStation(String lineCode, int version){
//删除当前站点路由表数据
String sql = "delete from bsth_c_stationroute where line_code=" + lineCode;
jdbcTemplate.update(sql);
//将新版本数据插入当前表
sql = "select * from bsth_c_ls_stationroute where line_code=" + lineCode + " and versions=" + version;
List<EvStationRoute> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(EvStationRoute.class));
List<BeanPropertySqlParameterSource> sourceList = new ArrayList<>();
for(EvStationRoute s : list){
sourceList.add(new BeanPropertySqlParameterSource(s));
}
sql = "insert into bsth_c_stationroute(id,line,station,station_name,station_route_code,line_code,station_code,station_mark,out_station_nmber,directions,distances,to_time,first_time,end_time,descriptions,destroy,versions,create_date,update_date) " +
" values(:id,:line,:station,:stationName,:stationRouteCode,:lineCode,:stationCode,:stationMark,:outStationNmber,:directions,:distances,:toTime,:firstTime,:endTime,:descriptions,:destroy,:versions,SYSDATE(),SYSDATE())";
BeanPropertySqlParameterSource[] beanSources = sourceList.toArray(new BeanPropertySqlParameterSource[sourceList.size()]);
namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
}
/**
* 启用新版本路段
* @param lineCode
* @param version
*/
public void enableRoad(String lineCode, int version){
//删除当前路段路由表数据
String sql = "delete from bsth_c_sectionroute where line_code=" + lineCode;
jdbcTemplate.update(sql);
//将新版本数据插入当前表
sql = "select * from bsth_c_ls_sectionroute where line_code=" + lineCode + " and versions=" + version;
List<EvSectionRoute> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(EvSectionRoute.class));
List<BeanPropertySqlParameterSource> sourceList = new ArrayList<>();
for(EvSectionRoute s : list){
sourceList.add(new BeanPropertySqlParameterSource(s));
}
sql = "insert into bsth_c_sectionroute(id,line_code,section_code,sectionroute_code,directions,line,section,descriptions,create_by,create_date,update_by,update_date,versions,destroy,is_roade_speed) " +
" values(:id,:lineCode,:sectionCode,:sectionrouteCode,:directions,:line,:section,:descriptions,:createBy,SYSDATE(),:updateBy,SYSDATE(),:versions,:destroy,:isRoadeSpeed)";
BeanPropertySqlParameterSource[] beanSources = sourceList.toArray(new BeanPropertySqlParameterSource[sourceList.size()]);
namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
}
}