package idc.service.command;

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

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

import idc.model.*;

public class IdcServicePaymentUpdateCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
	
	//서비스 금액 수정하기
	public void execute(HttpServletRequest request, HttpServletResponse response){
		String client_code = request.getParameter("client_code");
		String service_code = request.getParameter("service_code");
		request.setAttribute("client_code",client_code);		

		String service_amount = request.getParameter("service_amount");
		String nonpayment = request.getParameter("nonpayment");
				
		updateServiceAmount(service_code, service_amount, client_code, nonpayment);
		
	}
		
	public void updateServiceAmount(String service_code, String service_amount, String client_code, String nonpayment){
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null;
		ResultSet rs = null;
		String sql;
		
		try {			
			conn = dao.getConn();									
			sql = "UPDATE addDB SET addDB_nonpayment='0', addDB_payment = addDB_amount, addDB_handle_date=curdate(), addDB_accountDay=curdate()";			
			sql +=" WHERE service_code=? ";
			pstmt = conn.prepareStatement(sql);			
			pstmt.setString(1, service_code);						
			pstmt.executeUpdate();
			
			sql = "UPDATE clientDB SET client_money=client_money-? ";
			sql += " WHERE client_code=?";
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setString(1, nonpayment);
			pstmt1.setString(2, client_code);			
			pstmt1.executeUpdate();
			
			sql = "UPDATE serviceDB SET service_nonpayment='0' ";
			sql += " WHERE service_code=?";
			pstmt2 = conn.prepareStatement(sql);			
			pstmt2.setString(1, service_code);			
			pstmt2.executeUpdate();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	

}
