Station2ParkBuffer.java
6.29 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
package com.bsth.data;
import com.bsth.common.ResponseCode;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.realcontrol.StationToPark;
import com.bsth.repository.realcontrol.StationToParkRepository;
import com.bsth.util.Arith;
import com.google.common.collect.ArrayListMultimap;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* 站到场 历时、公里 数据缓存
* Created by panzhao on 2017/7/10.
*/
@Component
public class Station2ParkBuffer implements CommandLineRunner {
@Autowired
StationToParkRepository stationToParkRepository;
private static ArrayListMultimap listMultimap;
private static Set<StationToPark> pstBuff = new HashSet<>();
static Logger log = LoggerFactory.getLogger(Station2ParkBuffer.class);
@Override
public void run(String... strings) throws Exception {
listMultimap = ArrayListMultimap.create();
Iterator<StationToPark> iterator = stationToParkRepository.findAll().iterator();
StationToPark stp;
while (iterator.hasNext()) {
stp = iterator.next();
listMultimap.put(stp.getLineCode(), stp);
}
}
public static List<StationToPark> get(String lineCode) {
return listMultimap.get(lineCode);
}
public static StationToPark get(String lineCode, String sName, String eName) {
List<StationToPark> list = get(lineCode);
if(null == list)
return null;
StationToPark stp = null;
for (StationToPark s : list) {
if (s.getStationName().equals(sName) && s.getParkName().equals(eName)) {
stp = s;
break;
}
}
return stp;
}
private static DateTimeFormatter fmtHHmm = DateTimeFormat.forPattern("HH:mm");
public static void put(ChildTaskPlan ctask) {
try{
String type2 = ctask.getType2();
String lineCode = ctask.getSchedule().getXlBm(), sName, eName;
if (type2.equals("2")) {
sName = ctask.getStartStationName();
eName = ctask.getEndStationName();
} else if (type2.equals("3")) {
eName = ctask.getStartStationName();
sName = ctask.getEndStationName();
} else {
return;
}
Float time = calcMinute(ctask);
Float mileage = ctask.getMileage();
StationToPark stp = get(lineCode, sName, eName);
if (stp == null) {
stp = new StationToPark();
stp.setLineCode(lineCode);
stp.setStationName(sName);
stp.setParkName(eName);
listMultimap.put(lineCode, stp);
}
if (type2.equals("2")) {
stp.setTime1(time);
stp.setMileage1(mileage);
} else {
stp.setTime2(time);
stp.setMileage2(mileage);
}
pstBuff.add(stp);
}catch (Exception e){
log.error("", e);
}
}
public static void put(ScheduleRealInfo sch){
try{
String type = sch.getBcType();
String lineCode = sch.getXlBm(), sName, eName;
if (type.equals("in")) {
sName = sch.getQdzName();
eName = sch.getZdzName();
} else if (type.equals("out")) {
eName = sch.getQdzName();
sName = sch.getZdzName();
} else {
return;
}
long dt = sch.getZdsjT() - sch.getDfsjT();
Float time = Float.parseFloat(String.valueOf(Arith.div(Arith.div(dt, 1000), 60)));
Float mileage = Float.parseFloat(sch.getJhlc().toString());
StationToPark stp = get(lineCode, sName, eName);
if (stp == null) {
stp = new StationToPark();
stp.setLineCode(lineCode);
stp.setStationName(sName);
stp.setParkName(eName);
listMultimap.put(lineCode, stp);
}
if (type.equals("in")) {
stp.setTime1(time);
stp.setMileage1(mileage);
} else {
stp.setTime2(time);
stp.setMileage2(mileage);
}
pstBuff.add(stp);
}catch (Exception e){
log.error("", e);
}
}
public static Float calcMinute(ChildTaskPlan ctask) {
long t = 0;
try {
long st = fmtHHmm.parseMillis(ctask.getStartDate());
long et = fmtHHmm.parseMillis(ctask.getEndDate());
t = et - st;
} catch (Exception e) {
e.printStackTrace();
}
return Float.parseFloat(String.valueOf(Arith.div(Arith.div(t, 1000), 60)));
}
public void saveAll(){
if (pstBuff.size()==0) {
return;
}
Set<StationToPark> pstBuffCopy = pstBuff;
pstBuff = new HashSet<>();
//持久化到数据库
stationToParkRepository.saveAll(pstBuffCopy);
}
public Map<String, Object> delete(String lineCode, Integer id) {
Map<String, Object> rs = new HashMap<>();
try {
List<StationToPark> list = listMultimap.get(lineCode);
StationToPark stp = null;
for(StationToPark temp : list){
if(temp.getId().equals(id)){
stp=temp;
break;
}
}
if(stp != null){
listMultimap.remove(lineCode, stp);
stationToParkRepository.deleteById(id);
rs.put("status", ResponseCode.SUCCESS);
}
else{
rs.put("status", ResponseCode.SUCCESS);
rs.put("msg", "操作失败,可能数据已经被删除!");
}
}catch (Exception e){
rs.put("status", ResponseCode.ERROR);
rs.put("msg", e.getMessage());
log.error("", e);
}
return rs;
}
}