From f45e4cb8045fe60f96f467dc4ea1e2ae57445712 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 03 七月 2024 23:13:40 +0800
Subject: [PATCH] 默认位置parent_id为视图位置id

---
 models/warehouse_month_stats.go |  104 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 96 insertions(+), 8 deletions(-)

diff --git a/models/warehouse_month_stats.go b/models/warehouse_month_stats.go
index 72f494c..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:"inputMoreUnitsArr"`                                          //鍏ュ簱鏄庣粏
-
+		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:"outputMoreUnitsArr"`                                          //鍑哄簱鏄庣粏
+		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,8 +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_month_stats"
+	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 {
@@ -96,6 +153,21 @@
 	return slf
 }
 
+func (slf *WarehouseMonthStatsSearch) SetWarehouseId(id int) *WarehouseMonthStatsSearch {
+	slf.WarehouseId = id
+	return slf
+}
+
+func (slf *WarehouseMonthStatsSearch) Save(record *WarehouseMonthStats) error {
+	var db = slf.build()
+
+	if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
+		return fmt.Errorf("save err: %v, record: %+v", err, record)
+	}
+
+	return nil
+}
+
 func (slf *WarehouseMonthStatsSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&WarehouseMonthStats{})
 
@@ -118,6 +190,14 @@
 
 	if slf.Fields != "" {
 		db = db.Select(slf.Fields)
+	}
+
+	if slf.WarehouseId != 0 {
+		db = db.Where("warehouse_id = ?", slf.WarehouseId)
+	}
+
+	if slf.Preload {
+		db = db.Preload("Items")
 	}
 
 	return db
@@ -279,6 +359,14 @@
 	return m
 }
 
+func WarehouseStatsItemMap(records []*WarehouseStatsItems) (m map[string]*WarehouseStatsItems) {
+	m = make(map[string]*WarehouseStatsItems, len(records))
+	for _, record := range records {
+		m[record.Name] = record
+	}
+	return m
+}
+
 func (slf *WarehouseMonthStatsSearch) Count() (int64, error) {
 	var (
 		total int64

--
Gitblit v1.8.0