package idc.deposit.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDepositDTO;

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.StringToDate;
public class IdcDepositWriteCmd implements IdcCmd 
{
	IdcDAO dao;
	StringToDate sd;
	public IdcDepositWriteCmd() 
	{
		dao = new IdcDAO();
		sd = new StringToDate();
	}
	
	
	//입금하기
	@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");
		}
		
		IdcDepositDTO dto= new IdcDepositDTO();
		dto.setClient_code(request.getParameter("client_code"));
		dto.setClient_name(request.getParameter("client_name"));
		if(request.getParameter("deposit_date")!=null && request.getParameter("deposit_date")!="")
			dto.setDeposit_date(Date.valueOf(request.getParameter("deposit_date")));
		
		String amount2 = request.getParameter("deposit_amount");
		//String deposit_amount = ;
		//String deposit_amount = String.format("%d", amount2 );
		dto.setDeposit_amount(Integer.parseInt(amount2.replaceAll("\\,","")));
		
		//dto.setDeposit_amount(Integer.parseInt(request.getParameter("deposit_amount")));


				
		//dto.setDeposit_amount(request.getParameter("deposit_amount2"));
		
		
		dto.setDeposit_bank(request.getParameter("deposit_bank"));
		dto.setDeposit_name(request.getParameter("deposit_name"));
		dto.setDeposit_note(request.getParameter("deposit_note"));
		//입금등록하기
		idcDepositWrite(dto);
		//입금액 고객db로 이동
		idcDepositToClient(dto);
	}
	//입금액 고객db로 이동
	private void idcDepositToClient(IdcDepositDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE clientdb SET client_money=client_money+? WHERE client_name=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, dto.getDeposit_amount());
			pstmt.setString(2, dto.getClient_name());
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IDCDepositWriteCMD 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IDCDepositWriteCMD 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	public void idcDepositWrite(IdcDepositDTO dto)
	{
		int num=1;
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt2=null;
		ResultSet rs=null;
		try
		{
			conn = dao.getConn();
			
			String sql ="SELECT IFNULL(MAX(deposit_num),0)+1 AS deposit_num FROM depositdb";
			pstmt2 = conn.prepareStatement(sql);
			rs = pstmt2.executeQuery();
			if(rs.next()) num=rs.getInt("deposit_num");
			
			sql = "INSERT INTO depositdb(deposit_num, client_code, client_name,deposit_date,deposit_amount,deposit_bank, ";
			sql+="deposit_name, deposit_note) ";
			sql+="values(?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setInt(1, num);
			pstmt.setString(2, dto.getClient_code());
			pstmt.setString(3, dto.getClient_name());
			pstmt.setDate(4, dto.getDeposit_date());
			pstmt.setInt(5, dto.getDeposit_amount());
			pstmt.setString(6, dto.getDeposit_bank());
			pstmt.setString(7, dto.getDeposit_name());
			pstmt.setString(8, dto.getDeposit_note());
			
			pstmt.executeUpdate();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(pstmt2!=null) pstmt2.close();
				if(rs!=null) rs.close();
			}
			catch (SQLException e2) 
			{
				e2.printStackTrace();
			}
		}
	}
}
