Commit f881b1a016b61aaaad0727198981f8334f379ba9

Authored by 娄高锋
1 parent f0e8ad31

按日期查询线路首班详情与末班详情接口

src/main/java/com/bsth/server_rs/bigdata/BigscreenService.java
@@ -7,7 +7,11 @@ import java.text.DecimalFormat; @@ -7,7 +7,11 @@ import java.text.DecimalFormat;
7 import java.text.ParseException; 7 import java.text.ParseException;
8 import java.text.SimpleDateFormat; 8 import java.text.SimpleDateFormat;
9 import java.util.ArrayList; 9 import java.util.ArrayList;
  10 +import java.util.Calendar;
  11 +import java.util.Collections;
  12 +import java.util.Comparator;
10 import java.util.Date; 13 import java.util.Date;
  14 +import java.util.GregorianCalendar;
11 import java.util.HashMap; 15 import java.util.HashMap;
12 import java.util.HashSet; 16 import java.util.HashSet;
13 import java.util.Iterator; 17 import java.util.Iterator;
@@ -20,6 +24,7 @@ import javax.ws.rs.Path; @@ -20,6 +24,7 @@ import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam; 24 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces; 25 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType; 26 import javax.ws.rs.core.MediaType;
  27 +import javax.ws.rs.ext.MessageBodyWriter;
