From 307c699810c39714e82efac9de56f70b93a718a2 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期二, 31 十月 2023 11:54:23 +0800
Subject: [PATCH] 重订货规则开发

---
 models/location_product.go |   47 ++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/models/location_product.go b/models/location_product.go
index 520b18a..f737765 100644
--- a/models/location_product.go
+++ b/models/location_product.go
@@ -22,12 +22,13 @@
 
 	LocationProductSearch struct {
 		LocationProduct
-		Order    string
-		PageNum  int
-		PageSize int
-		Keyword  string
-		Orm      *gorm.DB
-		Preload  bool
+		Order       string
+		PageNum     int
+		PageSize    int
+		Keyword     string
+		Orm         *gorm.DB
+		Preload     bool
+		LocationIds []int
 	}
 )
 
@@ -79,6 +80,16 @@
 	return slf
 }
 
+func (slf *LocationProductSearch) SetLocationIds(locationIds []int) *LocationProductSearch {
+	slf.LocationIds = locationIds
+	return slf
+}
+
+func (slf *LocationProductSearch) SetAreaId(areaId int) *LocationProductSearch {
+	slf.AreaId = areaId
+	return slf
+}
+
 func (slf *LocationProductSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&LocationProduct{})
 
@@ -90,7 +101,13 @@
 	}
 
 	if slf.Keyword != "" {
-		db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
+		//db = db.Where("Product.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
+		db.Joins("left join wms_location on wms_location_product.location_id = wms_location.id").
+			Joins("left join material on wms_location_product.product_id = material.id").
+			Joins("left join wms_product_category on wms_location_product.product_category_id = wms_product_category.id").
+			Where("wms_location.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)).
+			Or("material.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)).
+			Or("wms_product_category.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
 	}
 
 	if slf.Preload {
@@ -103,6 +120,14 @@
 
 	if slf.ProductId != "" {
 		db = db.Where("product_id=?", slf.ProductId)
+	}
+
+	if len(slf.LocationIds) > 0 {
+		db = db.Where("location_id in (?)", slf.LocationIds)
+	}
+
+	if slf.AreaId != 0 {
+		db = db.Where("area_id = ?", slf.AreaId)
 	}
 
 	return db
@@ -190,3 +215,11 @@
 
 	return records, nil
 }
+
+func (slf *LocationProductSearch) FirstRes() (*LocationProduct, *gorm.DB) {
+	var (
+		record = new(LocationProduct)
+		db     = slf.build()
+	)
+	return record, db.First(record)
+}

--
Gitblit v1.8.0