package idc.service.command;

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

import utility.StringToDate;
import idc.model.*;

import java.sql.*;

public class IdcServiceEtcHandleCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	//1회성 서비스 추가처리
	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 service_code = request.getParameter("service_code");
		String client_code = request.getParameter("client_code");
		
		request.setAttribute("service_code", service_code);
		request.setAttribute("client_code", client_code);
		
		String onetime_case = request.getParameter("onetime_case");
		String onetime_sub = request.getParameter("onetime_sub");
				
		String onetime_amount = request.getParameter("onetime_amount");
		String onetime_payment = request.getParameter("onetime_payment");
		String onetime_nonpayment = request.getParameter("onetime_nonpayment");
		
		if(onetime_amount == null || onetime_amount == ""){
			onetime_amount = "0";
		}
		
		if(onetime_payment == null || onetime_payment == ""){
			onetime_payment = "0";
		}
		
		if(onetime_nonpayment == null || onetime_nonpayment == ""){
			onetime_nonpayment = "0";
		}
		
		IdcMemberDBDTO d1to = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		String id = d1to.getId();
		String onetime_manager = id;		
		String request_date = request.getParameter("onetime_request_date");
		String payment_date = request.getParameter("onetime_payment_date");
		String onetime_note = request.getParameter("onetime_note");
		
		StringToDate st = new StringToDate();				
		
		Date onetime_request_date = null;
		if(request_date != null && request_date != ""){
			onetime_request_date = st.stringToDate(request_date);			
		}
		
		Date onetime_payment_date = null;
		if(payment_date != null && payment_date != ""){
			onetime_payment_date = st.stringToDate(payment_date);			
		}
				
		int amount = Integer.parseInt(onetime_amount);
		int payment = Integer.parseInt(onetime_payment);
		int result = amount - payment;
		String onetime_accountStatus;
		
		if(result <= 0 ){
			onetime_accountStatus =  "complete";
		}else{
			onetime_accountStatus = "incomplete";
		}
		
		String client_name = request.getParameter("client_name");
		String server_code = request.getParameter("server_code");
		
		String vpnFlag = request.getParameter("vpnFlag");
		request.setAttribute("vpnFlag", vpnFlag);
				
		insertOnetimeDB(service_code, client_code, onetime_case, onetime_sub, onetime_amount, onetime_payment,
				onetime_nonpayment, onetime_accountStatus, onetime_request_date, onetime_payment_date, onetime_note, 
				onetime_manager, client_name, server_code);
		
	}
	
	private void insertOnetimeDB(String service_code, String client_code, String onetime_case, String onetime_sub, 
			String onetime_amount, String onetime_payment, String onetime_nonpayment, String onetime_accountStatus, 
			Date onetime_request_date, Date onetime_payment_date, String onetime_note, String onetime_manager,
			String cn, String sc
			){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt3=null, pstmt4=null;
		ResultSet rs = null,rs3=null, rs4=null;
		
		try {
			conn = dao.getConn();
			String sql = "INSERT INTO onetimeDB (client_code,service_code, onetime_case, onetime_sub, ";
			sql += " onetime_amount, onetime_payment, onetime_nonpayment, ";
			sql += " onetime_accountStatus, onetime_request_date, onetime_payment_date, onetime_note, ";
			sql += " onetime_handle_date, onetime_manager, client_name, server_code) ";
			sql += " values(?,?,?,?,?,?,?,?,?,?,?,curdate(),?,?,?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			pstmt.setString(2, service_code);
			pstmt.setString(3, onetime_case);
			pstmt.setString(4, onetime_sub);
			pstmt.setString(5, onetime_amount);
			pstmt.setString(6, onetime_payment);
			pstmt.setString(7, onetime_nonpayment);
			pstmt.setString(8, onetime_accountStatus);
			pstmt.setDate(9, onetime_request_date);
			pstmt.setDate(10, onetime_payment_date);
			pstmt.setString(11, onetime_note);
			pstmt.setString(12, onetime_manager);
			pstmt.setString(13, cn);
			pstmt.setString(14, sc);			
			pstmt.executeUpdate();
			
			String server_code = "";
			String client_name="";
			String amount=onetime_amount;
			Date adddb_payment_date=null;
			
			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");
			}
			
			//adddb 넣기 결제용
			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");

			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, adddb_onetime) ";
			sql+=" values(?,?,curdate(),?,?,?,'o','incomplete',curdate(),?,'0',?,'o')";
	
			String addDB_case = onetime_case; 
			String addDB_code = onetime_sub;
						
//			히스토리 등록
			IdcSetHistory.setHistory(service_code, server_code, addDB_case + "추가", addDB_code, addDB_case, "",client_name);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(rs3 != null) rs3.close();
				if(rs4 != null) rs4.close();				
				if(pstmt != null) pstmt.close();
				if(pstmt3 != null) pstmt3.close();
				if(pstmt4 != null) pstmt4.close();
				
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
		
}