'''
图谱相关连接配置
'''
from flask import Flask
from py2neo import Graph
from configs.nacos_config import configs
from py2neo import Node
app = Flask(__name__)
neo4j_url = configs['neo4j_url']
username = configs['neo4j_username']
password = configs['neo4j_password']
connect_graph = Graph(neo4j_url, auth=(username, password))


# 创建或获取节点
def create_or_get_node(label,**properties):
    node = connect_graph.nodes.match(label,**properties).first()
    if node is None:
        node = Node(label,**properties)
        connect_graph.create(node)
    return node

# 查询是否存在节点
def get_node(label,**properties):
    node = connect_graph.nodes.match(label,**properties).first()
    # 如果没有找到匹配的节点，node 将会是 None
    return node

# 查询是否存在关系
def relationship_exists(start_node, rel_type, end_node, **properties):
    matcher = connect_graph.match(nodes=[start_node, end_node], r_type=rel_type)
    # 如果需要匹配关系的属性
    if properties:
        matcher = matcher.where(**properties)
    result = matcher.first()
    return result is not None
