Commit 59e72a4b0b8b1fa9a517ba8de9e2bd91be47a001

Authored by 潘钊
1 parent 2237c740

update...

src/main/java/com/bsth/data/car_out_info/CarOutInfoHandler.java
1 1 package com.bsth.data.car_out_info;
2 2  
  3 +import com.bsth.Application;
3 4 import com.bsth.data.BasicData;
4 5 import com.bsth.data.schedule.DayOfSchedule;
5 6 import com.bsth.data.schedule.ScheduleComparator;
... ... @@ -12,13 +13,18 @@ import org.springframework.beans.factory.annotation.Autowired;
12 13 import org.springframework.boot.CommandLineRunner;
13 14 import org.springframework.jdbc.core.BatchPreparedStatementSetter;
14 15 import org.springframework.jdbc.core.JdbcTemplate;
  16 +import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15 17 import org.springframework.stereotype.Component;
16 18 import org.springframework.stereotype.Service;
  19 +import org.springframework.transaction.TransactionDefinition;
  20 +import org.springframework.transaction.TransactionStatus;
17 21 import org.springframework.transaction.annotation.Transactional;
  22 +import org.springframework.transaction.support.DefaultTransactionDefinition;
18 23  
19 24 import java.sql.PreparedStatement;
20 25 import java.sql.SQLException;
21 26 import java.util.*;
  27 +import java.util.concurrent.TimeUnit;
22 28  
23 29 /**
24 30 * 发车信息表处理程序
... ... @@ -60,7 +66,7 @@ public class CarOutInfoHandler implements CommandLineRunner, CarOutInfo {
60 66 Set<String> ks = lpScheduleMap.keySet();
61 67 String prefix = lineCode + "_";
62 68 for (String k : ks) {
63   - if (k.indexOf(prefix) != -1) {
  69 + if (k.startsWith(prefix)) {
64 70 list.addAll(lpScheduleMap.get(k));
65 71 }
66 72 }
... ... @@ -90,36 +96,48 @@ public class CarOutInfoHandler implements CommandLineRunner, CarOutInfo {
90 96 downArray = nexts(downs);
91 97  
92 98 final List<ScheduleRealInfo> pstArray = mergeArray(upArray, downArray);
93   - //删除
94   - jdbcTemplate.update("delete from bsth_t_clfcxxb where line_code=?", lineCode);
95   - //重新写入
96   - jdbcTemplate.batchUpdate("insert into bsth_t_clfcxxb(rq, line_code, line_name, lp_name, lp_sn, dfsj, nbbm, cph, bc_type, end_station_name, updown, jGh, jName, remarks, sn)" +
97   - " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
98   - @Override
99   - public void setValues(PreparedStatement ps, int i) throws SQLException {
100   - ScheduleRealInfo sch = pstArray.get(i);
101   - ps.setString(1, sch.getScheduleDateStr());
102   - ps.setString(2, sch.getXlBm());
103   - ps.setString(3, sch.getXlName());
104   - ps.setString(4, /*sch.getLpName()*/"0");
105   - ps.setInt(5, sch.getFcno()==null?-1:sch.getFcno());
106   - ps.setString(6, sch.getDfsj().replace(":", ""));
107   - ps.setString(7, sch.getClZbh().replace("-", ""));
108   - ps.setString(8, BasicData.nbbmCompanyPlateMap.get(sch.getClZbh()));
109   - ps.setString(9, bcTypeMap.containsKey(sch.getBcType()) ? bcTypeMap.get(sch.getBcType()) : sch.getBcType());
110   - ps.setString(10, sch.getZdzName());
111   - ps.setInt(11, Integer.parseInt(sch.getXlDir()));
112   - ps.setString(12, sch.getjGh());
113   - ps.setString(13, sch.getjName());
114   - ps.setString(14, sch.getRemarks());
115   - ps.setInt(15, sch.getFcpSn());
116   - }
  99 + //编程式事务
  100 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  101 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  102 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  103 + TransactionStatus status = tran.getTransaction(def);
  104 +
  105 + try{
  106 + //删除
  107 + jdbcTemplate.update("delete from bsth_t_clfcxxb where line_code=?", lineCode);
  108 + //重新写入
  109 + jdbcTemplate.batchUpdate("insert into bsth_t_clfcxxb(rq, line_code, line_name, lp_name, lp_sn, dfsj, nbbm, cph, bc_type, end_station_name, updown, jGh, jName, remarks, sn)" +
  110 + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
  111 + @Override
  112 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  113 + ScheduleRealInfo sch = pstArray.get(i);
  114 + ps.setString(1, sch.getScheduleDateStr());
  115 + ps.setString(2, sch.getXlBm());
  116 + ps.setString(3, sch.getXlName());
  117 + ps.setString(4, /*sch.getLpName()*/"0");
  118 + ps.setInt(5, sch.getFcno()==null?-1:sch.getFcno());
  119 + ps.setString(6, sch.getDfsj().replace(":", ""));
  120 + ps.setString(7, sch.getClZbh().replace("-", ""));
  121 + ps.setString(8, BasicData.nbbmCompanyPlateMap.get(sch.getClZbh()));
  122 + ps.setString(9, bcTypeMap.containsKey(sch.getBcType()) ? bcTypeMap.get(sch.getBcType()) : sch.getBcType());
  123 + ps.setString(10, sch.getZdzName());
  124 + ps.setInt(11, Integer.parseInt(sch.getXlDir()));
  125 + ps.setString(12, sch.getjGh());
  126 + ps.setString(13, sch.getjName());
  127 + ps.setString(14, sch.getRemarks());
  128 + ps.setInt(15, sch.getFcpSn());
  129 + }
117 130  
118   - @Override
119   - public int getBatchSize() {
120   - return pstArray.size();
121   - }
122   - });
  131 + @Override
  132 + public int getBatchSize() {
  133 + return pstArray.size();
  134 + }
  135 + });
  136 +
  137 + tran.commit(status);
  138 + }catch (Exception e){
  139 + tran.rollback(status);
  140 + }
123 141 }
124 142  
125 143 private List<ScheduleRealInfo> mergeArray(ScheduleRealInfo[] upArray, ScheduleRealInfo[] downArray) {
... ... @@ -190,7 +208,7 @@ public class CarOutInfoHandler implements CommandLineRunner, CarOutInfo {
190 208 bcTypeMap.put("venting", "直放");
191 209 bcTypeMap.put("major", "放站");
192 210 bcTypeMap.put("ldks", "两点间空驶");
193   - //Application.mainServices.scheduleWithFixedDelay(updateInfoThread, 60, 40, TimeUnit.SECONDS);
  211 + Application.mainServices.scheduleWithFixedDelay(updateInfoThread, 60, 40, TimeUnit.SECONDS);
194 212 }
195 213  
196 214 @Component
... ...