LineVersionsServiceImpl.java
10.5 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
package com.bsth.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.LineVersions;
import com.bsth.entity.LsSectionRoute;
import com.bsth.entity.LsStationRoute;
import com.bsth.entity.SectionRoute;
import com.bsth.entity.StationRoute;
import com.bsth.repository.LineRepository;
import com.bsth.repository.LineVersionsRepository;
import com.bsth.repository.LsSectionRouteRepository;
import com.bsth.repository.LsStationRouteRepository;
import com.bsth.repository.SectionRouteRepository;
import com.bsth.repository.StationRouteRepository;
import com.bsth.service.LineVersionsService;
import com.bsth.service.SectionRouteService;
import com.bsth.service.StationRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @ClassName: LineVersionsServiceImpl(线路service业务层实现类)
*
* @Extends : BaseService
*
* @Description: TODO(线路service业务层)
*
* @Author bsth@lq
*
* @Version 公交调度系统BS版 0.1
*
*/
@Service
public class LineVersionsServiceImpl extends BaseServiceImpl<LineVersions, Integer> implements LineVersionsService{
@Autowired
private LineVersionsRepository repository;
@Autowired
LineRepository lineRepository;
@Autowired
SectionRouteService sectionRouteService;
@Autowired
StationRouteService stationRouteService;
@Autowired
SectionRouteRepository sectionRouteRepository ;
@Autowired
StationRouteRepository stationRouteRepository ;
@Autowired
LsSectionRouteRepository lsSectionRouteRepository ;
@Autowired
LsStationRouteRepository lsStationRouteRepository ;
@Override
public List<LineVersions> findByLineCode(int lineId) {
return repository.findBylineId(lineId);
}
@Override
public Map<String, Object> update(Map<String, Object> map) {
Map<String, Object> resultMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date startDate = simpleDateFormat.parse(map.get("startDate").toString());
Date endDate = simpleDateFormat.parse(map.get("endDate").toString());
Integer id = Integer.valueOf(map.get("Id").toString());
Integer lineid = Integer.valueOf(map.get("lineId").toString());
String lineCode = map.get("lineCode").toString();
Integer versions = Integer.valueOf(map.get("versions").toString());
Integer status = Integer.valueOf(map.get("status").toString());
String name = map.get("name").toString();
String remark = map.get("remark").toString();
Line line = lineRepository.findOne(lineid);
int statu = repository.update(id,line,lineCode,new java.sql.Date(startDate.getTime()),
new java.sql.Date(endDate.getTime()),versions,status,remark,name);
LineVersions upLineVersions = repository.findBylineIdAndVersions(line.getId(),versions);
if(upLineVersions != null){
upLineVersions.setEndDate(new java.sql.Date(startDate.getTime()));
repository.save(upLineVersions);
}
if (statu==1) {
resultMap.put("status", ResponseCode.SUCCESS);
} else {
resultMap.put("status", ResponseCode.ERROR);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
resultMap.put("status", ResponseCode.ERROR);
}
return resultMap;
}
@Override
public List<LineVersions> findupdated() {
return repository.findupdated();
}
@Autowired
JdbcTemplate jdbcTemplate;
// 线路更新
@Transactional
@Override
public List<LineVersions> lineUpdate() {
List<LineVersions> list = findupdated();
for (LineVersions lineVersions : list) {
Integer lineId = lineVersions.getLine().getId();
String lineCode = lineVersions.getLineCode();
Integer versions = lineVersions.getVersions();
// 需要更新线路的路由
List<LsSectionRoute> lsSectionRoutes = lsSectionRouteRepository.findupdated(lineId, lineCode, versions);
List<LsStationRoute> lsStationRoutes = lsStationRouteRepository.findupdated(lineId, lineCode, versions);
if (lsSectionRoutes.size() != 0 && lsStationRoutes.size() != 0) {
// 更新
final List<StationRoute> stationRoutes = JSONArray.parseArray(JSON.toJSONString(lsStationRoutes), StationRoute.class);
final List<SectionRoute> sectionRoutes = JSONArray.parseArray(JSON.toJSONString(lsSectionRoutes), SectionRoute.class);
// 只能使用jdbc插入,应为历史表同步到当前表的时候,带有id,用jpa会报错
jdbcTemplate.update("delete from bsth_c_stationroute where line = ? and line_code = ?", lineId, lineCode);
String stationSql ="insert into bsth_c_stationroute(id,line,station,station_name,station_route_code,"
+ "line_code,station_code,station_mark,directions,distances,to_time,destroy,versions,"
+ "create_date,update_date,industry_code) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
jdbcTemplate.batchUpdate(stationSql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
StationRoute sRoute = stationRoutes.get(i);
sRoute.getStationRouteCode();
ps.setInt(1, sRoute.getId());
ps.setInt(2, sRoute.getLine().getId());
ps.setInt(3, sRoute.getStation().getId());
ps.setString(4, sRoute.getStationName());
ps.setInt(5, sRoute.getStationRouteCode());
ps.setString(6, sRoute.getLineCode());
ps.setString(7, sRoute.getStationCode());
ps.setString(8, sRoute.getStationMark());
ps.setInt(9, sRoute.getDirections());
ps.setDouble(10, sRoute.getDistances());
ps.setDouble(11, sRoute.getToTime());
ps.setInt(12, sRoute.getDestroy());
ps.setInt(13, sRoute.getVersions());
ps.setDate(14, new java.sql.Date(sRoute.getCreateDate().getTime()));
ps.setDate(15, new java.sql.Date(sRoute.getUpdateDate().getTime()));
ps.setString(16,sRoute.getIndustryCode());
}
@Override
public int getBatchSize() {
// TODO Auto-generated method stub
return stationRoutes.size();
}
} );
jdbcTemplate.update("delete from bsth_c_sectionroute where line = ? and line_code = ?", lineId, lineCode);
String sectionSql ="insert into bsth_c_sectionroute(id,line_code,section_code,sectionroute_code,"
+ "directions,line,section,create_date,update_date,versions,"
+ "destroy) values(?,?,?,?,?,?,?,?,?,?,?)";
jdbcTemplate.batchUpdate(sectionSql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
SectionRoute sRoute = sectionRoutes.get(i);
ps.setInt(1, sRoute.getId());
ps.setString(2, sRoute.getLineCode());
ps.setString(3, sRoute.getSectionCode());
ps.setInt(4, sRoute.getSectionrouteCode());
ps.setInt(5, sRoute.getDirections());
ps.setInt(6, sRoute.getLine().getId());
ps.setInt(7, sRoute.getSection().getId());
ps.setDate(8, new java.sql.Date(sRoute.getCreateDate().getTime()));
ps.setDate(9, new java.sql.Date(sRoute.getUpdateDate().getTime()));
ps.setInt(10, sRoute.getVersions());
ps.setInt(11, sRoute.getDestroy());
}
@Override
public int getBatchSize() {
// TODO Auto-generated method stub
return sectionRoutes.size();
}
} );
// 更新线路版本
repository.updateOdlVersions(lineId, lineCode);
repository.updateNewVersions(lineId,lineCode,versions);
// 记录版本更新日志
jdbcTemplate.update("INSERT into bsth_c_line_versions_enablog(line_code,`line_versions`,`t`) VALUES (?,?,?)",lineCode,versions, lineVersions.getStartDate().getTime());
}
}
return list;
}
/**
* 查询线路版本最大值
*/
@Override
public LineVersions findLineVersionsMax(int lineId) {
return repository.findLineVersionsMax(lineId);
}
/**
* 线路版本添加
*/
@Override
public Map<String, Object> add(Map<String, Object> map) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, Object> resultMap = new HashMap<>();
LineVersions lineVersions = new LineVersions();
try {
Date startDate = simpleDateFormat.parse(map.get("startDate").toString());
Date endDate = simpleDateFormat.parse(map.get("endDate").toString());
Line line = lineRepository.findOne(Integer.valueOf(map.get("lineId").toString()));
Integer versions = Integer.valueOf(map.get("versions").toString());
lineVersions.setName(map.get("name").toString());
lineVersions.setLine(line);
lineVersions.setLineCode(map.get("lineCode").toString());
lineVersions.setStartDate(new java.sql.Date(startDate.getTime()));
lineVersions.setEndDate(new java.sql.Date(endDate.getTime()));
lineVersions.setVersions(versions);
// 添加的线路版本默认为待更新的
lineVersions.setStatus(2);
lineVersions.setRemark(map.get("remark").toString());
repository.save(lineVersions);
// 更新上一版本时间
LineVersions upLineVersions = repository.findBylineIdAndVersions(line.getId(),versions);
upLineVersions.setEndDate(new java.sql.Date(startDate.getTime()));
repository.save(upLineVersions);
resultMap.put("status", ResponseCode.SUCCESS);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
resultMap.put("status", ResponseCode.ERROR);
}
return resultMap;
}
/**
* 获取当前线路版本号
*/
@Override
public Integer findCurrentVersion(int lineId) {
return repository.findCurrentVersion(lineId);
}
@Override
public Map<String, Object> issueVersion(int id) {
Map<String, Object> resultMap = new HashMap<>();
int statu = repository.issueVersion(id);
if (statu == 1)
resultMap.put("status", ResponseCode.SUCCESS);
else
resultMap.put("status", ResponseCode.ERROR);
return resultMap;
}
}