package org.diagbot;

import org.diagbot.pub.jdbc.MysqlJdbc;
import org.diagbot.pub.utils.security.EncrypDES;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

/**
 * @ClassName org.diagbot.CacheFileManagerTest
 * @Description TODO
 * @Author fyeman
 * @Date 2019/3/8/008 14:05
 * @Version 1.0
 **/
public class CacheFileManagerTest {

    public static void main(String[] args) {
        CacheFileManagerTest test = new CacheFileManagerTest();
        test.contextMappingInitialized();
    }

    public void contextMappingInitialized() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "lantone", "jdbc:mysql://192.168.2.121:3306/med?useUnicode=true&characterEncoding=UTF-8");
        Connection conn = nlpJdbc.connect();
        Statement st = null;
        ResultSet rs = null;
        try {
            EncrypDES encrypDES = new EncrypDES();

            String path = this.getClass().getClassLoader().getResource("").getPath();
            path = path.substring(0, path.indexOf("target"));
            //体征衍射
//            String sql = "select distinct name, name_mapping from doc_result_mapping_vital order by name";
//            st = conn.createStatement();
//            rs = st.executeQuery(sql);
//            List<Map.Entry<String, String>> libraryList = rsToMap(rs, true);
//
//            FileWriter fw = new FileWriter(path + "src/main/resources/doc_result_mapping_vital.dict");
//            for (Map.Entry<String, String> entry : libraryList) {
//                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
//                fw.write("\n");
//            }
//            fw.close();
            //疾病科室
            String sql = "SELECT k1.lib_name diag_name, k2.lib_name dept_name FROM kl_concept_common kcc, kl_concept k1, kl_concept k2 " +
                    "where kcc.concept_id = k1.id and kcc.dept_id = k2.id " +
                    "and k1.lib_type = 18 and kcc.dept_id  is not null";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            List<Map.Entry<String, String>> libraryList = rsToMap(rs, true);

            FileWriter fw = new FileWriter(path + "src/main/resources/doc_result_mapping_diag.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                fw.write("\n");
            }
            fw.close();
            //性别年龄
            sql = "SELECT k1.lib_name, k1.lib_type, kcc.sex_type, kcc.min_age, kcc.max_age " +
                    "FROM kl_concept_common kcc, kl_concept k1 " +
                    "where kcc.concept_id = k1.id " +
                    "and k1.lib_type in (1, 18)";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            fw = new FileWriter(path + "src/main/resources/doc_result_mapping_filter.dict");

            String r1, r2, r3, r4, r5;
            while (rs.next()) {
                r1 = rs.getString(1);
                r2 = rs.getString(2);
                r3 = rs.getString(3);
                r4 = rs.getString(4);
                r5 = rs.getString(5);
                if ("18".equals(r2)) {
                    r2 = "2";
                }
                fw.write(encrypDES.encrytor(r1 + "|" + r2 + "|" + r3 + "|" + r4 + "|" + r5));
                fw.write("\n");
            }
            fw.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }

    public void standwordMappingInitialized() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8");
        Connection conn = nlpJdbc.connect();
        Statement st = null;
        ResultSet rs = null;
        try {
            EncrypDES encrypDES = new EncrypDES();

            String path = this.getClass().getClassLoader().getResource("").getPath();
            path = path.substring(0, path.indexOf("target"));
            String sql = "SELECT lib_name FROM kl_concept WHERE is_deleted = 'N' AND lib_type = 70 AND lib_name regexp '[0-9]'";
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            FileWriter fw = new FileWriter(path + "src/main/resources/kl_result_mapping_standword.dict");
            while (rs.next()) {
                fw.write(encrypDES.encrytor(rs.getString(1)));
                fw.write("\n");
            }
            fw.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }

    private List<Map.Entry<String, String>> rsToMap(ResultSet rs, boolean isJoin) throws SQLException{
        String r1 = "";
        String r2 = "";
        Map<String, String> libraryMap = new HashMap<>(10);
        while (rs.next()) {
            r1 = rs.getString(1);
            r2 = rs.getString(2);
            if (libraryMap.get(r1) == null) {
                libraryMap.put(r1, r2);
            } else if (isJoin && libraryMap.get(r1) != null) {
                libraryMap.put(r1, libraryMap.get(r1) + "," + r2);
            }
        }

        List<Map.Entry<String, String>> libraryList = new ArrayList<Map.Entry<String, String>>(libraryMap.entrySet());
        Collections.sort(libraryList, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });

        return libraryList;
    }
}
