package com.dgt.graphdemo.service;

import com.dgt.graphdemo.entity.GraphData;
import com.dgt.graphdemo.entity.MyNode;
import com.dgt.graphdemo.utils.Constants;
import com.dgt.graphdemo.utils.Neo4jUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lugela
 * @Title: GraphService
 * @ProjectName graphdemo
 * @Description: TODO
 * @date 2019/6/27 9:22
 */
@Service
public class GraphService {
   @Autowired
    private Neo4jUtil neo4jUtil;

   public GraphData deatilGraphRep(List<String> claimnos, List<String> weaverIdList){
        String claimnoAraays = StringUtils.join(claimnos,",");
       String nodeSql= String.format( "MATCH p=(c:Claimno)-[*1..3]->(m) WHERE c.name IN [%s] RETURN p",claimnoAraays);
       GraphData graphData = neo4jUtil.GetGraph(nodeSql);
       List<MyNode> nodes = graphData.getNodes();
       if (nodes!=null && !nodes.isEmpty()){
           nodes.forEach(node ->{
               String weaverId = node.getWeaverId();
               if (!weaverIdList.contains(weaverId)){
                   weaverIdList.add(weaverId);
               }
           });
       }
       graphData.setWeaverIdList(weaverIdList);
       //为了给前台不报错的凑数代码
       Map<String,String> map= new HashMap<>();
       map.put("value","0");
       map.put("Imagepath","sxlpclaimNum.png");
       map.put("type","A");
       map.put("cname","案件数量");
       List<Map<String,String>> list=new ArrayList<>();
       list.add(map);
       graphData.setGraphlist(list);
       return graphData;

   }


    /**
     *
     * @author lugela
     * @date 2019/7/2 13:26
     * @param
     * @return
     * 双击扩展图谱，深度为1层
     * filterType 规则过滤
     */
     public GraphData deatilGraphExtend(String filterType,String nodeType,String id,List<String> weaverIdList){
         //普通情况下
         String nodeName = Constants.NODETYPEMAP.get(nodeType);
         boolean flag =true;
         if ("H".equals(nodeType)){
             //说明当前查询的为医院节点进行扩展，扩展的时候分为规则过滤
             if ("same".equals(filterType)){
                 flag = false;
             }
         }
         String nodeSql =null;
         //为相似度过滤，单独处理
         if (!flag){
              nodeSql= String.format( "MATCH p=(c:%s)-[*1..1]-(m) WHERE c.id='%s' AND m.size is null RETURN p",nodeName,id);
         }

         if (flag){
              nodeSql= String.format( "MATCH p=(c:%s{id:'%s'})-[*1..1]-(m) RETURN p",nodeName,id);
         }


         //普通情况下
         GraphData graphData = neo4jUtil.GetGraph(nodeSql);
         List<MyNode> nodes = graphData.getNodes();
         if (nodes!=null && !nodes.isEmpty()){
             nodes.forEach(node ->{
                 String weaverId = node.getWeaverId();
                 if (!weaverIdList.contains(weaverId)){
                     weaverIdList.add(weaverId);
                 }
             });
         }
         graphData.setWeaverIdList(weaverIdList);
         //为了给前台不报错的凑数代码
         Map<String,String> map= new HashMap<>();
         map.put("value","0");
         map.put("Imagepath","sxlpclaimNum.png");
         map.put("type","A");
         map.put("cname","案件数量");
         List<Map<String,String>> list=new ArrayList<>();
         list.add(map);
         graphData.setGraphlist(list);
         return graphData;
     }

}
