From 37a64e6972df2b705670774047c65f9a0ce60ac1 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 03 七月 2024 16:15:10 +0800 Subject: [PATCH] 月度统计定时任务按设定的时间查询相应数据&修复若干bug --- models/warehouse_month_stats.go | 69 ++++++++++++++++++++++++++++++---- 1 files changed, 61 insertions(+), 8 deletions(-) diff --git a/models/warehouse_month_stats.go b/models/warehouse_month_stats.go index d0c43d0..4a829e9 100644 --- a/models/warehouse_month_stats.go +++ b/models/warehouse_month_stats.go @@ -21,19 +21,20 @@ BeginAmount decimal.Decimal `json:"beginAmount" gorm:"type:decimal(30,10);not null;comment:鏁伴噺"` //鏈熷垵鏁伴噺 EndAmount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;comment:鏁伴噺"` //鏈熸湯缁撲綑鏁伴噺 - InputAmount decimal.Decimal `json:"inputAmount" gorm:"type:decimal(30,10);not null;comment:鏁伴噺"` //鍏ュ簱鏁伴噺 - InputItems []*WarehouseStatsItems `json:"inputItems"` //鍏ュ簱鏄庣粏 - + InputAmount decimal.Decimal `json:"inputAmount" gorm:"type:decimal(30,10);not null;comment:鏁伴噺"` //鍏ュ簱鏁伴噺 + InputItems []*WarehouseStatsItems `json:"inputItems" gorm:"-"` //鍏ュ簱鏄庣粏 + Items []*WarehouseStatsItems `json:"-"` OutputAmount decimal.Decimal `json:"outputAmount" gorm:"type:decimal(30,10);not null;comment:鏁伴噺"` //鍑哄簱鏁伴噺 - OutputItems []*WarehouseStatsItems `json:"outputItems"` //鍑哄簱鏄庣粏 + OutputItems []*WarehouseStatsItems `json:"outputItems" gorm:"-"` //鍑哄簱鏄庣粏 Date string `json:"date" gorm:"index;type:varchar(255); not null;default ''"` //鏃ユ湡 2024-04 } WarehouseStatsItems struct { - WarehouseMonthStatsId int `json:"warehouseMonthStatsId"` - Name string `json:"name" gorm:"type:varchar(255);not null;default:''"` //鍏ュ簱鏉ユ簮锛屽嚭搴撳幓澶� - Amount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;"` //鏁伴噺 + WarehouseMonthStatsId int `json:"warehouseMonthStatsId"` + Type MonthStatsItemsType `json:"type" gorm:"type:tinyint;not null;default:1"` + Name string `json:"name" gorm:"type:varchar(255);not null;default:''"` //鍏ュ簱鏉ユ簮锛屽嚭搴撳幓澶� + Amount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;"` //鏁伴噺 } WarehouseMonthStatsSearch struct { @@ -48,12 +49,64 @@ } ) +type MonthStatsItemsType int + +const ( + MonthStatsItemsTypeInput MonthStatsItemsType = 1 //鍏ュ簱 + MonthStatsItemsTypeOutput MonthStatsItemsType = 2 //鍑哄簱 +) + func (slf *WarehouseStatsItems) TableName() string { return "wms_warehouse_month_stats_items" } func (slf *WarehouseMonthStats) TableName() string { return "wms_warehouse_month_stats" +} + +func (slf *WarehouseMonthStats) BeforeCreate(tx *gorm.DB) error { + if len(slf.InputItems) != 0 || len(slf.OutputItems) != 0 { + items := make([]*WarehouseStatsItems, 0, len(slf.InputItems)+len(slf.OutputItems)) + for _, item := range slf.InputItems { + items = append(items, &WarehouseStatsItems{ + Type: MonthStatsItemsTypeInput, + Name: item.Name, + Amount: item.Amount, + }) + } + + for _, item := range slf.OutputItems { + items = append(items, &WarehouseStatsItems{ + Type: MonthStatsItemsTypeOutput, + Name: item.Name, + Amount: item.Amount, + }) + } + + slf.Items = items + } + + return nil +} + +func (slf *WarehouseMonthStats) AfterFind(tx *gorm.DB) error { + if len(slf.Items) != 0 { + inputItems := make([]*WarehouseStatsItems, 0) + outputItems := make([]*WarehouseStatsItems, 0) + for _, v := range slf.Items { + item := WarehouseStatsItems{ + Type: v.Type, + Name: v.Name, + Amount: v.Amount, + } + if v.Type == MonthStatsItemsTypeInput { + inputItems = append(inputItems, &item) + } else { + outputItems = append(outputItems, &item) + } + } + } + return nil } func NewWarehouseMonthStatsSearch() *WarehouseMonthStatsSearch { @@ -144,7 +197,7 @@ } if slf.Preload { - db = db.Preload("InputItems").Preload("OutputItems") + db = db.Preload("Items") } return db -- Gitblit v1.8.0