From 35d641adccfd2aa2bcd0090415ff7fdb18e9773d Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 01 八月 2024 14:40:56 +0800 Subject: [PATCH] 库存报表多单位数据查询修正 --- service/more_units.go | 31 +++++++++++++++++++++---------- 1 files changed, 21 insertions(+), 10 deletions(-) diff --git a/service/more_units.go b/service/more_units.go index b141c17..f4bfe42 100644 --- a/service/more_units.go +++ b/service/more_units.go @@ -24,18 +24,29 @@ } func AddMoreUnit(units []models.UnitItems, units2 []models.UnitItems) []models.UnitItems { - moreValueArr := make([]models.UnitItems, 0, len(units)) - for _, unitItem1 := range units { - for _, unitItem2 := range units2 { - if unitItem1.Unit == unitItem2.Unit { - moreValueArr = append(moreValueArr, models.UnitItems{ - Amount: unitItem1.Amount.Add(unitItem2.Amount), - Unit: unitItem1.Unit, - Floating: unitItem1.Floating, - }) - } + unitMap := make(map[string]models.UnitItems) + + // 灏� units 涓殑鎵�鏈夊厓绱犳坊鍔犲埌 map 涓� + for _, unitItem := range units { + unitMap[unitItem.Unit] = unitItem + } + + // 閬嶅巻 units2锛屽悎骞剁浉鍚屽崟浣嶇殑鏁伴噺锛屽苟灏� units2 涓笉瀛樺湪浜� units 涓殑鍗曚綅娣诲姞鍒� map 涓� + for _, unitItem2 := range units2 { + if unitItem1, exists := unitMap[unitItem2.Unit]; exists { + unitItem1.Amount = unitItem1.Amount.Add(unitItem2.Amount) + unitMap[unitItem2.Unit] = unitItem1 + } else { + unitMap[unitItem2.Unit] = unitItem2 } } + + // 灏� map 杞崲鍥� slice + moreValueArr := make([]models.UnitItems, 0, len(unitMap)) + for _, unitItem := range unitMap { + moreValueArr = append(moreValueArr, unitItem) + } + return moreValueArr } -- Gitblit v1.8.0