package com.qcs.service;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.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;import com.qcs.bean.Plan;
import com.qcs.util.DB;
import com.qcs.util.DateUtils;public class PlanService { public static PlanService instance = new PlanService();

private PlanService() {
super();
}

public static PlanService getInstance() {
return instance;
}

private String getPlanDriver(Connection conn, String busNum, Date startTime) throws SQLException {
String planDriver = null;
String date = new SimpleDateFormat("yyyy-MM-dd").format(startTime);
String time = new SimpleDateFormat("HH:mm").format(startTime);
String sql1 = "select distinct xm from xl_bcb_plan_ry_fcsj_view where clbh = ? and rq = ? and fcsj = ?";
String sql2 = "select distinct xm from xl_bcb_plan_ry_fcsj_wb_view where clbh = ? and rq = ? and fcsj = ?";
String sql3 = "select distinct xm from xl_bcb_work_view where clbh = ? and rq = ? ";
PreparedStatement pstmt = null;
ResultSet rs = null;
pstmt = conn.prepareStatement(sql1);
pstmt.setString(1, busNum);
pstmt.setString(2, date);
pstmt.setString(3, time);
rs = pstmt.executeQuery();
if(rs.next()) {
planDriver = rs.getString("xm");
} else {
pstmt = conn.prepareStatement(sql2);
pstmt.setString(1, busNum);
pstmt.setString(2, date);
pstmt.setString(3, time);
rs = pstmt.executeQuery();
if(rs.next()) {
planDriver = rs.getString("xm");
}else {
pstmt = conn.prepareStatement(sql3);
pstmt.setString(1, busNum);
pstmt.setString(2, date);
rs = pstmt.executeQuery();
if(rs.next()){
planDriver = rs.getString("xm");
}
}

return planDriver;
}

public List<Plan> queryPlanByLine(String startDate, String endDate, String lineNum) {
StringBuffer sql = new StringBuffer();
sql.append("select distinct qdfcjg,zdfcjg,jh.clbh, jh.cl_zbh,jh.cl_pzh, sj.xm, jh.qdzdmc, " +
"jh.zdzdmc, jh.fcsj, jh.yysj, sj.sjfcsj, sj.jhdzsj, sj.sjdzsj, st.StopValue," +
" st.memo from Server_dd_yyjh_bak_view jh ")
   .append("left join server_dd_yysjsave_view sj ")
   .append("on jh.xlbh = sj.xlbh and jh.clbh = sj.clbh and jh.fcsj = sj.jhfcsj ") 
   .append("left join xlb_2th xl ")
   .append("on xl.xlbh = jh.xlbh and xl.qdzbh = jh.qdzbh ") 
   //and xl.qdzbh = jh.qdzbh
   .append("left join server_dd_stopLSZ st ")
   .append("on jh.fcsj = st.lastmodi and jh.clbh = st.clbh ") 
   .append("where jh.rq >= ? and jh.rq <= ? and jh.xlbh = ? ")
   .append("order by jh.cl_pzh,jh.fcsj");
//System.out.println(sql);
List<Plan> planList = new ArrayList<Plan>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int index = 0;
try {
conn = DB.getConnection();
pstmt = conn.prepareStatement(sql.toString());
pstmt.setString(1, startDate);
pstmt.setString(2, endDate);
pstmt.setString(3, lineNum);
rs = pstmt.executeQuery();
while(rs.next()) {
Plan plan = new Plan();
plan.setIndex(++ index);
plan.setZdzjg(rs.getInt("zdfcjg"));
plan.setQdzjg(rs.getInt("qdfcjg"));
plan.setSelfNum(rs.getString("cl_zbh"));
plan.setBusPNum(rs.getString("cl_pzh"));
plan.setDriver(rs.getString("xm"));//得不到姓名
if(plan.getDriver() == null) {
String clbh = rs.getString("clbh");
Date fcsj = rs.getTimestamp("fcsj");
plan.setPlanDriver(this.getPlanDriver(conn, clbh, fcsj));
}
plan.setStartPoint(rs.getString("qd"));
plan.setEndPoint(rs.getString("zd"));
plan.setPlanStartTime(rs.getTimestamp("fcsj"));
plan.setPlanEndTime(rs.getTimestamp("jhdzsj"));
if(plan.getPlanEndTime() == null) {
plan.setPlanEndTime(DateUtils.getEndDate(plan.getPlanStartTime(), rs.getInt("yysj")));
}
plan.setFactStartTime(rs.getTimestamp("sjfcsj"));
plan.setFactEndTime(rs.getTimestamp("sjdzsj"));
plan.setAssess(rs.getString("StopValue") == null ? " " : rs.getString("StopValue"));
plan.setRe(rs.getString("memo") == null ? " " : rs.getString("memo"));
planList.add(plan);
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(pstmt);
DB.close(conn);
}
return planList;
}

public List<Plan> queryPlanByDriver(String startDate, String endDate, String driverNum) {
StringBuffer sql = new StringBuffer();
sql.append("select distinct jh.qdfcjg,jh.zdfcjg,jh.clbh, jh.cl_zbh,jh.cl_pzh,sj.rybh, " +
"sj.xm,jhxm=(bc.xm), jh.fcsj, jh.yysj, sj.sjfcsj, sj.jhdzsj, sj.sjdzsj,st.StopValue," +
" st.memo from Server_dd_yyjh_bak_view jh ")
   .append("left join xl_bcb_work_view bc ")
   .append("on jh.rq = bc.rq and jh.xlbh_2th = bc.xlbh_2th and jh.bc = bc.dbbc ")
   .append("left join server_dd_yysjsave_view sj ")
   .append("on jh.xlbh = sj.xlbh and jh.clbh = sj.clbh and jh.fcsj = sj.jhfcsj ") 
   .append("left join xlb_2th xl ")
   .append("on xl.xlbh = jh.xlbh ") 
   //and xl.qdzbh = jh.qdzbh 
   .append("left join server_dd_stopLSZ st ")
   .append("on jh.fcsj = st.lastmodi and jh.clbh = st.clbh ") 
   //.append("left join server_dd_yycl_changeInfo ch ") 
   //.append("on jh.fcsj = ch.lastmodi and jh.clbh = ch.clbh ") 
   .append("where jh.rq >= ? and jh.rq <= ? and bc.rybh = ? ")
//    .append("and (sj.rybh = ? or sj.rybh is null) ")
   .append("order by jh.cl_pzh,jh.fcsj");
List<Plan> planList = new ArrayList<Plan>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int index = 0;
try {
conn = DB.getConnection();
pstmt = conn.prepareStatement(sql.toString());
pstmt.setString(1, startDate);
pstmt.setString(2, endDate);
pstmt.setString(3, driverNum);
rs = pstmt.executeQuery();
while(rs.next()) {
Plan plan = new Plan();
plan.setIndex(++ index);
plan.setZdzjg(rs.getInt("zdfcjg"));
plan.setQdzjg(rs.getInt("qdfcjg"));
plan.setSelfNum(rs.getString("cl_zbh"));
plan.setBusPNum(rs.getString("cl_pzh"));
plan.setDriver(rs.getString("xm"));
if(plan.getDriver() == null) {
plan.setPlanDriver(this.getPlanDriver(conn, rs.getString("clbh"), rs.getTimestamp("fcsj")));
}
plan.setStartPoint(rs.getString("qd"));
plan.setEndPoint(rs.getString("zd"));
plan.setPlanStartTime(rs.getTimestamp("fcsj"));
plan.setPlanEndTime(rs.getTimestamp("jhdzsj"));
if(plan.getPlanEndTime() == null) {
plan.setPlanEndTime(DateUtils.getEndDate(plan.getPlanStartTime(), rs.getInt("yysj")));
}
plan.setFactStartTime(rs.getTimestamp("sjfcsj"));
plan.setFactEndTime(rs.getTimestamp("sjdzsj"));
plan.setAssess(rs.getString("StopValue") == null ? " " : rs.getString("StopValue"));
plan.setRe(rs.getString("memo") == null ? " " : rs.getString("memo"));
planList.add(plan);
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(pstmt);
DB.close(conn);
}
return planList;
}

public List<Plan> queryPlanByBus(String startDate, String endDate, String busNum) {
StringBuffer sql = new StringBuffer();
sql.append("select distinct jh.qdfcjg,jh.zdfcjg,jh.clbh, jh.cl_zbh,jh.cl_pzh, sj.xm, jh.fcsj, jh.yysj, sj.sjfcsj, sj.jhdzsj, sj.sjdzsj, st.StopValue, st.memo from Server_dd_yyjh_bak_view jh ")
   .append("left join server_dd_yysjsave_view sj ")
   .append("on jh.xlbh = sj.xlbh and jh.clbh = sj.clbh and jh.fcsj = sj.jhfcsj ") 
   .append("left join xlb_2th xl ")
   .append("on xl.xlbh = jh.xlbh  ") 
   //and xl.qdzbh = jh.qdzbh
   .append("left join server_dd_stopLSZ st ")
   .append("on jh.fcsj = st.lastmodi and jh.clbh = st.clbh ") 
   //.append("left join server_dd_yycl_changeInfo ch ") 
   //.append("on jh.fcsj = ch.lastmodi and jh.clbh = ch.clbh ") 
   .append("where jh.rq >= ? and jh.rq <= ? and jh.clbh = ? ")
   .append("order by jh.cl_pzh,jh.fcsj");
List<Plan> planList = new ArrayList<Plan>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int index = 0;
try {
conn = DB.getConnection();
pstmt = conn.prepareStatement(sql.toString());
pstmt.setString(1, startDate);
pstmt.setString(2, endDate);
pstmt.setString(3, busNum);
rs = pstmt.executeQuery();
while(rs.next()) {
Plan plan = new Plan();
plan.setIndex(++ index);
plan.setZdzjg(rs.getInt("zdfcjg"));
plan.setQdzjg(rs.getInt("qdfcjg"));
plan.setSelfNum(rs.getString("cl_zbh"));
plan.setBusPNum(rs.getString("cl_pzh"));
plan.setDriver(rs.getString("xm"));
if(plan.getDriver() == null) {
plan.setPlanDriver(this.getPlanDriver(conn, rs.getString("clbh"), rs.getTimestamp("fcsj")));
}
plan.setStartPoint(rs.getString("qd"));
plan.setEndPoint(rs.getString("zd"));
plan.setPlanStartTime(rs.getTimestamp("fcsj"));
plan.setPlanEndTime(rs.getTimestamp("jhdzsj"));
if(plan.getPlanEndTime() == null) {
plan.setPlanEndTime(DateUtils.getEndDate(plan.getPlanStartTime(), rs.getInt("yysj")));
}
plan.setFactStartTime(rs.getTimestamp("sjfcsj"));
plan.setFactEndTime(rs.getTimestamp("sjdzsj"));
plan.setAssess(rs.getString("StopValue") == null ? " " : rs.getString("StopValue"));
plan.setRe(rs.getString("memo") == null ? " " : rs.getString("memo"));
planList.add(plan);
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(pstmt);
DB.close(conn);
}
return planList;
}
}