package org.diagbot.nlp.test;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.diagbot.pub.jdbc.MysqlJdbc;
import org.springframework.util.StringUtils;

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

/**
 * @ClassName org.diagbot.nlp.test.ExcelRead
 * @Description TODO
 * @Author fyeman
 * @Date 2019/5/28/028 13:28
 * @Version 1.0
 **/
public class ExcelReadTest {
    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    private static Map<String, String> lis_words = new HashMap<>();
    private static Map<String, String> pacs_words = new HashMap<>();
    private static Map<String, String> symptom_words = new HashMap<>();
    private static Map<String, String> vital_words = new HashMap<>();
    private static Map<String, String> disease_words = new HashMap<>();

    private static final String lis_path  = "E:\\git\\docs\\医学知识库\\化验\\化验标准库第一期-邵启华0618.xlsx";
    private static final String pacs_path  = "E:\\git\\docs\\医学知识库\\辅检\\辅检标准库第一期-邵青华0617.xlsx";
    private static final String symptom_path  = "E:\\git\\docs\\医学知识库\\症状\\症状标准库-王玲0625.xlsx";
    private static final String drug_path  = "E:\\git\\docs\\医学知识库\\药品\\药品标准库-邵启华0515.xlsx";
    private static final String vital_path  = "E:\\git\\docs\\医学知识库\\体征\\体征标准库第一阶段-吕纯0620.xlsx";
    private static final String part_path  = "E:\\git\\docs\\医学知识库\\部位和科室\\部位标准库-邵启华0605.xlsx";
    private static final String disease_path = "E:\\git\\docs\\医学知识库\\诊断名和治疗方案\\知识库标准疾病名称和疾病治疗方案-邵青华0627.xlsx";

    public static void main(String[] args) {
        try {
//            readLis();
//            readDrug();
//            readDisease();
//            readPart();
//            readPacs();
            readPart1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

        Map<String, String> notin_words = new HashMap<>();
        int count = 0;
        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);
                Cell cell12 = row.getCell(12);
                put(lis_words, cell0);
                put(lis_words, cell2);
                put(lis_words, cell4);

                if (cell1 != null) {
                    String[] val =  cell1.getStringCellValue().split("、");
                    for (int i = 0; i < val.length; i++) {
                        put(lis_words, val[i], null, null);
                    }
                }
                if (cell3 != null) {
                    String[] val =  cell3.getStringCellValue().split("、");
                    for (int i = 0; i < val.length; i++) {
                        put(lis_words, val[i], null, null);
                    }
                }

                if (cell12 == null || StringUtils.isEmpty(cell12.getStringCellValue())) {
                    put(lis_words, cell2, "增高");
                    put(lis_words, cell2, "降低");
                } else {
                    if (cell12.getStringCellValue().indexOf("阴性") > -1) {
                        put(lis_words, cell2, "阴性");
                        put(lis_words, cell2, "阳性");
                    }
                }
            }catch (Exception e) {
                System.out.println("第" + count + "出现错误!");
            }
        }

