package org.diagbot.graph;

import org.diagbot.graph.jdbc.Neo4jJdbc;
import org.neo4j.driver.v1.types.Path;
import org.neo4j.driver.v1.types.Relationship;

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

/**
 * @Auther: fyeman
 * @Date: 2018/9/28/028 09:55
 * @Description:
 */
public class GraphTest {
    public static void main(String[] args) {
//        save("小明", "8", "爸爸", "38", "父子");
//        save("小天", "3", "爸爸", "38", "父子");
//        save("琳琳", "1", "爸爸", "38", "父女");

//        findNode();

        findPath();
    }

    private static void save(String child, String c_age, String father, String f_age, String relation) {
        ModelTest model = new ModelTest(child, c_age, father, f_age, relation);
        Neo4jJdbc<ModelTest> jdbc = new Neo4jJdbc<>();
        try {
            jdbc.save(model, "Child", "Father");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void findNode() {
        String labelName = "Father";
        Map<String, Object> titleMap = new HashMap<>();
        titleMap.put("title", "爸爸");

        Neo4jJdbc jdbc = new Neo4jJdbc();
        List<Path> paths = jdbc.findNode(labelName, titleMap);
        for (Path path : paths) {
            System.out.println(".........图谱内部ID:" + path.start().id());
            for (Map.Entry<String, Object> entry :path.start().asMap().entrySet()) {
                System.out.println(".........图谱属性:" + entry.getKey() + " = " + entry.getValue());
            }
        }
    }

    private static void findPath() {
        String toLabelName = "Father";
        Map<String, Object> toTitleMap = new HashMap<>();
        toTitleMap.put("title", "爸爸");

        Neo4jJdbc jdbc = new Neo4jJdbc();
        List<Path> paths = jdbc.find("", toLabelName, "", null, toTitleMap);

        for (Path path : paths) {
            Iterator<Relationship> ships = path.relationships().iterator();
            while (ships.hasNext()) {
                //节点间关系可以是多个
                System.out.println(".........图谱关系:" + ships.next().type());
            }
            for (Map.Entry<String, Object> entry :path.start().asMap().entrySet()) {
                System.out.println(".........图谱属性:" + entry.getKey() + " = " + entry.getValue());
            }
        }
    }
}
