package org.diagbot.graph.encryptionNeo4j;
import org.diagbot.graph.encryption.Coder;
import org.diagbot.graph.util.NodeRelationUtil;
import org.diagbot.pub.utils.PropertiesUtil;
import org.neo4j.driver.v1.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 把233 neo4j里的数据读出来,写到232中
 * 1:加密
 * 0:不加密
 */
public class ReadNeo4jData2Neo4j{
    public static PropertiesUtil propertiesUtil = new PropertiesUtil("query.properties");
    public static void main(String[] args) {
        String[] diseaseArray ={"慢性胃炎","急性胰腺炎","心脏病","肺部感染"};
//        String[] diseaseArray ={"肺部感染"};
        NodeRelationUtil nodeRelationUtil = new NodeRelationUtil();
        Driver driver233 = nodeRelationUtil.getDriver();//读
        Driver driver232 = nodeRelationUtil.get232Driver();//写
        List<String> diseaseList =  new ArrayList<String>();
        Session session = null;
        StatementResult result =null;
        session = driver233.session(AccessMode.WRITE);
        //把233的数据存到232,包括症状,体征,化验,辅检,诊断依据
//        from233to232(diseaseArray,nodeRelationUtil, driver232, session);
        //把233的治疗数据存入232,用药数据
       from233treatto232(diseaseArray,nodeRelationUtil, driver232, session);
        session.close();
        driver233.close();
        driver232.close();

    }

    /**
     * 把233的治疗数据存入232
     * @param diseaseArray
     * @param nodeRelationUtil
     * @param driver
     * @param session
     */
    private static void from233treatto232(String[] diseaseArray,NodeRelationUtil nodeRelationUtil,Driver driver,Session session) {
        List<String> diseaseNameList;
        String queryWord;
        StatementResult resultSet;
        diseaseNameList = new ArrayList<String>(Arrays.asList(diseaseArray));
        for (String diseaseName:diseaseNameList) {
            nodeRelationUtil.AddNode(driver,0,"Disease",diseaseName.replace("\"",""));//添加疾病
            queryWord =propertiesUtil.getProperty("diseaseDrugsMedication").replace("diseaseName",diseaseName);
            resultSet = session.run(queryWord);
            while (resultSet.hasNext()){
                Record record = resultSet.next();
                String drug = record.get("drug").toString().replace("\"","");//药类
                String label = record.get("label").toString().replace("\"","");//类型
                int p1 = record.get("p").asInt();
                List<Object> collectName = record.get("collectName").asList();
                String s = collectName.get(0).toString();
                nodeRelationUtil.AddNode(driver,0,"Drugs",drug);
                nodeRelationUtil.addRelationWithProperty(driver,"Disease",diseaseName.replace("\"",""),"推荐",p1,"Drugs",drug);
                for (Object o:collectName) {
                    String medicationName = o.toString();
                    nodeRelationUtil.AddNode(driver,0,"Medicine",medicationName);
                    nodeRelationUtil.addRelation(driver,0,"Drugs",drug,"包含","Medicine",medicationName);
                }
            }
            queryWord = propertiesUtil.getProperty("diseaseMedication").replace("diseaseName",diseaseName);
            resultSet = session.run(queryWord);
            while (resultSet.hasNext()){
                Record record = resultSet.next();
                double p1 = record.get("p").asDouble();
                String medication = record.get("collectName").toString().replace("\"","");//类型
                nodeRelationUtil.AddNode(driver,0,"Medicine",medication);
                nodeRelationUtil.addRelationWithPropertyDouble(driver,"Disease",diseaseName.replace("\"",""),"推荐",p1,"Medicine",medication);
            }
            queryWord = propertiesUtil.getProperty("bigDrugAndSubDrugs").replace("diseaseName",diseaseName);
            resultSet = session.run(queryWord);
            while (resultSet.hasNext()){
                Record record = resultSet.next();
                String bigDrugs = record.get("big").toString().replace("\"","");//大类型
                String subDrugs = record.get("sub").toString().replace("\"","");//小类型
                nodeRelationUtil.AddNode(driver,0,"Drugs",bigDrugs);
                nodeRelationUtil.AddNode(driver,0,"Drugs",subDrugs);
                nodeRelationUtil.addRelation(driver,0,"Drugs",bigDrugs,"包含","Drugs",subDrugs);
                nodeRelationUtil.addRelation(driver,0,"Disease",diseaseName.replace("\"",""),"推荐","Drugs",bigDrugs);
            }
        }
    }

