package idc.service.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcSetHistory;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import utility.IdcGetClientNameCode;

public class IdcETCAddCmd implements IdcCmd
{
	private IdcDAO dao = new IdcDAO();
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		
		setETC(request);
	}
	
	//adddb에 추가
	private void setETC(HttpServletRequest request){
		
		String service_code = request.getParameter("service_code");
		String server_code = request.getParameter("server_code");
		String client_name = request.getParameter("client_name");
		String client_code = IdcGetClientNameCode.getClientCode(client_name);
		request.setAttribute("client_code", client_code);
		String amount = request.getParameter("account1");//가격
		String deduction = request.getParameter("account2");//일할과금
		
		String adddb_code = request.getParameter("addDB_code");//부가서비스 명
		String adddb_case = "etc";
		
		String service_ip = request.getParameter("service_ip");
		String adddb_note = request.getParameter("addDB_note");
		
		Date payment_date=null, request_date=null, cancel_date=null;
		String spayment_date=null, srequest_date=null, scancel_date=null;
		spayment_date = request.getParameter("service_payment_date");
		srequest_date = request.getParameter("service_request_date");
		scancel_date = request.getParameter("service_cancel_date");
		
		if(spayment_date!=null)
			payment_date = Date.valueOf(spayment_date);
		if(srequest_date!=null)
			request_date = Date.valueOf(srequest_date);
		if(scancel_date!=null)
			cancel_date = Date.valueOf(scancel_date);
				
		
		Connection conn=null;
		PreparedStatement pstmt[] = new PreparedStatement[5];
		ResultSet rs[] = new ResultSet[5];
		try	
		{
			conn=dao.getConn();
			String sql="INSERT INTO  adddb (adddb_case,	 			adddb_amount, 				adddb_nonpayment,		adddb_payment, service_code, ";
			sql+="										addDB_accountStatus, 	adddb_united,	 				adddb_payment_date, 		adddb_ip, 				adddb_code, ";
			sql+="										client_code,		 		adddb_handle_Date, 		adddb_manager, 			adddb_oneTime, 		adddb_cancel_date, ";
			sql+="										adddb_request_date, 	adddb_note,					isNext, 							adddb_original_date) ";
			sql+="values(?, ?, ?, ?, ?,        'incomplete', 'o', ?, ?, ?,      ?, curdate(), ?, 'x', ?,      ?, ?, 'o', ?)";
			pstmt[0] =conn.prepareStatement(sql);
			pstmt[0].setString(1, adddb_case);
			pstmt[0].setString(2, amount);
			pstmt[0].setString(3, Integer.toString(Integer.parseInt(amount)-Integer.parseInt(deduction)));
			pstmt[0].setString(4, deduction);
			pstmt[0].setString(5, service_code);
			
//			incomplete
//			o
			pstmt[0].setDate(6, payment_date);
			pstmt[0].setString(7, service_ip);
			pstmt[0].setString(8, adddb_code);
			
			pstmt[0].setString(9, client_code);
//			curdate
			pstmt[0].setString(10, null);
//			x
			pstmt[0].setDate(11, cancel_date);
			
			pstmt[0].setDate(12, request_date);
			pstmt[0].setString(13, adddb_note);
//			'o'
			pstmt[0].setDate(14, payment_date);
			pstmt[0].executeUpdate();
					
//			amountdb 업뎃
			sql="UPDATE amountdb SET amount_etc=?, service_amount=service_amount+?, amount_cost=amount_cost+? WHERE service_code=?";
			pstmt[2] = conn.prepareStatement(sql);
			pstmt[2].setString(1, amount);
			pstmt[2].setString(2, amount);
			pstmt[2].setString(3, amount);
			pstmt[2].setString(4, service_code);
			pstmt[2].executeUpdate();
			
//			history 설정.
			IdcSetHistory.setHistory(service_code, server_code, "기타 부가서비스 추가", service_ip, adddb_code, "",client_name);
			//IdcSetHistory.setHistory(service_code, server_code, "기타 부가서비스 추가","","기타 부가서비스 추가",mkworker, IdcGetClientNameCode.getClientName(client_code));
		}
		catch (Exception e) 
		{
			System.out.println("IdcETCAddCmd 에서의 exception");
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null) conn.close();
				for(int i=0;i<pstmt.length;i++)
				{
					if(rs[i]!=null)rs[i].close();
					if(pstmt[i]!=null)pstmt[i].close();
				}
			}catch (SQLException e){
				System.out.println("IdcETCAddCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
