package org.diagbot.graph.Diagnose;

import javafx.scene.effect.SepiaTone;
import org.diagbot.graph.jdbc.DriverManager;
import org.diagbot.pub.utils.PropertiesUtil;
import org.neo4j.driver.v1.*;

import java.util.*;

public class DiagnoseProcess {
    PropertiesUtil propertiesUtil = new PropertiesUtil("bolt.properties");
    protected Map<String,String> getDiagnose(String ...filds) throws Exception {
        Map<String,String> quezhenDiagnose = new LinkedHashMap<>();
        Set<String> startList = new HashSet<>();
        List<String> newList = new ArrayList<>();
        for (String fild:filds) {
            startList.add("\""+fild+"\"");
        }
        String query = "";
        StatementResult run =null;
        //获取driver和session
        Driver driver = DriverManager.newDrive("192.168.2.232","neo4j","root");
        Session session = driver.session(AccessMode.READ);
        //第一步查询是否有组合的词
        query = propertiesUtil.getProperty("searchCollection").replace("fildList",startList.toString());
        System.out.println(query);
        run = session.run(query);
        while (run.hasNext()){
            Record next = run.next();
            String jundgement = next.get("jundgement").toString();
            String standName = next.get("standName").toString();
            if ("TRUE".equals(jundgement)){
                startList.add(standName);
            }
        }

        //第一步查找近义词
        query = propertiesUtil.getProperty("serchCollect").replace("fildList",startList.toString());
        System.out.println(query);
        run = session.run(query);
        while (run.hasNext()){
            Record record = run.next();
            String fild = record.get("fild").toString();
            List<Object> typeCollect = record.get("typeCollect").asList();
            //如果不包含诊断依据就找它的同义词,否则就要这个词
            if("近义词".equals(typeCollect.get(0)) && typeCollect.size() ==1){
                query = "match(l)-[r:近义词]->(h) where l.name="+fild+" return h.name as js";
                StatementResult jinyiResult = session.run(query);
                while (jinyiResult.hasNext()){
                    Record next = jinyiResult.next();
                    String js = next.get("js").toString();
                    startList.remove(fild);
                    startList.add(js);
                }
            }
//            else {
//                startList.add(fild);
//                query = "match(l)-[r:近义词]->(h) where l.name="+fild+" return h.name as js";
//                StatementResult run1 = session.run(query);
//                while (run1.hasNext()){
//                    Record next = run1.next();
//                    String js = next.get("js").toString();
//                    startList.add(js);
//                }
//            }
        }
        System.out.println(startList);
        newList.addAll(startList);
        int i =0;
        //第二步查找能推诊断
        while (newList.size()>0){
            i++;
            query = propertiesUtil.getProperty("searchCondition").replace("newList",newList.toString()).replace("fildList",startList.toString());
            run = session.run(query);
            newList.clear();
            while (run.hasNext()){
                Record record = run.next();
                String condition = record.get("condition").toString();
                String jundgement = record.get("jundgement").toString();
                String label = record.get("label").toString();
                if ("TRUE".equals(jundgement)) {
                    newList.add(condition);
                    startList.add(condition);
                }
            }
        }
        //第三步查找确诊
        query = propertiesUtil.getProperty("searchQuezhen").replace("fildList",startList.toString());
        run = session.run(query);
        Set<String> dis = new LinkedHashSet<>();
        while (run.hasNext()){
            Record next = run.next();
            String quezhenName = next.get("name").toString().replace("\"","");
            String conditionType = next.get("relationType").toString().replace("\"","");
            if("确诊".equals(conditionType)){
                dis.add(quezhenName);
            }else if("拟诊".equals(conditionType)){
                dis.add(quezhenName);
            }
            for (String dise:dis
                 ) {
               quezhenDiagnose.put(dise,"疑似诊断");
            }

        }
        session.close();
        driver.close();
        return quezhenDiagnose;
    }
}
