From 076f911a3b8d09364d1d8c98a92b48e601ae6a21 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 31 七月 2024 11:30:27 +0800
Subject: [PATCH] 位置对应产品列表增加产品类别筛选

---
 models/location_product_amount.go |   29 ++++++++++++++++++++++++-----
 1 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index 81cc55b..928dddb 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -36,11 +36,13 @@
 		Orm      *gorm.DB
 		Preload  bool
 		//LocationProductIds []int
-		LocationIds []int
-		ProductIds  []string
-		Ids         []int
-		Query       string
-		Fields      string
+		LocationIds     []int
+		ProductIds      []string
+		Ids             []int
+		Query           string
+		Fields          string
+		CategoryIds     []int
+		JoinedMaterials bool
 	}
 
 	LocationProductAmountWithOperation struct {
@@ -172,6 +174,11 @@
 	return slf
 }
 
+func (slf *LocationProductAmountSearch) SetCategoryIds(ids []int) *LocationProductAmountSearch {
+	slf.CategoryIds = ids
+	return slf
+}
+
 func (slf *LocationProductAmountSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&LocationProductAmount{})
 
@@ -183,6 +190,10 @@
 	}
 
 	if slf.Keyword != "" {
+		if !slf.JoinedMaterials {
+			db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
+			slf.JoinedMaterials = true
+		}
 		db = db.Joins("left join wms_location on wms_location_product_amount.location_id = wms_location.id").
 			Joins("left join material on wms_location_product_amount.product_id = material.id").
 			Joins("left join wms_product_category on wms_location_product_amount.product_category_id = wms_product_category.id").
@@ -226,6 +237,14 @@
 		db = db.Select(slf.Fields)
 	}
 
+	if len(slf.CategoryIds) > 0 {
+		if !slf.JoinedMaterials {
+			db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
+			slf.JoinedMaterials = true
+		}
+		db = db.Where("material.category_id in ?", slf.CategoryIds)
+	}
+
 	return db
 }
 

--
Gitblit v1.8.0