package com.chenyang.nse.bussiness.tools.correlation;

import com.chenyang.nse.bussiness.tools.jdbc.JdbcTool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CorrelationTool {
   public static Map<String, Float> check(ArrayList<List<String>> data, int a, double rate) {
      Map<String, Float> map = new HashMap();
      List<String> target = (List)data.get(a);

      for(int i = 0; i < data.size(); ++i) {
         Map<String, Integer> checkMap = new HashMap();
         if (i != a) {
            List<String> current = (List)data.get(i);
            ArrayList<String> list = new ArrayList();

            for(int j = 0; j < ((List)data.get(i)).size(); ++j) {
               Integer count = (Integer)checkMap.get(current.get(j));
               String s = (String)target.get(j);
               if (count == null) {
                  count = 1;
                  list.add(s);
               } else if (list.contains(s)) {
                  count = count + 1;
                  list.add(s);
               }

               checkMap.put(current.get(j), count);
            }

            DecimalFormat df2 = new DecimalFormat("0.000");
            String confidence = df2.format((double)((float)list.size() / (float)((List)data.get(0)).size()));
            float confidenceRate = Float.parseFloat(confidence);
            if ((double)confidenceRate > rate) {
               map.put((data.get(i)).get(0), confidenceRate);
            }
         }
      }

      return map;
   }

   public static void main(String[] args) throws SQLException {
      Connection conn = JdbcTool.getConnection("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@192.168.2.106:1521:orcl", "test", "test");
      Statement sm = null;
      sm = conn.createStatement();
      String querySql = "select * from HAILONG.IP_INVPTY101";
      ResultSet rs = sm.executeQuery(querySql);
      ResultSetMetaData rsmd = rs.getMetaData();
      int colCount = rsmd.getColumnCount();
      Object columnValue = null;
      long rowCount = 0L;

      ArrayList<List<String>> list;
      for(list = new ArrayList(); rs.next(); ++rowCount) {
         for(int i = 0; i < colCount; ++i) {
            if (rowCount == 0L) {
               List<String> strlist = new ArrayList();
               strlist.add(rsmd.getColumnName(i + 1));
               list.add(i, strlist);
            }

            columnValue = rs.getObject(i + 1);
            ((List)list.get(i)).add(columnValue == null ? "" : columnValue.toString());
         }
      }

      check(list, 4, 0.6);
   }
}
