| | |
| | | package service |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/xuri/excelize/v2" |
| | | "strconv" |
| | | "time" |
| | | "wms/constvar" |
| | | "wms/models" |
| | | "wms/pkg/logx" |
| | | "wms/request" |
| | | "wms/utils" |
| | | ) |
| | | |
| | | type MonthFormsService struct{} |
| | |
| | | |
| | | return fileName, nil |
| | | } |
| | | |
| | | func GetCurrentStats(date string, productIds []string) (statRecords []*models.MonthStats, err error) { |
| | | //本月期初数量/上月结余数量 |
| | | groupSumList, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).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() |
| | | if err != nil { |
| | | logx.Errorf("MonthStats GetCurrentStats get products err:%v", err) |
| | | return |
| | | } |
| | | productMap := models.MaterialMap(products) |
| | | |
| | | beginTime, endTime := utils.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 |
| | | } |
| | | |
| | | 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, |
| | | 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 = CreateMoreUnit(amount, product.MoreUnitList) |
| | | } |
| | | if !inputMap[productId].IsZero() { |
| | | inputMoreValueArr = CreateMoreUnit(inputMap[productId], product.MoreUnitList) |
| | | } |
| | | |
| | | if !outputMap[productId].IsZero() { |
| | | outputMoreValueArr = 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.InputAmount = inputMap[productId] |
| | | record.InputMoreUnits = inputMoreUnits |
| | | record.OutputAmount = outputMap[productId] |
| | | record.OutputMoreUnits = outputMoreUnits |
| | | record.EndAmount = amount |
| | | record.EndMoreUnits = moreUnits |
| | | statRecords = append(statRecords, &record) |
| | | } |
| | | |
| | | 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 |
| | | } |