//        List<Map<String, Object>> lists = new ArrayList<>();
//        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
//        List<Map<String, String>> data = nlpJdbc.query("medicine_rule_part1", new String[]{"id","standWord","correlationWord","result"}, " where number like '3.%'");
//        for (Map<String, String> map : data) {
//            String standWord = map.get("standWord");
//            String correlationWord = map.get("correlationWord");
//            if (lis_words.get(standWord) == null) {
//                Map<String, Object> line = new HashMap<>();
//                line.put("meal_name", standWord);
//                lists.add(line);
//                lis_words.put(standWord, standWord);
//            }
//            if (lis_words.get(correlationWord) == null) {
//                Map<String, Object> line = new HashMap<>();
//                line.put("meal_name", correlationWord);
//                lists.add(line);
//                lis_words.put(correlationWord, correlationWord);
//            }
//        }
//
//        nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
//        data = nlpJdbc.query("kl_standard_info", new String[]{"id","name"}, " where category_id in (13, 14)");
//        for (Map<String, String> map : data) {
//            String name = map.get("name");
//            if (lis_words.get(name) == null) {
//                Map<String, Object> line = new HashMap<>();
//                line.put("meal_name", name);
//                lists.add(line);
//                lis_words.put(name, name);
//            }
//        }
//
//        nlpJdbc.insert(lists, "lis_notin", new String[]{"meal_name"});
    }

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

        Map<String, String> words = new HashMap<>();
        words = loadDataFromSheet(sheet, words, new int[]{0, 1, 2, 3, 4});

        sheet = workbook.getSheetAt(1);
        words = loadDataFromSheet(sheet, words, new int[]{0});

        List<Map<String, Object>> lists = new ArrayList<>();
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("kl_standard_info", new String[]{"id","name"}, " where category_id = 10");
        for (Map<String, String> map : data) {
            String name = map.get("name");
            if (words.get(name) == null) {
                Map<String, Object> line = new HashMap<>();
                line.put("drug_name", name);
                lists.add(line);
                words.put(name, name);
            }
        }

        nlpJdbc.insert(lists, "drug_notin", new String[]{"drug_name"});
    }

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

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

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

        Map<String, String> words = new HashMap<>();
        int count = 0;
        Cell cell = null;
        String[] prefix = null;
        String[] suffix = 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);
                Cell cell5 = row.getCell(5);
                Cell cell6 = row.getCell(6);
                Cell cell7 = row.getCell(7);
                Cell cell8 = row.getCell(8);
                if (cell0 != null && !"".equals(cell0.getStringCellValue())) {
                    cell = cell0;
                } else if (cell1 != null && !"".equals(cell1.getStringCellValue())) {
                    cell = cell1;
                } else if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
                    cell = cell2;
                } else if (cell3 != null && !"".equals(cell3.getStringCellValue())) {
                    cell = cell3;
                } else if (cell4 != null && !"".equals(cell4.getStringCellValue())) {
                    cell = cell4;
                } else if (cell5 != null && !"".equals(cell5.getStringCellValue())) {
                    cell = cell5;
                } else if (cell6 != null && !"".equals(cell6.getStringCellValue())) {
                    cell = cell6;
                }

                if (cell7 != null) {
                    prefix  = cell7.getStringCellValue().split("、");
                }
                if (cell8 != null) {
                    suffix  = cell8.getStringCellValue().split("、");
                }
                String part = cell.getStringCellValue();
                if (part.equals("")) {
                    System.out.println(count + part);
                }
                if (part.indexOf("(") > -1) {
                    String part1 = part.substring(0, part.indexOf("("));
                    String part2 = part.substring(part.indexOf("(") + 1, part.indexOf(")"));
                    put(words, part1, prefix, suffix);
                    put(words, part2, prefix, suffix);
                } else {
                    put(words, part, prefix, suffix);
                }
                prefix = null;
                suffix = null;
            }catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

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

        for (Map.Entry<String, String> entry : words.entrySet()) {
            Map<String, Object> line = new HashMap<>();
            line.put("part", entry.getKey());
            line.put("concept", entry.getKey());
            lists.add(line);
        }

        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("kl_standard_info", new String[]{"id","name"}, " where category_id = 3");
        for (Map<String, String> map : data) {
            String name = map.get("name");
            if (words.get(name) == null) {
                Map<String, Object> line = new HashMap<>();
                line.put("part", name);
                lists.add(line);
                words.put(name, name);
            }
        }

        nlpJdbc.insert(lists, "part_notin", new String[]{"part", "concept"});
    }

    public static void readPacs() 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;
        String[] alias = 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 && !"".equals(cell0.getStringCellValue())) {
                    put(pacs_words, cell0);
                }
                if (cell1 != null && !"".equals(cell1.getStringCellValue())) {
                    put(pacs_words, cell1);
                }
                if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
                    put(pacs_words, cell2);
                }
                if (cell3 != null && !"".equals(cell3.getStringCellValue())) {
                    put(pacs_words, cell3);
                }
                if (cell4 != null && !"".equals(cell4.getStringCellValue())) {
                    String val = cell4.getStringCellValue();
                    if (val.indexOf("、") >  -1) {
                        alias = val.split("、");
                        for (int i = 0; i < alias.length; i++) {
                            put(pacs_words, alias[i], null,  null);
                        }
                    } else {
                        put(pacs_words, cell4);
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }

//        List<Map<String, Object>> lists = new ArrayList<>();
//        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
//        List<Map<String, String>> data = nlpJdbc.query("kl_standard_info", new String[]{"id","name"}, " where category_id = 17");
//        for (Map<String, String> map : data) {
//            String name = map.get("name");
//            if (pacs_words.get(name) == null) {
//                Map<String, Object> line = new HashMap<>();
//                line.put("pacs_name", name);
//                lists.add(line);
//                pacs_words.put(name, name);
//            }
//        }
//
//        nlpJdbc.insert(lists, "pacs_notin", new String[]{"pacs_name"});
    }

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

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

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

        Map<String, String> words = new HashMap<>();
        int count = 0;
        Cell cell = null;
        String[] prefix = null;
        String[] suffix = null;
        String[] self = 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);
                Cell cell5 = row.getCell(5);
                Cell cell6 = row.getCell(6);
                if (cell0 != null && !"".equals(cell0.getStringCellValue())) {
                    put(vital_words, cell0);
                }
                if (cell1 != null && !"".equals(cell1.getStringCellValue())) {
                    self = cell1.getStringCellValue().split("、");
                    for (int i = 0; i  < self.length; i++) {
                        put(vital_words, self[i], null, null);
                    }
                }
                if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
                    self = cell2.getStringCellValue().split("、");
                    for (int i = 0; i  < self.length; i++) {
                        put(vital_words, self[i], null, null);
                    }
                }
                if (cell6 != null && !"".equals(cell6.getStringCellValue())) {
                    put(vital_words, cell6);
                }
                if (cell3 != null && !"".equals(cell3.getStringCellValue())) {
                    self = cell3.getStringCellValue().split("、");
                    if (cell4 != null && !"".equals(cell4.getStringCellValue())) {
                        suffix = cell4.getStringCellValue().split("、");
                        for (int i = 0; i  < self.length; i++) {
                            put(vital_words, self[i], null, suffix);
                        }
                    }
                    if (cell5 != null && !"".equals(cell5.getStringCellValue())) {
                        prefix = cell5.getStringCellValue().split("、");
                        for (int i = 0; i  < self.length; i++) {
                            put(vital_words, self[i], prefix, null);
                        }
                    }
                }
                prefix = null;
                suffix = null;
            }catch (Exception e) {
                e.printStackTrace();
                System.out.println("第" + count + "出现错误!");
            }
        }
    }

    public static void readPart1() throws Exception {
//        readPacs();
//        readLis();
//        readSymptom();
//        readVital();
        readDisease();

        Map<String, String> all_words = new HashMap<>();

        String key = "";
        String value = "";
//        for (Map.Entry<String, String> entry : symptom_words.entrySet()) {
//            key = entry.getKey().trim().replace("（", "(").replace("）",")");
//            value = entry.getValue().trim().replace("（", "(").replace("）",")");
//            all_words.put(key, value);
//        }
//        for (Map.Entry<String, String> entry : vital_words.entrySet()) {
//            key = entry.getKey().trim().replace("（", "(").replace("）",")");
//            value = entry.getValue().trim().replace("（", "(").replace("）",")");
//            all_words.put(key, value);
//        }
//        for (Map.Entry<String, String> entry : lis_words.entrySet()) {
//            if (key.equals("血气分析")) {
//                System.out.println("血气分析");
//            }
//            key = entry.getKey().trim().replace("（", "(").replace("）",")");
//            value = entry.getValue().trim().replace("（", "(").replace("）",")");
//            all_words.put(key, value);
//        }
//        for (Map.Entry<String, String> entry : pacs_words.entrySet()) {
//            key = entry.getKey().trim().replace("（", "(").replace("）",")");
//            value = entry.getValue().trim().replace("（", "(").replace("）",")");
//            all_words.put(key, value);
//        }

        for (Map.Entry<String, String> entry : disease_words.entrySet()) {
            key = entry.getKey().trim().replace("（", "(").replace("）",")");
            value = entry.getValue().trim().replace("（", "(").replace("）",")");
            all_words.put(key, value);
        }



        File directFile = new File("E:\\git\\docs\\医学知识库\\诊断依据\\全部依据\\part1"); // 创建文件对象


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

        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(1);

        String diag = "";
        List<Map<String, Object>> lists = new ArrayList<>();

        List<Map<String, Object>> not_in_lists = new ArrayList<>();
        if (directFile.isDirectory()) {
            File[] files = directFile.listFiles();
            for (File file : files) {
                File[] excelFile = file.listFiles();
                for (int i = 0; i < excelFile.length; i++) {
                    FileInputStream in = new FileInputStream(excelFile[i]); // 文件流
                    Workbook workbook = getWorkbok(in, excelFile[i]);
                    Sheet sheet = workbook.getSheetAt(0);

                    diag = excelFile[i].getName().substring(0, excelFile[i].getName().indexOf(".xlsx"));
                    Map<String, String> words = new HashMap<>();
                    int count = 0;
                    String[] alias = 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.isEmpty(cell0.getStringCellValue())) {
                                break;
                            }

                            String number = "";
                            if (CellType.STRING == cell1.getCellTypeEnum()) {
                                number = cell1.getStringCellValue();
                            } else if (CellType.NUMERIC == cell1.getCellTypeEnum()) {
                                number = nf.format(cell1.getNumericCellValue());
                            }
                            if (number.indexOf("5.") > -1 || "鉴别诊断".equals(cell0.getStringCellValue())) {
                                if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
                                    Map<String, Object> line = new HashMap<>();
                                    line.put("disease", diag);
                                    line.put("type", cell0.getStringCellValue());
                                    line.put("number", number);
                                    line.put("standWord", cell2.getStringCellValue().trim());
                                    line.put("update_date", updateDate);
                                    lists.add(line);

                                    notinFound(all_words, not_in_lists, line);
                                }

                                if (cell3 != null && !"".equals(cell3.getStringCellValue())) {
                                    String val = cell3.getStringCellValue();
                                    if (val.indexOf("、") > -1) {
                                        alias = val.split("、");
                                        for (int j = 0; j < alias.length; j++) {
                                            Map<String, Object> line = new HashMap<>();
                                            line.put("disease", diag);
                                            line.put("type", cell0.getStringCellValue());
                                            line.put("number", number);
                                            line.put("standWord", alias[j].trim());
                                            line.put("update_date", updateDate);
                                            lists.add(line);

                                            notinFound(all_words, not_in_lists, line);
                                        }
                                    } else {
                                        Map<String, Object> line = new HashMap<>();
                                        line.put("disease", diag);
                                        line.put("type", cell0.getStringCellValue());
                                        line.put("number", number);
                                        line.put("standWord", val.trim());
                                        line.put("update_date", updateDate);
                                        lists.add(line);

                                        notinFound(all_words, not_in_lists, line);
                                    }
                                }
                            }
//                            if (number.indexOf("1.") > -1 || number.indexOf("2.") > -1) {
//                                if (cell2 != null && !"".equals(cell2.getStringCellValue())) {
//                                    Map<String, Object> line = new HashMap<>();
//                                    line.put("disease", diag);
//                                    line.put("type", cell0.getStringCellValue());
//                                    line.put("number", number);
//                                    line.put("standWord", cell2.getStringCellValue().trim());
//                                    line.put("update_date", updateDate);
//                                    lists.add(line);
//
//                                    notinFound(all_words, not_in_lists, line);
//                                }
//                                if (cell3 != null && !"".equals(cell3.getStringCellValue())) {
//                                    String val = cell3.getStringCellValue();
//                                    if (val.indexOf("、") > -1) {
//                                        alias = val.split("、");
//                                        for (int j = 0; j < alias.length; j++) {
//                                            Map<String, Object> line = new HashMap<>();
//                                            line.put("disease", diag);
//                                            line.put("type", cell0.getStringCellValue());
//                                            line.put("number", number);
//                                            line.put("standWord", alias[j].trim());
//                                            line.put("update_date", updateDate);
//                                            lists.add(line);
//
//                                            notinFound(all_words, not_in_lists, line);
//                                        }
//                                    } else {
//                                        Map<String, Object> line = new HashMap<>();
//                                        line.put("disease", diag);
//                                        line.put("type", cell0.getStringCellValue());
//                                        line.put("number", number);
//                                        line.put("standWord", val.trim());
//                                        line.put("update_date", updateDate);
//                                        lists.add(line);
//
//                                        notinFound(all_words, not_in_lists, line);
//                                    }
//                                }
//                            }else if (number.indexOf("3.") > -1) {
//                                if (cell3 != null && cell4 != null) {
//                                    String val3 = cell3.getStringCellValue();
//                                    String val4 = cell4.getStringCellValue();
//                                    if ("血气分析".equals(val3)) {
//                                        System.out.println("val3 血气分析");
//                                    }
//                                    if (val4.indexOf("、") > -1) {
//                                        alias = val4.split("、");
//                                        for (int j = 0; j < alias.length; j++) {
//                                            Map<String, Object> line = new HashMap<>();
//                                            line.put("disease", diag);
//                                            line.put("type", cell0.getStringCellValue());
//                                            line.put("number", number);
//                                            line.put("standWord", cell2.getStringCellValue().trim());
//                                            line.put("correlationWord", val3.trim());
//                                            line.put("result", alias[j].trim());
//                                            line.put("update_date", updateDate);
//                                            lists.add(line);
//
//                                            notinFound(all_words, not_in_lists, line);
//                                        }
//                                    } else {
//                                        Map<String, Object> line = new HashMap<>();
//                                        line.put("disease", diag);
//                                        line.put("type", cell0.getStringCellValue());
//                                        line.put("number", number);
//                                        line.put("standWord", cell2.getStringCellValue().trim());
//                                        line.put("correlationWord", val3.trim());
//                                        line.put("result", val4.trim());
//                                        line.put("update_date", updateDate);
//                                        lists.add(line);
//
//                                        notinFound(all_words, not_in_lists, line);
//                                    }
//                                } else {
//                                    if (cell2 != null) {
//                                        Map<String, Object> line = new HashMap<>();
//                                        line.put("disease", diag);
//                                        line.put("type", cell0.getStringCellValue());
//                                        line.put("number", number);
//                                        line.put("standWord", cell2.getStringCellValue().trim());
//                                        line.put("check_label", "4");
//                                        line.put("update_date", updateDate);
//                                        not_in_lists.add(line);
//                                    }
//                                }
//                            } else {
//                                Map<String, Object> line = new HashMap<>();
//                                line.put("disease", diag);
//                                line.put("type", cell0.getStringCellValue());
//                                line.put("number", number);
//                                line.put("standWord", cell2.getStringCellValue().trim());
//                                if (cell3 != null) {
//                                    line.put("correlationWord", cell3.getStringCellValue().trim());
//                                }
//                                if (cell4 != null) {
//                                    line.put("result", cell4.getStringCellValue().trim());
//                                }
//                                line.put("update_date", updateDate);
//                                lists.add(line);
//
//                                notinFound(all_words, not_in_lists, line);
//                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                            System.out.println("第" + count + "出现错误!" + excelFile[i].getName());
                        }
                    }
                }
            }
        }

