Browse Source

数据模型的增删改查功能前后端联调,修改后端代码的问题。数据格式问题。

maxiaolong 1 month ago
parent
commit
1977f8cf96

+ 9 - 0
app/core/data_metric/metric_interface.py

@@ -220,11 +220,17 @@ def handle_data_metric(metric_name, result_list, receiver):
         child = get_node_by_id_no_label(child_id)
         child = get_node_by_id_no_label(child_id)
         # 建立关系:当前节点的childrenId指向,以及关系child
         # 建立关系:当前节点的childrenId指向,以及关系child
         if child and not relationship_exists(data_metric_node, 'child', child):
         if child and not relationship_exists(data_metric_node, 'child', child):
+            # 获取节点ID
+            dm_id = data_metric_node.id if hasattr(data_metric_node, 'id') else data_metric_node
+            child_id = child.id if hasattr(child, 'id') else child
             connect_graph.create(Relationship(data_metric_node, 'child', child))
             connect_graph.create(Relationship(data_metric_node, 'child', child))
 
 
     if receiver.get('tag'):
     if receiver.get('tag'):
         tag = get_node_by_id('data_label', receiver['tag'])
         tag = get_node_by_id('data_label', receiver['tag'])
         if tag and not relationship_exists(data_metric_node, 'label', tag):
         if tag and not relationship_exists(data_metric_node, 'label', tag):
+            # 获取节点ID
+            dm_id = data_metric_node.id if hasattr(data_metric_node, 'id') else data_metric_node
+            tag_id = tag.id if hasattr(tag, 'id') else tag
             connect_graph.create(Relationship(data_metric_node, 'label', tag))
             connect_graph.create(Relationship(data_metric_node, 'label', tag))
 
 
     return data_metric_node.id, id_list
     return data_metric_node.id, id_list
@@ -581,6 +587,9 @@ def data_metric_edit(data):
         child = get_node_by_id_no_label(child_id)
         child = get_node_by_id_no_label(child_id)
         # 建立关系:当前节点的childrenId指向,以及关系child
         # 建立关系:当前节点的childrenId指向,以及关系child
         if child and not relationship_exists(node_a, 'child', child):
         if child and not relationship_exists(node_a, 'child', child):
+            # 获取节点ID
+            node_a_id = node_a.id if hasattr(node_a, 'id') else node_a
+            child_id = child.id if hasattr(child, 'id') else child
             connection = Relationship(node_a, 'child', child)
             connection = Relationship(node_a, 'child', child)
             connect_graph.create(connection)
             connect_graph.create(connection)
 
 

+ 105 - 47
app/core/data_model/model.py

@@ -131,37 +131,15 @@ def handle_data_model(data_model, result_list, result, receiver):
         receiver.update(add_attribute)
         receiver.update(add_attribute)
         data_model_node = get_node('data_model', name=data_model) or create_or_get_node('data_model', **receiver)
         data_model_node = get_node('data_model', name=data_model) or create_or_get_node('data_model', **receiver)
 
 
-        # 安全地处理子节点关系
-        child_list = receiver.get('childrenId', [])
-        for child_id in child_list:
-            child_node = get_node_by_id_no_label(child_id)
-            if child_node and not relationship_exists(data_model_node, 'child', child_node):
-                with connect_graph().session() as session:
-                    session.execute_write(
-                        lambda tx: tx.run(
-                            "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:child]->(b)",
-                            a_id=data_model_node.id, b_id=child_node.id
-                        )
-                    )
-
-        # 根据传入参数id,和数据标签建立关系
-        if receiver.get('tag'):
-            tag = get_node_by_id('data_label', receiver['tag'])
-            if tag and not relationship_exists(data_model_node, 'label', tag):
-                with connect_graph().session() as session:
-                    session.execute_write(
-                        lambda tx: tx.run(
-                            "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:label]->(b)",
-                            a_id=data_model_node.id, b_id=tag.id
-                        )
-                    )
-
-        # 获取节点ID
-        node_id = None
+        logger.info(f"通过查询或创建节点获得节点ID111,data_model_node: {data_model_node}")
+        # 获取节点ID,确保我们能安全地访问节点ID
+        node_id = data_model_node
         if hasattr(data_model_node, 'id'):
         if hasattr(data_model_node, 'id'):
