Commit f07c06b3f378d8df7d07e685507fa20b75f3b075
1 parent
31c8db92
到离站查询优化
Showing
1 changed file
with
35 additions
and
9 deletions
src/main/java/com/bsth/service/report/impl/ReportServiceImpl.java
| @@ -253,6 +253,7 @@ public class ReportServiceImpl implements ReportService{ | @@ -253,6 +253,7 @@ public class ReportServiceImpl implements ReportService{ | ||
| 253 | 253 | ||
| 254 | 254 | ||
| 255 | public List<ArrivalInfo> load2(String line,Long date1,Long date2,Date dates1,Date dates2,String zd,String zdlx,String fcsj){ | 255 | public List<ArrivalInfo> load2(String line,Long date1,Long date2,Date dates1,Date dates2,String zd,String zdlx,String fcsj){ |
| 256 | + SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); | ||
| 256 | List<ArrivalInfo> list = new ArrayList<ArrivalInfo>(); | 257 | List<ArrivalInfo> list = new ArrayList<ArrivalInfo>(); |
| 257 | Calendar cal = Calendar.getInstance(); | 258 | Calendar cal = Calendar.getInstance(); |
| 258 | cal.setTime(dates1); | 259 | cal.setTime(dates1); |
| @@ -264,18 +265,43 @@ public class ReportServiceImpl implements ReportService{ | @@ -264,18 +265,43 @@ public class ReportServiceImpl implements ReportService{ | ||
| 264 | PreparedStatement ps = null; | 265 | PreparedStatement ps = null; |
| 265 | ResultSet rs = null; | 266 | ResultSet rs = null; |
| 266 | String year=fcsj.substring(0,4); | 267 | String year=fcsj.substring(0,4); |
| 267 | - String sql = "select * from bsth_c_arrival_info_"+year+" where line_id=? AND weeks_year>=? " | ||
| 268 | - + " AND weeks_year<=? AND ts > ? AND ts <=? AND up_down=? AND stop_no like ? order by ts"; | 268 | + |
| 269 | + String year1 = yearFormat.format(dates1); | ||
| 270 | + String year2 = yearFormat.format(dates2); | ||
| 271 | + List<Integer> weeksList = new ArrayList<Integer>(); | ||
| 272 | + if(year1.equals(year2)){ | ||
| 273 | + //把每一周的编号加入list,一年最多五十几周。最多循环60次避免意外死循环 | ||
| 274 | + for(int w = 0, weeks_num = weeks_year1; w < 60 && weeks_num <= weeks_year2; w++, weeks_num++){ | ||
| 275 | + weeksList.add(weeks_num); | ||
| 276 | + } | ||
| 277 | + } else { | ||
| 278 | + weeksList.add(weeks_year1); | ||
| 279 | + weeksList.add(weeks_year2); | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + String tempStr = ""; | ||
| 283 | + for(int i = 0; i < weeksList.size(); i++){ | ||
| 284 | + tempStr += "?,"; | ||
| 285 | + } | ||
| 286 | + if(tempStr.length() > 0){ | ||
| 287 | + tempStr = tempStr.substring(0, tempStr.length() - 1); | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + String sql = "select * from bsth_c_arrival_info_"+year+" where weeks_year in ("+tempStr+") " | ||
| 291 | + + " AND line_id=? AND up_down=? AND stop_no like ? AND ts between ? AND ? order by ts"; | ||
| 292 | + System.out.println(sql); | ||
| 269 | try{ | 293 | try{ |
| 270 | conn = DBUtils_MS.getConnection(); | 294 | conn = DBUtils_MS.getConnection(); |
| 271 | ps = conn.prepareStatement(sql); | 295 | ps = conn.prepareStatement(sql); |
| 272 | - ps.setString(1, line); | ||
| 273 | - ps.setInt(2, weeks_year1); | ||
| 274 | - ps.setInt(3, weeks_year2); | ||
| 275 | - ps.setLong(4, date1); | ||
| 276 | - ps.setLong(5, date2); | ||
| 277 | - ps.setString(6, zdlx); | ||
| 278 | - ps.setString(7, "%"+zd+"%"); | 296 | + int no = 1; |
| 297 | + for(;no <= weeksList.size(); no++){ | ||
| 298 | + ps.setInt(no, weeksList.get(no-1)); | ||
| 299 | + } | ||
| 300 | + ps.setString(no++, line); | ||
| 301 | + ps.setString(no++, zdlx); | ||
| 302 | + ps.setString(no++, ""+zd+"%"); | ||
| 303 | + ps.setLong(no++, date1); | ||
| 304 | + ps.setLong(no++, date2); | ||
| 279 | rs = ps.executeQuery(); | 305 | rs = ps.executeQuery(); |
| 280 | 306 | ||
| 281 | list = resultSet2Set(rs); | 307 | list = resultSet2Set(rs); |