|
@@ -8,6 +8,7 @@ from flask import current_app
|
|
|
from app.services.neo4j_driver import Neo4jDriver
|
|
|
import json
|
|
|
import logging
|
|
|
+from datetime import datetime
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
@@ -16,6 +17,9 @@ class MyEncoder(json.JSONEncoder):
|
|
|
def default(self, obj):
|
|
|
if isinstance(obj, (int, float, str, bool, list, dict, tuple, type(None))):
|
|
|
return super(MyEncoder, self).default(obj)
|
|
|
+ # 处理DateTime对象
|
|
|
+ if hasattr(obj, 'isoformat'):
|
|
|
+ return obj.isoformat()
|
|
|
return str(obj)
|
|
|
|
|
|
class GraphOperations:
|
|
@@ -229,6 +233,21 @@ def execute_cypher_query(cypher, params=None):
|
|
|
if params is None:
|
|
|
params = {}
|
|
|
|
|
|
+ def convert_value(value):
|
|
|
+ """转换Neo4j返回的值为JSON可序列化的格式"""
|
|
|
+ # 处理DateTime对象
|
|
|
+ if hasattr(value, 'isoformat'):
|
|
|
+ return value.isoformat()
|
|
|
+ # 处理Date对象
|
|
|
+ elif hasattr(value, 'year') and hasattr(value, 'month') and hasattr(value, 'day'):
|
|
|
+ return str(value)
|
|
|
+ # 处理Time对象
|
|
|
+ elif hasattr(value, 'hour') and hasattr(value, 'minute') and hasattr(value, 'second'):
|
|
|
+ return str(value)
|
|
|
+ # 处理其他对象
|
|
|
+ else:
|
|
|
+ return value
|
|
|
+
|
|
|
try:
|
|
|
with connect_graph().session() as session:
|
|
|
result = session.run(cypher, **params)
|
|
@@ -240,13 +259,17 @@ def execute_cypher_query(cypher, params=None):
|
|
|
for key, value in record.items():
|
|
|
# 节点处理
|
|
|
if hasattr(value, 'id') and hasattr(value, 'labels') and hasattr(value, 'items'):
|
|
|
- node_dict = dict(value)
|
|
|
+ node_dict = {}
|
|
|
+ for prop_key, prop_value in dict(value).items():
|
|
|
+ node_dict[prop_key] = convert_value(prop_value)
|
|
|
node_dict['_id'] = value.id
|
|
|
node_dict['_labels'] = list(value.labels)
|
|
|
record_dict[key] = node_dict
|
|
|
# 关系处理
|
|
|
elif hasattr(value, 'id') and hasattr(value, 'type') and hasattr(value, 'start_node'):
|
|
|
- rel_dict = dict(value)
|
|
|
+ rel_dict = {}
|
|
|
+ for prop_key, prop_value in dict(value).items():
|
|
|
+ rel_dict[prop_key] = convert_value(prop_value)
|
|
|
rel_dict['_id'] = value.id
|
|
|
rel_dict['_type'] = value.type
|
|
|
rel_dict['_start_node_id'] = value.start_node.id
|
|
@@ -255,13 +278,25 @@ def execute_cypher_query(cypher, params=None):
|
|
|
# 路径处理
|
|
|
elif hasattr(value, 'start_node') and hasattr(value, 'end_node') and hasattr(value, 'nodes'):
|
|
|
path_dict = {
|
|
|
- 'nodes': [dict(node) for node in value.nodes],
|
|
|
- 'relationships': [dict(rel) for rel in value.relationships]
|
|
|
+ 'nodes': [],
|
|
|
+ 'relationships': []
|
|
|
}
|
|
|
+ # 处理路径中的节点
|
|
|
+ for node in value.nodes:
|
|
|
+ node_dict = {}
|
|
|
+ for prop_key, prop_value in dict(node).items():
|
|
|
+ node_dict[prop_key] = convert_value(prop_value)
|
|
|
+ path_dict['nodes'].append(node_dict)
|
|
|
+ # 处理路径中的关系
|
|
|
+ for rel in value.relationships:
|
|
|
+ rel_dict = {}
|
|
|
+ for prop_key, prop_value in dict(rel).items():
|
|
|
+ rel_dict[prop_key] = convert_value(prop_value)
|
|
|
+ path_dict['relationships'].append(rel_dict)
|
|
|
record_dict[key] = path_dict
|
|
|
# 其他类型直接转换
|
|
|
else:
|
|
|
- record_dict[key] = value
|
|
|
+ record_dict[key] = convert_value(value)
|
|
|
data.append(record_dict)
|
|
|
|
|
|
return data
|