+            logger.info(f"通过节点ID获取节点ID222,data_model_node: {data_model_node}")
             node_id = data_model_node.id
             node_id = data_model_node.id
         else:
         else:
-            # 如果节点没有identity属性,尝试通过查询获取
+            logger.info(f"通过查询节点名称获取节点ID333,data_model_node: {data_model_node}")
+            # 如果节点没有id属性,尝试通过查询获取
             query = """
             query = """
             MATCH (n:data_model {name: $name})
             MATCH (n:data_model {name: $name})
             RETURN id(n) as node_id
             RETURN id(n) as node_id
@@ -169,8 +147,59 @@ def handle_data_model(data_model, result_list, result, receiver):
             with connect_graph().session() as session:
             with connect_graph().session() as session:
                 result = session.run(query, name=data_model)
                 result = session.run(query, name=data_model)
                 record = result.single()
                 record = result.single()
+                logger.info(f"通过查询节点名称获取节点ID444,record: {record}")
                 if record and "node_id" in record:
                 if record and "node_id" in record:
+                    logger.info(f"通过查询节点名称获取节点ID555,record: {record}")
                     node_id = record["node_id"]
                     node_id = record["node_id"]
+        
+        # 安全地处理子节点关系
+        child_list = receiver.get('childrenId', [])
+        for child_id in child_list:
+            child_node = get_node_by_id_no_label(child_id)
+            if child_node:
+                # 直接使用Cypher查询检查关系是否存在
+                with connect_graph().session() as session:
+                    rel_query = """
+                    MATCH (a)-[r:child]->(b)
+                    WHERE id(a) = $start_id AND id(b) = $end_id
+                    RETURN count(r) > 0 as exists
+                    """
+                    rel_result = session.run(rel_query, 
+                                            start_id=int(node_id), 
+                                            end_id=int(child_node.id)).single()
+                    
+                    # 如果关系不存在,则创建关系
+                    if not (rel_result and rel_result["exists"]):
+                        session.execute_write(
+                            lambda tx: tx.run(
+                                "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:child]->(b)",
+                                a_id=int(node_id), b_id=int(child_node.id)
+                            )
+                        )
+
+        # 根据传入参数id,和数据标签建立关系
+        if receiver.get('tag'):
+            tag = get_node_by_id('data_label', receiver['tag'])
+            if tag:
+                # 直接使用Cypher查询检查关系是否存在
+                with connect_graph().session() as session:
+                    rel_query = """
+                    MATCH (a)-[r:label]->(b)
+                    WHERE id(a) = $start_id AND id(b) = $end_id
+                    RETURN count(r) > 0 as exists
+                    """
+                    rel_result = session.run(rel_query, 
+                                            start_id=int(node_id), 
+                                            end_id=int(tag.id)).single()
+                    
+                    # 如果关系不存在,则创建关系
+                    if not (rel_result and rel_result["exists"]):
+                        session.execute_write(
+                            lambda tx: tx.run(
+                                "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:label]->(b)",
+                                a_id=int(node_id), b_id=int(tag.id)
+                            )
+                        )
 
 
         return node_id, data_model_node
         return node_id, data_model_node
     except Exception as e:
     except Exception as e:
@@ -204,7 +233,7 @@ def resource_handle_meta_data_model(id_lists, data_model_node_id):
         if meta_ids:
         if meta_ids:
             logger.info("开始创建数据模型与元数据的关系")
             logger.info("开始创建数据模型与元数据的关系")
             query = """
             query = """
-            MATCH (source:data_model), (target:meta_node)
+            MATCH (source:data_model), (target:meta_data)
             WHERE id(source)=$source_id AND id(target) IN $target_ids
             WHERE id(source)=$source_id AND id(target) IN $target_ids
             MERGE (source)-[:INCLUDE]->(target)
             MERGE (source)-[:INCLUDE]->(target)
             RETURN count(*) as count
             RETURN count(*) as count
@@ -356,15 +385,29 @@ def handle_no_meta_data_model(id_lists, receiver, data_model_node):
                 # 创建meta_node节点
                 # 创建meta_node节点
                 meta_node = create_or_get_node('meta_node', **meta_params)
                 meta_node = create_or_get_node('meta_node', **meta_params)
                 
                 
