From 19bbf202e4f38f067db9c2800e764512ad67dcd9 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 27 六月 2024 22:34:32 +0800
Subject: [PATCH] 当月的统计报表(未至库存结算时间点)支持实时查询入库数量出库数量期末数量

---
 task/month_stats.go |  140 +++++-----------------------------------------
 1 files changed, 16 insertions(+), 124 deletions(-)

diff --git a/task/month_stats.go b/task/month_stats.go
index a6b2669..b2de4fb 100644
--- a/task/month_stats.go
+++ b/task/month_stats.go
@@ -1,12 +1,9 @@
 package task
 
 import (
-	"encoding/json"
 	"fmt"
-	"github.com/shopspring/decimal"
 	"gorm.io/gorm"
 	"time"
-	"wms/constvar"
 	"wms/models"
 	"wms/pkg/logx"
 	"wms/service"
@@ -45,108 +42,40 @@
 	oldRecordsMap := models.MonthStatsMap(oldRecords)
 
 	//鏈湀鏈熷垵鏁伴噺/涓婃湀缁撲綑鏁伴噺
-	groupSumList, err := models.NewLocationProductAmountSearch().GroupSum("product_id", "amount")
-
-	productIds := make([]string, 0, len(groupSumList))
-
-	for _, groupSum := range groupSumList {
-		productIds = append(productIds, groupSum.Class)
-	}
-	products, err := models.NewMaterialSearch().SetFields("id, name, unit, weight, more_unit, more_unit_value").SetIDs(productIds).FindNotTotal()
+	statsRecords, err := service.GetCurrentStats(date, nil)
 	if err != nil {
-		logx.Errorf("MonthStats get products err:%v", err)
-		return
+		return err
 	}
-	productMap := models.MaterialMap(products)
-
-	beginTime, endTime := GetLastMonthPeriod()
-	inputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeIncoming)
-	if err != nil {
-		logx.Errorf("MonthStats GetStatsByOperationType input err:%v", err)
-		return
-	}
-
-	outputMap, err := GetStatsByOperationType(beginTime, endTime, constvar.BaseOperationTypeOutgoing)
-	if err != nil {
-		logx.Errorf("MonthStats GetStatsByOperationType output err:%v", err)
-		return
-	}
-	var record models.MonthStats
 
 	err = models.NewMonthStatsSearch().SetDate(date).Delete()
 	if err != nil {
 		return
 	}
 