//        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
//        nlpJdbc.insert(lists, "medicine_rule_part1", new String[]{"disease", "type", "number", "standWord", "correlationWord", "result", "update_date", "check_label"});


        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        nlpJdbc.insert(not_in_lists, "medicine_rule_part1_notin", new String[]{"disease", "type", "number", "standWord", "correlationWord", "result", "update_date", "check_label"});

    }

    public static void notinFound(Map<String, String> words, List<Map<String, Object>> not_in_list, Map<String, Object> line) {
        if (line.get("disease").toString().equals("肠息肉") && line.get("number").toString().equals("1.6")) {
            System.out.println("11111111");
        }

        String standWord = String.valueOf(line.get("standWord"));
        if (standWord != null) {
            standWord = standWord.replace("（", "(").replace("）",")");
        }

        if (words.get(standWord) == null) {
            line.put("check_label", "1");
        }
        if (line.get("correlationWord") != null) {
            String correlationWord = String.valueOf(line.get("correlationWord"));
            if (correlationWord != null) {
                correlationWord = correlationWord.replace("（", "(").replace("）",")");
            }

            String number = String.valueOf(line.get("number"));
            if (number.indexOf("4.") < 0) {
                if (words.get(correlationWord) == null) {
                    if (line.get("check_label") != null) {
                        line.put("check_label", line.get("check_label") + ",2");
                    } else {
                        line.put("check_label", "2");
                    }
                }
            }
        }
        if (line.get("result") != null) {
            String result = String.valueOf(line.get("result"));
            if (result != null) {
                result = result.replace("（", "(").replace("）",")");
            }
            if (words.get(result) == null) {
                if (line.get("check_label") != null) {
                    line.put("check_label", line.get("check_label") + ",3");
                } else {
                    line.put("check_label", "3");
                }
            }
        }
        if (line.get("check_label") !=  null) {
            not_in_list.add(line);
        }
    }

    public static Map<String, String> loadDataFromSheet(Sheet sheet, Map<String, String> words, int[] indexs) {
        int count = 0;
        for (Row row : sheet) {
            if (count < 1) {
                count++;
                continue;
            }
            count++;
            try {
                Cell cell = null;
                for (int i =  0; i < indexs.length; i++) {
                    cell = row.getCell(indexs[i]);
                    put(words, cell);
                }
            }catch (Exception e) {
                System.out.println("第" + count + "出现错误!");
            }
        }
        return words;
    }

    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;
    }
}
