package idc.service.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import idc.model.*;

import java.sql.*;

import utility.*;

public class IdcKCSWriteHandleCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
				
		String kcs_code = request.getParameter("kcs_code");
		String kcs_ip = request.getParameter("kcs_ip");
		String service_code = request.getParameter("service_code");
		String kcs_use_domain = request.getParameter("kcs_use_domain");
		String kcs_connect_domain = request.getParameter("kcs_connect_domain");
		String last_date = request.getParameter("kcs_last_date");
		String kcs_note = request.getParameter("kcs_note");
		String kcs_admin = request.getParameter("service_admin");
		StringToDate st = new StringToDate();			
		Date kcs_last_date; 
		
		if(last_date == "" || last_date == null){
			kcs_last_date = null;
		}else{
			kcs_last_date = st.stringToDate(last_date);
		}
				
		insertKCSDB(kcs_code, kcs_ip, service_code, kcs_use_domain, kcs_connect_domain, kcs_last_date, kcs_note, kcs_admin);
		
	}
	
	//KCS DB입력
	private void insertKCSDB(String kcs_code, String kcs_ip, String service_code, String kcs_use_domain, String kcs_connect_domain,
			Date kcs_last_date, String kcs_note, String kcs_admin){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2=null, pstmt3=null,pstmt4=null,pstmt5=null, pstmt6=null;
		ResultSet rs = null, rs3=null,rs4=null,rs5=null, rs6=null;
		
		try {
			conn = dao.getConn();
			String sql="";
			
			int adddb_num=0;
			String client_code="";
			String server_code = "";
			String client_name="";
			String amount="50000";
			Date adddb_payment_date=null;
			int csCount=0;
			
			//cs갯수 얻어와서 금액 바꾸기
			sql="SELECT COUNT(adddb_case) as a FROM adddb WHERE service_code=? AND adddb_case='cs' ";
			pstmt6 = conn.prepareStatement(sql);
			pstmt6.setString(1, service_code);
			rs6 = pstmt6.executeQuery();
			if(rs6.next())
				csCount=rs6.getInt("a");
			if(csCount==0)
				amount="0";
			
//			client_code, server_code, client_name을 얻어온다.
			sql="SELECT client_name, server_code, s.client_code FROM servicedb as s, clientdb as c WHERE service_code=? AND s.client_code = c.client_code";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3 = pstmt3.executeQuery();
			if(rs3.next())
			{
				client_code = rs3.getString("client_code");
				server_code = rs3.getString("server_code");
				client_name = rs3.getString("client_name");
			}
			
//			통합으로 할 경우 결제일을 얻어온다.
			sql="SELECT adddb_payment_date FROM adddb WHERE service_code=? AND adddb_case='service'";
			pstmt4=conn.prepareStatement(sql);
			pstmt4.setString(1, service_code);
			rs4=pstmt4.executeQuery();
			if(rs4.next())
				adddb_payment_date = rs4.getDate("adddb_payment_date");

//			adddb에 넣기.
			sql="INSERT INTO adddb (adddb_case, service_code, adddb_request_date, adddb_amount, adddb_code, client_code, ";
			sql+="adddb_united, adddb_accountStatus, adddb_handle_date, adddb_nonpayment, adddb_payment,adddb_payment_date, ";
			sql+="adddb_original_date) ";
			sql+=" values(?,?,curdate(),?,?,?,'o','incomplete',curdate(),?,'0',?,?)";
			
			String addDB_case = "cs"; 
			String addDB_code = kcs_code;
			
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, addDB_case);
			pstmt2.setString(2, service_code);
			pstmt2.setString(3, amount);
			pstmt2.setString(4, addDB_code);
			pstmt2.setString(5, client_code);
			pstmt2.setString(6, amount);
			pstmt2.setDate(7, adddb_payment_date);
			pstmt2.setDate(8, adddb_payment_date);
			pstmt2.executeUpdate();
			
//			히스토리 등록
			IdcSetHistory.setHistory(service_code, server_code, "kcs-"+addDB_case+" 추가", kcs_ip, addDB_code, "",client_name);
//			서비스에 해당 금액 업데이트.
			IdcUpdateServiceAmount.UpdateAmount(amount, service_code, true, adddb_num,null);
//			amountdb에 데이터 업데이트
			IdcUpdateServiceAmount.amountdbUpdate(amount, service_code, "cs");
			
//			IP업뎃
//			csdb 수정하기
			IdcSetCS.setCS(service_code, server_code, kcs_code);
			IdcSetCS.modifyCS(kcs_code, "kcs");
//			adddb 넘버링
			sql="SELECT adddb_num FROM adddb WHERE adddb_code=?";
			pstmt5=conn.prepareStatement(sql);
			pstmt5.setString(1, kcs_code);
			rs5=pstmt5.executeQuery();
			if(rs5.next())
				adddb_num=rs5.getInt("adddb_num");
			
			sql = "INSERT INTO kcsDB (kcs_code, kcs_ip, service_code, kcs_use_domain, kcs_connect_domain, kcs_last_date, kcs_note, kcs_request_date, adddb_num"
					+ ", kcs_admin) ";			
			sql += "values(?, ?, ?, ?, ?, ?, ?, curdate(),?,?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, kcs_code);
			pstmt.setString(2, kcs_ip);
			pstmt.setString(3, service_code);
			pstmt.setString(4, kcs_use_domain);
			pstmt.setString(5, kcs_connect_domain);
			pstmt.setDate(6, kcs_last_date);
			pstmt.setString(7, kcs_note);		
			pstmt.setInt(8, adddb_num);
			pstmt.setString(9, kcs_admin);
			
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
				if(rs3 != null) rs3.close();
				if(pstmt3 != null) pstmt3.close();
				if(rs4 != null) rs4.close();
				if(pstmt4 != null) pstmt4.close();
				if(rs5 != null) rs5.close();
				if(pstmt5 != null) pstmt5.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

}
