Ver Fonte

添加orderby

yjp há 4 meses atrás
pai
commit
32b628d8d0

+ 2 - 1
convenient/relation/many2many/service.go

@@ -125,7 +125,7 @@ func Update(middleTableName string,
 
 func Query[TI any](middleTableName string,
 	fromTableName string, fromRelationColumnName string,
-	toTableName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[TI]] {
+	toTableName string, toRelationColumnName string, orderBy string) binding.ServiceFunc[response.InfosData[TI]] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[TI], error) {
 		errResponse := response.InfosData[TI]{
 			Infos: make([]TI, 0),
@@ -193,6 +193,7 @@ func Query[TI any](middleTableName string,
 		toResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
 			TableName:  toTableName,
 			Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
+			OrderBy:    orderBy,
 		})
 		if err != nil {
 			return errResponse, err

+ 20 - 2
convenient/relation/many2many/simple.go

@@ -80,7 +80,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 				Objects:          []domain.Object{simple.Left},
 				ServiceFunc: Query[RI](middleTableName,
 					leftTableName, leftRelationColumnName,
-					rightTableName, rightRelationColumnName),
+					rightTableName, rightRelationColumnName, options.leftQueryOrderBy),
 			}, leftQueryMiddlewares...)
 		}
 	}
@@ -112,7 +112,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 				Objects:          []domain.Object{simple.Right},
 				ServiceFunc: Query[LI](middleTableName,
 					rightTableName, rightRelationColumnName,
-					leftTableName, leftRelationColumnName),
+					leftTableName, leftRelationColumnName, options.rightQueryOrderBy),
 			}, rightQueryMiddlewares...)
 		}
 	}
@@ -160,11 +160,17 @@ type Options struct {
 	// 左侧查询中间件
 	leftQueryMiddlewares []binding.Middleware
 
+	// 左侧查询OrderBy
+	leftQueryOrderBy string
+
 	// 右侧更新中间件
 	rightUpdateMiddlewares []binding.Middleware
 
 	// 右侧查询中间件
 	rightQueryMiddlewares []binding.Middleware
+
+	// 右侧查询OrderBy
+	rightQueryOrderBy string
 }
 
 func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
@@ -221,6 +227,12 @@ func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
 	}
 }
 
+func WithLeftQueryOrderBy(orderBy string) Option {
+	return func(options *Options) {
+		options.leftQueryOrderBy = orderBy
+	}
+}
+
 func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
 	return func(options *Options) {
 		options.rightUpdateMiddlewares = middlewares
@@ -232,3 +244,9 @@ func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
 		options.rightQueryMiddlewares = middlewares
 	}
 }
+
+func WithRightQueryOrderBy(orderBy string) Option {
+	return func(options *Options) {
+		options.rightQueryOrderBy = orderBy
+	}
+}

+ 2 - 1
convenient/relation/one2many/service.go

@@ -118,7 +118,7 @@ func UpdateLeft(leftTableName string, leftDomainCNName string, leftRelationField
 }
 
 func QueryLeft[RI any](leftTableName string, leftRelationColumnName string,
-	rightTableName string) binding.ServiceFunc[response.InfosData[RI]] {
+	rightTableName string, leftQueryOrderBy string) binding.ServiceFunc[response.InfosData[RI]] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[RI], error) {
 		errResponse := response.InfosData[RI]{
 			Infos: make([]RI, 0),
@@ -161,6 +161,7 @@ func QueryLeft[RI any](leftTableName string, leftRelationColumnName string,
 		rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
 			TableName:  rightTableName,
 			Conditions: sql.NewConditions().Equal(leftRelationColumnName, leftEntity.GetID()),
+			OrderBy:    leftQueryOrderBy,
 		})
 		if err != nil && !database.IsErrorDBRecordNotExist(err) {
 			return errResponse, err

+ 10 - 1
convenient/relation/one2many/simple.go

@@ -78,7 +78,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 				SendResponseFunc: response.SendInfosResponse[RI],
 				RequestParams:    simple.LeftQueryQueryParams,
 				Objects:          []domain.Object{simple.Left},
-				ServiceFunc:      QueryLeft[RI](leftTableName, leftRelationColumnName, rightTableName),
+				ServiceFunc:      QueryLeft[RI](leftTableName, leftRelationColumnName, rightTableName, options.leftQueryOrderBy),
 			}, leftQueryMiddlewares...)
 		}
 	}
@@ -174,6 +174,9 @@ type Options struct {
 	// 左侧查询中间件
 	leftQueryMiddlewares []binding.Middleware
 
+	// 左侧查询OrderBy
+	leftQueryOrderBy string
+
 	// 右侧更新中间件
 	rightUpdateMiddlewares []binding.Middleware
 
@@ -214,6 +217,12 @@ func WithDisableLeftQuery() Option {
 	}
 }
 
+func WithLeftQueryOrderBy(orderBy string) Option {
+	return func(options *Options) {
+		options.leftQueryOrderBy = orderBy
+	}
+}
+
 func WithDisableRightUpdate() Option {
 	return func(options *Options) {
 		options.disableRightUpdate = true