wcc 3 місяців тому
батько
коміт
d6bd05f185
4 змінених файлів з 22 додано та 15 видалено
  1. BIN
      graph/graph-http
  2. 1 1
      graph/graph_test.go
  3. 2 1
      graph/main.go
  4. 19 13
      graph/utils.go

BIN
graph/graph-http


+ 1 - 1
graph/graph_test.go

@@ -14,7 +14,7 @@ func TestCheckInvestRelation(t *testing.T) {
 	defer session.Release()
 	names := []string{"北京剑鱼信息技术有限公司", "河南折扣牛哟有限公司", "南京三六五网络公司", ""}
 	//res, err := CheckLegalRelationsGraph(session, names, 3)
-	has, res, err := CheckLegalRelationships(session, names, 3)
+	has, res, err := CheckLegalRelationships(session, names, 3, 1)
 	if err != nil {
 		log.Println(res, err, has)
 	}

+ 2 - 1
graph/main.go

@@ -95,6 +95,7 @@ func ConnectToNebula(hosts []nebula.HostAddress, username, password string) (*ne
 type CheckRequest struct {
 	Names []string `json:"names"`
 	Deep  int      `json:"deep"`
+	Stype int      `json:"stype"` //0.简易模式,匹配到直接返回;1.匹配完所有的数据
 }
 
 func main() {
@@ -124,7 +125,7 @@ func main() {
 			return
 		}
 
-		_, results, err := CheckLegalRelationships(session, req.Names, req.Deep)
+		_, results, err := CheckLegalRelationships(session, req.Names, req.Deep, req.Stype)
 		if err != nil {
 			c.JSON(http.StatusInternalServerError, gin.H{"error": "查询失败", "details": err.Error()})
 			return

+ 19 - 13
graph/utils.go

@@ -480,15 +480,15 @@ FETCH PROP ON Legal "%s" YIELD Legal.name;
 //2222222222222222//
 
 // CheckLegalRelationships 检查给定企业之间是否存在关系
-func CheckLegalRelationships(session *nebula.Session, names []string, deep int) (bool, string, error) {
+func CheckLegalRelationships(session *nebula.Session, names []string, deep, stype int) (bool, string, error) {
 	if len(names) < 2 {
 		return false, "", fmt.Errorf("企业数量不足,至少需要两个")
 	}
 
-	// 遍历所有企业组合,只要发现有一组之间有关系就返回
+	found := false // 是否找到任意关系
+
 	for i := 0; i < len(names); i++ {
 		start := names[i]
-
 		// 构造剩下的企业列表作为目标
 		targets := []string{}
 		for j := 0; j < len(names); j++ {
@@ -500,11 +500,12 @@ func CheckLegalRelationships(session *nebula.Session, names []string, deep int)
 
 		// 构造 nGQL 查询
 		query := fmt.Sprintf(`
-USE `+Table_Space+`;
-	MATCH p=(a:Legal{name:"%s"})-[*1..%d]-(b:Legal)
-	WHERE b.Legal.name IN [%s]
-	RETURN p LIMIT 1
-`, start, deep, targetList)
+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 {
@@ -514,14 +515,19 @@ USE `+Table_Space+`;
 			return false, "", fmt.Errorf("查询执行失败: %s", resp.GetErrorMsg())
 		}
 
-		//ss := resp.GetRows()
-		//log.Println(ss)
-		// 有结果表示找到关系
 		if resp.GetRowSize() > 0 {
-			return true, fmt.Sprintf("企业【%s】与其他企业存在关系", start), nil
+			found = true
+			// stype == 0:只要找到一个关系就返回
+			if stype == 0 {
+				return true, "企业之间存在关系", nil
+			}
 		}
 	}
 
-	// 所有组合都查完了,没有找到关系
+	// 所有组合查完,判断是否找到关系
+	if found {
+		return true, "企业之间存在关系", nil
+	}
+
 	return false, "企业之间无关联", nil
 }