    private static void from233to232(String[] diseaseArray,NodeRelationUtil nodeRelationUtil, Driver driver232, Session session) {
        List<String> diseaseList;
        String query;
        StatementResult result;
        //        String[] diseaseArray = {"胃癌", "胆囊癌", "胰腺癌", "膀胱癌", "甲状腺功能减退", "甲状腺结节", "高血脂", "癫痫病", "胆囊结石", "青光眼", "高血压", "心包积液", "阵发性室上性心动过速", "食管癌", "窦性心动过速", "心房颤动", "轮状病毒肠炎", "急性胃炎", "慢性胃炎", "克罗恩病", "结肠炎", "帕金森病", "胆管炎", "肝脓肿", "盆腔炎", "上消化道出血", "泌尿系结石", "输尿管结石", "贫血", "胆囊炎", "脑出血", "脑梗死", "痛风", "急性肠炎", "慢性咽炎", "自身免疫性甲状腺炎", "鼻咽癌", "鼻炎", "多囊卵巢综合征", "贫血", "肺癌", "肺栓塞", "肝癌", "肝功能异常", "肝硬化", "干燥综合征", "宫颈炎", "骨质疏松症", "过敏性鼻炎", "甲状腺癌", "甲状腺功能亢进", "甲状腺炎", "类风湿性关节炎", "尿路感染", "泌尿系结石", "前列腺恶性肿瘤", "前列腺增生", "缺铁性贫血", "乳腺癌", "上呼吸道感染", "肾病综合征", "肾盂肾炎", "湿疹", "糖尿病", "系统性红斑狼疮", "消化性溃疡", "支气管哮喘", "异位妊娠", "阴道炎", "原发性肝癌", "支气管哮喘", "支气管炎", "脂肪肝", "自身免疫性甲状腺炎", "低钙血症", "低钾血症", "低血糖症", "高尿酸血症", "慢性鼻炎", "急性咽炎", "支气管扩张症", "慢性阻塞性肺疾病", "胸腔积液", "气胸", "阻塞性睡眠呼吸暂停综合征", "急性心肌梗死", "主动脉夹层", "肥厚型心肌病", "肾结石", "附件炎", "慢性萎缩性胃炎", "急性阑尾炎", "急性胆囊炎", "溃疡性结肠炎", "直肠癌", "结肠癌", "急性上呼吸道感染", "慢性支气管炎", "围绝经期综合征", "垂体瘤", "急性喉炎", "急性支气管炎", "急性扁桃体炎", "呼吸衰竭", "室上性心动过速", "多发性骨髓瘤", "膀胱结石", "骨质疏松症", "骨关节炎", "强直性脊柱炎", "偏头痛", "蛛网膜下腔出血", "颈椎病", "腰椎间盘突出症", "肩周炎", "重症肌无力"};
//        String[] diseaseArray ={"鼻咽癌","类风湿性关节炎","急性上呼吸道感染","肾病综合征","输尿管结石","甲状腺功能减退","湿疹","帕金森病","胆囊结石"};
//        String[] diseaseArray ={"骨质疏松症","青光眼","贫血","围绝经期综合征","室上性心动过速","骨关节炎"};
        diseaseList = new ArrayList<String>(Arrays.asList(diseaseArray));
        //第一步把每个病的第一层表现和近义词写入图谱
        for (String diseaseName:diseaseList) {
            System.out.println(diseaseName);
            nodeRelationUtil.AddNode(driver232,0,"Disease",diseaseName.replace("\"",""));//添加疾病
            query="match p=(d:Disease)-[r1:表现]->(k)<-[r2:近义词]-(h) where d.name='"+diseaseName+"'\n" +
                    "return k.name as name,labels(k)[0] as type,h.name as gg,labels(h)[0] as tt";
            StatementResult result1 = session.run(query);
            while (result1.hasNext()){
                Record next = result1.next();
                String name = next.get("name").toString().replace("\"", "");
                String type = next.get("type").toString().replace("\"", "");
                String gg = next.get("gg").toString().replace("\"", "");
                String tt = next.get("tt").toString().replace("\"", "");
                nodeRelationUtil.AddNode(driver232,0,tt,gg);//近义词
                nodeRelationUtil.AddNode(driver232,0,type,name);//表现
                nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",type,name);
                nodeRelationUtil.addRelation(driver232,0,tt,gg,"近义词",type,name);
            }

                query="match p=(d:Disease)-[r1:表现]->(k) where d.name='"+diseaseName+"'\n" +
                        "return k.name as name,labels(k)[0] as type";
                StatementResult run = session.run(query);
                while (run.hasNext()){
                    Record next = run.next();
                    String name = next.get("name").toString().replace("\"", "");
                    String type = next.get("type").toString().replace("\"", "");
                    nodeRelationUtil.AddNode(driver232,0,type,name);
                    nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",type,name);
                }
                query="match p=(d:Disease)-[r1:表现]->(k)<-[r2:结果]-(h) where d.name='"+diseaseName+"'\n" +
                        "return k.name as name,labels(k)[0] as type,h.name as gg,labels(h)[0] as tt";
                StatementResult run1 = session.run(query);
                while (run1.hasNext()){
                    Record next = run1.next();
                    String name = next.get("name").toString().replace("\"", "");
                    String type = next.get("type").toString().replace("\"", "");
                    String gg = next.get("gg").toString().replace("\"", "");
                    String tt = next.get("tt").toString().replace("\"", "");
                    nodeRelationUtil.AddNode(driver232,0,tt,gg);//项目
                    nodeRelationUtil.AddNode(driver232,0,type,name);//结果
                    nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"推荐",tt,gg);
                    nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",type,name);
                    nodeRelationUtil.addRelation(driver232,0,tt,gg,"结果",type,name);
                }

            query="match (n)-[r:确诊]->(m{name:'"+diseaseName+"'})\n" +
                    "with distinct n,r\n" +
                    "return n.name as name,labels(n)[0] as label, n.path as path,n.relation as relationType";
            result = session.run(query);
            while (result.hasNext()){
                Record record = result.next();
                String nodeName = record.get("name").toString().replace("\"","");
                String label = record.get("label").toString().replace("\"","");
                String path = record.get("path").toString().replace("\"","");
                String relationType = record.get("relationType").toString().replace("\"","");
                //化验结果就可以直接确诊
                if("LIS_Result".equals(label) && path == "NULL" && relationType == "NULL"){
                    nodeRelationUtil.AddNode(driver232,0,"LIS_Result",nodeName);
                    nodeRelationUtil.addRelation(driver232,0,"LIS_Result",nodeName,"确诊","Disease",diseaseName.replace("\"",""));
                }else if("Condition".equals(label)){ //有诊断依据才能够确诊
                    //添加确诊依据和确诊依据和疾病的关系
                    nodeRelationUtil.AddNode(driver232,0,"Condition",nodeName,Integer.parseInt(path),relationType);
                    nodeRelationUtil.addRelation(driver232,0,"Condition",nodeName,"确诊","Disease",diseaseName.replace("\"",""));
                    //查询推出确诊依据的诊断依据
                    query="match(v)-[r1:诊断依据]->(c:Condition)-[r2:确诊]->(d:Disease)\n" +
                            "where c.name='"+nodeName+"' and d.name='"+diseaseName+"'\n" +
                            "return c.name as quezhen, v.name as n,labels(v)[0] as l,v.path as path,v.relation as relation";
                    StatementResult statementResult = session.run(query);
                    while (statementResult.hasNext()){
                       Record next = statementResult.next();
                       String n = next.get("n").toString().replace("\"","");
                       String l = next.get("l").toString().replace("\"","");
                       String quezhen= next.get("quezhen").toString().replace("\"","");
                       String patha = next.get("path").toString().replace("\"","");
                       String relationa = next.get("relation").toString().replace("\"","");
                       if("PACS_Result".equals(l) || "LIS_Result".equals(l)){
                           nodeRelationUtil.AddNode(driver232,0,l,n);
                           nodeRelationUtil.addRelation(driver232,0,l,n,"诊断依据","Condition",nodeName);
                           nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",l,n);
                           query="match(d:Disease)-[r1:推荐]->(k)-[r2:结果]->(f)\n" +
                                   "where d.name='"+diseaseName+"' and f.name='"+n+"'\n" +
                                   "return k.name as name,labels(k)[0] as jj,f.name as kk,labels(f)[0] as ll";
                           StatementResult dd = session.run(query);
                           while (dd.hasNext()){
                               Record next1 = dd.next();
                               String name = next1.get("name").toString().replace("\"","");
                               String jj = next1.get("jj").toString().replace("\"","");
                               String kk = next1.get("kk").toString().replace("\"","");
                               String ll = next1.get("ll").toString().replace("\"","");
                               if("PACS".equals(jj)){
                                   nodeRelationUtil.AddNode(driver232,0,"PACS",name);
                                   nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"推荐","PACS",name);
                                   nodeRelationUtil.addRelation(driver232,0,"PACS",name,"结果",ll,kk);
                               }else if ("LIS".equals(jj)){
                                   nodeRelationUtil.AddNode(driver232,0,"LIS",name);
                                   nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"推荐","LIS",name);
                                   nodeRelationUtil.addRelation(driver232,0,"LIS",name,"结果",ll,kk);
                               }

                           }
                       }else if("Condition".equals(l)){
                           nodeRelationUtil.AddNode(driver232,0,"Condition",n,Integer.parseInt(patha),relationa);
                           nodeRelationUtil.addRelation(driver232,0,"Condition",n,"诊断依据","Condition",quezhen);
                           query = "match(f)-[r1:诊断依据]->(c:Condition) where c.name='"+n+"' return f.name as name,labels(f)[0] as lab";
                           StatementResult fhsdjfh = session.run(query);
                           while (fhsdjfh.hasNext()){
                               Record nextRecord = fhsdjfh.next();
                               String name = nextRecord.get("name").toString().replaceAll("\"","");
                               String lab = nextRecord.get("lab").toString().replaceAll("\"", "");
                               nodeRelationUtil.AddNode(driver232,0,lab,name);
                               nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",lab,name);
                               nodeRelationUtil.addRelation(driver232,0,lab,name,"诊断依据","Condition",n);

                           }

                       }else {
                           nodeRelationUtil.AddNode(driver232,0,l,n);
                           nodeRelationUtil.addRelation(driver232,0,l,n,"诊断依据",label,nodeName);
                           nodeRelationUtil.addRelation(driver232,0,"Disease",diseaseName.replace("\"",""),"表现",l,n);

                       }
                   }
                }
            }
        }
    }

}
