package idc.service.command;

import idc.model.*;

import java.sql.*;

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

public class IdcServiceAmountUpdateCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO(); 
			
	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 client_code = request.getParameter("client_code");		
		request.setAttribute("client_code", client_code);
		
		String service_code = request.getParameter("mo_service_code");		
		String amount_rack = request.getParameter("mo_amount_rack");
		String amount_line = request.getParameter("mo_amount_line");
		String amount_cpu = request.getParameter("mo_amount_cpu");
		String amount_ip = request.getParameter("mo_amount_ip");
		String amount_cs = request.getParameter("mo_amount_cs");
		String amount_kcs = request.getParameter("mo_amount_kcs");
		String amount_defence = request.getParameter("mo_amount_defence");		
		String amount_reseller = request.getParameter("mo_amount_reseller");
		String last_long = request.getParameter("mo_last_long");
		String last_pre = request.getParameter("mo_last_pre");
		String amount_authority = request.getParameter("mo_amount_authority");
		String amount_contact = request.getParameter("mo_amount_contact");
		String amount_total_discount = request.getParameter("mo_amount_total_discount");
		String service_amount = request.getParameter("mo_service_amount");
		String service_cost = request.getParameter("mo_service_cost");		
		String amount_note = request.getParameter("mo_amount_note");
		String service_deduction = request.getParameter("mo_service_deduction");
		String sub_discount = request.getParameter("mo_amount_sub_discount");
		String service_united = request.getParameter("mo_service_united");
		String service_nonAmount = request.getParameter("mo_service_nonAmount");
		String amount_etc = request.getParameter("mo_amount_etc");
									
		updateAmountDB(service_code,  amount_rack,  amount_line,  amount_cpu, amount_defence,  amount_reseller,   
				  amount_authority,  amount_contact,  amount_total_discount,  service_amount, last_long,  last_pre,
				  amount_ip,  amount_cs,  amount_kcs	, service_cost, amount_note, service_deduction, service_united, service_nonAmount,
				  sub_discount, amount_etc);
	}	
	
	//amountDB에 서비스 과금 등록
	public void updateAmountDB(String service_code, String amount_rack, String amount_line, String amount_cpu, 
			 String amount_defence, String amount_reseller,   
			 String amount_authority, String amount_contact, String amount_total_discount, String service_amount, 
			 String last_long, String last_pre,
			 String ip_total, String cs_total, String kcs_total	, String service_cost, String amount_note, 
			 String service_deduction, String service_united, String service_nonAmount, String amount_sub_discount,
			 String amount_etc){
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2=null, pstmt3=null, pstmt4=null, pstmt5=null, pstmt6=null, pstmt7=null, pstmt8=null;
		ResultSet rs = null, rs2=null, rs3=null, rs5=null, rs6=null, rs8=null;
		String sql;
		String change_cost="";
		try {
			
			conn = dao.getConn();					
			sql="SELECT service_cost FROm servicedb WHERE service_code = ?";
			pstmt5=conn.prepareStatement(sql);
			pstmt5.setString(1, service_code);
			rs5 = pstmt5.executeQuery();
			if(rs5.next())
			{
				change_cost = rs5.getString("service_cost");
				change_cost = Integer.toString(Integer.parseInt(service_cost) - Integer.parseInt(change_cost));
			}
//			계산식
			int rack = Integer.parseInt(amount_rack);
			int line = Integer.parseInt(amount_line);
			int ip = Integer.parseInt(ip_total);
			int cs = Integer.parseInt(cs_total);
			int reseller = Integer.parseInt(amount_reseller);
			int ilast_pre = Integer.parseInt(last_pre);
			int ilast_long = Integer.parseInt(last_long);
			int cpu = Integer.parseInt(amount_cpu);
			int authority = Integer.parseInt(amount_authority);
			int contact = Integer.parseInt(amount_contact);
			int sub_discount = Integer.parseInt(amount_sub_discount);
			int etc = Integer.parseInt(amount_etc);
			int defence = Integer.parseInt(amount_defence);
			
			int temp=(rack+line+cpu-authority-contact);
			//int cost_price = temp + ip + cs + defence + etc;
			int temp2=0;
			int first_dis=0, second_dis = 0;
			int iservice_cost = 0;
			int iamount_total_discount=0;
			
			if(reseller==0)				
				first_dis = temp*ilast_long/100;
			else
				first_dis = temp*reseller/100;
			
			temp2 = temp-first_dis;
			second_dis = temp2*ilast_pre/100;
			iamount_total_discount =(int)(Math.ceil((first_dis+second_dis)/1000.0)*1000);
			iservice_cost = temp - iamount_total_discount - sub_discount;
			if(iservice_cost < 0)
				iservice_cost = 0;
			int service_calc = iservice_cost + ip + cs + defence + etc;
			
			sql = "UPDATE amountDB SET amount_rack=?, amount_line=?, amount_cpu=?, amount_ip=?, amount_cs=?, ";
			sql += " amount_kcs=?, amount_defence=?, amount_authority=?, amount_contact=?, amount_total_discount=?, ";
			sql +=" service_amount=?, last_long=?, last_pre=?, amount_cost=?, amount_note=?, ";
			sql +=" amount_sub_discount=? ";
			sql +=" WHERE service_code=?";
						
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			
			pstmt.setString(index++, Integer.toString(rack));
			pstmt.setString(index++, Integer.toString(line));
			pstmt.setString(index++, Integer.toString(cpu));
			pstmt.setString(index++, Integer.toString(ip));
			pstmt.setString(index++, Integer.toString(cs));
			pstmt.setString(index++, "0");
			pstmt.setString(index++, Integer.toString(defence));
			pstmt.setString(index++, Integer.toString(authority));
			pstmt.setString(index++, Integer.toString(contact));
			pstmt.setString(index++, Integer.toString(iamount_total_discount));
			pstmt.setString(index++, Integer.toString(service_calc));
			pstmt.setString(index++, Integer.toString(ilast_long));
			pstmt.setString(index++, Integer.toString(ilast_pre));
			pstmt.setString(index++, Integer.toString(iservice_cost));
			pstmt.setString(index++, amount_note);
			pstmt.setString(index++, Integer.toString(sub_discount));
			pstmt.setString(index++, service_code);
						
			pstmt.executeUpdate();
			
			int pay = 0, amo=0;
			sql="SELECT sum(adddb_payment) as pay FROM adddb WHERE service_code=? ";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			rs2 = pstmt2.executeQuery();
			if(rs2.next())
				pay = rs2.getInt("pay");
		
			sql="SELECT sum(onetime_amount) as amo, sum(onetime_payment) as pay FROM onetimedb WHERE service_code=? AND onetime_accountstatus='incomplete' ";
			pstmt6 = conn.prepareStatement(sql);
			pstmt6.setString(1, service_code);
			rs6 = pstmt6.executeQuery();
			if(rs6.next())
				amo = rs6.getInt("amo");
				pay+=rs6.getInt("pay");
		
			//service 변경
			sql="UPDATE servicedb SET service_amount=?, service_nonpayment=?, service_cost=?, service_unitedAmount=? ";
			sql+="WHERE service_code=?";
			pstmt7=conn.prepareStatement(sql);
			pstmt7.setString(1, Integer.toString(service_calc+amo));
			
			int nonpayment = service_calc+amo - pay;
			//int nonpayment = service_calc+amo - pay - Integer.parseInt(service_deduction);
			
			if(nonpayment<0)
				nonpayment=0;
			pstmt7.setString(2, Integer.toString(nonpayment));
			pstmt7.setString(3, Integer.toString(iservice_cost));
			pstmt7.setString(4, Integer.toString(service_calc));
			pstmt7.setString(5, service_code);
			pstmt7.executeUpdate();
			
			//adddb - service 바꾸기 위한 선작업			
			sql= "SELECT adddb_payment FROM adddb WHERE service_code=? AND adddb_case='service' ";
			pstmt8 = conn.prepareStatement(sql);
			pstmt8.setString(1, service_code);
			rs8 = pstmt8.executeQuery();
			int spayment = 0;
			if(rs6.next())
			{
				spayment = rs8.getInt("adddb_payment");
			}
			int ideduc = Integer.parseInt(service_deduction);
			String account = "incomplete";

			if(iservice_cost<spayment+ideduc)
			{
				spayment = iservice_cost;
				account = "complete";
			}
			else
				spayment += ideduc;
			
			//adddb 수정
			sql="UPDATE adddb SET adddb_amount=?, adddb_nonpayment=?, adddb_payment=?, adddb_accountstatus=? ";
			sql+="WHERE service_code=? AND adddb_case='service' ";
			pstmt4 = conn.prepareStatement(sql);
			pstmt4.setString(1, Integer.toString(iservice_cost));
			pstmt4.setString(2, Integer.toString(iservice_cost-spayment));
			pstmt4.setString(3, Integer.toString(spayment));
			pstmt4.setString(4, account);
			pstmt4.setString(5, service_code);
			pstmt4.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();				
				if(rs3 != null) rs3.close();
				if(rs5 != null) rs5.close();
				if(rs6 != null) rs6.close();
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
				if(pstmt3 != null) pstmt3.close();
				if(pstmt4 != null) pstmt4.close();
				if(pstmt5 != null) pstmt5.close();
				if(pstmt6 != null) pstmt6.close();
				if(pstmt7 != null) pstmt7.close();
				if(pstmt8 != null) pstmt8.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}