package org.diagbot.nlp.util;

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.nlp.test.LexemeDicFromMed
 * @Description TODO
 * @Author fyeman
 * @Date 2019/4/10/010 13:32
 * @Version 1.0
 **/
public class DictUtil {
    public static void main(String[] args) {
        DictUtil test = new DictUtil();

        String path = test.getClass().getClassLoader().getResource("").getPath();
        path = path.substring(0, path.indexOf("target")) + "src/main/resources/";

        test.writeToDict(path);
    }

    public void writeToDict(String path) {
        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();
            //所有词典库 不能用concat_group 大小写不区分
            String sql = "select l_1.name l_1_name, l_1.type_id type_id, l_2.name l_2_name, l_1.concept_id from kl_library_info l_1\n" +
                    "                    left join kl_library_info l_2 on l_1.concept_id = l_2.concept_id and l_2.is_concept = 1\n" +
                    "left join kl_concept kc on l_1.concept_id = kc.id\n" +
                    "where kc.is_deleted = 'N' ";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            FileWriter fw = new FileWriter(path + "tc.dict");

            Map<String, String> idMap = new HashMap<>(10);
            Map<String, String> nameMap = new HashMap<>(10);
            String r1;
            String r2;
            String r3;
            while (rs.next()) {
                r1 = rs.getString(1);
                r2 = rs.getString(2);
                r3 = rs.getString(4);
                if (idMap.get(r1) == null) {
                    idMap.put(r1, r2);
                    nameMap.put(r1, r3);
                } else if (idMap.get(r1) != null) {
                    idMap.put(r1, idMap.get(r1) + "," + r2);
                    nameMap.put(r1, nameMap.get(r1) + "," + r3);
                }
            }

            List<Map.Entry<String, String>> libraryList = new ArrayList<Map.Entry<String, String>>(idMap.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());
                }
            });
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|9|"
                        + entry.getValue() + "|" + nameMap.get(entry.getKey())));
                fw.write("\n");
            }

            fw.close();

            sql = "SELECT l_1.name l_1_name, l_2.name l_2_name, l_1.type_id FROM kl_library_info l_1 " +
                    "left join kl_library_info l_2 on l_1.concept_id = l_2.concept_id and l_2.is_concept = '1' " +
                    "where l_1.is_concept = '0'";
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            fw = new FileWriter(path + "synonym.dict");
            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();

            sql = "SELECT l_1.name l_1_name, l_2.name l_2_name FROM kl_relation r " +
                    "left join kl_concept c1 on r.start_id = c1.id " +
                    "left join kl_concept c2 on r.end_id = c2.id " +
                    "left join kl_library_info l_1 on l_1.id = c1.lib_id " +
                    "left join kl_library_info l_2 on l_2.id = c2.lib_id where r.relation_id = 3";
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            libraryList = rsToMap(rs, false);

            fw = new FileWriter(path + "chronic.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_name, k2.lib_name k2_lib_name from kl_relation r " +
                    "left join kl_concept k1 on k1.id = r.start_id " +
                    "left join kl_concept k2 on k2.id = r.end_id " +
                    "where r.relation_id = 19 and k1.is_deleted = 'N' and k2.is_deleted = 'N'";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            libraryList = rsToMap(rs, true);

            fw = new FileWriter(path + "push-tc.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                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;
    }
}
