package org.diagbot.graph;

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

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

public class CacheFile {
    public static void main(String[] args) {
        CacheFile cacheFile = new CacheFile();
        cacheFile.diagClassifyMaping();

    }
    //疾病 -- 科室
    public void diagDepartMaping(){
        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 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>> libaryList = rsToMap(rs, true);
            FileWriter fw = new FileWriter(path + "src/main/resources/doc_result_mapping_diag.dict");
            for (Map.Entry<String, String> entry : libaryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                fw.write("\n");
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }
    //疾病分类归一
    public void diagClassifyMaping(){
        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 g.diag_name,g.diag_bigName_classify,g.diag_subName_classify FROM `doc_bigsub_classify_diag` g ";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "src/main/resources/diagClassify.dict");
            String  r1 ,r2,r3;
            while (rs.next()) {
                r1 = rs.getString(1);//本类名
                r2 = rs.getString(2);//大类名
                r3 = rs.getString(3);//小类名
                fw.write(encrypDES.encrytor(r1+ "|" + r2+ "|" + r3));
                fw.write("\n");
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }
    //诊断大小类对应
    public void diagSort(){
        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 diag_name_big,diag_name_type from doc_result_sort_diag";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "src/main/resources/diagSort.dict");
            String  r2 ,r3;
            while (rs.next()) {
                r2 = rs.getString(1);//大类名
                r3 = rs.getString(2);//小类名
                fw.write(encrypDES.encrytor(r2+ "|" + r3));
                fw.write("\n");
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }
    //诊断和性别,年龄的对应
    public void sexAge(){
        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 k1.lib_name, k1.lib_type, kcc.sex_type, kcc.min_age, kcc.max_age \n" +
                    "FROM kl_concept_common kcc, kl_concept k1 \n" +
                    "where kcc.concept_id = k1.id \n" +
                    "and k1.lib_type in (18)";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "src/main/resources/sexAge.dict");
            String r1, r2 ,r3,r4;
            while (rs.next()) {
                r1 = rs.getString(1);//诊断名
                r2 = rs.getString(3);//sexType 1:男 2:女 3:都可以
                r3 = rs.getString(4);//min_age
                r4 = rs.getString(5);//max_age
                fw.write(encrypDES.encrytor(r1 + "|" + r2+ "|" + r3+ "|" + r4));
                fw.write("\n");
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }

    //标签类型对应
    public void contentMaping()  {
        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 id,name FROM `kl_lexicon`";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "src/main/resources/lexicon.dict");
            String r1, r2;
            while (rs.next()) {
                r1 = rs.getString(1);
                r2 = rs.getString(2);
                fw.write(encrypDES.encrytor(r1 + "|" + r2));
                fw.write("\n");
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nlpJdbc.close(rs, st, conn);
        }
    }
    //体征及体征结果
    public void vitalMaping()  {
        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\n" +
                    "\ti1.lib_name name1,\n" +
                    "\ti2.lib_name name2 \n" +
                    "FROM\n" +
                    "\t`kl_relation` l \n" +
                    "\tLEFT JOIN kl_concept i1 ON l.start_id = i1.id \n" +
                    "\tLEFT JOIN kl_concept i2 ON l.end_id = i2.id \n" +
                    "where l.relation_id=19 AND i1.lib_type=35 AND i2.lib_type=33";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "src/main/resources/vital.dict");
            String r1, r2;
            while (rs.next()) {
                r1 = rs.getString(1);//体征结果
                r2 = rs.getString(2);//体征指标
                fw.write(encrypDES.encrytor(r1 + "|" + r2));
                fw.write("\n");
            }
            fw.close();
        } 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;
    }
}
