package utility;

import idc.model.IdcDAO;

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

public class IdcUpdateServiceAmount{
	
	//amountDB 업데이트
	public static void amountdbUpdate(String amount, String service_code, String item){
		switch(item)
		{
		case "ip":
			item="amount_ip = amount_ip+'"+amount+"', ";
			break;
		case "cs":
			item="amount_cs = amount_cs+'"+amount+"', ";
			break;
		case "kcs":
			item="amount_kcs = amount_kcs+'"+amount+"', ";
			break;
		case "vpc":
			item="amount_vpc = amount_vpc+'"+amount+"', ";
			break;
		case "defence":
			item="amount_defence = amount_defence+'"+amount+"', ";
			break;
		}
		
		IdcDAO dao = new IdcDAO();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE amountdb SET " + item + "service_amount=service_amount+'"+amount+"' ";
			sql+="WHERE service_code='"+service_code+"'";
			pstmt =conn.prepareStatement(sql);
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcUpdateServiceAmount - amountdbUpdate 에서의 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("IdcUpdateServiceAmount - amountdbUpdate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	//serviceDB 업데이트
	public static void UpdateAmount(String amount, String service_code, boolean isAdd, int adddb_num, String nonpayment){

		IdcDAO dao = new IdcDAO();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql=null;
		
		try	
		{
			conn=dao.getConn();													
			
			if(isAdd){
				sql="UPDATE servicedb SET service_amount=service_amount+?, service_nonpayment=service_nonpayment+? ";
				sql+=", service_unitedAmount=service_unitedAmount+? ";
			}else{
				sql="UPDATE servicedb SET service_amount=service_amount-?, service_nonpayment=service_nonpayment-? ";
				sql+=", service_unitedAmount=service_unitedAmount-? ";
			}
			
			int i=3;
						
			sql+="WHERE service_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, amount);
			if(isAdd)	
				pstmt.setString(2, amount);
			else
			{
				pstmt.setString(2, nonpayment);
			}
			pstmt.setString(i++, amount);
			pstmt.setString(i, service_code);
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println(isAdd + " IdcUpdateServiceAmount - UpdateAmount 에서의 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(isAdd + "IdcUpdateServiceAmount - UpdateAmount 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
