package org.diagbot.nlp.test;

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.*;

/**
 * standard_info 数据生成词典文件 tc.dict
 */
public class LexemeDicTest {
    public static void main(String[] args) {
        LexemeDicTest test = new LexemeDicTest();
        test.writeToDict();
    }

    private void writeToDict() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/nlp-web?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, category_id, status from kl_standard_info_0318 where category_id != 100 and category_id is not null && category is not null 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/tc.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|9|" + entry.getValue() + "|9"));
                fw.write("\n");
            }
            fw.close();
            //推送词典
            sql = "select distinct name, category_id from kl_standard_info_0318 where status = 1 and category_id != 100 and category_id is not null && category is not null order by name";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            libraryList = rsToMap(rs, true);

            fw = new FileWriter(path + "src/main/resources/push-tc.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                fw.write("\n");
            }
            fw.close();

            sql = "select distinct name, relation_name from kl_standard_info_0318 where relation_type = 2 and relation_name is not null order by name";
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            libraryList = rsToMap(rs, false);

            fw = new FileWriter(path + "src/main/resources/synonym.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                fw.write("\n");
            }
            fw.close();

            sql = "select distinct name, relation_name from kl_standard_info_0318 where relation_type = 3 and relation_name is not null order by name";
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            libraryList = rsToMap(rs, false);

            fw = new FileWriter(path + "src/main/resources/classify.dict");
            for (Map.Entry<String, String> entry : libraryList) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|" + entry.getValue()));
                fw.write("\n");
            }
            fw.close();

            fw = new FileWriter(path + "src/main/resources/relation.dict");
            sql = "select distinct prop1_id, prop2_id from re_lexicon_property_pair where has_relation = 1";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                fw.write(encrypDES.encrytor(rs.getString(1) + "|" + rs.getString(2)));
                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;
    }
}
