Commit 6bd273f2a6fbb194d0ad4d20c5045a9aa25c39fa

Authored by 王通
1 parent baeb60fc

1.3月31日更新

src/main/java/com/bsth/data/car_out_info/CarOutInfoHandler.java
1   -package com.bsth.data.car_out_info;
2   -
3   -import com.bsth.data.BasicData;
4   -import com.bsth.data.gpsdata_v2.cache.GeoCacheData;
5   -import com.bsth.data.gpsdata_v2.entity.StationRoute;
6   -import com.bsth.data.schedule.DayOfSchedule;
7   -import com.bsth.data.schedule.ScheduleComparator;
8   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
9   -import com.fasterxml.jackson.core.JsonProcessingException;
10   -import com.fasterxml.jackson.databind.ObjectMapper;
11   -import com.google.common.collect.ArrayListMultimap;
12   -import org.apache.commons.lang3.StringUtils;
13   -import org.slf4j.Logger;
14   -import org.slf4j.LoggerFactory;
15   -import org.springframework.beans.factory.annotation.Autowired;
16   -import org.springframework.jdbc.core.BatchPreparedStatementSetter;
17   -import org.springframework.jdbc.core.JdbcTemplate;
18   -import org.springframework.stereotype.Component;
19   -import org.springframework.transaction.TransactionStatus;
20   -import org.springframework.transaction.support.TransactionCallbackWithoutResult;
21   -import org.springframework.transaction.support.TransactionTemplate;
22   -
23   -import java.sql.PreparedStatement;
24   -import java.sql.SQLException;
25   -import java.util.*;
26   -
27   -/**
28   - * 发车信息表处理程序
29   - * Created by panzhao on 2017/5/5.
30   - */
31   -@Component
32   -public class CarOutInfoHandler {
33   -
34   - @Autowired
35   - DayOfSchedule dayOfSchedule;
36   -
37   - //班次类型对照表
38   - static Map<String, String> bcTypeMap;
39   -
40   - static{
41   - bcTypeMap = new HashMap<>();
42   - bcTypeMap.put("normal", "正常班次");
43   - bcTypeMap.put("region", "区间");
44   - bcTypeMap.put("venting", "直放");
45   - bcTypeMap.put("major", "放站");
46   - bcTypeMap.put("ldks", "两点间空驶");
47   - }
48   -
49   - private static ScheduleComparator.DFSJ2 schDFSJComparator = new ScheduleComparator.DFSJ2();
50   -
51   - @Autowired
52   - JdbcTemplate jdbcTemplate;
53   -
54   - @Autowired
55   - private TransactionTemplate transactionTemplate;
56   -
57   - @Autowired
58   - private ObjectMapper mapper;
59   -
60   - Logger logger = LoggerFactory.getLogger(this.getClass());
61   -
62   -
63   - ArrayListMultimap<String, ScheduleRealInfo> xlMaps = ArrayListMultimap.create();
64   - List<ScheduleRealInfo> pstList = new ArrayList<>();
65   - /**
66   - * 全量更新发车信息表
67   - */
68   - public void updateAll() {
69   - logger.warn("发车信息更新开始");
70   - try{
71   - //将班次按线路分组
72   - List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll());
73   - for(ScheduleRealInfo sch : all){
74   - xlMaps.put(sch.getXlBm(), sch);
75   - }
76   -
77   - Set<String> ks = xlMaps.keySet();
78   - for (String k : ks) {
79   - pstList.addAll(update(xlMaps.get(k)));
80   - }
81   -
82   - save(pstList);
83   - pstList.clear();
84   - xlMaps.clear();
85   - }catch (Exception e){
86   - logger.error("", e);
87   - }
88   - logger.warn("发车信息更新结束");
89   - }
90   -
91   - public List<ScheduleRealInfo> update(List<ScheduleRealInfo> list) {
92   - if (list.size() == 0)
93   - return new ArrayList<>();
94   - //按上下行分组
95   - List<ScheduleRealInfo> ups = new ArrayList<>(), downs = new ArrayList<>();
96   - for (ScheduleRealInfo sch : list) {
97   - if (sch.getXlDir().equals("0"))
98   - ups.add(sch);
99   - else
100   - downs.add(sch);
101   - }
102   -
103   - ScheduleRealInfo[] upArray = nexts(ups),
104   - downArray = nexts(downs);
105   -
106   - List<ScheduleRealInfo> pstArray = mergeArray(upArray, downArray);
107   -
108   - return pstArray;
109   - }
110   -
111   - private void save(final List<ScheduleRealInfo> pstList){
112   - transactionTemplate.execute(new TransactionCallbackWithoutResult() {
113   -
114   - @Override
115   - public void doInTransactionWithoutResult(TransactionStatus status) {
116   - try {
117   - //删除
118   - jdbcTemplate.update("delete from bsth_t_clfcxxb");
119   - //重新写入
120   - 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, sch)" +
121   - " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
122   - @Override
123   - public void setValues(PreparedStatement ps, int i) throws SQLException {
124   - ScheduleRealInfo sch = pstList.get(i);
125   - ps.setString(1, sch.getScheduleDateStr());
126   - ps.setString(2, sch.getXlBm());
127   - ps.setString(3, sch.getXlName());
128   - ps.setString(4, /*sch.getLpName()*/"0");
129   - ps.setInt(5, sch.getFcno()==null?-1:sch.getFcno());
130   - ps.setString(6, sch.getDfsj().replace(":", ""));
131   - ps.setString(7, sch.getClZbh().replace("-", ""));
132   - ps.setString(8, BasicData.nbbmCompanyPlateMap.get(sch.getClZbh()));
133   - ps.setString(9, bcTypeMap.containsKey(sch.getBcType()) ? bcTypeMap.get(sch.getBcType()) : sch.getBcType());
134   - ps.setString(10, sch.getZdzName());
135   - ps.setInt(11, Integer.parseInt(sch.getXlDir()));
136   - ps.setString(12, sch.getjGh());
137   - ps.setString(13, sch.getjName());
138   - ps.setString(14, sch.getRemarks());
139   - ps.setInt(15, sch.getFcpSn());
140   - ps.setLong(16, sch.getId());
141   - }
142   -
143   - @Override
144   - public int getBatchSize() {
145   - return pstList.size();
146   - }
147   - });
148   - } catch (Exception e) {
149   - status.setRollbackOnly();
150   - try {
151   - logger.error(String.format("发车信息批量保存异常: %s", mapper.writeValueAsString(pstList)), e);
152   - } catch (JsonProcessingException jsonProcessingException) {
153   - jsonProcessingException.printStackTrace();
154   - }
155   - }
156   - }
157   - });
158   - }
159   -
160   - private List<ScheduleRealInfo> mergeArray(ScheduleRealInfo[] upArray, ScheduleRealInfo[] downArray) {
161   - List<ScheduleRealInfo> list = new ArrayList<>();
162   - for (int i = 0; i < upArray.length; i++) {
163   - if (upArray[i] != null) {
164   - upArray[i].setFcpSn(i + 1);
165   - list.add(upArray[i]);
166   - }
167   - }
168   - for (int i = 0; i < downArray.length; i++) {
169   - if (downArray[i] != null) {
170   - downArray[i].setFcpSn(i + 1);
171   - list.add(downArray[i]);
172   - }
173   - }
174   - return list;
175   - }
176   -
177   - /**
178   - * 接下来要执行的3个班次
179   - *
180   - * @param list
181   - * @return
182   - */
183   - private static String[] fls = new String[]{"in","out","ldks","venting","major"};
184   - private static List<String> clearTypes;
185   - static {
186   - clearTypes = Arrays.asList(fls);
187   - }
188   - private ScheduleRealInfo[] nexts(List<ScheduleRealInfo> list) {
189   - ScheduleRealInfo[] array = new ScheduleRealInfo[3];
190   - Collections.sort(list, schDFSJComparator);
191   -
192   - int count = 0;//, threshold = 1000 * 60 * 60 * 4;
193   - //long t = System.currentTimeMillis();
194   - for (ScheduleRealInfo sch : list) {
195   - if (count == 3)
196   - break;
197   -
198   - //烂班
199   - if (sch.isDestroy())
200   - continue;
201   -
202   - //进场、出场、2点间空驶
203   - if (clearTypes.contains(sch.getBcType()))
204   - continue;
205   -
206   - //区间
207   - if (sch.getBcType().equals("region")){
208   - //是否起点发出
209   - if(!isStartOut(sch))
210   - continue;
211   - }
212   -
213   - //有实发实达时间的
214   - if (StringUtils.isNotEmpty(sch.getFcsjActual())
215   - || StringUtils.isNotEmpty(sch.getZdsjActual()))
216   - continue;
217   -
218   - /*if (t - sch.getDfsjT() > threshold)
219   - continue;*/
220   -
221   - array[count] = sch;
222   - count++;
223   - }
224   - return array;
225   - }
226   -
227   - private static StationRouteComp sComp = new StationRouteComp();
228   - private boolean isStartOut(ScheduleRealInfo sch) {
229   - try{
230   - List<StationRoute> list = GeoCacheData.getStationRoute(sch.getXlBm(), Integer.parseInt(sch.getXlDir()));
231   -
232   - if(null == list && list.size() == 0)
233   - return false;
234   - //排序
235   - Collections.sort(list, sComp);
236   -
237   - if(sch.getQdzName().equals(list.get(0).getName()))
238   - return true;
239   - }catch (Exception e){
240   - logger.error("", e);
241   - }
242   - return false;
243   - }
244   -
245   - private static class StationRouteComp implements Comparator<StationRoute> {
246   -
247   - @Override
248   - public int compare(StationRoute s1, StationRoute s2) {
249   - return s1.getRouteSort() - s2.getRouteSort();
250   - }
251   - }
252   -}
  1 +package com.bsth.data.car_out_info;
  2 +
  3 +import com.bsth.data.BasicData;
  4 +import com.bsth.data.gpsdata_v2.cache.GeoCacheData;
  5 +import com.bsth.data.gpsdata_v2.entity.StationRoute;
  6 +import com.bsth.data.schedule.DayOfSchedule;
  7 +import com.bsth.data.schedule.ScheduleComparator;
  8 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  9 +import com.fasterxml.jackson.core.JsonProcessingException;
  10 +import com.fasterxml.jackson.databind.ObjectMapper;
  11 +import com.google.common.collect.ArrayListMultimap;
  12 +import org.apache.commons.lang3.StringUtils;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.jdbc.core.BatchPreparedStatementSetter;
  17 +import org.springframework.jdbc.core.JdbcTemplate;
  18 +import org.springframework.stereotype.Component;
  19 +import org.springframework.transaction.TransactionStatus;
  20 +import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  21 +import org.springframework.transaction.support.TransactionTemplate;
  22 +
  23 +import java.sql.PreparedStatement;
  24 +import java.sql.SQLException;
  25 +import java.util.*;
  26 +
  27 +/**
  28 + * 发车信息表处理程序
  29 + * Created by panzhao on 2017/5/5.
  30 + */
  31 +@Component
  32 +public class CarOutInfoHandler {
  33 +
  34 + @Autowired
  35 + DayOfSchedule dayOfSchedule;
  36 +
  37 + //班次类型对照表
  38 + static Map<String, String> bcTypeMap;
  39 +
  40 + static{
  41 + bcTypeMap = new HashMap<>();
  42 + bcTypeMap.put("normal", "正常班次");
  43 + bcTypeMap.put("region", "区间");
  44 + bcTypeMap.put("venting", "直放");
  45 + bcTypeMap.put("major", "放站");
  46 + bcTypeMap.put("ldks", "两点间空驶");
  47 + }
  48 +
  49 + private static ScheduleComparator.DFSJ2 schDFSJComparator = new ScheduleComparator.DFSJ2();
  50 +
  51 + @Autowired
  52 + JdbcTemplate jdbcTemplate;
  53 +
  54 + @Autowired
  55 + private TransactionTemplate transactionTemplate;
  56 +
  57 + @Autowired
  58 + private ObjectMapper mapper;
  59 +
  60 + Logger logger = LoggerFactory.getLogger(this.getClass());
  61 +
  62 +
  63 + ArrayListMultimap<String, ScheduleRealInfo> xlMaps = ArrayListMultimap.create();
  64 + List<ScheduleRealInfo> pstList = new ArrayList<>();
  65 + /**
  66 + * 全量更新发车信息表
  67 + */
  68 + public void updateAll() {
  69 + logger.warn("发车信息更新开始");
  70 + try{
  71 + //将班次按线路分组
  72 + List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll());
  73 + for(ScheduleRealInfo sch : all){
  74 + xlMaps.put(sch.getXlBm(), sch);
  75 + }
  76 +
  77 + Set<String> ks = xlMaps.keySet();
  78 + for (String k : ks) {
  79 + pstList.addAll(update(xlMaps.get(k)));
  80 + }
  81 +
  82 + save(pstList);
  83 + pstList.clear();
  84 + xlMaps.clear();
  85 + }catch (Exception e){
  86 + logger.error("", e);
  87 + }
  88 + logger.warn("发车信息更新结束");
  89 + }
  90 +
  91 + public List<ScheduleRealInfo> update(List<ScheduleRealInfo> list) {
  92 + if (list.size() == 0)
  93 + return new ArrayList<>();
  94 + //按上下行分组
  95 + List<ScheduleRealInfo> ups = new ArrayList<>(), downs = new ArrayList<>();
  96 + for (ScheduleRealInfo sch : list) {
  97 + if (sch.getXlDir().equals("0"))
  98 + ups.add(sch);
  99 + else
  100 + downs.add(sch);
  101 + }
  102 +
  103 + ScheduleRealInfo[] upArray = nexts(ups),
  104 + downArray = nexts(downs);
  105 +
  106 + List<ScheduleRealInfo> pstArray = mergeArray(upArray, downArray);
  107 +
  108 + return pstArray;
  109 + }
  110 +
  111 + private void save(final List<ScheduleRealInfo> pstList){
  112 + transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  113 +
  114 + @Override
  115 + public void doInTransactionWithoutResult(TransactionStatus status) {
  116 + try {
  117 + //删除
  118 + jdbcTemplate.update("delete from bsth_t_clfcxxb");
  119 + //重新写入
  120 + 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, sch)" +
  121 + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
  122 + @Override
  123 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  124 + ScheduleRealInfo sch = pstList.get(i);
  125 + ps.setString(1, sch.getScheduleDateStr());
  126 + ps.setString(2, sch.getXlBm());
  127 + ps.setString(3, sch.getXlName());
  128 + ps.setString(4, /*sch.getLpName()*/"0");
  129 + ps.setInt(5, sch.getFcno()==null?-1:sch.getFcno());
  130 + ps.setString(6, sch.getDfsj().replace(":", ""));
  131 + ps.setString(7, sch.getClZbh().replace("-", ""));
  132 + ps.setString(8, BasicData.nbbmCompanyPlateMap.get(sch.getClZbh()));
  133 + ps.setString(9, bcTypeMap.containsKey(sch.getBcType()) ? bcTypeMap.get(sch.getBcType()) : sch.getBcType());
  134 + ps.setString(10, sch.getZdzName());
  135 + ps.setInt(11, Integer.parseInt(sch.getXlDir()));
  136 + ps.setString(12, sch.getjGh());
  137 + ps.setString(13, sch.getjName());
  138 + ps.setString(14, sch.getRemarks());
  139 + ps.setInt(15, sch.getFcpSn());
  140 + ps.setLong(16, sch.getId());
  141 + }
  142 +
  143 + @Override
  144 + public int getBatchSize() {
  145 + return pstList.size();
  146 + }
  147 + });
  148 + } catch (Exception e) {
  149 + status.setRollbackOnly();
  150 + try {
  151 + logger.error(String.format("发车信息批量保存异常: %s", mapper.writeValueAsString(pstList)), e);
  152 + } catch (JsonProcessingException jsonProcessingException) {
  153 + jsonProcessingException.printStackTrace();
  154 + }
  155 + }
  156 + }
  157 + });
  158 + }
  159 +
  160 + private List<ScheduleRealInfo> mergeArray(ScheduleRealInfo[] upArray, ScheduleRealInfo[] downArray) {
  161 + List<ScheduleRealInfo> list = new ArrayList<>();
  162 + for (int i = 0; i < upArray.length; i++) {
  163 + if (upArray[i] != null) {
  164 + upArray[i].setFcpSn(i + 1);
  165 + list.add(upArray[i]);
  166 + }
  167 + }
  168 + for (int i = 0; i < downArray.length; i++) {
  169 + if (downArray[i] != null) {
  170 + downArray[i].setFcpSn(i + 1);
  171 + list.add(downArray[i]);
  172 + }
  173 + }
  174 + return list;
  175 + }
  176 +
  177 + /**
  178 + * 接下来要执行的3个班次
  179 + *
  180 + * @param list
  181 + * @return
  182 + */
  183 + private static String[] fls = new String[]{"in","out","ldks","venting","major"};
  184 + private static List<String> clearTypes;
  185 + static {
  186 + clearTypes = Arrays.asList(fls);
  187 + }
  188 + private ScheduleRealInfo[] nexts(List<ScheduleRealInfo> list) {
  189 + ScheduleRealInfo[] array = new ScheduleRealInfo[3];
  190 + Collections.sort(list, schDFSJComparator);
  191 +
  192 + int count = 0;//, threshold = 1000 * 60 * 60 * 4;
  193 + //long t = System.currentTimeMillis();
  194 + for (ScheduleRealInfo sch : list) {
  195 + if (count == 3)
  196 + break;
  197 +
  198 + //烂班
  199 + if (sch.isDestroy())
  200 + continue;
  201 +
  202 + //进场、出场、2点间空驶
  203 + if (clearTypes.contains(sch.getBcType()))
  204 + continue;
  205 +
  206 + //区间
  207 + if (sch.getBcType().equals("region")){
  208 + //是否起点发出
  209 + if(!isStartOut(sch))
  210 + continue;
  211 + }
  212 +
  213 + //有实发实达时间的
  214 + if (StringUtils.isNotEmpty(sch.getFcsjActual())
  215 + || StringUtils.isNotEmpty(sch.getZdsjActual()))
  216 + continue;
  217 +
  218 + /*if (t - sch.getDfsjT() > threshold)
  219 + continue;*/
  220 +
  221 + array[count] = sch;
  222 + count++;
  223 + }
  224 + return array;
  225 + }
  226 +
  227 + private static StationRouteComp sComp = new StationRouteComp();
  228 + private boolean isStartOut(ScheduleRealInfo sch) {
  229 + try{
  230 + List<StationRoute> list = GeoCacheData.getStationRoute(sch.getXlBm(), Integer.parseInt(sch.getXlDir()));
  231 +
  232 + if(null == list && list.size() == 0)
  233 + return false;
  234 + //排序
  235 + Collections.sort(list, sComp);
  236 +
  237 + if(sch.getQdzName().equals(list.get(0).getName()))
  238 + return true;
  239 + }catch (Exception e){
  240 + logger.error("", e);
  241 + }
  242 + return false;
  243 + }
  244 +
  245 + private static class StationRouteComp implements Comparator<StationRoute> {
  246 +
  247 + @Override
  248 + public int compare(StationRoute s1, StationRoute s2) {
  249 + return s1.getRouteSort() - s2.getRouteSort();
  250 + }
  251 + }
  252 +}
... ...