wcc 3 月之前
父節點
當前提交
6bc79a88fd
共有 2 個文件被更改,包括 137 次插入2 次删除
  1. 16 2
      graph/graph_test.go
  2. 121 0
      graph/utils.go

+ 16 - 2
graph/graph_test.go

@@ -12,11 +12,25 @@ func TestCheckInvestRelation(t *testing.T) {
 	}
 	defer pool.Close()
 	defer session.Release()
-	names := []string{"北京剑鱼信息技术有限公司", "河南折扣牛哟有限公司", "南京三六五网络公司", ""}
+	names := []string{"北京剑鱼信息技术有限公司", "河南折扣牛哟有限公司", "南京三六五网络公司", "上海元藩投资有限公司"}
 	//res, err := CheckLegalRelationsGraph(session, names, 3)
-	has, res, err := CheckLegalRelationships(session, names, 3, 1)
+	has, res, err := CheckLegalRelationships2(session, names, 3, 1)
 	if err != nil {
 		log.Println(res, err, has)
 	}
 	log.Println(has, res)
 }
+
+func TestFetchLegalByVid(t *testing.T) {
+	session, pool, err := ConnectToNebula(HostList, UserName, PassWord)
+	if err != nil {
+		log.Fatalf("Failed to connect to Nebula Graph: %v", err)
+	}
+	defer pool.Close()
+	defer session.Release()
+
+	vid := "fb7c72258ec3f57bedd0657b0c3e90d2"
+
+	res, err := getLegalByVid(session, vid)
+	log.Println(res, err)
+}

+ 121 - 0
graph/utils.go

@@ -531,3 +531,124 @@ RETURN p LIMIT 1
 
 	return false, "企业之间无关联", nil
 }
+
+func CheckLegalRelationships2(session *nebula.Session, names []string, deep, stype int) (bool, []string, error) {
+	if len(names) < 2 {
+		return false, nil, fmt.Errorf("企业数量不足,至少需要两个")
+	}
+
+	var allPaths []string
+
+	for i := 0; i < len(names); i++ {
+		start := names[i]
+
+		// 构造剩下的企业列表作为目标
+		targets := []string{}
+		for j := 0; j < len(names); j++ {
+			if i != j {
+				targets = append(targets, fmt.Sprintf(`"%s"`, names[j]))
+			}
+		}
+		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)
+
+		resp, err := session.Execute(query)
+		if err != nil {
+			return false, nil, fmt.Errorf("查询失败: %w", err)
+		}
+		if !resp.IsSucceed() {
+			return false, nil, fmt.Errorf("查询执行失败: %s", resp.GetErrorMsg())
+		}
+
+		if resp.GetRowSize() > 0 {
+			// 解析路径
+			for _, row := range resp.GetRows() {
+				if len(row.Values) == 0 {
+					continue
+				}
+				val := row.Values[0]
+				if !val.IsSetPVal() {
+					continue
+				}
+
+				path := val.GetPVal()
+				var pathNames []string
+
+				// 起点
+				srcVertex := path.Src
+				if srcVertex != nil && srcVertex.Vid != nil && srcVertex.Vid.IsSetSVal() {
+					vid := string(srcVertex.Vid.GetSVal())
+					lea, err := getLegalByVid(session, vid)
+					if err != nil {
+						log.Println("getVidByName err", err, vid)
+					}
+					pathNames = append(pathNames, lea.Name)
+				}
+
+				// 步骤中目的点
+				for _, step := range path.Steps {
+					dstVertex := step.Dst
+					if dstVertex != nil && dstVertex.Vid != nil && dstVertex.Vid.IsSetSVal() {
+						vid := string(dstVertex.Vid.GetSVal())
+						lea, err := getLegalByVid(session, vid)
+						if err != nil {
+							log.Println("getVidByName err", err, vid)
+						}
+						pathNames = append(pathNames, lea.Name)
+					}
+				}
+
+				if len(pathNames) >= 2 {
+					allPaths = append(allPaths, strings.Join(pathNames, " -> "))
+					if stype == 0 {
+						return true, allPaths, nil
+					}
+				}
+			}
+		}
+	}
+
+	if len(allPaths) > 0 {
+		return true, allPaths, nil
+	}
+
+	return false, nil, nil
+}
+
+// getLegalByVid 根据vid 获取顶点
+func getLegalByVid(session *nebula.Session, vid string) (*Legal, error) {
+	query := fmt.Sprintf(`
+USE %s;
+FETCH PROP ON Legal "%s" YIELD Legal.name, Legal.code;`, Table_Space, vid)
+
+	resp, err := session.Execute(query)
+	if err != nil {
+		return nil, fmt.Errorf("failed to execute fetch query: %w", err)
+	}
+	if !resp.IsSucceed() {
+		return nil, fmt.Errorf("query failed: %s", resp.GetErrorMsg())
+	}
+
+	rows := resp.GetRows()
+	if len(rows) == 0 {
+		return nil, nil // 没查到
+	}
+
+	values := rows[0].Values
+
+	info := &Legal{}
+	if values[0].GetSVal() != nil {
+		info.Name = string(values[0].GetSVal())
+	}
+	if values[1].GetSVal() != nil {
+		info.Code = string(values[1].GetSVal())
+	}
+	return info, nil
+}