From fc3313955a083c9480e4ea74398f72f9ba6addcd Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 01 八月 2024 20:29:51 +0800 Subject: [PATCH] 月度统计查询多单位数据计算改查询。 --- service/month_forms.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 49 insertions(+), 10 deletions(-) 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