Sfoglia il codice sorgente

完成携带信息的查询

yjp 1 anno fa
parent
commit
ab833afe2c

+ 102 - 2
convenient/relation/one2one/service.go

@@ -195,12 +195,112 @@ func Query[TI any](fromTableName string, toTableName string, toRelationColumnNam
 	}
 }
 
-func QueryWithOtherInfo[FI any, TI any](tableName string) binding.ServiceFunc[map[string]any] {
+func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string, toRelationColumnName string) binding.ServiceFunc[map[string]any] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
-		info := new(struct {
+		var outputFromZero FI
+		var outputToZero TI
+
+		outputFromZeroValue := reflect.New(reflect.TypeOf(outputFromZero)).Elem()
+		if outputFromZeroValue.Kind() == reflect.Pointer {
+			outputFromZeroValue.Set(reflect.New(outputFromZeroValue.Type().Elem()))
+		}
+
+		outputFromZero = outputFromZeroValue.Interface().(FI)
+
+		outputToZeroValue := reflect.New(reflect.TypeOf(outputToZero)).Elem()
+		if outputToZeroValue.Kind() == reflect.Pointer {
+			outputToZeroValue.Set(reflect.New(outputToZeroValue.Type().Elem()))
+		}
+
+		outputToZero = outputToZeroValue.Interface().(TI)
+
+		zeroInfo := &struct {
 			Self FI `json:"self"`
 			With TI `json:"with"`
+		}{Self: outputFromZero, With: outputToZero}
+
+		zeroInfoJson, err := json.Marshal(zeroInfo)
+		if err != nil {
+			return nil, err
+		}
+
+		zeroRetMap := make(map[string]any)
+		err = json.Unmarshal(zeroInfoJson, &zeroRetMap)
+		if err != nil {
+			return nil, err
+		}
+
+		dbExecutor := i.DBExecutor()
+
+		object := objects[0]
+		if object == nil {
+			return zeroRetMap, fserr.New("领域实体为空")
+		}
+
+		fromEntity, ok := object.(domain.Entity)
+		if !ok {
+			return zeroRetMap, fserr.New("领域对象不是实体")
+		}
+
+		// from存在性校验
+		fromResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
+			TableName:  fromTableName,
+			Conditions: sql.NewConditions().Equal(domain.ColumnID, fromEntity.GetID()),
+		})
+		if err != nil {
+			if database.IsErrorDBRecordNotExist(err) {
+				return zeroRetMap, fserr.New(fromEntity.DomainCNName() + "不存在")
+			}
+
+			return zeroRetMap, err
+		}
+
+		existFrom := reflect.New(reflect.TypeOf(object).Elem()).Interface()
+		err = sql.ParseSqlResult(fromResult, existFrom)
+		if err != nil {
+			return zeroRetMap, err
+		}
+
+		existFromEntity := existFrom.(domain.Entity)
+
+		toResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
+			TableName:  toTableName,
+			Conditions: sql.NewConditions().Equal(toRelationColumnName, existFromEntity.GetID()),
 		})
+		if err != nil {
+			return zeroRetMap, err
+		}
+
+		var fromInfo FI
+		var fromInfoPointer any
+
+		fromInfoPointer = &fromInfo
+		if outputFromZeroValue.Kind() == reflect.Pointer {
+			fromInfoPointer = fromInfo
+		}
+
+		err = sql.ParseSqlResult(fromResult, fromInfoPointer)
+		if err != nil {
+			return zeroRetMap, err
+		}
+
+		var toInfo TI
+		var toInfoPointer any
+
+		toInfoPointer = &toInfo
+		if outputToZeroValue.Kind() == reflect.Pointer {
+			toInfoPointer = toInfo
+		}
+
+		err = sql.ParseSqlResult(toResult, toInfoPointer)
+		if err != nil {
+			return zeroRetMap, err
+		}
+
+		info := &struct {
+			Self FI `json:"self"`
+			With TI `json:"with"`
+		}{Self: fromInfo, With: toInfo}
 
 		infoJson, err := json.Marshal(info)
 		if err != nil {

+ 22 - 0
convenient/relation/one2one/simple.go

@@ -84,6 +84,17 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 				ServiceFunc:   Query[RI](simple.LeftTableName, simple.RightTableName, rightRelationColumnName),
 			})
 		}
+
+		if !options.disableLeftWithRightQuery {
+			// 左到右查询,携带右方信息
+			binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
+				Path:          simple.LeftDomainPath + simple.RightDomainPath + "/queryWith",
+				ResponseFunc:  response.SendMapResponse,
+				RequestParams: simple.LeftQueryWithRightQueryParams,
+				Objects:       []domain.Object{simple.Left},
+				ServiceFunc:   QueryWithOtherInfo[LI, RI](simple.LeftTableName, simple.RightTableName, rightRelationColumnName),
+			})
+		}
 	}
 
 	if !options.disableRight {
@@ -109,6 +120,17 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 				ServiceFunc:   Query[RI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
 			})
 		}
+
+		if !options.disableRightWithLeftQuery {
+			// 右到左查询,携带左方信息
+			binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
+				Path:          simple.RightDomainPath + simple.LeftDomainPath + "/queryWith",
+				ResponseFunc:  response.SendMapResponse,
+				RequestParams: simple.RightQueryWithLeftQueryParams,
+				Objects:       []domain.Object{simple.Right},
+				ServiceFunc:   QueryWithOtherInfo[RI, LI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
+			})
+		}
 	}
 
 }

+ 2 - 0
examples/examples/project/main.go

@@ -38,10 +38,12 @@ import (
 // Student-Family
 // curl -X POST -H "Content-Type: application/json" -d '{"id":"fc3e96926aac46268ae783c4ad675d43", "familyId": "ed86bbb59a80429eb654d7f9f13ff116"}' "http://localhost:31000/example/v1/student/family/update"
 // curl -X GET "http://localhost:31000/example/v1/student/family/query?id=fc3e96926aac46268ae783c4ad675d43"
+// curl -X GET "http://localhost:31000/example/v1/student/family/queryWith?id=fc3e96926aac46268ae783c4ad675d43"
 
 // Family-Student
 // curl -X POST -H "Content-Type: application/json" -d '{"id":"ed86bbb59a80429eb654d7f9f13ff116", "studentId": "fc3e96926aac46268ae783c4ad675d43"}' "http://localhost:31000/example/v1/family/student/update"
 // curl -X GET "http://localhost:31000/example/v1/family/student/query?id=ed86bbb59a80429eb654d7f9f13ff116"
+// curl -X GET "http://localhost:31000/example/v1/family/student/queryWith?id=ed86bbb59a80429eb654d7f9f13ff116"
 
 func main() {
 	application.NewApp()