NowOutboundServiceImpl.java
13.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.bsth.service.excep.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.bsth.data.BasicData;
import com.bsth.entity.excep.Outbound;
import com.bsth.service.excep.NowOutboundService;
import com.bsth.util.EchartConver;
import com.bsth.util.PageHelper;
import com.bsth.util.PageObject;
import com.bsth.util.CoordinateConverter;
import com.bsth.util.CoordinateConverter.Location;
import com.bsth.util.db.DBUtils_MS;
import com.github.abel533.echarts.Option;
import com.google.gson.Gson;
@Service
public class NowOutboundServiceImpl implements NowOutboundService{
@Autowired
JdbcTemplate jdbcTemplate;
static List<Outbound> findAll(Map<String, Object> map) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int page=Integer.parseInt(map.get("page").toString());
List<Outbound> list=new ArrayList<Outbound>();
String sql="select * from bsth_c_outbound_handle where 1=1 ";
Object line=map.get("line");
Object nbbm=map.get("nbbm");
Object updown=map.get("updown");
Object date=map.get("date");
if(line!=null){
sql +=" and line like'%"+line.toString().trim()+"%'";
}
if(nbbm!=null){
nbbm=BasicData.deviceId2NbbmMap.inverse().get(nbbm);
if(nbbm!=null)
sql +=" and vehicle like '%"+nbbm.toString()+"%'";
}
if(updown!=null){
sql +="and up_down like '%"+updown.toString()+"%'";
}
if(date!=null){
if (date.toString().length()>0) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Long t1=sdf.parse(date.toString()+" 00:00:00").getTime();
Long t2=sdf.parse(date.toString()+" 23:59:59").getTime();
sql += " and starttimestamp >="+t1 +" and endtimestamp <="+t2;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sql +=" and endtimestamp - starttimestamp >10000 ";//越界超过30秒的才显示,有待探讨。
sql +=" order by starttimestamp limit ?,?";
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, page*10); // O-最大条数 -- M-起始条数
ps.setInt(2, 10); // O-最小条数 -- M-显示条数
rs = ps.executeQuery();
list = resultSet2Set(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return list;
}
static List<Outbound> resultSet2Set(ResultSet rs) throws SQLException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Outbound> list=new ArrayList<Outbound>();
Outbound outbound;
Location location;
while(rs.next()){
outbound=new Outbound();
outbound.setId(Integer.valueOf(rs.getObject("id").toString()));
outbound.setLine(Integer.valueOf(rs.getObject("line").toString()));
outbound.setUpDown(Integer.valueOf(rs.getObject("up_down").toString()));
//将gps的经纬度转成百度的经纬度
location = CoordinateConverter.bd_encrypt(CoordinateConverter.transformFromWGSToGCJ(CoordinateConverter.LocationMake(rs.getFloat("startLon"), rs.getFloat("startLat"))));
outbound.setLon((float)location.getLng());
outbound.setLat((float)location.getLat());
location = CoordinateConverter.bd_encrypt(CoordinateConverter.transformFromWGSToGCJ(CoordinateConverter.LocationMake(rs.getFloat("endLon"), rs.getFloat("endLat"))));
outbound.setEndlon((float)location.getLng());
outbound.setEndlat((float)location.getLat());
outbound.setTimestamp((Long.valueOf(rs.getObject("startTimestamp").toString())));
outbound.setTimestampDate(sdf.format(new Date(outbound.getTimestamp())));
outbound.setEndtimestamp((Long.valueOf(rs.getObject("endTimestamp").toString())));
outbound.setEndtimestampDate(sdf.format(new Date(outbound.getEndtimestamp())));
//run 时注解
outbound.setLineName(BasicData.lineCode2NameMap.get(outbound.getLine().toString()));
outbound.setVehicle(BasicData.deviceId2NbbmMap.get(rs.getObject("vehicle").toString()));
list.add(outbound);
}
return list;
}
@Override
public PageObject<Outbound> Pagequery(Map<String, Object> map) {
// TODO Auto-generated method stub PageObject<Outbound> Pagequery
String sql="select count(*) record from bsth_c_outbound_handle where 1=1 ";
Object line=map.get("line");
Object nbbm=map.get("nbbm");
Object updown=map.get("updown");
Object date=map.get("date");
if(line!=null){
sql +=" and line like '%"+line.toString().trim()+"%'";
}
if(nbbm!=null){
nbbm=BasicData.deviceId2NbbmMap.inverse().get(nbbm);
if(nbbm!=null)
sql +=" and vehicle like '%"+nbbm.toString()+"%'";
}
if(updown!=null){
sql +=" and up_down like '%"+updown.toString()+"%'";
}
if(date!=null){
if (date.toString().length()>0) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Long t1=sdf.parse(date.toString()+" 00:00:00").getTime();
Long t2=sdf.parse(date.toString()+" 23:59:59").getTime();
sql += " and starttimestamp >="+t1 +" and endtimestamp <="+t2;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sql +=" and endtimestamp - starttimestamp >10000 ";//yue越界超过10秒的才显示,有待探讨。
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int totalData = 0;
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
totalData=rs.getInt("record");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils_MS.close(rs, ps, conn);
}
PageHelper pageHelper = new PageHelper(totalData, map);
List<Outbound> list=findAll(pageHelper.getMap());
/* for (int i = 0; i < list.size(); i++) {
String nbbm2=list.get(i).getVehicle() ;
Long d1=list.get(i).getTimestamp();
Date datess = new Date(d1);
String dates=new SimpleDateFormat("yyyy-MM-dd").format(datess);
String sk=new SimpleDateFormat("HH:mm").format(datess);
String sqlPbb="SELECT * FROM ( select lp_name,cl_zbh,j_gh,j_name,MIN(fcsj) as fcsj,MAX(zdsj) as ddsj"
+ " from bsth_c_s_sp_info_real where cl_zbh='"+nbbm2+"' and"
+ " real_exec_date='"+dates+"' GROUP BY cl_zbh,lp_name,j_gh,j_name ) t WHERE t.fcsj<='"+sk+"' AND t.ddsj>='"+sk+"' " ;
List<ScheduleRealInfo> pbb =jdbcTemplate.query(sqlPbb,
new RowMapper<ScheduleRealInfo>(){
@Override
public ScheduleRealInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
ScheduleRealInfo s = new ScheduleRealInfo();
s.setjGh(rs.getString("j_gh"));
s.setjName(rs.getString("j_name"));
s.setLpName(rs.getString("lp_name"));
return s;
}
});
if(pbb.size()>0){
list.get(i).setJsy(pbb.get(0).getjGh()+"/"+pbb.get(0).getjName());
list.get(i).setLpname(pbb.get(0).getLpName());
}
}*/
PageObject<Outbound> pageObject = pageHelper.getPageObject();
pageObject.setDataList(list);
return pageObject;
}
@Override
public Map<String, Object> getReport(Map<String, Object> map) {
// TODO Auto-generated method stub
String chartStr = "";
Map<String, Object> mapValue = new HashMap<String, Object>();
try {
String xAxisName = map.get("xAxisName").toString();
String legendName = map.get("legendName").toString();
String xAxisItem = "";
String xAxisOrderBy = "";
String legendItem = "";
String legendOrderBy = "";
if(xAxisName!=null){
String [] xaxis = xAxisName.split(":");
if(xaxis.length>1 && xaxis[1].toLowerCase().contains("date")){
//本来还要判断下日期格式是否标准
xAxisItem = "to_char("+xaxis[0]+",'"+xaxis[2]+"') ";
xAxisOrderBy = (xaxis.length>3?xaxis[3]:"");
}else{
xAxisItem = xaxis[0];
}
}
if(legendName!=null){
String [] legend = legendName.split(":");
if(legend.length>1 && legend[1].toLowerCase().contains("date")){
//本来还要判断下日期格式是否标准
legendItem = "to_char("+legend[0]+",'"+legend[2]+"') ";
legendOrderBy = (legend[3].length()>3?legend[3]:"");
}else{
legendItem = legend[0];
}
}
map.put("xAxisItem", xAxisItem);
map.put("xAxisOrderBy", xAxisOrderBy);
map.put("legendItem", legendItem);
map.put("legendOrderBy", legendOrderBy);
List dataList = new ArrayList();
dataList =getReportList(map);
EchartConver echart = new EchartConver(map);
Option option = echart.getOption(dataList);
Gson gson = new Gson();
chartStr = gson.toJsonTree(option).toString();
mapValue.put("dataList", dataList);
mapValue.put("option", chartStr);
} catch (Exception e) {
e.printStackTrace();
}
return mapValue;
}
static List getReportList(Map<String, Object> map) {
List dataList = new ArrayList();
String times="";
Object dataTime=map.get("dataTime");
Object line=map.get("line");
String addSql="";
if(line !=null){
if(line.toString()!=""){
addSql =" and line =" +line;
}
}
if(dataTime!=null){
times=dataTime.toString();
}
if(times.equals("")){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
times=sdf.format(new Date());
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String times1=times+" 00:00:00";
String times2=times+" 23:59:59";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Long t1=simpleDateFormat.parse(times1).getTime();
Long t2=simpleDateFormat.parse(times2).getTime();
String sql="SELECT up_down xAxis , line legend,count(*) series"+
" FROM bsth_c_outbound "+
"where timestamp>? and timestamp<? "+addSql+" group by xAxis ,legend";
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setLong(1, t1);
ps.setLong(2, t2);
rs = ps.executeQuery();
while(rs.next()){
Map<String, Object> newMap=new HashMap<String,Object>();
if(rs.getObject("xAxis").toString().equals("0"))
newMap.put("xAxis", "$$$$$${txt-3858}");
else if(rs.getObject("xAxis").toString().equals("1"))
newMap.put("xAxis", "$$$$$${txt-3857}");
else
newMap.put("xAxis", "$$$$$${txt-4006}");
if(BasicData.lineCode2NameMap.get(rs.getObject("legend").toString())!=null && BasicData.lineCode2NameMap.get(rs.getObject("legend").toString())!="" )
newMap.put("legend", BasicData.lineCode2NameMap.get(rs.getObject("legend").toString()));
else
newMap.put("legend", "编码"+rs.getObject("legend").toString());
newMap.put("series", Integer.parseInt(rs.getObject("series").toString()));
dataList.add(newMap);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return dataList;
}
@Override
public List<Outbound> findPosition(String deviceid, String startdate,
String enddate){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "select vehicle,line,up_down,lon,lat,timestamp from bsth_c_outbound where vehicle = ? and timestamp >= ? and timestamp <= ? order by timestamp ";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Outbound> listResult = new ArrayList<Outbound>();
Outbound outbound = null;
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
long startTime = sdf.parse(startdate).getTime();
long endTime = sdf.parse(enddate).getTime();
ps.setString(1, deviceid);
ps.setLong(2,startTime);
ps.setLong(3,endTime);
rs = ps.executeQuery();
Location location;
while (rs.next()) {
outbound = new Outbound();
outbound.setVehicle(BasicData.deviceId2NbbmMap.get(rs.getObject("vehicle").toString()));
location = CoordinateConverter.LocationMake(rs.getFloat("lon"), rs.getFloat("lat"));
location = CoordinateConverter.bd_encrypt(CoordinateConverter.transformFromWGSToGCJ(location));
outbound.setLon((float)location.getLng());
outbound.setLat((float)location.getLat());
outbound.setTimestamp(rs.getLong("timestamp"));
// 上下行
listResult.add(outbound);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils_MS.close(rs, ps, conn);
}
return listResult;
}
}