From 541486c20ad74c2bcbb2e42137a116981b313787 Mon Sep 17 00:00:00 2001
From: jiangshuai <291802688@qq.com>
Date: 星期一, 25 九月 2023 10:18:06 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/WMS

---
 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 ab762ae..126a539 100644
--- a/controllers/product_controller.go
+++ b/controllers/product_controller.go
@@ -82,6 +82,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