wcc 3 сар өмнө
parent
commit
031086d0ce
2 өөрчлөгдсөн 66 нэмэгдсэн , 21 устгасан
  1. 13 5
      graph/main.go
  2. 53 16
      graph/utils.go

+ 13 - 5
graph/main.go

@@ -124,6 +124,13 @@ type CheckResponse struct {
 	Msg  string   `json:"msg"`
 	Msg  string   `json:"msg"`
 }
 }
 
 
+type GraphResponse struct {
+	Code   int          `json:"code"`
+	Data   []string     `json:"data"`
+	Msg    string       `json:"msg"`
+	Echars *GraphResult `json:"echars"`
+}
+
 // SuspectInvestResponse 意思关系返回结果
 // SuspectInvestResponse 意思关系返回结果
 type SuspectInvestResponse struct {
 type SuspectInvestResponse struct {
 	Code int           `json:"code"`
 	Code int           `json:"code"`
@@ -220,9 +227,9 @@ func handHttp() {
 			return
 			return
 		}
 		}
 
 
-		has, results, err := client.CheckLegalRelationships(req.Names, req.Deep, req.Stype)
+		has, results, resb, err := client.CheckLegalRelationships(req.Names, req.Deep, req.Stype)
 		if err != nil {
 		if err != nil {
-			res := CheckResponse{
+			res := GraphResponse{
 				Code: -1,
 				Code: -1,
 				Data: results,
 				Data: results,
 				Msg:  "请求失败;" + err.Error(),
 				Msg:  "请求失败;" + err.Error(),
@@ -231,9 +238,10 @@ func handHttp() {
 			return
 			return
 		}
 		}
 
 
-		res := CheckResponse{
-			Code: 200,
-			Data: results,
+		res := GraphResponse{
+			Code:   200,
+			Data:   results,
+			Echars: resb,
 		}
 		}
 		if has {
 		if has {
 			res.Msg = "存在投资关系"
 			res.Msg = "存在投资关系"

+ 53 - 16
graph/utils.go

@@ -1060,17 +1060,34 @@ RETURN p LIMIT 1
 	return false, nil, nil
 	return false, nil, nil
 }
 }
 
 
-// CheckLegalRelationships 判断提供的企业名单,是否存在投资关系
-func (c *NebulaClient) CheckLegalRelationships(names []string, deep, stype int) (bool, []string, error) {
+type GraphNode struct {
+	Name string `json:"name"`
+}
+
+type GraphLink struct {
+	Source string `json:"source"`
+	Target string `json:"target"`
+	Type   string `json:"type"` // 可选字段,比如 "投资"
+}
+
+type GraphResult struct {
+	Nodes []GraphNode `json:"nodes"`
+	Links []GraphLink `json:"links"`
+}
+
+func (c *NebulaClient) CheckLegalRelationships(names []string, deep, stype int) (bool, []string, *GraphResult, error) {
 	if len(names) < 2 {
 	if len(names) < 2 {
-		return false, nil, fmt.Errorf("企业数量不足,至少需要两个")
+		return false, nil, nil, fmt.Errorf("企业数量不足,至少需要两个")
 	}
 	}
 
 
 	if deep == 0 {
 	if deep == 0 {
 		deep = 1
 		deep = 1
 	}
 	}
+
 	var rawPaths []string
 	var rawPaths []string
 	var rawNodeLists [][]string
 	var rawNodeLists [][]string
+	nodeSet := make(map[string]struct{})
+	graph := &GraphResult{}
 
 
 	for i := 0; i < len(names); i++ {
 	for i := 0; i < len(names); i++ {
 		start := names[i]
 		start := names[i]
@@ -1083,23 +1100,17 @@ func (c *NebulaClient) CheckLegalRelationships(names []string, deep, stype int)
 		}
 		}
 		targetList := strings.Join(targets, ", ")
 		targetList := strings.Join(targets, ", ")
 
 
-		query := fmt.Sprintf(`
-USE %s;
-MATCH p=(a:Legal{name:"%s"})-[*1..%d]-(b:Legal)
-WHERE b.Legal.name IN [%s]
-RETURN p LIMIT 1
-`, Table_Space, start, deep, targetList)
+		query := fmt.Sprintf(`USE %s; MATCH p=(a:Legal{name:"%s"})-[*1..%d]-(b:Legal) WHERE b.Legal.name IN [%s] RETURN p LIMIT 1`, Table_Space, start, deep, targetList)
 
 
 		resp, err := c.ExecuteWithReconnect(query)
 		resp, err := c.ExecuteWithReconnect(query)
 		if err != nil {
 		if err != nil {
-			return false, nil, fmt.Errorf("查询失败: %w", err)
+			return false, nil, nil, fmt.Errorf("查询失败: %w", err)
 		}
 		}
-
 		if resp == nil {
 		if resp == nil {
-			return false, nil, fmt.Errorf("查询失败,response is nil")
+			return false, nil, nil, fmt.Errorf("查询失败,response is nil")
 		}
 		}
 		if !resp.IsSucceed() {
 		if !resp.IsSucceed() {
-			return false, nil, fmt.Errorf("执行失败: %s", resp.GetErrorMsg())
+			return false, nil, nil, fmt.Errorf("执行失败: %s", resp.GetErrorMsg())
 		}
 		}
 
 
 		if resp.GetRowSize() > 0 {
 		if resp.GetRowSize() > 0 {
@@ -1127,6 +1138,14 @@ RETURN p LIMIT 1
 				builder.WriteString(curName)
 				builder.WriteString(curName)
 				nodeNames = append(nodeNames, curName)
 				nodeNames = append(nodeNames, curName)
 
 
+				// 图谱节点添加
+				if curName != "" {
+					if _, ok := nodeSet[curName]; !ok {
+						nodeSet[curName] = struct{}{}
+						graph.Nodes = append(graph.Nodes, GraphNode{Name: curName})
+					}
+				}
+
 				// 步长处理
 				// 步长处理
 				for _, step := range path.Steps {
 				for _, step := range path.Steps {
 					dstName := ""
 					dstName := ""
@@ -1149,13 +1168,31 @@ RETURN p LIMIT 1
 					}
 					}
 					builder.WriteString(dstName)
 					builder.WriteString(dstName)
 					nodeNames = append(nodeNames, dstName)
 					nodeNames = append(nodeNames, dstName)
+
+					// 图谱节点添加
+					if dstName != "" {
+						if _, ok := nodeSet[dstName]; !ok {
+							nodeSet[dstName] = struct{}{}
+							graph.Nodes = append(graph.Nodes, GraphNode{Name: dstName})
+						}
+					}
+
+					// 图谱边添加
+					if curName != "" && dstName != "" {
+						graph.Links = append(graph.Links, GraphLink{
+							Source: curName,
+							Target: dstName,
+							Type:   "投资",
+						})
+					}
+					curName = dstName
 				}
 				}
 
 
 				rawPaths = append(rawPaths, builder.String())
 				rawPaths = append(rawPaths, builder.String())
 				rawNodeLists = append(rawNodeLists, nodeNames)
 				rawNodeLists = append(rawNodeLists, nodeNames)
 
 
 				if stype == 0 {
 				if stype == 0 {
-					return true, []string{builder.String()}, nil
+					return true, []string{builder.String()}, graph, nil
 				}
 				}
 			}
 			}
 		}
 		}
@@ -1190,7 +1227,7 @@ RETURN p LIMIT 1
 	}
 	}
 
 
 	if len(finalPaths) > 0 {
 	if len(finalPaths) > 0 {
-		return true, finalPaths, nil
+		return true, finalPaths, graph, nil
 	}
 	}
-	return false, nil, nil
+	return false, nil, nil, nil
 }
 }