yjp 1 жил өмнө
parent
commit
702e68981b

+ 2 - 1
convenient/entity_crud/service.go

@@ -185,7 +185,7 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
 
 type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
 
-func Query[O any](tableName string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
+func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
 		errResponse := response.InfosData[O]{
 			Infos: make([]O, 0),
@@ -244,6 +244,7 @@ func Query[O any](tableName string, callbacks *QueryCallbacks[O], conditionField
 		results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
 			TableName:  tableName,
 			Conditions: conditions,
+			OrderBy:    orderBy,
 			PageNo:     queryParams.GetPageNo(),
 			PageSize:   queryParams.GetPageSize(),
 		})

+ 10 - 1
convenient/entity_crud/simple.go

@@ -130,7 +130,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 			SendResponseFunc: response.SendInfosResponse[I],
 			RequestParams:    simple.QueryQueryParams,
 			Objects:          []domain.Object{simple.Entity},
-			ServiceFunc:      Query[I](tableName, queryOptions.callbacks, queryOptions.conditionFieldCallback),
+			ServiceFunc:      Query[I](tableName, queryOptions.orderBy, queryOptions.callbacks, queryOptions.conditionFieldCallback),
 		}, queryMiddlewares...)
 	}
 
@@ -256,6 +256,9 @@ type QueryOptions[I any] struct {
 
 	// 查询中间件
 	middlewares []binding.Middleware
+
+	// OrderBy
+	orderBy string
 }
 
 type GetByIDOptions[I any] struct {
@@ -383,6 +386,12 @@ func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[
 	}
 }
 
+func WithQueryOrderBy[I any](orderBy string) QueryOption[I] {
+	return func(options *QueryOptions[I]) {
+		options.orderBy = orderBy
+	}
+}
+
 func WithDisableGetByID[I any]() GetByIDOption[I] {
 	return func(options *GetByIDOptions[I]) {
 		options.disable = true

+ 4 - 1
framework/core/infrastructure/database/sql/sql_template.go

@@ -170,7 +170,8 @@ SELECT
 FROM
     {{ .table_name }}
 WHERE
-    {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1 
+    {{ range .conditions }} {{ . }} AND {{ end }} 1 = 1
+{{ if .order_by }}ORDER BY {{ .order_by }}{{ end }}
 {{ if .limit }}LIMIT {{ .limit }}{{ end }}
 {{ if .offset }}OFFSET {{ .offset }}{{ end }}
 `
@@ -179,6 +180,7 @@ type QueryExecuteParams struct {
 	TableName     string
 	SelectColumns []string
 	*Conditions
+	OrderBy  string
 	PageNo   int
 	PageSize int
 }
@@ -207,6 +209,7 @@ func (params QueryExecuteParams) Map() (map[string]any, error) {
 		"conditions":     conditions,
 		"limit":          limit,
 		"offset":         offset,
+		"order_by":       params.OrderBy,
 	}, nil
 }