From 749064932c066192cd89be0ee8cf347f9b1af821 Mon Sep 17 00:00:00 2001 From: fengsilin Date: Thu, 23 Apr 2026 09:27:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20PUT=20/api/vendors?= =?UTF-8?q?/reorder=20=E5=92=8C=20/api/models/reorder=20=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- controller/reorder.go | 55 +++++++++++++++++++++++++++++++++++++++++++ model/model_meta.go | 15 ++++++++++++ model/vendor_meta.go | 15 ++++++++++++ router/api-router.go | 2 ++ 4 files changed, 87 insertions(+) create mode 100644 controller/reorder.go diff --git a/controller/reorder.go b/controller/reorder.go new file mode 100644 index 0000000..5a02481 --- /dev/null +++ b/controller/reorder.go @@ -0,0 +1,55 @@ +package controller + +import ( + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/model" + + "github.com/gin-gonic/gin" +) + +// ReorderVendors 批量更新供应商排序 +func ReorderVendors(c *gin.Context) { + var req struct { + Items []struct { + Id int `json:"id"` + SortOrder int `json:"sort_order"` + } `json:"items"` + } + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiError(c, err) + return + } + if len(req.Items) == 0 { + common.ApiErrorMsg(c, "items 不能为空") + return + } + if err := model.ReorderVendors(req.Items); err != nil { + common.ApiError(c, err) + return + } + common.ApiSuccess(c, nil) +} + +// ReorderModels 批量更新模型排序 +func ReorderModels(c *gin.Context) { + var req struct { + Items []struct { + Id int `json:"id"` + SortOrder int `json:"sort_order"` + } `json:"items"` + } + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiError(c, err) + return + } + if len(req.Items) == 0 { + common.ApiErrorMsg(c, "items 不能为空") + return + } + if err := model.ReorderModels(req.Items); err != nil { + common.ApiError(c, err) + return + } + model.RefreshPricing() + common.ApiSuccess(c, nil) +} diff --git a/model/model_meta.go b/model/model_meta.go index b55faea..b9c1ad5 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -171,3 +171,18 @@ func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Mode } return models, total, nil } + +// ReorderModels 批量更新模型排序值 +func ReorderModels(items []struct { + Id int `json:"id"` + SortOrder int `json:"sort_order"` +}) error { + return DB.Transaction(func(tx *gorm.DB) error { + for _, item := range items { + if err := tx.Model(&Model{}).Where("id = ?", item.Id).Update("sort_order", item.SortOrder).Error; err != nil { + return err + } + } + return nil + }) +} diff --git a/model/vendor_meta.go b/model/vendor_meta.go index a0bbf52..f6b7e16 100644 --- a/model/vendor_meta.go +++ b/model/vendor_meta.go @@ -87,3 +87,18 @@ func SearchVendors(keyword string, offset int, limit int) ([]*Vendor, int64, err } return vendors, total, nil } + +// ReorderVendors 批量更新供应商排序值 +func ReorderVendors(items []struct { + Id int `json:"id"` + SortOrder int `json:"sort_order"` +}) error { + return DB.Transaction(func(tx *gorm.DB) error { + for _, item := range items { + if err := tx.Model(&Vendor{}).Where("id = ?", item.Id).Update("sort_order", item.SortOrder).Error; err != nil { + return err + } + } + return nil + }) +} diff --git a/router/api-router.go b/router/api-router.go index 9d56d21..7a0d166 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -356,6 +356,7 @@ func SetApiRouter(router *gin.Engine) { vendorRoute.GET("/:id", controller.GetVendorMeta) vendorRoute.POST("/", controller.CreateVendorMeta) vendorRoute.PUT("/", controller.UpdateVendorMeta) + vendorRoute.PUT("/reorder", controller.ReorderVendors) vendorRoute.DELETE("/:id", controller.DeleteVendorMeta) } @@ -370,6 +371,7 @@ func SetApiRouter(router *gin.Engine) { modelsRoute.GET("/:id", controller.GetModelMeta) modelsRoute.POST("/", controller.CreateModelMeta) modelsRoute.PUT("/", controller.UpdateModelMeta) + modelsRoute.PUT("/reorder", controller.ReorderModels) modelsRoute.DELETE("/:id", controller.DeleteModelMeta) }