23 28
24 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger; 30 import org.slf4j.Logger;
@@ -2305,6 +2310,266 @@ public class BigscreenService { @@ -2305,6 +2310,266 @@ public class BigscreenService {
2305 return resList; 2310 return resList;
2306 } 2311 }
2307 2312
  2313 + /** 按日期查询所有线路首班末班班次数 */
  2314 + @GET
  2315 + @Path("/selectData/getFirstAndLast/{date}")
  2316 + public List<Map<String, Object>> getFirstAndLast(@PathParam("date") String date){
  2317 + List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
  2318 +
  2319 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  2320 + SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd");
  2321 + DecimalFormat df = new DecimalFormat("0.00");
  2322 + Date createDate = new Date();
  2323 + String sfss = "1"; //是否实时;=1为实时。
  2324 +
  2325 + String yyxlSql="SELECT line_code from bsth_c_line "
  2326 + + " where nature in ('yxl','cgxl','gjxl','csbs','cctxl')";
  2327 + List<Map<String, Object>> yyxlList=jdbcTemplate.query(yyxlSql,
  2328 + new RowMapper<Map<String, Object>>(){
  2329 + @Override
  2330 + public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
  2331 + Map<String, Object> m=new HashMap<String,Object>();
  2332 + m.put("lineCode", rs.getString("line_code"));
  2333 + return m;
  2334 + }
  2335 + });
  2336 +
  2337 + //key:线路编码;value:每日营运开始时间
  2338 + Map<String, Long> yyLine = new HashMap<String, Long>();
  2339 + for(Map<String, Object> t : yyxlList){
  2340 + if(t.get("lineCode") != null){
  2341 + String lineCode = t.get("lineCode").toString();
  2342 +// yyLine.add(t.get("lineCode").toString());
  2343 + String minfcsj="02:00";
  2344 + String sqlMinYysj="select start_opt from bsth_c_line_config where "
  2345 + + " id = ("
  2346 + + "select max(id) from bsth_c_line_config where line ='"+ lineCode +"'"
  2347 + + ")";
  2348 + List<String> list= jdbcTemplate.query(sqlMinYysj,
  2349 + new RowMapper<String>(){
  2350 + @Override
  2351 + public String mapRow(ResultSet rs, int rowNum) throws SQLException {
  2352 + String startopt=rs.getString("start_opt");
  2353 + return startopt;
  2354 + }
  2355 + });
  2356 + if(list.size()>0){
  2357 + minfcsj=list.get(0);
  2358 + }
  2359 + String[] minSjs = minfcsj.split(":");
  2360 + //车辆最早发车时间 用来过滤超第二天0点的数据
  2361 + Long minSj=Long.parseLong(minSjs[0])*60+Long.parseLong(minSjs[1]);
  2362 +
  2363 + yyLine.put(lineCode, minSj);
  2364 + }
  2365 + }
  2366 +
  2367 + List<ScheduleRealInfo> findAll = scheduleRealInfoRepository.findAll(date);
  2368 + Map<String, List<ScheduleRealInfo>> keyMap = new HashMap<String, List<ScheduleRealInfo>>();
  2369 + Map<String, Map<String, Object>> lineXX = new HashMap<String, Map<String, Object>>();
  2370 + for(ScheduleRealInfo s : findAll){
  2371 + if(s.getBcType().equals("region") || isInOut(s)){
  2372 + continue; //区间班次与空驶班次不做首末班统计
  2373 + }
  2374 + if(yyLine.containsKey(s.getXlBm())){ //只统计营运线路
  2375 + if(!(keyMap.containsKey(s.getXlBm()))){
  2376 + keyMap.put(s.getXlBm(), new ArrayList<ScheduleRealInfo>());
  2377 + }
  2378 + keyMap.get(s.getXlBm()).add(s);
  2379 +
  2380 + if(!(lineXX.containsKey(s.getXlBm()))){
  2381 + lineXX.put(s.getXlBm(), new HashMap<String, Object>());
  2382 + }
  2383 + Map<String, Object> xx = lineXX.get(s.getXlBm());
  2384 + if(!(xx.containsKey("lineName")) && s.getXlName() != null && s.getXlName().trim().length() > 0){
  2385 + xx.put("lineName", s.getXlName());
  2386 + }
  2387 + if(!(xx.containsKey("gsBm")) && s.getGsBm() != null && s.getGsBm().trim().length() > 0){
  2388 + xx.put("gsBm", s.getGsBm());
  2389 + }
  2390 + if(!(xx.containsKey("gsName")) && s.getGsName() != null && s.getGsName().trim().length() > 0){
  2391 + xx.put("gsName", s.getGsName());
  2392 + }
  2393 + if(!(xx.containsKey("fgsBm")) && s.getFgsBm() != null && s.getFgsBm().trim().length() > 0){
  2394 + xx.put("fgsBm", s.getFgsBm());
  2395 + }
  2396 + if(!(xx.containsKey("fgsName")) && s.getFgsName() != null && s.getFgsName().trim().length() > 0){
  2397 + xx.put("fgsName", s.getFgsName());
  2398 + }
  2399 + }
  2400 + }
  2401 +
  2402 + for(String lineCode : keyMap.keySet()){
  2403 + List<ScheduleRealInfo> listjh0 = new ArrayList<ScheduleRealInfo>();
  2404 + List<ScheduleRealInfo> listjh1 = new ArrayList<ScheduleRealInfo>();
  2405 + List<ScheduleRealInfo> listsj0 = new ArrayList<ScheduleRealInfo>();
  2406 + List<ScheduleRealInfo> listsj1 = new ArrayList<ScheduleRealInfo>();
  2407 + Long minFcsj = yyLine.get(lineCode);
  2408 + for(ScheduleRealInfo s : keyMap.get(lineCode)){
  2409 + String[] fcsj = s.getFcsj().split(":");
  2410 + Long fcsjL = Long.parseLong(fcsj[0]) * 60 + Long.parseLong(fcsj[1]);
  2411 + Long fscjT = 0L;
  2412 + if (fcsjL < minFcsj) {
  2413 + Calendar calendar = new GregorianCalendar();
  2414 + calendar.setTime(s.getScheduleDate());
  2415 + calendar.add(calendar.DATE, 1);
  2416 + Date d = calendar.getTime();
  2417 + try {
  2418 + fscjT = sdf.parse(sdf_.format(d) + " " + s.getFcsj()).getTime();
  2419 + } catch (ParseException e) {
  2420 + // TODO Auto-generated catch block
  2421 + e.printStackTrace();
  2422 + }
  2423 + } else {
  2424 + try {
  2425 + fscjT = sdf.parse(s.getScheduleDateStr() + " " + s.getFcsj()).getTime();
  2426 + } catch (ParseException e) {
  2427 + // TODO Auto-generated catch block
  2428 + e.printStackTrace();
  2429 + }
  2430 + }
  2431 +
  2432 + if (StringUtils.isEmpty(s.getFcsjActual())) {
  2433 + s.setFcsjActualTime(0L);
  2434 + s.setFcsjT(fscjT);
  2435 + } else {
  2436 + Long fcsjAcual = 0L;
  2437 + try {
  2438 + fcsjAcual = sdf.parse(s.getRealExecDate() + " " + s.getFcsjActual()).getTime();
  2439 + if(fcsjAcual - fscjT > (20l*60*60*1000)){//如果实发比计发大20小时则认为日期有误,如实发在次日,计发为00:00,实发为23:59
  2440 + fcsjAcual -= 1l*24*60*60*1000;
  2441 + } if(fscjT - fcsjAcual > (20l*60*60*1000)){
  2442 + fcsjAcual += 1l*24*60*60*1000;
  2443 + }
  2444 + s.setFcsjActualTime(fcsjAcual);
  2445 + s.setFcsjT(fscjT);
  2446 + } catch (ParseException e) {
  2447 + // TODO Auto-generated catch block
  2448 + e.printStackTrace();
  2449 + }
  2450 + }
  2451 +
  2452 +// if (!(s.isSflj())) {
  2453 + if (s.getXlDir().equals("0")) {
  2454 + listjh0.add(s);
  2455 + } else {
  2456 + listjh1.add(s);
  2457 + }
  2458 +// }
  2459 +
  2460 + if(s.getFcsjActual() != null && s.getFcsjActual().trim().length() > 0){
  2461 + if (s.getXlDir().equals("0")) {
  2462 + listsj0.add(s);
  2463 + } else {
  2464 + listsj1.add(s);
  2465 + }
  2466 + }
  2467 + }
  2468 +
  2469 + Collections.sort(listjh0, new ComparableJhfc());
  2470 + Collections.sort(listjh1, new ComparableJhfc());
  2471 + Collections.sort(listsj0, new ComparableAcual());
  2472 + Collections.sort(listsj1, new ComparableAcual());
  2473 + int jhFirst = 0, jhLast = 0, sjzdFirst = 0, sjzdLast = 0;
  2474 +
  2475 + //上行
  2476 + if(listjh0.size() > 0){
  2477 + ScheduleRealInfo jhsb = listjh0.get(0); //计划首班
  2478 + if("1".equals(sfss) ? createDate.getTime() > jhsb.getFcsjT() : true){
  2479 + jhFirst++;
  2480 + if(listsj0.size() > 0){
  2481 + ScheduleRealInfo sjsb = listsj0.get(0); //实际首班
  2482 + long wd = sjsb.getFcsjActualTime() - jhsb.getFcsjT(); //误点,快1慢2
  2483 + if(-1l * 1000 * 60 <= wd && wd <= 2l * 1000 * 60){
  2484 + sjzdFirst++;
  2485 + }
  2486 + }
  2487 + }
  2488 +
  2489 + if(listjh0.size() > 1){
  2490 + ScheduleRealInfo jhmb = listjh0.get(listjh0.size() - 1); //计划末班
  2491 + if("1".equals(sfss) ? createDate.getTime() > jhmb.getFcsjT() : true){
  2492 + jhLast++;
  2493 + if(listsj0.size() > 1){
  2494 + ScheduleRealInfo sjmb = listsj0.get(listsj0.size() - 1); //实际末班
  2495 + long wd = sjmb.getFcsjActualTime() - jhmb.getFcsjT(); //误点,快1慢2
  2496 + if(-1l * 1000 * 60 <= wd && wd <= 2l * 1000 * 60){
  2497 + sjzdLast++;
  2498 + }
  2499 + }
  2500 + }
  2501 + }
  2502 + }
  2503 +
  2504 + //下行
  2505 + if(listjh1.size() > 0){
  2506 + ScheduleRealInfo jhsb = listjh1.get(0); //计划首班
  2507 + if("1".equals(sfss) ? createDate.getTime() > jhsb.getFcsjT() : true){
  2508 + jhFirst++;
  2509 + if(listsj1.size() > 0){
  2510 + ScheduleRealInfo sjsb = listsj1.get(0); //实际首班
  2511 + long wd = sjsb.getFcsjActualTime() - jhsb.getFcsjT(); //误点,快1慢2
  2512 + if(-1l * 1000 * 60 <= wd && wd <= 2l * 1000 * 60){
  2513 + sjzdFirst++;
  2514 + }
  2515 + }
  2516 + }
  2517 +
  2518 + if(listjh1.size() > 1){
  2519 + ScheduleRealInfo jhmb = listjh1.get(listjh1.size() - 1); //计划末班
  2520 + if("1".equals(sfss) ? createDate.getTime() > jhmb.getFcsjT() : true){
  2521 + jhLast++;
  2522 + if(listsj1.size() > 1){
  2523 + ScheduleRealInfo sjmb = listsj1.get(listsj1.size() - 1); //实际末班
  2524 + long wd = sjmb.getFcsjActualTime() - jhmb.getFcsjT(); //误点,快1慢2
  2525 + if(-1l * 1000 * 60 <= wd && wd <= 2l * 1000 * 60){
  2526 + sjzdLast++;
  2527 + }
  2528 + }
  2529 + }
  2530 + }
  2531 + }
  2532 +
  2533 + Map<String, Object> map = new HashMap<String, Object>();
  2534 + map.put("lineCode", lineCode);
  2535 + Map<String, Object> xx = lineXX.get(lineCode);
  2536 + map.put("lineName", xx!=null&&xx.get("lineName")!=null?xx.get("lineName"):"");
  2537 + map.put("gsBm", xx!=null&&xx.get("gsBm")!=null?xx.get("gsBm"):"");
  2538 + map.put("gsName", xx!=null&&xx.get("gsName")!=null?xx.get("gsName"):"");
  2539 + map.put("fgsBm", xx!=null&&xx.get("fgsBm")!=null?xx.get("fgsBm"):"");
  2540 + map.put("fgsName", xx!=null&&xx.get("fgsName")!=null?xx.get("fgsName"):"");
  2541 + map.put("planFirst", jhFirst);
  2542 + map.put("planLast", jhLast);
  2543 + map.put("realFirst", sjzdFirst);
  2544 + map.put("realLast", sjzdLast);
  2545 + int jhbc = jhFirst + jhLast;
  2546 + int sjzdbc = sjzdFirst + sjzdLast;
  2547 + if(jhbc > 0){
  2548 + double zdl = (sjzdbc * 1.0) / (jhbc * 1.0) * 100;
  2549 + map.put("zdl", df.format(zdl));
  2550 + } else {
  2551 + map.put("zdl", "0");
  2552 + }
  2553 + if(jhFirst > 0){
  2554 + double zdlFirst = (sjzdFirst * 1.0) / (jhFirst * 1.0) * 100;
  2555 + map.put("zdlFirst", df.format(zdlFirst));
  2556 + } else {
  2557 + map.put("zdlFirst", "0");
  2558 + }
  2559 + if(jhLast > 0){
  2560 + double zdlLast = (sjzdLast * 1.0) / (jhLast * 1.0) * 100;
  2561 + map.put("zdlLast", df.format(zdlLast));
  2562 + } else {
  2563 + map.put("zdlLast", "0");
  2564 + }
  2565 +
  2566 + resList.add(map);
  2567 +
  2568 + }
  2569 +
  2570 + return resList;
  2571 + }
  2572 +
2308 public List<Map<String, Object>> createMap(String type, String[] dates){ 2573 public List<Map<String, Object>> createMap(String type, String[] dates){
2309 List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); 2574 List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
2310 2575
@@ -2556,4 +2821,21 @@ public class BigscreenService { @@ -2556,4 +2821,21 @@ public class BigscreenService {
2556 return ksgl; 2821 return ksgl;
2557 } 2822 }
2558 2823
  2824 + class ComparableJhfc implements Comparator<ScheduleRealInfo>{
  2825 +
  2826 + @Override
  2827 + public int compare(ScheduleRealInfo o1, ScheduleRealInfo o2) {
  2828 + // TODO Auto-generated method stub
  2829 + return o1.getFcsjT().compareTo(o2.getFcsjT());
  2830 + }
  2831 + }
  2832 + class ComparableAcual implements Comparator<ScheduleRealInfo>{
  2833 +
  2834 + @Override
  2835 + public int compare(ScheduleRealInfo o1, ScheduleRealInfo o2) {
  2836 + // TODO Auto-generated method stub
  2837 + return o1.getFcsjActualTime().compareTo(o2.getFcsjActualTime());
  2838 + }
  2839 + }
  2840 +
2559 } 2841 }