|
@@ -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
|
|
|
+}
|