package neu.integration.ctic.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;

import neu.integration.ctic.util.DBUtil;

public class WLupdate {

	public static Map<String,List<Map<String,String>>> getwlupdate() throws IOException{
//		String sql = "SELECT records,create_time FROM watcherlab_all_update_sum ORDER BY create_time DESC LIMIT 7";// 编写sql语句，第一个字段不需要插入，是自动增加的
//		Statement stmt = null;
//		ResultSet rs = null;
//		PreparedStatement pstmt = null;
//		Map<String,List<Map<String,String>>> lines = new HashMap<String, List<Map<String,String>>>();
//		List<Map<String,String>> records= new ArrayList<Map<String,String>>();
//		List<String> date = new ArrayList<String>();
//		List<Map<String,String>> c2= new ArrayList<Map<String,String>>();
//		List<Map<String,String>> fastflux= new ArrayList<Map<String,String>>();
//		List<Map<String,String>> proxy= new ArrayList<Map<String,String>>();
//		List<Map<String,String>> tor= new ArrayList<Map<String,String>>();
//		//Map<Map<String, String>,> records = new HashMap<String, Integer>();
//		try{
//		Connection conn = DBUtil.getConn("219.216.64.50:3306/diting2","root","123qwe_456");
//		stmt = conn.createStatement();
//		pstmt = DBUtil.prepareStmt(conn,sql);
//		rs = DBUtil.executeQuery(stmt, sql);
//		//rs = stmt.executeQuery(sql);
//			while(rs.next()){
//			String[] temp1;
//			String[] temp2;
//			String rec = rs.getString("records");
//			String updateTime = rs.getString("create_time");
//			//System.out.println(updateTime);
//			String[] time_temp = updateTime.split(" ");
//			String time = time_temp[0].replace("2017-","");
//			date.add(time);
//			temp1 = rec.split(",");
//			
//			for(int i=0;i<temp1.length;i++){
//				Map<String,String> record = new HashMap<String, String>();
//				temp2 = temp1[i].split(":");
//				if(temp2[0].equals("c2")){
//					record.put("value", temp2[1]);
//					record.put("time", time);
//					c2.add(record);
//				}
//				else if(temp2[0].equals("fastflux")){
//					record.put("value", temp2[1]);
//					record.put("time", time);
//					fastflux.add(record);
//				}
//				else if(temp2[0].equals("proxy")){
//					record.put("value", temp2[1]);
//					record.put("time", time);
//					proxy.add(record);
//				}
//				else if(temp2[0].equals("tor")){
//					record.put("value", temp2[1]);
//					record.put("time", time);
//					tor.add(record);
//				}
//				else continue;
//				/*if(temp2[0].equals("c2")||temp2[0].equals("fastflux")||temp2[0].equals("proxy")||temp2[0].equals("tor")){
//				record.put("name", temp2[0]);
//				record.put("value", temp2[1]);
//				String[] time_temp = updateTime.split(" ");
//				String time = time_temp[0];
//				record.put("time", time);
//				records.add(record);}*/
//			}
//				
//			lines.put("c2",c2);
//			lines.put("fastflux",fastflux);
//			lines.put("proxy",proxy);
//			lines.put("tor",tor);
//			
//		//	System.out.println(lines);
//			//System.out.println(rs.getMetaData().getColumnName(1));
//		}
//		//System.out.println(lines);
//		DBUtil.close(conn, stmt, pstmt, rs);
//		
//		}
//		catch(SQLException se){
//		      //Handle errors for JDBC
//		      se.printStackTrace();
//		   }catch(Exception e){
//		      //Handle errors for Class.forName
//		      e.printStackTrace();
//		   }
//		return lines;
		 //    DBUtil dbUtil=new com.neu.diting.honeypot.controller.DBUtil();
	   
//	        String line = "";
//	        String HdSerial = "";//定义变量 硬盘序列号
//	            Process proces = Runtime.getRuntime().exec("wmic bios get serialnumber");//获取命令行参数
//	            BufferedReader buffreader = new BufferedReader(
//	                    new InputStreamReader(proces.getInputStream()));
//	            while ((line = buffreader.readLine()) != null) {
//	                if(line==null||line.equals("")){
//	                    continue;
//	                }
//	                HdSerial=line;
//	                }
//	            if(!HdSerial.equals("4CV7445LD3")){
//	                Process processTomcat=Runtime.getRuntime().exec("cmd /c D:\\apache-tomcat-8.0.49\\bin\\shutdown.bat");
//	                System.out.println("项目启动失败，不允许在此电脑部署");
//	            }

        String sql = "SELECT all_proxy,all_black,all_tor,all_phish,all_botscout,datatime FROM day_sum WHERE datatime>(SELECT DATE_SUB(CURDATE(),INTERVAL 6 DAY))";// 编写sql语句，第一个字段不需要插入，是自动增加的
        Statement stmt = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Map<String, List<Map<String, String>>> lines = new HashMap<String, List<Map<String, String>>>();
        List<Map<String, String>> records = new ArrayList<Map<String, String>>();
        List<String> date = new ArrayList<String>();
        List<Map<String, String>> proxy = new ArrayList<Map<String, String>>();
        List<Map<String, String>> balck = new ArrayList<Map<String, String>>();
        List<Map<String, String>> phish = new ArrayList<Map<String, String>>();
        List<Map<String, String>> tor = new ArrayList<Map<String, String>>();
        List<Map<String, String>> botscout = new ArrayList<Map<String, String>>();
        //Map<Map<String, String>,> records = new HashMap<String, Integer>();
        try {
            Connection conn = DBUtil.getConn("219.216.65.41:3306/mydata","root","123456");
            //Connection conn = DBUtil.getConn("localhost:3306/beijing","root","root");
            stmt = conn.createStatement();
            pstmt =DBUtil.prepareStmt(conn, sql);
            rs = DBUtil.executeQuery(stmt, sql);
//            String all_proxy = rs.getString("all_proxy");
//            String all_black = rs.getString("all_black");
//            String all_tor = rs.getString("all_tor");
//            String all_phish = rs.getString("all_phish");
//            String all_botscout = rs.getString("all_botscout");
//            String datatime = rs.getString("datatime");
            	if(rs==null) System.out.println("kong");
           while (rs.next()) {
               for(int i=1;i<=5;i++) {
                   Map<String, String> record = new HashMap<String, String>();
                   record.put("value",rs.getObject(i).toString());
                   record.put("time",rs.getObject(6).toString().substring(0,10));
                   if(i==1){
                       proxy.add(record);
                   }
                   if(i==2){
                       balck.add(record);
                   }
                   if(i==3){
                       phish.add(record);
                   }
                   if(i==4){
                       tor.add(record);
                   }
                   if(i==5){
                       botscout.add(record);
                   }
               }
           }
           lines.put("proxy",proxy);
           lines.put("balck",balck);
           lines.put("phish",phish);
           lines.put("tor",tor);
           lines.put("botscout",botscout);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lines;
	}
	
	public static Map<String,Integer> getThreatNum(){
		String sql = "SELECT records FROM watcherlab_all_update_sum ORDER BY create_time DESC LIMIT 1";// 编写sql语句，第一个字段不需要插入，是自动增加的
		Statement stmt = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		Map<String,Integer> result = new HashMap<String,Integer>();
		//Map<Map<String, String>,> records = new HashMap<String, Integer>();
		try{
		Connection conn = DBUtil.getConn("localhost:3306/beijing","root","root");
		stmt = conn.createStatement();
		pstmt = DBUtil.prepareStmt(conn,sql);
		rs = DBUtil.executeQuery(stmt, sql);
		//rs = stmt.executeQuery(sql);
		int total_num = 0;
			while(rs.next()){
			String[] temp_category;
			int temp2;
			String rec = rs.getString("records");
			temp_category = rec.split(",");
			total_num = 0;
			for(int i=0;i<temp_category.length;i++){     
				temp2 = Integer.parseInt(temp_category[i].split(":")[1]);
				total_num +=temp2;
			}

		//	System.out.println(lines);
			//System.out.println(rs.getMetaData().getColumnName(1));
		}
		result.put("pronum", total_num);
		DBUtil.close(conn, stmt, pstmt, rs);
		
		}
		catch(SQLException se){
		      //Handle errors for JDBC
		      se.printStackTrace();
		   }catch(Exception e){
		      //Handle errors for Class.forName
		      e.printStackTrace();
		   }
		return result;
	}
}
