package idc.service.command;

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

import utility.IdcGetClientNameCode;
import utility.StringToDate;
import idc.model.*;

import java.sql.*;

public class IdcServiceUpdateCmd 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 service_united = request.getParameter("service_united");
		String service_nonAmount = request.getParameter("service_nonAmount");
				
		String service_status = request.getParameter("service_status");
		String client_code = request.getParameter("client_code");
		String service_code = request.getParameter("service_code");
		String service_line = request.getParameter("service_line");
		String server_code = request.getParameter("server_code");
		String service_sw = request.getParameter("service_sw");
		String service_os = request.getParameter("service_os");
		
		String server_ram1 = request.getParameter("server_ram1");
		String server_ram2 = request.getParameter("server_ram2");
		String server_ram3 = request.getParameter("server_ram3");
		String server_ram4 = request.getParameter("server_ram4");
		String server_ram5 = request.getParameter("server_ram5");
		
		String server_hdd1 = request.getParameter("server_hdd1");
		String server_hdd2 = request.getParameter("server_hdd2");
		String server_hdd3 = request.getParameter("server_hdd3");
		String server_hdd4 = request.getParameter("server_hdd4");
		String server_hdd5 = request.getParameter("server_hdd5");

		String server_location = request.getParameter("server_location");				
		String request_date = request.getParameter("service_request_date");
		String open_date = request.getParameter("service_open_date");
		String payment_date = request.getParameter("service_payment_date");
		String sub_discount = request.getParameter("amount_sub_discount");
		String amount_etc = request.getParameter("amount_etc");
		String mkworker = request.getParameter("mkworker");
		StringToDate st = new StringToDate();		
		Date service_request_date;		
		if(request_date == "" || request_date == null){
			service_request_date = null;
		}else{
			service_request_date = st.stringToDate(request_date);
		}

		Date service_open_date;
		if(open_date == "" || open_date == null){
			service_open_date = null;
		}else{
			service_open_date = st.stringToDate(open_date);
		}
				
		String amount_rack = request.getParameter("amount_rack");
		String amount_line = request.getParameter("amount_line");
		String amount_cpu = request.getParameter("amount_cpu");
		String service_defence = request.getParameter("service_defence");
		String amount_defence = request.getParameter("amount_defence");
		String amount_reseller = request.getParameter("amount_reseller");
		String last_long = request.getParameter("last_long");
		String last_pre = request.getParameter("last_pre");
		String amount_authority = request.getParameter("amount_authority");
		String amount_contact = request.getParameter("amount_contact");
		String amount_total_discount = request.getParameter("amount_total_discount");
		String service_cost = request.getParameter("service_cost");
		String service_amount = request.getParameter("service_amount");
		//String add_amount = request.getParameter("add_amount");
		String service_note = request.getParameter("mo_note");
		String ip_total = request.getParameter("ip_total");
		String cs_total = request.getParameter("cs_total");
		String kcs_total = request.getParameter("kcs_total");
		String service_deduction = request.getParameter("service_deduction");
		String defence_discount = request.getParameter("defence_discount");
		String change_cost = "";
		request.setAttribute("client_code", client_code);

		IdcSetHistory.setHistory(service_code, server_code, "서비스정보수정","","서비스 정보수정",mkworker, IdcGetClientNameCode.getClientName(client_code));
		
		insertAmountDB( 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,
				  ip_total,  cs_total,  kcs_total, service_cost, service_deduction, service_united, 
				  service_nonAmount, change_cost, defence_discount, service_defence, service_line, server_location, client_code, sub_discount,
				  amount_etc,
				  chageDate(service_code, payment_date, service_sw, service_os));	
		
		serviceUpdate2( service_status,  service_code, 
				 server_code,  service_sw,  service_os,
				 server_ram1,  server_ram2,  server_ram3,  server_ram4,  server_ram5, 
				 server_hdd1,  server_hdd2,  server_hdd3,  server_hdd4,  server_hdd5,				 
				 service_request_date,  service_open_date,  service_note	,	service_line			 
				);		
	}
	

	//amountDB에 서비스 과금 등록
	public void insertAmountDB(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 service_deduction	 
			 , String service_united, String service_nonAmount, String change_cost, String amount_defence_discount,
			 String service_defence, String service_line, String server_location, String client_code, String sub_discount,
			 String amount_etc, boolean isChangeDate)
	{
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2=null, pstmt3=null, pstmt4=null, pstmt5=null, pstmt6=null, pstmt7=null, 
				pstmt8=null,pstmt11 = null, pstmt12=null, pstmt13=null;
		ResultSet rs = null, rs2=null, rs3=null, rs5=null,rs6=null, rs7=null, rs11=null;
		String sql;
		String old_service_deduction = "";
		
		try {
			conn = dao.getConn();	
			//서비스 방어추가.
			switch(service_defence)
			{
			case "없음":
				service_defence="x";
				break;
			case "일반":
				service_defence="normal";
				break;
			case "상시":
				service_defence="ordinary";
				break;
			case "cs-pre":
				service_defence="cs-pre";
				break;
			}
						
			
			//havelist 위치 변경위한 이전 라인과 위치 얻어오기.			
			String oldline ="",oldline2="", newline=service_line, newline2=service_line;
			sql="SELECT service_line, service_deduction FROM servicedb WHERE service_code=?";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3=pstmt3.executeQuery();
			if(rs3.next()){
				oldline2 = oldline = rs3.getString("service_line");
				old_service_deduction = rs3.getString("service_deduction");
			}
			String tot = "total";
			String tst = "test";
			switch(server_location)
			{
			case "chiba":
				oldline+="_c";
				newline+="_c";
				tot+="_c";
				tst+="_c";
				break;
			case "tokyo":
				oldline+="_t";
				newline+="_t";
				tot+="_t";
				tst+="_t";
				break;
			}
						
			//havelist 변경하기--수정
			switch (oldline2)
			{
			//soloLine
			case "soloLine":
				switch (newline2)
				{
				case "soloLine":
					sql="UPDATE havelist SET normal_t = normal_t + 0 ";
					break;
				case "test":
					sql="UPDATE havelist SET "+newline+"="+newline+"+1, test_total = test_total + 1 ";
					break;
				//일반	
				default:
					sql="UPDATE havelist SET "+newline+"="+newline+"+1, "+tot+"="+tot+"+1, total_total = total_total+1";
					break;
				}
				break;
			
			//test
			case "test":
				switch (newline2)
				{
				case "soloLine":
					sql="UPDATE havelist SET "+oldline+"="+oldline+"-1 , test_total = test_total-1";
					break;
				case "test":
					sql="UPDATE havelist SET "+oldline+"="+oldline+"-1 , "+newline+"="+newline+"+1 ";
					break;
				//일반	
				default:
					sql="UPDATE havelist SET "+newline+"="+newline+"+1, "+tot+"="+tot+"+1, total_total = total_total+1, "+oldline+"=" + oldline+"-1, test_total = test_total-1 ";
					break;
				}
				break;

			//일반	
			default:
				switch (newline2)
				{				
				case "soloLine":
					sql="UPDATE havelist SET "+oldline+"="+oldline+"-1 , "+tot+"="+tot+"-1, total_total = total_total-1";
					break;
				case "test":
					sql="UPDATE havelist SET "+oldline+"="+oldline+"-1 , "+tot+"="+tot+"-1, total_total = total_total-1, "+newline+"="+newline+"+1, test_total=test_total+1 ";
					break;				
				//일반	
				default:
					sql="UPDATE havelist SET "+oldline+"="+oldline+"-1 , "+newline+"="+newline+"+1 ";
					break;
				}				
				break;
			}
			
			if(Integer.parseInt(amount_reseller)>0)
				sql+=" WHERE client_code='"+client_code+"' ";
			else
				sql+=" WHERE client_code='IDCJP' ";
			pstmt4 = conn.prepareStatement(sql);
			pstmt4.executeUpdate();
			
			int defence = Integer.parseInt(amount_defence);
			int rack = Integer.parseInt(amount_rack);
			int line = Integer.parseInt(amount_line);
			int cpu = Integer.parseInt(amount_cpu);
			int ip = Integer.parseInt(ip_total);
			int cs = Integer.parseInt(cs_total);
			int authority = Integer.parseInt(amount_authority);
			int contact = Integer.parseInt(amount_contact);
			int reseller = Integer.parseInt(amount_reseller);
			int ilast_pre = Integer.parseInt(last_pre);
			int ilast_long = Integer.parseInt(last_long);
			int etc = Integer.parseInt(amount_etc);
			int sub_amount = 0, sub_payment=0;
			
			sql="SELECT sum(adddb_payment) as pay FROM adddb ";
			sql+="WHERE service_code=? ";
			pstmt2 =conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			rs2 = pstmt2.executeQuery();
			if(rs2.next())
			{		
					sub_payment = rs2.getInt("pay");
			}
//			sql="SELECT sum(onetime_amount) as amo, sum(onetime_payment) as pay FROM onetimedb WHERE service_code=? AND onetime_accountStatus='incomplete' ";
//			pstmt7 = conn.prepareStatement(sql);
//			pstmt7.setString(1, service_code);
//			rs7 = pstmt7.executeQuery();
//			if(rs7.next())
//			{
//				sub_amount = rs7.getInt("amo");
//				sub_payment += rs7.getInt("pay"); 
//			}
			
			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;//cost_price-service_calc;
			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-Integer.parseInt(sub_discount);
			if(iservice_cost < 0)
				iservice_cost = 0;
			int service_calc = iservice_cost + ip + cs + defence + etc;
			
			//service 변경
			sql="UPDATE servicedb SET service_amount=?, service_nonpayment=?, service_cost=?, service_unitedAmount=?, ";
			sql+="service_line=?, service_deduction=? ";
			sql+="WHERE service_code=?";
			
			pstmt5=conn.prepareStatement(sql);
			pstmt5.setString(1, Integer.toString(service_calc+sub_amount));
			
			int old_deduction = 0;
			if(old_service_deduction != "" && old_service_deduction != null){
				old_deduction = Integer.parseInt(old_service_deduction);
			}
			
			int nonpayment = service_calc+sub_amount - sub_payment +old_deduction - Integer.parseInt(service_deduction);
						
			if(nonpayment<0)
				nonpayment=0;
			pstmt5.setString(2, Integer.toString(nonpayment));
			pstmt5.setString(3, Integer.toString(iservice_cost));
			pstmt5.setString(4, Integer.toString(service_calc));
			pstmt5.setString(5, service_line);
			pstmt5.setString(6, service_deduction);			
			pstmt5.setString(7, service_code);
			pstmt5.executeUpdate();
			
			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_sub_discount=?, ";
			sql +="amount_etc=? ";
			sql +=" WHERE service_code=?";
			
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			
			pstmt.setString(index++, amount_rack);
			pstmt.setString(index++, amount_line);
			pstmt.setString(index++, amount_cpu);
			pstmt.setString(index++, ip_total);
			pstmt.setString(index++, cs_total);
			pstmt.setString(index++, kcs_total);
			pstmt.setString(index++, amount_defence);
			pstmt.setString(index++, amount_authority);
			pstmt.setString(index++, amount_contact);
			pstmt.setString(index++, amount_total_discount);
			pstmt.setString(index++, service_amount);
			pstmt.setString(index++, last_long);
			pstmt.setString(index++, last_pre);
			pstmt.setString(index++, service_cost);						
			pstmt.setString(index++, sub_discount);
			pstmt.setString(index++, amount_etc);
			pstmt.setString(index++, service_code);
			pstmt.executeUpdate();
						
			sql= "SELECT adddb_payment FROM adddb WHERE service_code=? AND adddb_case='service' ";
			pstmt6 = conn.prepareStatement(sql);
			pstmt6.setString(1, service_code);
			rs6 = pstmt6.executeQuery();
			int spayment = 0;
			if(rs6.next())
			{
				spayment = rs6.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 - old_deduction);
				//spayment += ideduc;
			}
			
			//adddb 수정(service)
			sql="UPDATE adddb SET adddb_amount=?, adddb_nonpayment=?, adddb_payment=?, adddb_accountstatus=? ";
			sql+="WHERE service_code=? AND adddb_case='service' ";
			pstmt8 = conn.prepareStatement(sql);
			pstmt8.setString(1, Integer.toString(iservice_cost));
			pstmt8.setString(2, Integer.toString(iservice_cost-spayment));
			pstmt8.setString(3, Integer.toString(spayment));
			pstmt8.setString(4, account);
			pstmt8.setString(5, service_code);
			pstmt8.executeUpdate();
			
			sql = "SELECT * FROM servicedb WHERE service_code=?";
			pstmt11 = conn.prepareStatement(sql);
			pstmt11.setString(1, service_code);
			rs11 = pstmt11.executeQuery();
			if(rs11.next())
			{
				if(!rs11.getString("service_nonpayment").equalsIgnoreCase(rs11.getString("service_amount")) && isChangeDate)
				{
					sql = "UPDATE servicedb SET service_nonpayment = service_amount WHERE service_code = '"+rs11.getString("service_code")+"' ";
					pstmt12 = conn.prepareStatement(sql);
					pstmt12.executeUpdate();
					
					sql = "UPDATE adddb SET addDB_nonpayment = addDB_amount, adddb_payment = 0, adddb_accountStatus = 'incomplete' ";
					sql+="WHERE service_code = ? ";
					pstmt13 = conn.prepareStatement(sql);
					pstmt13.setString(1, service_code);
					//pstmt13.setString(2, adddb_payment_date);
					pstmt13.executeUpdate();
				}
			}
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs2 != null) rs2.close();
				if(pstmt2!=null)pstmt2.close();
				if(rs3 != null) rs3.close();
				if(pstmt3!=null)pstmt3.close();
				if(pstmt4!=null)pstmt4.close();
				if(rs5 != null) rs5.close();
				if(pstmt5!=null)pstmt5.close();
				if(rs6 != null) rs6.close();
				if(pstmt6!=null)pstmt6.close();
				if(rs7 != null) rs7.close();
				if(pstmt7!=null)pstmt7.close();
				if(rs11 != null) rs11.close();
				if(pstmt11!=null)pstmt11.close();
				
				if(pstmt8!=null)pstmt8.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//날짜 && sw/os
	public boolean chageDate(String service_code, String d, String sw, String os)
	{
		Date payment_request=null;
		
		if(d==null || d=="")
			payment_request=null;
		else
			payment_request = Date.valueOf(d);
		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null, rs3=null;
		String sql="";
		Date oldDate=null;
		
		try	
		{
			conn=dao.getConn();
			sql="SELECT service_payment_date FROM servicedb WHERE service_code=?";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3 = pstmt3.executeQuery();
			if(rs3.next())
				oldDate = rs3.getDate("service_payment_date");
		
			if(!payment_request.equals(oldDate))
			{
				sql="UPDATE servicedb SET service_payment_date=?, service_original_date=?, service_sw=?, service_os=? "
						+ " WHERE service_code=?";
				pstmt =conn.prepareStatement(sql);
				pstmt.setDate(1, payment_request);
				pstmt.setDate(2, payment_request);
				pstmt.setString(3, sw);
				pstmt.setString(4, os);
				pstmt.setString(5, service_code);
				pstmt.executeUpdate();
				
				sql="UPDATE adddb SET adddb_payment_date=?, adddb_original_date=? "+
					  "WHERE service_code=? AND adddb_payment_Date=? ";
				pstmt2=conn.prepareStatement(sql);
				pstmt2.setDate(1, payment_request);
				pstmt2.setDate(2, payment_request);
				pstmt2.setString(3, service_code);
				pstmt2.setDate(4, oldDate);
				pstmt2.executeUpdate();
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcServiceUpdateCmd - changeDate 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
				if(rs!=null)rs.close();
				if(rs3!=null)rs3.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcServiceUpdateCmd - changeDate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		if(!payment_request.equals(oldDate))
			return true;
		return false;
	}
	
	
	//서비스 정보 수정하기
	public void serviceUpdate2(String service_status, String service_code, 
			String server_code, String service_sw, String service_os,
			String server_ram1, String server_ram2, String server_ram3, String server_ram4, String server_ram5, 
			String server_hdd1, String server_hdd2, String server_hdd3, String server_hdd4, String server_hdd5, 
			Date service_request_date, Date service_open_date, String service_note, String service_line
			){		
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2=null;
		String sql;
		
		try {
			conn = dao.getConn();			
			
			sql = "UPDATE serviceDB SET service_status=?, service_sw=?, service_os=?, ";
			sql += " service_request_date=?, service_open_date=?, service_note=? , service_line=? ";
			sql += " WHERE service_code=?";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, service_status);
			pstmt.setString(2, service_sw);
			pstmt.setString(3, service_os);			
			pstmt.setDate(4, service_request_date);
			pstmt.setDate(5, service_open_date);
			pstmt.setString(6, service_note);
			pstmt.setString(7, service_line);
			pstmt.setString(8, service_code);
			

			pstmt.executeUpdate();
			
			
			sql = "UPDATE serverDB SET  server_ram=?, server_ram1=?, server_ram2=?, server_ram3=?, server_ram4=?, server_ram5=?,";
			sql += " server_hdd=?, server_hdd1=?, server_hdd2=?, server_hdd3=?, server_hdd4=?, server_hdd5=? ";
			sql += " WHERE server_code=?";
			
			String server_ram = server_ram1 + " " + server_ram2 + " " + server_ram3 + " " + server_ram4 + " " + server_ram5;
			String server_hdd = server_hdd1 + " " + server_hdd2 + " " + server_hdd3 + " " + server_hdd4 + " " + server_hdd5;
			
			pstmt2 = conn.prepareStatement(sql);			
			pstmt2.setString(1, server_ram);
			pstmt2.setString(2, server_ram1);
			pstmt2.setString(3, server_ram2);
			pstmt2.setString(4, server_ram3);
			pstmt2.setString(5, server_ram4);
			pstmt2.setString(6, server_ram5);
			pstmt2.setString(7, server_hdd);
			pstmt2.setString(8, server_hdd1);
			pstmt2.setString(9, server_hdd2);
			pstmt2.setString(10, server_hdd3);
			pstmt2.setString(11, server_hdd4);
			pstmt2.setString(12, server_hdd5);			
			pstmt2.setString(13, server_code);
			
			pstmt2.executeUpdate();						
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}		
}

