| | |
| | | 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 |
| | |
| | | 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) |
| | |
| | | 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 |
| | | } |