package org.diagbot.repository;

import org.diagbot.entity.node.base.BaseNode;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface BaseNodeRepository extends Neo4jRepository<BaseNode, Long> {
    // update Node by Name
    @Query("MATCH (n) WHERE ID(n) = {0} SET n.name = {2} RETURN n")
    BaseNode updateNodeByName(int id, String old_name, String new_name);

    // get All Node
    @Query("MATCH (n) RETURN n.name as name, LABELS(n) as label")
    List<Map<String, Object>> getAllNode();

//    Stream<BaseNode> findAllBy();
    //get high risk
    @Query("match(d:Disease) where d.name in {0} return d.name as name, d.high_risk as risk")
    List<Map<String, Object>> getHighRisk(List diagList);
    //get VITAL
    @Query("unwind {0} as row \n" +
            " match (d:Disease)-[r1:表现]->(m:Vital) where d.name=row return m.name as name")
    List<Map<String,Object>> getVitalList(List diagList);
    //get LIS PACS
    @Query("unwind {0} as row \n" +
            " match (d:Disease)-[r1:推荐]->(m) where d.name=row return  labels(m)[0] as label,m.name as name")
    List<Map<String,Object>> getLisPacsList(List diagList);

    // get disease
    @Query("with {0} as data unwind data as row \n" +
            "match (n)-[r:属于]->(m)-[r1:组合]->(k) \n" +
            "where n.name= row \n" +
            "return m.name as condition, count(distinct r)>=m.path as jundgement, labels(m)[0] as label,k.name as standName")
    List<Map<String,Object>> getCollectionWord(List inputFilds);
    @Query("match (n)-[r:诊断依据|:近义词]->(e) where n.name in {0} return n.name as fild,collect(distinct type(r)) as typeCollect")
    List<Map<String,Object>> getCollectJYWord(List inputFilds);
    @Query("match(l)-[r:近义词]->(h) where l.name={0} return h.name as js")
    List<Map<String,Object>> getJYWord(String filds);
    @Query("with {0} as data unwind data as row \n" +
            "match (l)-[r:诊断依据]->(m) \n" +
            "where l.name= row \n" +
            "with m,{1} as data unwind data as row \n" +
            "match (n)-[r:诊断依据]->(m) \n" +
            "where n.name= row \n" +
            "return m.name as condition, count(distinct r)>=m.path as jundgement, labels(m)[0] as label")
    List<Map<String,Object>> getCondition(List newList,List inputList);
    @Query("with {0} as data unwind data as row \n" +
            "match (n)-[r:确诊|:拟诊]->(m)\n" +
            "where n.name=row \n" +
            "with distinct m,r \n" +
            "return m.name as name, labels(m)[0] as label,type(r) as relationType")
    List<Map<String,Object>> getDisease(List fildList);
    //get treat
    @Query("match (d:Disease{name:{0}})-[r0:推荐]->(m:Drugs)-[:包含]->(n:Medicine),(d)-[r1:推荐]->(n) \n" +
            "where r0.p<6 \n" +
            "return m.name as drugs, r0.p as sort, n.name as medication, r1.rate as rate order by sort ,rate desc")
    List<Map<String,Object>> getTreatMedicaton(String dis);
    @Query("match (n:Disease{name:{0}})-[r0:推荐]->(m:Drugs)-[:包含]->(k:Drugs) return k.name as short,m.name as large")
    List<Map<String,Object>> getShortLarge(String dis);
    @Query("match (n:Disease{name:{0}})-[:推荐]->(d)<-[r0:慎用]-(m) \n" +
            "where m.name in {1} and labels(d)[0] in ['Drugs','Medicine'] return d.name as name,type(r0) as type \n" +
            "union \n" +
            "match (n:Disease{name:{0}})-[:推荐]->(d)<-[r0:忌用]-(m) where m.name in {1} and labels(d)[0] in ['Drugs','Medicine'] \n" +
            "return d.name as name,type(r0) as type")
    List<Map<String,Object>> getShenJiDrugsMedication(String dis,List filds);


    //新结构推送诊断
    //查找词所在序号
    @Query("match(h)-[r:属于|:诊断依据]->(c:Condition) \n" +
            "where h.name in {0}\n" +
            "with c.name as v,count(distinct r)>=c.path as hh\n" +
            "where hh =true\n" +
            "return v")
    Set<String> getFildInNumber(Set diagList);
    //查找符合的诊断依据
    @Query("match (l)-[r:诊断依据]->(m:Condition)\n "+
    "where l.name in {0} \n" +
    "with m,{1} as data\n" +
    "match (n)-[r:诊断依据]->(m:Condition)\n" +
    "where n.name in data\n" +
    "with m.name as condition, count(distinct r) as sd,m.path as jundgement\n" +
    "where sd>=jundgement return condition")
    List<String> getFildInCondition(List<String> startList,Set<String> fildInNumber);
    //查找疑诊
    @Query("match (n:Condition)-[r:确诊|:拟诊]->(m:Disease)\n" +
            "where n.name in {0}\n" +
            "with distinct m,r\n" +
            "return m.name as name, labels(m)[0] as label,type(r) as relationType;")
    List<Map<String,Object>> getFildDis(Set<String> startList);
    //查找鉴别诊断
    @Query("match(d:Disease)-[r:鉴别诊断]->(h) where d.name={0} return h.name as name")
    List<String> getDiffDis(String mainDis);
    //查找急诊
    @Query("match(d:Disease) where d.name in {0} and d.emergency=1 return d.name as emDis")
    List<String> getEmergency(Set disSet);

    //查找诊断对应的不良反应集合
    @Query("match(d:Disease)-[r:不良反应]->(u:UntowardEffect) where d.name in {0}\n" +
            "return d.name as dis,collect(u.name) as ueCollect")
    List<Map<String,Object>> getUntowardEffect(String[] webDiag);
    //通过页面内容推出不良反应集合
    @Query("match (n)-[r:确诊|:拟诊]->(m:UntowardEffect) where n.name in {0} return distinct m.name as name")
    Set<String> pushUntowardEffects(Set<String> ss);

    //指标推送
    //通过页面内容查找指标
    @Query("match (n)-[r:确诊|:拟诊]->(m:Indicators) where n.name in {0} return distinct m.name as name")
    Set<String> pushInd(Set<String> ss);
    //处理某个具体的指标(肾功能不全)
    @Query("match(d:Indicators{name:{0}})-[r2:内容]->(n:Content) where r2.p=1 \n" +
            "return r2.p as sort1 ,n.name as conten,null as sort2,null as item,null as controlType,null as state,null as value,null as uint,null as details\n" +
            "union\n" +
            "match(d:Indicators{name:{0}})-[r2:内容]->(n:Content)-[r3:项目]->(i:Item)\n" +
            "return r2.p as sort1 ,n.name as conten,r3.p as sort2,i.name as item,i.controlType as controlType,i.state as state,i.value as value,i.uint as uint,i.details as details order by sort1,sort2")
    List<Map<String,Object>> allSort(String ind);
    //指标item详细
    @Query("match(i:Item{name:{0}})-[r:详细]->(d:Detail)\n" +
            "return d.name as name,d.state as state,d.value as value order by r.p")
    List<Map<String,Object>> indItemDetail(String item);
    //指标content详细
    @Query("match(c:Content{name:{0}})-[r:结果]->(f:Result) return f.name as name,f.text as text order by r.p")
    List<Map<String,Object>> indcontent(String item);

    //推送管理评估,只有慢病才有
    //糖尿病管理评估
    @Query("match(d:Disease{name:{0}})-[r1:管理评估]-(m:Management)-[r2:疗效评估]->(e:Effect)-[r3:结果]-(u:Result)\n" +
            "return d.name as dis,e.name as eff,u.name as res order by r2.p,r3.p")
    List<Map<String,Object>> meAllQuery(String dis);
    //计算血脂情况
    @Query("match(e:Effect{name:{0}})-[r:指标]->(l:LIS)-[r1:计算结果]->(k:Result)\n" +
            "return l.name as name,r1.between as between ,k.name as res")
    List<Map<String,Object>> culXuezhi(String dis);

}