-	for _, groupSum := range groupSumList {
-		productId := groupSum.Class
-		if productMap[productId] == nil {
-			continue
-		}
-		product := productMap[productId]
-		amount := groupSum.Sum
-		record = models.MonthStats{
-			ProductId:   productId,
-			ProductName: product.Name,
-			BeginAmount: amount,
-			Unit:        product.Unit,
-			Weight:      product.Weight.Mul(amount),
-			Date:        date,
-		}
-
-		var (
-			moreUnits       string
-			inputMoreUnits  string
-			outputMoreUnits string
-		)
-		if *product.MoreUnit {
-			moreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
-			inputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
-			outputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
-			if !amount.IsZero() {
-				moreValueArr = service.CreateMoreUnit(amount, product.MoreUnitList)
-			}
-			if !inputMap[productId].IsZero() {
-				inputMoreValueArr = service.CreateMoreUnit(inputMap[productId], product.MoreUnitList)
-			}
-
-			if !outputMap[productId].IsZero() {
-				outputMoreValueArr = service.CreateMoreUnit(outputMap[productId], product.MoreUnitList)
-			}
-			bys, _ := json.Marshal(moreValueArr)
-			moreUnits = string(bys)
-			bys, _ = json.Marshal(inputMoreValueArr)
-			inputMoreUnits = string(bys)
-			bys, _ = json.Marshal(outputMoreValueArr)
-			outputMoreUnits = string(bys)
-		}
-
-		record.BeginMoreUnits = moreUnits
+	for _, record := range statsRecords {
+		record.BeginAmount = record.EndAmount
+		record.BeginMoreUnits = record.EndMoreUnits
 		err = models.WithTransaction(func(db *gorm.DB) error {
-			err = models.NewMonthStatsSearch().SetOrm(db).Create(&record)
+			err = models.NewMonthStatsSearch().SetOrm(db).Create(record)
 			if err != nil {
 				logx.Errorf("NewMonthStatsSearch Create err:%v, record: %+v", err, record)
 				service.SendAlarm("鏈堝害缁熻鍒涘缓鏈湀澶辫触", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record))
 			}
 
-			if oldRecordsMap[productId] != nil {
+			if oldRecordsMap[record.ProductId] != nil && oldRecordsMap[record.ProductId].Id != 0 {
 				m := map[string]interface{}{
-					"end_more_units": moreUnits,
-					"end_amount":     amount,
+					"end_more_units": record.BeginMoreUnits,
+					"end_amount":     record.BeginAmount,
 				}
-				if !inputMap[productId].IsZero() || !outputMap[productId].IsZero() {
-					record.InputAmount = inputMap[productId]
-					record.InputMoreUnits = inputMoreUnits
-					record.OutputAmount = outputMap[productId]
-					record.OutputMoreUnits = outputMoreUnits
-					m["input_amount"] = inputMap[productId]
-					m["input_more_units"] = inputMoreUnits
-					m["output_amount"] = outputMap[productId]
-					m["output_more_units"] = outputMoreUnits
+				if !record.InputAmount.IsZero() || !record.InputAmount.IsZero() {
+					m["input_amount"] = record.InputAmount
+					m["input_more_units"] = record.InputMoreUnits
+					m["output_amount"] = record.InputAmount
+					m["output_more_units"] = record.OutputMoreUnits
 				}
-				err = models.NewMonthStatsSearch().SetOrm(db).SetID(oldRecordsMap[productId].Id).UpdateByMap(m)
+				err = models.NewMonthStatsSearch().SetOrm(db).SetID(oldRecordsMap[record.ProductId].Id).UpdateByMap(m)
 				if err != nil {
-					logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[productId].ID, m)
+					logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[record.ProductId].Id, m)
 					service.SendAlarm("鏈堝害缁熻鏇存敼涓婃湀澶辫触", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record))
 				}
 			}
@@ -157,41 +86,4 @@
 		service.SendAlarm("鏈堝害缁熻鎵ц鎴愬姛", "")
 	}
 	return
-}
-
-func GetStatsByOperationType(beginTime, endTime time.Time, operationType constvar.BaseOperationType) (m map[string]decimal.Decimal, 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")
-	if err != nil {
-		return
-	}
-	m = make(map[string]decimal.Decimal, len(groupSumList))
-	for _, v := range groupSumList {
-		m[v.Class] = v.Sum
-	}
-	return
-}
-
-// GetLastMonthPeriod 杩斿洖涓婁釜鏈堢殑鏈堝垵鏃堕棿鍜屾湀鏈椂闂�
-func GetLastMonthPeriod() (time.Time, time.Time) {
-	// 鑾峰彇褰撳墠鏃堕棿
-	now := time.Now()
-
-	// 璁$畻涓婁釜鏈堢殑骞翠唤鍜屾湀浠�
-	lastMonth := now.AddDate(0, -1, 0)
-	lastYear, lastMonthNum, _ := lastMonth.Date()
-
-	// 鑾峰彇涓婁釜鏈堢殑绗竴澶╃殑鏃ユ湡锛堝嵆涓婁釜鏈堟湀鍒濓級
-	firstDayOfLastMonth := time.Date(lastYear, lastMonthNum, 1, 0, 0, 0, 0, now.Location())
-
-	// 鑾峰彇鏈湀绗竴澶╃殑鏃ユ湡锛堝嵆鏈湀鏈堝垵锛�
-	firstDayOfThisMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
-
-	// 涓婁釜鏈堟湀鏈椂闂村嵆涓烘湰鏈堟湀鍒濆噺鍘讳竴绉�
-	lastDayOfLastMonth := firstDayOfThisMonth.Add(-time.Second)
-
-	return firstDayOfLastMonth, lastDayOfLastMonth
 }

--
Gitblit v1.8.0