package org.diagbot.nlp.test;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.diagbot.pub.Constants;
import org.diagbot.pub.jdbc.MysqlJdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @ClassName org.diagbot.nlp.test.ConceptTest
 * @Description TODO
 * @Author fyeman
 * @Date 2019/6/6/006 10:18
 * @Version 1.0
 **/
public class ConceptTest {
    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    private static final String lis_path = "E:\\git\\docs\\医学知识库\\化验\\化验标准库第一期-邵启华0701.xlsx";
    private static final String pacs_path = "E:\\git\\docs\\医学知识库\\辅检\\辅检标准库第一期-邵青华0612.xlsx";
    private static final String symptom_path = "E:\\git\\docs\\医学知识库\\症状\\症状标准库-王玲0625.xlsx";
    private static final String drug_path = "E:\\git\\docs\\医学知识库\\药品\\药品标准库-邵启华0618.xlsx";
    private static final String vital_path = "E:\\git\\docs\\医学知识库\\体征\\体征标准库第一阶段-吕纯0710_部分修改.xlsx";
    private static final String part_path = "E:\\git\\docs\\医学知识库\\部位和科室\\部位标准库-最新结构0621.xlsx";
    private static final String disease_path = "E:\\git\\docs\\医学知识库\\诊断名和治疗方案\\知识库标准疾病名称和疾病治疗方案-邵青华0605.xlsx";
    private static final String dept_path = "E:\\git\\docs\\医学知识库\\部位和科室\\科室标准库-邵启华0521.xlsx";
    private static final String scale_path = "E:\\git\\docs\\医学知识库\\量表\\量表标准词-邵青华0618.xlsx";
    private static final String other_path = "E:\\git\\docs\\医学知识库\\其他史\\其他史-邵青华0619.xls";
    private static final String zdyj_path = "E:\\git\\docs\\医学知识库\\诊断依据\\全部依据\\带数字的词\\带数字的词.xlsx";
    private static final String zdyjqd_path = "E:\\git\\docs\\医学知识库\\诊断依据\\全部依据\\其他有问题的词\\诊断依据问题词0701.xlsx";
    private static final String pacsresult_path = "E:\\git\\docs\\医学知识库\\诊断依据\\全部依据\\其他有问题的词\\诊断依据问题词0702.xlsx";
    private static final String lisresult_path = "E:\\git\\docs\\医学知识库\\化验\\化验结果_诊断依据0702.xlsx";
    private static final String vital_result_index_path = "E:\\git\\docs\\医学知识库\\体征\\体征结果映射关系.xlsx";

