From aeed976c2999e2cea097cdee38d8baeefe323f3d Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期六, 30 三月 2024 16:15:59 +0800
Subject: [PATCH] 修改

---
 models/location_product_amount.go |  114 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 94 insertions(+), 20 deletions(-)

diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index 0700984..4f1af2b 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -11,22 +11,31 @@
 type (
 	LocationProductAmount struct {
 		WmsModel
-		Id                int             `json:"id"  gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		LocationProductId int             `json:"locationProductId" gorm:"type:int;not null;comment:涓婃灦瑙勫垯id"` //涓婃灦瑙勫垯id
-		LocationProduct   LocationProduct `json:"locationProduct" gorm:"foreignKey:LocationProductId;references:Id"`
+		Id int `json:"id"  gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		//LocationProductId int             `json:"locationProductId" gorm:"type:int;not null;comment:涓婃灦瑙勫垯id"` //涓婃灦瑙勫垯id
+		//LocationProduct   LocationProduct `json:"locationProduct" gorm:"foreignKey:LocationProductId;references:Id"`
+		LocationId        int             `json:"locationId" gorm:"type:int;not null;comment:浣嶇疆id"` //浣嶇疆id
+		Location          Location        `json:"location" gorm:"foreignKey:LocationId;references:id"`
+		ProductCategoryID int             `json:"productCategoryId" gorm:"type:int;not null;comment:浜у搧绉嶇被id"` //浜у搧绉嶇被id
+		ProductCategory   ProductCategory `json:"productCategory" gorm:"foreignKey:ProductCategoryID;references:Id"`
+		ProductId         string          `json:"productId" gorm:"type:varchar(191);not null;comment:浜у搧id"` //浜у搧id
+		Product           Material        `json:"product" gorm:"foreignKey:ProductId;references:ID"`
 		Amount            decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:搴撳瓨鏁伴噺"` //搴撳瓨鏁伴噺
 		CreateDate        string          `json:"createDate" gorm:"type:varchar(63);comment:鏃ユ湡"`          //鏃ユ湡
 	}
 
 	LocationProductAmountSearch struct {
 		LocationProductAmount
-		Order              string
-		PageNum            int
-		PageSize           int
-		Keyword            string
-		Orm                *gorm.DB
-		Preload            bool
-		LocationProductIds []int
+		Order    string
+		PageNum  int
+		PageSize int
+		Keyword  string
+		Orm      *gorm.DB
+		Preload  bool
+		//LocationProductIds []int
+		LocationIds []int
+		ProductIds  []string
+		Ids         []int
 	}
 
 	LocationProductAmountWithOperation struct {
@@ -75,6 +84,11 @@
 	return slf
 }
 
+func (slf *LocationProductAmountSearch) SetIds(ids []int) *LocationProductAmountSearch {
+	slf.Ids = ids
+	return slf
+}
+
 func (slf *LocationProductAmountSearch) SetKeyword(keyword string) *LocationProductAmountSearch {
 	slf.Keyword = keyword
 	return slf
@@ -85,13 +99,23 @@
 	return slf
 }
 
-func (slf *LocationProductAmountSearch) SetLocationProductId(id int) *LocationProductAmountSearch {
-	slf.LocationProductId = id
+func (slf *LocationProductAmountSearch) SetProductId(productId string) *LocationProductAmountSearch {
+	slf.ProductId = productId
 	return slf
 }
 
-func (slf *LocationProductAmountSearch) SetLocationProductIds(ids []int) *LocationProductAmountSearch {
-	slf.LocationProductIds = ids
+func (slf *LocationProductAmountSearch) SetLocationId(locationId int) *LocationProductAmountSearch {
+	slf.LocationId = locationId
+	return slf
+}
+
+func (slf *LocationProductAmountSearch) SetProductIds(ids []string) *LocationProductAmountSearch {
+	slf.ProductIds = ids
+	return slf
+}
+
+func (slf *LocationProductAmountSearch) SetLocationIds(ids []int) *LocationProductAmountSearch {
+	slf.LocationIds = ids
 	return slf
 }
 
@@ -106,17 +130,37 @@
 	}
 
 	if slf.Keyword != "" {
-		db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
+		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").
+			Where("wms_location.name like ?", "%"+slf.Keyword+"%").Or("material.name like ?", "%"+slf.Keyword+"%").
+			Or("wms_product_category.name like ?", "%"+slf.Keyword+"%")
 	}
 
 	if slf.Preload {
-		db = db.Model(&LocationProductAmount{}).Preload("LocationProduct").Preload("LocationProduct.Location").Preload("LocationProduct.Product")
+		db = db.Model(&LocationProductAmount{}).Preload("Location").Preload("Product").Preload("ProductCategory")
 	}
-	if slf.LocationProductId != 0 {
-		db = db.Where("location_product_id=?", slf.LocationProductId)
+	//if slf.LocationProductId != 0 {
+	//	db = db.Where("location_product_id=?", slf.LocationProductId)
+	//}
+	if len(slf.ProductIds) > 0 {
+		db = db.Where("product_id in (?)", slf.ProductIds)
 	}
-	if len(slf.LocationProductIds) > 0 {
-		db = db.Where("location_product_id in (?)", slf.LocationProductIds)
+
+	if len(slf.LocationIds) > 0 {
+		db = db.Where("location_id in (?)", slf.LocationIds)
+	}
+
+	if slf.LocationId != 0 {
+		db = db.Where("location_id = ?", slf.LocationId)
+	}
+
+	if slf.ProductId != "" {
+		db = db.Where("product_id=?", slf.ProductId)
+	}
+
+	if len(slf.Ids) > 0 {
+		db = db.Where("id in (?)", slf.Ids)
 	}
 
 	return db
@@ -204,3 +248,33 @@
 
 	return records, nil
 }
+
+func (slf *LocationProductAmountSearch) FirstRes() (*LocationProductAmount, *gorm.DB) {
+	var (
+		record = new(LocationProductAmount)
+		db     = slf.build()
+	)
+
+	return record, db.First(&record)
+}
+
+func (slf *LocationProductAmountSearch) Count() (int64, error) {
+	var (
+		total int64
+		db    = slf.build()
+	)
+	err := db.Count(&total).Error
+	return total, err
+}
+
+func (slf *LocationProductAmountSearch) UpdateByMap(upMap map[string]interface{}) error {
+	var (
+		db = slf.build()
+	)
+
+	if err := db.Updates(upMap).Error; err != nil {
+		return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
+	}
+
+	return nil
+}

--
Gitblit v1.8.0