From ea02a2a9a8219d44b103f0a31cf9fc81ff8495e1 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期六, 23 九月 2023 16:57:48 +0800
Subject: [PATCH] 产品列表添加类型名称
---
controllers/product_controller.go | 16 ++++++++++++++++
models/product_category.go | 8 ++++++++
2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/controllers/product_controller.go b/controllers/product_controller.go
index 0f57587..a47c9a1 100644
--- a/controllers/product_controller.go
+++ b/controllers/product_controller.go
@@ -74,6 +74,22 @@
return
}
+ ids := make([]int, 0)
+ for _, product := range products {
+ ids = append(ids, product.CategoryId)
+ }
+ categories, err := models.NewProductCategorySearch().SetIds(ids).FindNotTotal()
+ if err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "浜у搧绫诲瀷鏌ユ壘澶辫触")
+ return
+ }
+ for _, product := range products {
+ for _, category := range categories {
+ if product.CategoryId == int(category.ID) {
+ product.CategoryName = category.Name
+ }
+ }
+ }
util.ResponseFormatList(c, code.Success, products, int(total))
}
diff --git a/models/product_category.go b/models/product_category.go
index 5f6ebbf..cb91852 100644
--- a/models/product_category.go
+++ b/models/product_category.go
@@ -28,6 +28,7 @@
Keyword string
Orm *gorm.DB
Preload bool
+ Ids []int
}
)
@@ -73,6 +74,10 @@
slf.Preload = preload
return slf
}
+func (slf *ProductCategorySearch) SetIds(ids []int) *ProductCategorySearch {
+ slf.Ids = ids
+ return slf
+}
func (slf *ProductCategorySearch) build() *gorm.DB {
var db = slf.Orm.Model(&ProductCategory{})
@@ -92,6 +97,9 @@
if slf.Name != "" {
db = db.Where("name = ?", slf.Name)
}
+ if len(slf.Ids) > 0 {
+ db = db.Where("id in (?)", slf.Ids)
+ }
return db
}
--
Gitblit v1.8.0