| | |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | "wms/constvar" |
| | | "wms/models" |
| | |
| | | "wms/service" |
| | | ) |
| | | |
| | | func MonthStats() { |
| | | func MonthStats() (err error) { |
| | | //加锁,只需要一个进程运行此任务 |
| | | var ( |
| | | lockName = "monthStats" |
| | | serviceID = "wms" |
| | | ) |
| | | err := models.NewLockSearch().AcquireLock(lockName, serviceID) |
| | | err = models.NewLockSearch().AcquireLock(lockName, serviceID) |
| | | if err != nil { |
| | | logx.Errorf("MonthStats AcquireLock err:%v", err) |
| | | return |
| | | } |
| | | defer func() { |
| | | err := models.NewLockSearch().ReleaseLock(lockName, serviceID) |
| | | if err != nil { |
| | | service.SendAlarm("月度统计执行失败", "报错: "+err.Error()) |
| | | } |
| | | err = models.NewLockSearch().ReleaseLock(lockName, serviceID) |
| | | if err != nil { |
| | | logx.Errorf("MonthStats ReleaseLock err:%v", err) |
| | | service.SendAlarm("月度统计执行成功解锁失败", err.Error()) |
| | | } |
| | | }() |
| | | |
| | |
| | | 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 { |
| | |
| | | inputMoreUnits string |
| | | outputMoreUnits string |
| | | ) |
| | | if product.MoreUnit { |
| | | 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)) |
| | | for _, unitItem := range product.MoreUnitList { |
| | | moreValueArr = append(moreValueArr, models.UnitItems{ |
| | | Amount: amount.Mul(unitItem.Amount), |
| | | Unit: unitItem.Unit, |
| | | Floating: unitItem.Floating, |
| | | }) |
| | | bys, _ := json.Marshal(moreValueArr) |
| | | moreUnits = string(bys) |
| | | if !amount.IsZero() { |
| | | moreValueArr = append(moreValueArr, models.UnitItems{ |
| | | Amount: amount.Mul(unitItem.Amount), |
| | | Unit: unitItem.Unit, |
| | | Floating: unitItem.Floating, |
| | | }) |
| | | } |
| | | |
| | | if !inputMap[productId].IsZero() { |
| | | inputMoreValueArr = append(inputMoreValueArr, models.UnitItems{ |
| | |
| | | Unit: unitItem.Unit, |
| | | Floating: unitItem.Floating, |
| | | }) |
| | | bys, _ = json.Marshal(inputMoreValueArr) |
| | | inputMoreUnits = string(bys) |
| | | |
| | | } |
| | | |
| | | if !outputMap[productId].IsZero() { |
| | |
| | | Unit: unitItem.Unit, |
| | | Floating: unitItem.Floating, |
| | | }) |
| | | bys, _ = json.Marshal(outputMoreValueArr) |
| | | outputMoreUnits = string(bys) |
| | | |
| | | } |
| | | } |
| | | bys, _ := json.Marshal(moreValueArr) |
| | | if len(moreValueArr) > 0 { |
| | | fmt.Println(moreValueArr) |
| | | } |
| | | moreUnits = string(bys) |
| | | bys, _ = json.Marshal(inputMoreValueArr) |
| | | inputMoreUnits = string(bys) |
| | | bys, _ = json.Marshal(outputMoreValueArr) |
| | | outputMoreUnits = string(bys) |
| | | } |
| | | |
| | | record.BeginMoreUnits = moreUnits |
| | | err = models.NewMonthStatsSearch().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 && (!inputMap[productId].IsZero() || !outputMap[productId].IsZero()) { |
| | | record.InputAmount = inputMap[productId] |
| | | record.InputMoreUnits = inputMoreUnits |
| | | record.OutputAmount = outputMap[productId] |
| | | record.OutputMoreUnits = outputMoreUnits |
| | | m := map[string]interface{}{ |
| | | "input_amount": inputMap[productId], |
| | | "input_more_units": inputMoreUnits, |
| | | "output_amount": outputMap[productId], |
| | | "output_more_units": outputMoreUnits, |
| | | "end_more_units": moreUnits, |
| | | "end_amount": amount, |
| | | } |
| | | err = models.NewMonthStatsSearch().SetID(oldRecordsMap[productId].Id).UpdateByMap(m) |
| | | err = models.WithTransaction(func(db *gorm.DB) error { |
| | | err = models.NewMonthStatsSearch().SetOrm(db).Create(&record) |
| | | if err != nil { |
| | | logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[productId].ID, m) |
| | | service.SendAlarm("月度统计更改上月失败", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record)) |
| | | 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 { |
| | | m := map[string]interface{}{ |
| | | "end_more_units": moreUnits, |
| | | "end_amount": amount, |
| | | } |
| | | 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 |
| | | } |
| | | err = models.NewMonthStatsSearch().SetOrm(db).SetID(oldRecordsMap[productId].Id).UpdateByMap(m) |
| | | if err != nil { |
| | | logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[productId].ID, m) |
| | | service.SendAlarm("月度统计更改上月失败", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record)) |
| | | } |
| | | } |
| | | return nil |
| | | }) |
| | | } |
| | | if err == nil { |
| | | service.SendAlarm("月度统计执行成功", "") |
| | | } |
| | | return |
| | | } |