-                # 创建与data_model的关系
-                if meta_node and not relationship_exists(data_model_node, 'component', meta_node):
+                # 获取数据模型节点ID
+                dm_id = data_model_node_id if data_model_node_id is not None else data_model_node
+                
+                if meta_node:
+                    # 直接使用Cypher查询检查关系是否存在
                     with connect_graph().session() as session:
                     with connect_graph().session() as session:
-                        session.execute_write(
-                            lambda tx: tx.run(
-                                "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:component]->(b)",
-                                a_id=data_model_node.id, b_id=meta_node.id
+                        rel_query = """
+                        MATCH (a)-[r:component]->(b)
+                        WHERE id(a) = $start_id AND id(b) = $end_id
+                        RETURN count(r) > 0 as exists
+                        """
+                        rel_result = session.run(rel_query, 
+                                               start_id=int(dm_id), 
+                                               end_id=int(meta_node)).single()
+                        
+                        # 如果关系不存在,则创建关系
+                        if not (rel_result and rel_result["exists"]):
+                            session.execute_write(
+                                lambda tx: tx.run(
+                                    "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:component]->(b)",
+                                    a_id=int(dm_id), b_id=int(meta_node)
+                                )
                             )
                             )
-                        )
 
 
 
 
 # 数据模型-详情接口
 # 数据模型-详情接口
@@ -397,8 +440,8 @@ def handle_id_model(id):
     node_id = id
     node_id = id
     cql = """
     cql = """
         MATCH (n:data_model) WHERE id(n) = $nodeId
         MATCH (n:data_model) WHERE id(n) = $nodeId
-        OPTIONAL MATCH (n)-[:connection]->(meta:meta_node)
-        OPTIONAL MATCH (n)<-[:belongs_to]-(resource:data_resource)
+        OPTIONAL MATCH (n)-[:INCLUDE]->(meta:meta_data)  
+        OPTIONAL MATCH (n)-[:DERIVES_FROM]->(resource:data_resource)
         OPTIONAL MATCH (n)-[:label]->(tag:data_label)
         OPTIONAL MATCH (n)-[:label]->(tag:data_label)
         OPTIONAL MATCH (uses:model_use)-[:use]->(n)
         OPTIONAL MATCH (uses:model_use)-[:use]->(n)
         OPTIONAL MATCH (n)-[:has_component]->(component)
         OPTIONAL MATCH (n)-[:has_component]->(component)
@@ -415,8 +458,7 @@ def handle_id_model(id):
             name: n.name,
             name: n.name,
             en_name: n.en_name,
             en_name: n.en_name,
             time: n.time,
             time: n.time,
-            description: n.description,
-            describe: n.description,  // 使用description作为describe字段
+            describe: n.describe,  
             category: n.category,
             category: n.category,
             level: n.level,
             level: n.level,
             tag: CASE WHEN size(tags) > 0 AND tags[0] IS NOT NULL THEN {id: id(tags[0]), name: tags[0].name} ELSE null END,
             tag: CASE WHEN size(tags) > 0 AND tags[0] IS NOT NULL THEN {id: id(tags[0]), name: tags[0].name} ELSE null END,
@@ -462,7 +504,7 @@ def handle_id_model(id):
             resource_selected = record["resource_selected"]
             resource_selected = record["resource_selected"]
             
             
             # 确保所有必需字段都有默认值,避免空值
             # 确保所有必需字段都有默认值,避免空值
-            required_fields = ['tag', 'description', 'leader', 'origin', 'blood_resource', 
+            required_fields = ['tag', 'leader', 'origin', 'blood_resource', 
                               'frequency', 'describe', 'organization', 'name', 'en_name', 
                               'frequency', 'describe', 'organization', 'name', 'en_name', 
                               'data_sensitivity', 'time', 'category', 'status', 'childrenId']
                               'data_sensitivity', 'time', 'category', 'status', 'childrenId']
             
             
@@ -1035,13 +1077,29 @@ def data_model_edit(receiver):
         tag_node = get_node_by_id('data_label', tag)
         tag_node = get_node_by_id('data_label', tag)
         if tag_node:
         if tag_node:
             model_node = get_node_by_id_no_label(id)
             model_node = get_node_by_id_no_label(id)
