#!/usr/bin/env python
# encoding: utf-8
'''
@author: gigi Gao
@contact: gaoqi@youotech.com
@file: eeeee.py
@time: 2021/4/23 15:07
@license: (C) Copyright 2021-2030, 优尔高科.
@desc:
'''
import json
from py2neo import Graph
from flask import Flask

app = Flask(__name__)
graph = Graph('http://localhost:7474', username='neo4j', password='youotech@123')


@app.route("/api/<field>/<scene>")
def query(field, scene):
# data = graph.run(
# cypher="match (a:{0})-[r]->(b:{1}) where a.field='{2}'and a.scene='{3}' return r".format('gc', 'gc', field, scene)).data()
data = graph.run(
cypher="match (a:{0})-[r]->(b:{1}) where a.field='{2}'and a.scene='{3}' return r".format('gc', 'gc', '领域TODO',
'场景TODO')).data()
node_ids = []
new_nodes = []
new_links = []
for a in data:
for tk, tv in a.items():
nodes = tv.nodes
relations = tv.relationships
for n in nodes:
if n.identity in node_ids:
continue
obj = {}
obj_properties = {}
obj["id"] = n.identity
obj["labels"] = ["User"]
for k, v in n.items():
obj_properties[k] = v
obj["properties"] = obj_properties
node_ids.append(n.identity)
new_nodes.append(obj)
for r in relations:
if r.identity in node_ids:
continue
li = {}
li["id"] = r.identity
if r.types() is not None:
li["label"] = []
for la in r.types():
li["label"].append(la)
li["type"] = "HAS_PHONE_NUMBER"
li["startNode"] = r.start_node.identity
li["endNode"] = r.end_node.identity
li["properties"] = {"from": 1473581532586}
for k, v in r.items():
li[k] = v
node_ids.append(r.identity)
new_links.append(li)

result = {"results": [{"columns": {"user", "entity"}, "data": {"graph": {
"nodes": new_nodes,
"relationships": new_links
}}}]}
return "{0}".format(result)


if __name__ == '__main__':
app.run(
host='0.0.0.0',
debug=True
)