TTInfoServiceImpl.java
10.2 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
package com.bsth.service.schedule.impl;
import com.bsth.entity.schedule.TTInfo;
import com.bsth.entity.schedule.TTInfoBackup;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.repository.schedule.TTInfoBackupRepository;
import com.bsth.repository.schedule.TTInfoDetailRepository;
import com.bsth.repository.schedule.TTInfoRepository;
import com.bsth.service.schedule.TTInfoService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.TimeTableProto;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.*;
/**
* Created by xu on 16/12/20.
*/
@Service
public class TTInfoServiceImpl extends BServiceImpl<TTInfo, Long> implements TTInfoService {
/** 日志记录器 */
private static final Logger LOG = LoggerFactory.getLogger(TTInfoServiceImpl.class);
@Autowired
private TTInfoRepository ttInfoRepository;
@Autowired
private TTInfoDetailRepository ttInfoDetailRepository;
@Autowired
private TTInfoBackupRepository ttInfoBackupRepository;
@Override
public void validate_name(TTInfo ttInfo) throws ScheduleException {
// 名字重复验证
Map<String, Object> param = new HashMap<>();
if (ttInfo.getId() != null) {
param.put("id_ne", ttInfo.getId());
}
param.put("xl.id_eq", ttInfo.getXl().getId());
param.put("name_eq", ttInfo.getName());
if (!CollectionUtils.isEmpty(list(param))) {
throw new ScheduleException("名字重复");
}
}
@Override
public void validate_n_d(TTInfo ttInfo) throws ScheduleException {
// 常规有效日重复验证
// 找出所有未作废,已启用的时刻表,验证
Map<String, Object> param = new HashMap<>();
if (ttInfo.getId() != null) {
param.put("id_ne", ttInfo.getId());
}
param.put("xl.id_eq", ttInfo.getXl().getId());
param.put("isCancel_eq", false);
param.put("isEnableDisTemplate_eq", true);
List<TTInfo> ttInfos = list(param);
if (StringUtils.isEmpty(ttInfo.getRule_days())) {
throw new ScheduleException("常规有效日为空");
} else {
String[] nds = ttInfo.getRule_days().split(",");
for (TTInfo t : ttInfos) {
String[] nds_e = t.getRule_days().split(",");
for (int i = 0; i < 7; i++) {
if ("0".equals(nds[i])) {
//
} else {
if (nds[i].equals(nds_e[i])) {
throw new ScheduleException("当前常规有效日期已经使用");
}
}
}
}
}
}
@Override
public void validate_s_d(TTInfo ttInfo) throws ScheduleException {
// 特殊有效日重复验证
// 找出所有未作废,已启用的时刻表,验证
Map<String, Object> param = new HashMap<>();
if (ttInfo.getId() != null) {
param.put("id_ne", ttInfo.getId());
}
param.put("xl.id_eq", ttInfo.getXl().getId());
param.put("isCancel_eq", false);
param.put("isEnableDisTemplate_eq", true);
List<TTInfo> ttInfos = list(param);
if (StringUtils.isEmpty(ttInfo.getSpecial_days())) {
//
} else {
String[] sds = ttInfo.getSpecial_days().split(",");
for (TTInfo t : ttInfos) {
if (StringUtils.isEmpty(t.getSpecial_days())) {
//
} else {
for (String sd : sds) {
if (t.getSpecial_days().indexOf(sd) != -1) {
throw new ScheduleException("当前特殊日期已经使用");
}
}
}
}
}
}
@Transactional
@Override
public void delete(Long aLong) throws ScheduleException {
toggleCancel(aLong);
}
@Transactional
@Override
public void toggleCancel(Long id) throws ScheduleException {
TTInfo ttInfo = findById(id);
if (ttInfo.getIsCancel()) {
ttInfo.setIsCancel(false);
} else {
ttInfo.setIsCancel(true);
}
}
@Override
public List<Map<String, Object>> getLineStationRouteVersions(Integer lineId) {
// 获取线路版本数据
List<Map<String, Object>> lineVersionDescs = ttInfoRepository.findLineVersionDescs(lineId);
// 按照version版本降序排序
Collections.sort(lineVersionDescs, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int o1_v = Integer.parseInt(o1.get("version").toString());
int o2_v = Integer.parseInt(o2.get("version").toString());
if (o1_v > o2_v) {
return -1;
} else if (o1_v < o2_v) {
return 1;
} else {
return 0;
}
}
});
// 取最开始的2条记录
List<Map<String, Object>> mapList = new ArrayList<>();
for (Map<String, Object> lv: lineVersionDescs) {
String t_name = (String) lv.get("name");
Timestamp t_sd = (Timestamp) lv.get("sd");
int status = Integer.parseInt(lv.get("status").toString());
int version = Integer.parseInt(lv.get("version").toString());
String vname = t_name;
String rq = t_sd == null ? "未知启用日期" : new DateTime(t_sd).toString("YYYY年MM月dd日");
String sdesc = status == 0 ? "历史" : (status == 1 ? "当前" : "待更新");
Map<String, Object> value = new HashMap<>();
value.put("desc", vname + "-" + rq + "-" + sdesc);
value.put("version", version);
mapList.add(value);
if (mapList.size() == 2) {
break;
}
}
return mapList;
}
@Override
public String getLineVersionDesc(Integer lineId, Integer version) {
// 查找指定版本,并判定
List<Map<String, Object>> lineVersionDescs = ttInfoRepository.findLineVersionDescs2(lineId, version);
Map<String, Object> lv;
if (CollectionUtils.isEmpty(lineVersionDescs)) {
return "未知版本";
} else if (lineVersionDescs.size() > 1) {
return "重复版本";
} else {
lv = lineVersionDescs.get(0);
}
String t_name = (String) lv.get("name");
Timestamp t_sd = (Timestamp) lv.get("sd");
int status = Integer.parseInt(lv.get("status").toString());
String vname = t_name;
String rq = t_sd == null ? "未知启用日期" : new DateTime(t_sd).toString("YYYY年MM月dd日");
String sdesc = status == 0 ? "历史" : (status == 1 ? "当前" : "待更新");
return String.format("%s-%s-%s", vname, rq, sdesc);
}
@Override
public List<Map<String, Object>> getLineVersions(Integer lineId, Integer status) {
return ttInfoRepository.findLineVersionDescs3(lineId, status);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
public void backUp(Long ttInfoId) throws ScheduleException {
LOG.info(">>>>>>开始备份时刻表<<<<<<");
try {
// 获取原始数据
TTInfo ttInfo = ttInfoRepository.findOne(ttInfoId);
List<TTInfoDetail> ttInfoDetails = ttInfoDetailRepository.findByTtinfoId(ttInfoId);
// protobuf序列化成二进制数据
TimeTableProto.TTInfo.Builder tb = ttInfo.toProtoBuilder();
for (TTInfoDetail ttInfoDetail : ttInfoDetails) {
tb.addBcInfo(ttInfoDetail.toProtoBuilder());
}
byte[] backupbytes = tb.build().toByteArray();
LOG.info("......时刻表={}", ttInfo.getName());
LOG.info("......时刻表protoBuf字节数={}", backupbytes.length);
// 更新备份日期
Date backupdate = new Date();
ttInfo.setLastBackUpDate(backupdate);
// 保存备份数据
TTInfoBackup ttInfoBackup = new TTInfoBackup();
ttInfoBackup.setXl(ttInfo.getXl().getId());
ttInfoBackup.setXlName(ttInfo.getXl().getName());
ttInfoBackup.setTtInfo(ttInfoId);
ttInfoBackup.setTtInfoName(ttInfo.getName());
ttInfoBackup.setLineVersion(ttInfo.getLineVersion());
ttInfoBackup.setBackUpDate(backupdate);
ttInfoBackup.setBackUpInfo(backupbytes);
ttInfoBackupRepository.save(ttInfoBackup);
// System.out.println(backupbytes.length);
// try {
//
// TimeTableProto.TTInfo tt1 = TimeTableProto.TTInfo.parseFrom(backupbytes);
// System.out.println(tt1.getName());
// System.out.println(tt1.getBcInfoCount());
// } catch (Exception exp) {
// exp.printStackTrace();
// }
LOG.info(">>>>>>备份时刻表success<<<<<<");
} catch (Exception exp) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
exp.printStackTrace(printWriter);
LOG.info("......异常stack->{}", stringWriter.toString());
LOG.info(">>>>>>备份时刻表failed<<<<<<");
throw new ScheduleException(exp);
}
}
}