    public static void main(String[] args) {
        try {
//            insertSymptom();
//            insertPushSymptom();
//            insertPart();
//            insertVital();
//            insertLis();
//            insertLisResult();
//            insertPacs();
//            insertPacsResult();
//            insertDisease();
//            insertDept();
//            insertDrug();
//            insertScale();
//            insertOther();

//            insertStandardInfo();
//            validateSplit();
//            insertZdyj();
//            insertZdyjQt();

//            insertRelationSymptom();
            insertVitalResultIndex();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void insertScale() throws Exception {
        insertOneColumn(0, scale_path, 0, "48", "量表");
        insertOneColumn(1, scale_path, 0, "49", "不良反应");
        insertOneColumn(2, scale_path, 0, "50", "核心指标");
    }

    public static void insertDept() throws Exception {
        insertTwoColumn(dept_path, 0, 1, "47", "科室");
    }

    public static void insertSymptom() throws Exception {
        insertTwoColumn(1, symptom_path, 0, 1, "1", "症状");
    }

    public static void insertPushSymptom() throws Exception {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("kl_library_info", new String[]{"id", "name", "type_id"}, " where type_id = 1 and is_concept = 1");
        Map<String, String> hasConceptWords = new HashMap<>();
        for (Map<String, String> map : data) {
            String name = map.get("name");
            hasConceptWords.put(name, name);
        }

        Sheet sheet = loadSheet(symptom_path, 1);
        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell = row.getCell(6);
                if (cell != null && StringUtils.isNotEmpty(cell.getStringCellValue()) && hasConceptWords.get(cell.getStringCellValue()) == null) {
                    all_words.put(cell.getStringCellValue().trim(), cell.getStringCellValue().trim());
                    concept_words.put(cell.getStringCellValue().trim(), cell.getStringCellValue().trim());
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        convertAndSave("1", "症状", all_words, concept_words);
    }

    public static void insertPart() throws Exception {
        insertTwoColumn(part_path, 0, 1, "3", "部位");
    }

    public static void insertVital() throws Exception {
        insertVitalIndex();
        insertVitalValue();
        insertVitalResult();
    }

    public static void insertLis() throws Exception {
        insertLisMeal();
        insertLisDetail();
        insertLisPublic();
        insertLisMealPublic();
    }

    public static void insertLisMeal() throws Exception {
        insertTwoColumn(lis_path, 0, 1, "12", "化验套餐");
    }

    public static void insertLisDetail() throws Exception {
        insertTwoColumn(lis_path, 2, 3, "13", "化验明细");
    }

    public static void insertLisPublic() throws Exception {
        insertOneColumn(lis_path, 4, "46", "化验公表项");
    }

    public static void insertLisMealPublic() throws Exception {
        insertOneColumn(lis_path, 5, "46", "化验公表项");
    }

    public static void insertOther() throws Exception {
        insertOneColumn(other_path, 0, "55", "既往疾病史");
        insertOneColumn(other_path, 1, "56", "外伤史");
        insertOneColumn(other_path, 2, "57", "手术史");
        insertOneColumn(other_path, 3, "58", "输血史");
        insertOneColumn(other_path, 4, "59", "传染病史");
        insertOneColumn(other_path, 5, "60", "不良习惯");
        insertOneColumn(other_path, 6, "61", "冶游史");
        insertOneColumn(other_path, 7, "62", "婚育史");
        insertOneColumn(other_path, 8, "63", "生育史");
        insertOneColumn(other_path, 9, "64", "家族史");
        insertOneColumn(other_path, 10, "65", "食物过敏史");
        insertOneColumn(other_path, 11, "66", "药物过敏史");
        insertOneColumn(other_path, 12, "67", "家族相关传染病史");

    }

    public static void insertZdyj() throws Exception {
        insertOneColumn(zdyj_path, 4, "69", "诊断依据条件");
    }

    public static void insertZdyjQt() throws Exception {
        insertOneColumn(zdyjqd_path, 4, "70", "诊断依据其他");
    }

    public static void insertPacsResult() throws Exception {
        insertOneColumn(pacsresult_path, 4, "17", "辅检结果");
    }

    public static void insertLisResult() throws Exception {
        insertOneColumn(lisresult_path, 4, "14", "化验结果");
    }

    public static void insertVitalResultIndex() throws Exception {
        insertOneColumn(vital_result_index_path, 1, "33", "体征指标");
    }

    public static void insertOneColumn(String path, int columnIndex, String typeId, String typeName) throws Exception {
        insertOneColumn(0, path, columnIndex, typeId, typeName);
    }

    public static void insertOneColumn(int sheetIndex, String path, int columnIndex, String typeId, String typeName) throws Exception {
        Sheet sheet = loadSheet(path, sheetIndex);
        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell = row.getCell(columnIndex);
                if (cell != null && StringUtils.isNotEmpty(cell.getStringCellValue())) {
                    all_words.put(cell.getStringCellValue().trim(), cell.getStringCellValue().trim());
                    concept_words.put(cell.getStringCellValue().trim(), cell.getStringCellValue().trim());
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        convertAndSave(typeId, typeName, all_words, concept_words);
    }

    public static void  insertTwoColumn(String path, int columnIndex0, int columnIndex1, String typeId, String typeName) throws Exception {
        insertTwoColumn(0, path, columnIndex0, columnIndex1, typeId, typeName);
    }

    public static void  insertTwoColumn(int sheetIndex, String path, int columnIndex0, int columnIndex1, String typeId, String typeName) throws Exception {
        Sheet sheet = loadSheet(path, sheetIndex);

        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();

        String[] self = null;
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell0 = row.getCell(columnIndex0);
                Cell cell1 = row.getCell(columnIndex1);
                if (cell0 != null && StringUtils.isNotEmpty(cell0.getStringCellValue())) {
                    concept_words.put(cell0.getStringCellValue().trim(), cell0.getStringCellValue().trim());
                    all_words.put(cell0.getStringCellValue().trim(), cell0.getStringCellValue().trim());
                }
                if (cell1 != null && StringUtils.isNotEmpty(cell1.getStringCellValue()) && cell0 != null) {
                    self = cell1.getStringCellValue().split("、");
                    for (int i = 0; i < self.length; i++) {
                        all_words.put(self[i].trim(), cell0.getStringCellValue().trim());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        convertAndSave(typeId, typeName, all_words, concept_words);
    }

    public static Sheet loadSheet(String path, int sheetIndex) throws Exception {
        File excelFile = new File(path); // 创建文件对象
        FileInputStream in = new FileInputStream(excelFile); // 文件流
        Workbook workbook = getWorkbok(in, excelFile);
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        return sheet;
    }

    public static void convertAndSave(String typeId, String typeName, Map<String, String> all_words, Map<String, String> concept_words) {
        List<Map<String, Object>> all_words_list = new ArrayList<>();
        for (Map.Entry<String, String> entry : all_words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            if (StringUtils.isNotEmpty(entry.getKey())) {
                line.put("name", entry.getKey());
                line.put("type_id", typeId);
                line.put("type", typeName);
                all_words_list.add(line);
            }
        }
        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertPacs() throws Exception {
        File excelFile = new File(pacs_path); // 创建文件对象
        FileInputStream in = new FileInputStream(excelFile); // 文件流
        Workbook workbook = getWorkbok(in, excelFile);
        Sheet sheet = workbook.getSheetAt(0);

        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();

        String[] self = null;
        Cell cell = null;
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell0 = row.getCell(0);
                Cell cell1 = row.getCell(1);
                Cell cell2 = row.getCell(2);
                Cell cell3 = row.getCell(3);
                Cell cell4 = row.getCell(4);
                if (cell0 != null && StringUtils.isNotEmpty(cell0.getStringCellValue())) {
                    cell = cell0;
                }
                if (cell1 != null && StringUtils.isNotEmpty(cell1.getStringCellValue())) {
                    cell = cell1;
                }
                if (cell2 != null && StringUtils.isNotEmpty(cell2.getStringCellValue())) {
                    cell = cell2;
                }
                if (cell3 != null && StringUtils.isNotEmpty(cell3.getStringCellValue())) {
                    cell = cell3;
                }
                if (cell != null) {
                    put(concept_words, cell);
                    all_words.put(cell.getStringCellValue(), cell.getStringCellValue());
                }
                if (cell4 != null && StringUtils.isNotEmpty(cell4.getStringCellValue()) && cell != null) {
                    self = cell4.getStringCellValue().split("、");
                    for (int i = 0; i < self.length; i++) {
                        all_words.put(self[i], cell.getStringCellValue());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
            cell = null;
        }

        List<Map<String, Object>> all_words_list = new ArrayList<>();
        for (Map.Entry<String, String> entry : all_words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            if (StringUtils.isNotEmpty(entry.getKey())) {
                line.put("name", entry.getKey());
                line.put("type_id", "16");
                line.put("type", "辅检项目");
                all_words_list.add(line);
            }
        }

        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertDisease() throws Exception {
        insertTwoColumn(disease_path, 0, 1, "18", "疾病");
//        String updateDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//
//        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8");
//        List<Map<String, String>> data = nlpJdbc.query("kl_library_info", new String[]{"id", "name", "type_id", "concept_id"}, " where type_id = 18 and name like '%，%' and is_concept = 0");
//        String diag = "";
//        List<Map<String, Object>> diags_list = new ArrayList<>();
//        Map<String, String> hasDiagMap = new HashMap<>();
//        for (Map<String, String> map : data) {
//            diag = map.get("name");
//            String[] diags = diag.split("，");
//            for (String d : diags) {
//                if (hasDiagMap.get(d) == null) {
//                    Map<String, Object> line = new HashMap<>();
//                    line.put("name", d);
//                    line.put("type_id", "18");
//                    line.put("concept_id", map.get("concept_id"));
//                    line.put("is_concept", "0");
//                    initInsertLine(line, updateDate);
//                    if (!d.equals("劳力性心绞痛") && !d.equals("妊娠期高血压疾病")) {
//                        diags_list.add(line);
//                    }
//
//                    hasDiagMap.put(d, d);
//                }
//            }
//        }
//        nlpJdbc.insertBatch(diags_list, "kl_library_info", new String[]{"name", "type_id", "is_concept", "concept_id", "creator", "modifier", "gmt_create", "gmt_modified", "is_deleted"});

    }

    public static void insertDrug() throws Exception {
        insertTwoColumn(drug_path, 0, 1, "53", "药品化学名");
        insertTwoColumn(drug_path, 2, 3, "10", "药品通用名");
        insertOneColumn(drug_path, 4, "54", "药品商品名");
        insertOneColumn(drug_path, 5, "29", "药品大类");
        insertOneColumn(drug_path, 6, "30", "药品小类");
    }

    public static void insertVitalIndex() throws Exception {
        File excelFile = new File(vital_path); // 创建文件对象
        FileInputStream in = new FileInputStream(excelFile); // 文件流
        Workbook workbook = getWorkbok(in, excelFile);
        Sheet sheet = workbook.getSheetAt(0);

        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();

        String[] self = null;
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell1 = row.getCell(1);
                Cell cell3 = row.getCell(3);

                String concept_word = "";
                if (cell1 != null && !"".equals(cell1.getStringCellValue())) {
                    self = cell1.getStringCellValue().split("、");
                    concept_word = self[0];
                    if (concept_words.get(self[0]) == null) {
                        put(concept_words, self[0], null, null);
                        for (int i = 0; i < self.length; i++) {
                            if (all_words.get(self[i]) == null) {
                                all_words.put(self[i], self[0]);
                            }
                        }
                    } else {
                        for (int i = 0; i < self.length; i++) {
                            if (all_words.get(self[i]) == null) {
                                all_words.put(self[i], self[0]);
                            }
                        }
                    }
                }

                if (cell3 != null && StringUtils.isNotEmpty(cell3.getStringCellValue())) {
                    self = cell3.getStringCellValue().split("、");
                    for (int i = 0; i < self.length; i++) {
                        if (all_words.get(self[i]) == null) {
                            all_words.put(self[i], concept_word);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        List<Map<String, Object>> all_words_list = new ArrayList<>();

        for (Map.Entry<String, String> entry : all_words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            if (StringUtils.isNotEmpty(entry.getKey())) {
                line.put("name", entry.getKey());
                line.put("type_id", "33");
                line.put("type", "体征指标");
                all_words_list.add(line);
            }
        }
        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertVitalValue() throws Exception {
        File excelFile = new File(vital_path); // 创建文件对象
        FileInputStream in = new FileInputStream(excelFile); // 文件流
        Workbook workbook = getWorkbok(in, excelFile);
        Sheet sheet = workbook.getSheetAt(0);

        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();

        String[] self = null;
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell2 = row.getCell(2);
                if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
                    self = cell2.getStringCellValue().split("、");
                    for (int i = 0; i < self.length; i++) {
                        put(concept_words, self[i], null, null);
                        all_words.put(self[i], self[i]);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        List<Map<String, Object>> all_words_list = new ArrayList<>();
        for (Map.Entry<String, String> entry : all_words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            if (StringUtils.isNotEmpty(entry.getKey())) {
                line.put("name", entry.getKey());
                line.put("type_id", "34");
                line.put("type", "体征指标值");
                all_words_list.add(line);
            }
        }

        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertVitalResult() throws Exception {
        File excelFile = new File(vital_path); // 创建文件对象
        FileInputStream in = new FileInputStream(excelFile); // 文件流
        Workbook workbook = getWorkbok(in, excelFile);
        Sheet sheet = workbook.getSheetAt(0);

        int count = 0;
        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();

        String[] self = null;
        String[] suffix = null;
        String[] prefix = null;
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell0 = row.getCell(0);
                Cell cell3 = row.getCell(3);
                Cell cell4 = row.getCell(4);
                Cell cell5 = row.getCell(5);
                Cell cell6 = row.getCell(6);
                if (cell0 != null && StringUtils.isNotEmpty(cell0.getStringCellValue())) {
                    put(concept_words, cell0);
                    all_words.put(cell0.getStringCellValue(), cell0.getStringCellValue());
                }
                if (cell3 != null && StringUtils.isNotEmpty(cell3.getStringCellValue())) {
                    self = cell3.getStringCellValue().split("、");
                    if (cell4 != null && StringUtils.isNotEmpty(cell4.getStringCellValue())) {
                        suffix = cell4.getStringCellValue().split("、");
                        for (int i = 0; i < self.length; i++) {
                            for (int j = 0; j < suffix.length; j++) {
                                all_words.put(self[i] + suffix[j], cell0.getStringCellValue());
                            }
                        }
                    }
                    if (cell5 != null && StringUtils.isNotEmpty(cell5.getStringCellValue())) {
                        prefix = cell5.getStringCellValue().split("、");
                        for (int i = 0; i < self.length; i++) {
                            for (int j = 0; j < prefix.length; j++) {
                                all_words.put(prefix[j] + self[i], cell0.getStringCellValue());
                            }
                        }
                    }
                }
                if (cell6 != null && StringUtils.isNotEmpty(cell6.getStringCellValue())) {
                    self = cell6.getStringCellValue().split("、");
                    for (int i = 0; i < self.length; i++) {
                        all_words.put(self[i], cell0.getStringCellValue());
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
            prefix = null;
            suffix = null;
        }

        List<Map<String, Object>> all_words_list = new ArrayList<>();
        for (Map.Entry<String, String> entry : all_words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            if (StringUtils.isNotEmpty(entry.getKey())) {
                line.put("name", entry.getKey());
                line.put("type_id", "35");
                line.put("type", "体征结果");
                all_words_list.add(line);
            }
        }

        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertRelationSymptom() throws Exception {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("kl_concept", new String[]{"id", "lib_name"}, " where lib_type = 1");
        Map<String, String> hasConceptWords = new HashMap<>();
        for (Map<String, String> map : data) {
            hasConceptWords.put(map.get("lib_name"), map.get("id"));
        }

        String updateDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        Sheet sheet = loadSheet(symptom_path, 1);
        int count = 0;
        List<Map<String, Object>> relation_words_list = new ArrayList<>();
        List<String> hasRelationList = new ArrayList<>();
        for (Row row : sheet) {
            // 跳过第一和第二行的目录
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell0 = row.getCell(0);
                Cell cell6 = row.getCell(6);
                if (cell0 != null && StringUtils.isNotEmpty(cell0.getStringCellValue())
                        && cell6 != null && StringUtils.isNotEmpty(cell6.getStringCellValue())) {
                    if (!hasRelationList.contains(cell0.getStringCellValue() + " " + cell6.getStringCellValue())
                            && !hasRelationList.contains(cell6.getStringCellValue() + " " + cell0.getStringCellValue())) {
                        Map<String, Object> line = new HashMap<>();
                        line.put("start_id", hasConceptWords.get(cell0.getStringCellValue()));
                        line.put("end_id", hasConceptWords.get(cell6.getStringCellValue()));
                        line.put("relation_id", "19");
                        initInsertLine(line, updateDate);
                        relation_words_list.add(line);

                        hasRelationList.add(cell0.getStringCellValue() + " " + cell6.getStringCellValue());
                        hasRelationList.add(cell6.getStringCellValue() + " " + cell0.getStringCellValue());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

        nlpJdbc.insertBatch(relation_words_list, "kl_relation", new String[]{"start_id", "end_id", "relation_id", "creator", "modifier", "gmt_create", "gmt_modified", "is_deleted"});
    }

    public static void insertStandardInfo() throws Exception {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-med-0606?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("kl_standard_info_0318", new String[]{"id", "name", "category_id", "category"}, " where category_id not in (1, 3, 13,14,15, 17,18,19,60,61,62, 10)");

        Map<String, String> concept_words = new HashMap<>();
        Map<String, String> all_words = new HashMap<>();
        List<Map<String, Object>> all_words_list = new ArrayList<>();
        for (Map<String, String> map : data) {
            String name = map.get("name");
            concept_words.put(name, name);
            all_words.put(name, name);

            Map<String, Object> line = new HashMap<>();
            line.put("name", map.get("name"));
            line.put("type_id", map.get("category_id"));
            line.put("type", map.get("category"));
            all_words_list.add(line);
        }

        insertLibraryInfo(all_words_list, all_words, concept_words);
    }

    public static void insertLibraryInfo(List<Map<String, Object>> list, Map<String, String> all_words, Map<String, String> concept_words) {
        String updateDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> line : list) {
            line.put("order_no", "0");
            line.put("is_concept", "0");
            initInsertLine(line, updateDate);
        }
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/med-s?useUnicode=true&characterEncoding=UTF-8");
//        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-med-0606?useUnicode=true&characterEncoding=UTF-8");
        int[] infoIds = nlpJdbc.insertBatch(list, "kl_library_info", new String[]{"name", "type_id", "is_concept", "creator", "modifier", "gmt_create", "gmt_modified", "is_deleted"});

        List<Map<String, Object>> concept_words_list = new ArrayList<>();
        String concept_word = "";
        for (int i = 0; i < infoIds.length; i++) {
            concept_word = list.get(i).get("name").toString();
            if (concept_words.get(concept_word) != null) {
                Map<String, Object> concept_line = new HashMap<>();
                concept_line.put("lib_id", infoIds[i]);
                concept_line.put("lib_name", concept_word);
                concept_line.put("lib_type", list.get(i).get("type_id"));
                initInsertLine(concept_line, updateDate);
                concept_words_list.add(concept_line);
            }
        }

        int[] conceptIds = nlpJdbc.insertBatch(concept_words_list, "kl_concept", new String[]{"lib_id", "lib_name", "lib_type", "creator", "modifier", "gmt_create", "gmt_modified", "is_deleted"});
        Map<String, String> concept_word_ids = new HashMap<>();
        for (int i = 0; i < conceptIds.length; i++) {
            concept_word_ids.put(concept_words_list.get(i).get("lib_name").toString(), String.valueOf(conceptIds[i]));
        }

        List<Map<String, Object>> updates = new ArrayList<>();
        List<Map<String, Object>> wheres = new ArrayList<>();

        for (int i = 0; i < infoIds.length; i++) {
            Map<String, Object> line = new HashMap<>();
            Map<String, Object> where = new HashMap<>();
if (concept_word_ids.get(all_words.get(list.get(i).get("name").toString())) == null) {
    System.out.println(list.get(i).get("name").toString());
    System.out.println(all_words.get(list.get(i).get("name").toString()));
}
            line.put("concept_id", concept_word_ids.get(all_words.get(list.get(i).get("name").toString())));
            concept_word = list.get(i).get("name").toString();
            if (concept_words.get(concept_word) != null) {
                line.put("is_concept", "1");
            } else {
                line.put("is_concept", "0");
            }
            updates.add(line);
            where.put("id", infoIds[i]);
            wheres.add(where);
        }
        nlpJdbc.update("kl_library_info", updates, wheres);
    }

    public static void initInsertLine(Map<String, Object> line, String updateDate) {
        line.put("is_deleted", "N");
        line.put("creator", "-99999");
        line.put("modifier", "-99999");
        line.put("gmt_create", updateDate);
        line.put("gmt_modified", updateDate);
    }

    public static Workbook getWorkbok(InputStream in, File file) throws IOException {
        Workbook wb = null;
        if (file.getName().endsWith(EXCEL_XLS)) {  //Excel 2003
            wb = new HSSFWorkbook(in);
        } else if (file.getName().endsWith(EXCEL_XLSX)) {  // Excel 2007/2010
            wb = new XSSFWorkbook(in);
        }
        return wb;
    }

    public static Map<String, String> put(Map<String, String> words, String word, String[] prefix, String[] suffix) {
        if (words.get(word) == null) {
            words.put(word, word);
        }
        if (prefix != null) {
            for (int i = 0; i < prefix.length; i++) {
                if (words.get(prefix[i] + word) == null) {
                    words.put(prefix[i] + word, prefix[i] + word);
                }
            }
        }
        if (suffix != null) {
            for (int i = 0; i < suffix.length; i++) {
                if (words.get(word + suffix[i]) == null) {
                    words.put(word + suffix[i], word + suffix[i]);
                }
            }
        }
        if (prefix != null && suffix != null) {
            for (int i = 0; i < prefix.length; i++) {
                for (int j = 0; j < suffix.length; j++) {
                    if (words.get(prefix[i] + word + suffix[j]) == null) {
                        words.put(prefix[i] + word + suffix[j], prefix[i] + word + suffix[j]);
                    }
                }
            }
        }
        return words;
    }

    public static Map<String, String> put(Map<String, String> words, Cell cell) {
        return put(words, cell, null);
    }

    public static Map<String, String> put(Map<String, String> words, Cell cell, String suffix) {
        if (cell == null) {
            return words;
        }
        String[] key = cell.getStringCellValue().split("、");
        for (int i = 0; i < key.length; i++) {
            if (words.get(key[i]) == null) {
                if (suffix == null) {
                    words.put(key[i], key[i]);
                } else {
                    words.put(key[i] + suffix, key[i] + suffix);
                }
            }
        }
        return words;
    }
}
