package idc.server.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import idc.model.*;
import java.sql.*;

public class IdcServerNumGenCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();	
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {

		String server_code = generatorServer_code();
		request.setAttribute("server_code", server_code);
		Date curdate = getCurdate();
		request.setAttribute("curdate", curdate);		
	}
	
	//서버코드 만들기
	private String generatorServer_code(){
		String server_code ="";
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2 = null;
		ResultSet rs = null, rs2= null;
		int num = 0;
		int num2 = 0;
		String max_code,max_code2;		
	/*	
		try {
			conn = dao.getConn();
			
			String sql = "SELECT max(server_code) AS max_code FROM serverDB where server_code like '%JPN-S%'";
			
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				max_code = rs.getString("max_code");
				if(max_code != null){
					num = removeCharExceptNumber(max_code);
				}
			}
			
			if(num < 999){
				server_code = "JPN-S" + String.format("%03d", num+1);				
				
			}else{
				sql = "SELECT max(server_code) AS max_code FROM serverDB where server_code like '%JPN-D%'";				
				pstmt2 = conn.prepareStatement(sql);
				rs2 = pstmt2.executeQuery();
				if(rs2.next()){
					max_code2 = rs2.getString("max_code");
					if(max_code2 != null){
						num2 = removeCharExceptNumber(max_code2);
					}
				}else{
					num2 = 0;					
				}				
				server_code = "JPN-D" + String.format("%03d", num2+1);
			}
*/			
		try {
			conn = dao.getConn();
			
			String sql = "SELECT server_code, substring_index(server_code,'-',-1) AS max_code FROM serverDB where server_code like '%-M%' order by max_code desc";
			
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				max_code = rs.getString("server_code");
				if(max_code != null){
					String[] result = max_code.split("-");
					num = removeCharExceptNumber(result[1]);
				}
			}
			if(num < 999){
				server_code = "154XX-M" + String.format("%03d", num+1);							
				
			}else{
				sql = "SELECT server_code, substring_index(server_code,'-',-1) AS max_code FROM serverDB where server_code like '%-M%' order by max_code desc";				
				pstmt2 = conn.prepareStatement(sql);
				rs2 = pstmt2.executeQuery();
				if(rs2.next()){
					max_code2 = rs2.getString("server_code");
					if(max_code2 != null){
						String[] result = max_code2.split("-");
						num2 = removeCharExceptNumber(result[1]);
					}
				}else{
					num2 = 0;					
				}				
				server_code = "154XX-K" + String.format("%03d", num2+1);			
			}			
			
					
									
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return server_code;
	}
	
	//현재 날짜 받아오기
	private Date getCurdate(){
		Date curdate = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try {
			conn = dao.getConn();
			String sql = "SELECT curdate() AS curdate FROM serverDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				curdate = rs.getDate("curdate");
			}			
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return curdate;
	}
	
	
	//숫자만 남기기
	public int removeCharExceptNumber(String str){
		if(str != null && str !=""){
			String temp = str.replaceAll("[^\\d]", "");	
			return Integer.parseInt(temp);
		}else{
			return 0;
		}				
	}

}
