package idc.business.command;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import idc.model.*;

public class IdcBusinesshandoverListCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
		
	public void execute(HttpServletRequest request, HttpServletResponse response){
		
		//본인 아이디/이름 받기
		IdcMemberDBDTO d1to = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		String id = d1to.getId();
		String name = d1to.getName();
		String part = d1to.getPart();
		String location = d1to.getLocation();
								
		//금일 완료된 업무 불러오기
		ArrayList<IdcWorkDTO> endWork_list = null;		
		endWork_list = getEndWorkDTO(1, name);		
		request.setAttribute("endWork_list", endWork_list);
		
		//금일 진행중인 업무
		ArrayList<IdcWorkDTO> ongoingWork_list = null;		
		ongoingWork_list = getEndWorkDTO(2, name);		
		request.setAttribute("ongoingWork_list", ongoingWork_list);
		
		//개인 업무 진행 사항 
		ArrayList<IdcWorkDTO> each_list = null;		
		each_list = getEndWorkDTO(3, name);		
		request.setAttribute("each_list", each_list);
				
		//전달사항
		ArrayList<IdcDeliveryDTO> delivery_list = null;
		delivery_list = getDeliveryDTO();
		request.setAttribute("delivery_list", delivery_list);
		
		//신규 상담 내역 불러오기
		ArrayList<IdcConsultDTO> consult_list = null;
		consult_list = getConsultDTO(id);
		request.setAttribute("consult_list", consult_list);
		
		//멤버별 IdcHandoverDTO
		IdcHandoverDTO ihd = null;
		ihd = getHandoverDTO(name, part, location);
		request.setAttribute("ihd", ihd);
		
	}
	
	//금일 완료된 업무 불러오기	
	public ArrayList<IdcWorkDTO> getEndWorkDTO(int work_flag, String name){
		ArrayList<IdcWorkDTO> work_list = new ArrayList<IdcWorkDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			
			String sql = "";
			if(work_flag == 1){				
				sql="SELECT * FROM workdb ";
				sql+=" WHERE work_finish_date=curdate()";				
			}else if(work_flag == 2){
				sql="SELECT * FROM workdb WHERE work_status!='end'";
			}else if(work_flag == 3){				
				sql="SELECT * FROM workdb WHERE work_status!='end' and work_manager='"+name+"'";								
			}
						
			pstmt = conn.prepareStatement(sql);			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				String work_sort = rs.getString("work_sort");
				String temp1 = "";
				switch (work_sort){
				case "new":
					temp1 = "신규";
					break;
				case "cancel":
					temp1 = "해지";
					break;
				case "test":
					temp1 = "테스트";
					break;
				case "substitution":
					temp1 = "대체";
					break;
				case "lineChange":
					temp1 = "회선변경";
					break;
				case "block":
					temp1 = "차단";
					break;
				case "defence":
					temp1 = "방어";
					break;
				case "disorder":
					temp1 = "장애";
					break;
				case "accounting":
					temp1 = "과금";
					break;
				case "request":
					temp1 = "요청";
					break;
				case "addService":
					temp1 = "부가서비스";
					break;
				}
				
				String client_name = rs.getString("client_name");
				String work_content = rs.getString("work_content");
				Date work_check_date = rs.getDate("work_check_date"); 
				Date work_end_date = rs.getDate("work_end_date");
				String work_status = rs.getString("work_status");
				String temp2 = "";
				
				switch (work_status){
				case "ongoing":
					temp2 = "진행중";
					break;
				case "delay":
					temp2 = "연기";
					break;
				case "end":
					temp2 = "마감";
					break;							
				}
				
				String work_manager = rs.getString("work_manager");
				
				IdcWorkDTO writing = new IdcWorkDTO();
				writing.setWork_sort(work_sort);
				writing.setClient_name(client_name);
				writing.setWork_content(work_content);
				writing.setWork_check_date(work_check_date);
				writing.setWork_end_date(work_end_date);
				writing.setWork_status(work_status);
				writing.setWork_manager(work_manager);
				writing.setTemp1(temp1);
				writing.setTemp2(temp2);
				
				work_list.add(writing);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return work_list;
	}

	public ArrayList<IdcDeliveryDTO> getDeliveryDTO(){
		ArrayList<IdcDeliveryDTO> delivery_list = new ArrayList<IdcDeliveryDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT delivery_sort, client_name, delivery_content, delivery_check_date, delivery_end_date, ";
			sql += "delivery_status, delivery_manager FROM deliveryDB ";
			sql += "WHERE delivery_status != 'end' ORDER BY delivery_num DESC";
			
			pstmt = conn.prepareStatement(sql);			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				String delivery_sort = rs.getString("delivery_sort");
				String delivery_temp = "";
				switch (delivery_sort){
				case "all":
					delivery_temp = "전체";
					break;
				case "group":
					delivery_temp = "그룹";
					break;
				case "each":
					delivery_temp = "개별";
					break;
				}
				
				String client_name = rs.getString("client_name");
				String delivery_content = rs.getString("delivery_content");
				Date delivery_check_date = rs.getDate("delivery_check_date"); 
				Date delivery_end_date = rs.getDate("delivery_end_date");
				String delivery_status = rs.getString("delivery_status");
				
				String delivery_temp1 = "";
				switch (delivery_status){
				case "ongoing":
					delivery_temp1 = "진행중";
					break;
				case "delay":
					delivery_temp1 = "연기";
					break;
				case "end":
					delivery_temp1 = "마감";
					break;
				}
				
				String delivery_manager = rs.getString("delivery_manager");
				
				IdcDeliveryDTO writing = new IdcDeliveryDTO();
				writing.setDelivery_sort(delivery_sort);
				writing.setClient_name(client_name);
				writing.setDelivery_content(delivery_content);
				writing.setDelivery_check_date(delivery_check_date);
				writing.setDelivery_end_date(delivery_end_date);
				writing.setDelivery_status(delivery_status);
				writing.setDelivery_manager(delivery_manager);
				writing.setDelivery_temp(delivery_temp);
				writing.setDelivery_temp1(delivery_temp1);				
				delivery_list.add(writing);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return delivery_list;
	}

	public ArrayList<IdcConsultDTO> getConsultDTO(String id){
		ArrayList<IdcConsultDTO> work_list = new ArrayList<IdcConsultDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			//오늘 완료된 업무
			String sql = "";		
			sql = "SELECT consult_manager, consult_name, consult_contact, consult_question, consult_answer, consult_add, consult_date FROM consultDB ";			
			sql += " WHERE consult_date > DATE_ADD(NOW(), INTERVAL -1 DAY)";			
			pstmt = conn.prepareStatement(sql);			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				String consult_manager = rs.getString("consult_manager");								
				String consult_name = rs.getString("consult_name");
				String consult_contact =rs.getString("consult_contact");
				String consult_question = rs.getString("consult_question");
				String consult_answer = rs.getString("consult_answer");
				String consult_add = rs.getString("consult_add");
				Date consult_date = rs.getDate("consult_date");				
				
				IdcConsultDTO writing = new IdcConsultDTO();				
				writing.setConsult_manager(consult_manager);
				writing.setConsult_name(consult_name);
				writing.setConsult_contact(consult_contact);
				writing.setConsult_question(consult_question);
				writing.setConsult_answer(consult_answer);
				writing.setConsult_add(consult_add);
				writing.setConsult_date(consult_date);				
				work_list.add(writing);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return work_list;
	}
	
	public IdcHandoverDTO getHandoverDTO(String name, String part, String location){		
		IdcHandoverDTO writing = new IdcHandoverDTO();
			
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			
		try {
			conn = dao.getConn();
			String sql = "SELECT *,curdate() as curdate FROM handoverDB WHERE name=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,name);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				
				String name1 = rs.getString("name1");
				String name2 = rs.getString("name2");
				String name3 = rs.getString("name3");
				String name4 = rs.getString("name4");
				String name5 = rs.getString("name5");
				String name6 = rs.getString("name6");
				String name7 = rs.getString("name7");
				String name8 = rs.getString("name8");
				Date curdate = rs.getDate("curdate");					

				writing.setName(name);
				writing.setName1(name1);
				writing.setName2(name2);
				writing.setName3(name3);
				writing.setName4(name4);
				writing.setName5(name5);
				writing.setName6(name6);
				writing.setName7(name7);
				writing.setName8(name8);
				writing.setCurdate(curdate);							
			}
												
			writing.setPart(part);
			writing.setLocation(location);
			
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {

			}
		}
				
		return writing;
	}
}
