From fc3313955a083c9480e4ea74398f72f9ba6addcd Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 01 八月 2024 20:29:51 +0800
Subject: [PATCH] 月度统计查询多单位数据计算改查询。

---
 models/location_product_amount.go |   13 ++++++
 service/month_forms.go            |   59 ++++++++++++++++++++++++-----
 models/operation_details.go       |   10 +++++
 3 files changed, 72 insertions(+), 10 deletions(-)

diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index 928dddb..279ab69 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -334,6 +334,19 @@
 	return records, nil
 }
 
+func (slf *LocationProductAmountSearch) FindAll() ([]*LocationProductAmount, error) {
+	var (
+		records = make([]*LocationProductAmount, 0)
+		db      = slf.build()
+	)
+
+	if err := db.Find(&records).Error; err != nil {
+		return records, fmt.Errorf("find records err: %v", err)
+	}
+
+	return records, nil
+}
+
 func (slf *LocationProductAmountSearch) FirstRes() (*LocationProductAmount, *gorm.DB) {
 	var (
 		record = new(LocationProductAmount)
diff --git a/models/operation_details.go b/models/operation_details.go
index f57c5b3..6af4c6e 100644
--- a/models/operation_details.go
+++ b/models/operation_details.go
@@ -55,6 +55,7 @@
 		Preload      bool
 		OperationIDs []int
 		Fields       string
+		ProductIds   []string
 	}
 )
 
@@ -147,6 +148,11 @@
 	return slf
 }
 
+func (slf *OperationDetailsSearch) SetProductIds(productIds []string) *OperationDetailsSearch {
+	slf.ProductIds = productIds
+	return slf
+}
+
 func (slf *OperationDetailsSearch) SetBaseOperationType(baseOperationType constvar.BaseOperationType) *OperationDetailsSearch {
 	slf.BaseOperationType = baseOperationType
 	return slf
@@ -189,6 +195,10 @@
 		db = db.Where("base_operation_type = ?", slf.BaseOperationType)
 	}
 
+	if len(slf.ProductIds) > 0 {
+		db = db.Where("product_id in ?", slf.ProductIds)
+	}
+
 	return db
 }
 
diff --git a/service/month_forms.go b/service/month_forms.go
index 34a9cc5..1ab8691 100644
--- a/service/month_forms.go
+++ b/service/month_forms.go
@@ -145,17 +145,19 @@
 		logx.Errorf("MonthStats GetCurrentStats get GetInventoryCutOffTime err:%v", err)
 		return
 	}
-	inputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeIncoming)
+	inputMap, inputProductMoreUnit, err := GetStatsByOperationType(beginTime, endTime, productIds, constvar.BaseOperationTypeIncoming)
 	if err != nil {
 		logx.Errorf("MonthStats GetStatsByOperationType input err:%v", err)
 		return
 	}
 
-	outputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeOutgoing)
+	outputMap, outputProductMoreUnit, err := GetStatsByOperationType(beginTime, endTime, productIds, constvar.BaseOperationTypeOutgoing)
 	if err != nil {
 		logx.Errorf("MonthStats GetStatsByOperationType output err:%v", err)
 		return
 	}
+
+	productMoreUnit, err := GetProductMoreUnitMap(productIds)
 
 	for _, groupSum := range groupSumList {
 		productId := groupSum.Class
@@ -182,14 +184,14 @@
 			inputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
 			outputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
 			if !amount.IsZero() {
-				moreValueArr = CreateMoreUnit(amount, product.MoreUnitList)
+				moreValueArr = productMoreUnit[productId]
 			}
 			if !inputMap[productId].IsZero() {
-				inputMoreValueArr = CreateMoreUnit(inputMap[productId], product.MoreUnitList)
+				inputMoreValueArr = inputProductMoreUnit[productId]
 			}
 
 			if !outputMap[productId].IsZero() {
-				outputMoreValueArr = CreateMoreUnit(outputMap[productId], product.MoreUnitList)
+				outputMoreValueArr = outputProductMoreUnit[productId]
 			}
 			bys, _ := json.Marshal(moreValueArr)
 			moreUnits = string(bys)
@@ -211,18 +213,55 @@
 	return
 }
 
-func GetStatsByOperationType(beginTime, endTime time.Time, operationType constvar.BaseOperationType) (m map[string]decimal.Decimal, err error) {
+func GetStatsByOperationType(beginTime, endTime time.Time, productIds []string, operationType constvar.BaseOperationType) (m map[string]decimal.Decimal, productMoreUnit map[string][]models.UnitItems, err error) {
 	operationIds, err := models.NewOperationSearch().SetBaseOperationType(operationType).SetFields("id").SetTimeBetween(beginTime, endTime).FindIds()
 	if err != nil {
 		return
 	}
-	groupSumList, err := models.NewOperationDetailsSearch().SetOperationIds(operationIds).SetFields("product_id, amount").GroupSum("product_id", "amount")
+
+	productMoreUnit = make(map[string][]models.UnitItems)
+	detailProducts, err := models.NewOperationDetailsSearch().
+		SetProductIds(productIds).SetOperationIds(operationIds).
+		SetFields("product_id, amount, more_unit_value").FindAll()
+	m = make(map[string]decimal.Decimal, 0)
 	if err != nil {
+		logx.Errorf("MonthStats GetMoreUnitRecords err:%v", err)
 		return
 	}
-	m = make(map[string]decimal.Decimal, len(groupSumList))
-	for _, v := range groupSumList {
-		m[v.Class] = v.Sum
+
+	for _, v := range detailProducts {
+		m[v.ProductId] = m[v.ProductId].Add(v.Amount)
+		if v.MoreUnitValue == "" {
+			continue
+		}
+		if productMoreUnit[v.ProductId] == nil {
+			productMoreUnit[v.ProductId] = v.MoreUnitList
+			continue
+		}
+		productMoreUnit[v.ProductId] = AddMoreUnit(productMoreUnit[v.ProductId], v.MoreUnitList)
+	}
+
+	return
+}
+
+func GetProductMoreUnitMap(productIds []string) (m map[string][]models.UnitItems, err error) {
+	m = make(map[string][]models.UnitItems)
+	locAmountRecords, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).
+		SetFields("product_id, more_unit_value").FindAll()
+	if err != nil {
+		logx.Errorf("MonthStats GetMoreUnitRecords err:%v", err)
+		return
+	}
+
+	for _, v := range locAmountRecords {
+		if v.MoreUnitValue == "" {
+			continue
+		}
+		if m[v.ProductId] == nil {
+			m[v.ProductId] = v.MoreUnitList
+			continue
+		}
+		m[v.ProductId] = AddMoreUnit(m[v.ProductId], v.MoreUnitList)
 	}
 	return
 }

--
Gitblit v1.8.0