-            if model_node and not relationship_exists(model_node, 'label', tag_node):
+            if model_node:
+                # 获取节点ID
+                model_id = model_node.id if hasattr(model_node, 'id') else model_node
+                tag_id = tag_node.id if hasattr(tag_node, 'id') else tag_node
+                
+                # 直接使用Cypher查询检查关系是否存在
                 with connect_graph().session() as session:
                 with connect_graph().session() as session:
-                    session.execute_write(
-                        lambda tx: tx.run(
-                            "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:label]->(b)",
-                            a_id=model_node.id, b_id=tag_node.id
+                    rel_query = """
+                    MATCH (a)-[r:label]->(b)
+                    WHERE id(a) = $start_id AND id(b) = $end_id
+                    RETURN count(r) > 0 as exists
+                    """
+                    rel_result = session.run(rel_query, 
+                                          start_id=int(model_id), 
+                                          end_id=int(tag_id)).single()
+                    
+                    # 如果关系不存在,则创建关系
+                    if not (rel_result and rel_result["exists"]):
+                        session.execute_write(
+                            lambda tx: tx.run(
+                                "MATCH (a), (b) WHERE id(a) = $a_id AND id(b) = $b_id CREATE (a)-[:label]->(b)",
+                                a_id=int(model_id), b_id=int(tag_id)
+                            )
                         )
                         )
-                    )
     
     
     return {"message": "数据模型更新成功"} 
     return {"message": "数据模型更新成功"} 

+ 1 - 1
app/core/data_resource/resource.py

@@ -24,7 +24,7 @@ def get_node_by_id(label, id):
                 logger.error(f"节点ID不是有效的整数: {id}")
                 logger.error(f"节点ID不是有效的整数: {id}")
                 return None
                 return None
                 
                 
-            cypher = f"MATCH (n:{label}) WHERE id(n) = $id RETURN n"
+            cypher = f"MATCH (n:{label}) WHERE id(n) = $id RETURN n "
             result = session.run(cypher, id=id_int)
             result = session.run(cypher, id=id_int)
             record = result.single()
             record = result.single()
             return record["n"] if record else None
             return record["n"] if record else None

+ 10 - 6
app/core/graph/graph_operations.py

@@ -305,13 +305,13 @@ def get_node(label, **properties):
             query = f"""
             query = f"""
             MATCH (n:{label})
             MATCH (n:{label})
             WHERE {where_clause}
             WHERE {where_clause}
-            RETURN n
+            RETURN id(n) as node_id
             LIMIT 1
             LIMIT 1
             """
             """
             
             
             # 执行查询
             # 执行查询
             result = session.run(query, **params).single()
             result = session.run(query, **params).single()
-            return result["n"] if result else None
+            return result["node_id"] if result else None
             
             
     except Exception as e:
     except Exception as e:
         logger.error(f"Error in get_node: {str(e)}")
         logger.error(f"Error in get_node: {str(e)}")
@@ -322,9 +322,9 @@ def relationship_exists(start_node, rel_type, end_node, **properties):
     检查两个节点之间是否存在指定类型和属性的关系
     检查两个节点之间是否存在指定类型和属性的关系
     
     
     Args:
     Args:
-        start_node: 起始节点
+        start_node: 起始节点或节点ID
         rel_type: 关系类型
         rel_type: 关系类型
-        end_node: 结束节点
+        end_node: 结束节点或节点ID
         **properties: 关系的属性
         **properties: 关系的属性
         
         
     Returns:
     Returns:
@@ -332,6 +332,10 @@ def relationship_exists(start_node, rel_type, end_node, **properties):
     """
     """
     try:
     try:
         with connect_graph().session() as session:
         with connect_graph().session() as session:
+            # 确定节点ID
+            start_id = start_node.id if hasattr(start_node, 'id') else start_node
+            end_id = end_node.id if hasattr(end_node, 'id') else end_node
+            
             # 构建查询语句
             # 构建查询语句
             query = """
             query = """
             MATCH (a)-[r:%s]->(b)
             MATCH (a)-[r:%s]->(b)
@@ -349,8 +353,8 @@ def relationship_exists(start_node, rel_type, end_node, **properties):
             
             
             # 执行查询
             # 执行查询
             params = {
             params = {
-                'start_id': start_node.id,
-                'end_id': end_node.id,
+                'start_id': int(start_id),
+                'end_id': int(end_id),
                 **properties
                 **properties
             }
             }
             result = session.run(query, **params).single()
             result = session.run(query, **params).single()