From fc3313955a083c9480e4ea74398f72f9ba6addcd Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 01 八月 2024 20:29:51 +0800 Subject: [PATCH] 月度统计查询多单位数据计算改查询。 --- models/location_product_amount.go | 78 ++++++++++++++++++++++++++++++++++++--- 1 files changed, 72 insertions(+), 6 deletions(-) diff --git a/models/location_product_amount.go b/models/location_product_amount.go index 2c59969..279ab69 100644 --- a/models/location_product_amount.go +++ b/models/location_product_amount.go @@ -1,6 +1,7 @@ package models import ( + "encoding/json" "fmt" "github.com/shopspring/decimal" "gorm.io/gorm" @@ -21,7 +22,9 @@ ProductId string `json:"productId" gorm:"type:varchar(191);not null;comment:浜у搧id"` //浜у搧id Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"` Amount decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:搴撳瓨鏁伴噺"` //搴撳瓨鏁伴噺 - CreateDate string `json:"createDate" gorm:"type:varchar(63);comment:鏃ユ湡"` //鏃ユ湡 + MoreUnitList []UnitItems `json:"amountMoreUnits" gorm:"-"` //鍦ㄥ簱鏁伴噺澶氬崟浣� + MoreUnitValue string `json:"-" gorm:"type:varchar(255);comment:澶氬崟浣嶅��"` + CreateDate string `json:"createDate" gorm:"type:varchar(63);comment:鏃ユ湡"` //鏃ユ湡 } LocationProductAmountSearch struct { @@ -33,11 +36,13 @@ Orm *gorm.DB Preload bool //LocationProductIds []int - LocationIds []int - ProductIds []string - Ids []int - Query string - Fields string + LocationIds []int + ProductIds []string + Ids []int + Query string + Fields string + CategoryIds []int + JoinedMaterials bool } LocationProductAmountWithOperation struct { @@ -62,6 +67,37 @@ func (slf *LocationProductAmount) TableName() string { return "wms_location_product_amount" +} + +func (slf *LocationProductAmount) AfterFind(tx *gorm.DB) (err error) { + if slf.MoreUnitValue != "" { + var arr []UnitItems + err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr) + if err != nil { + return err + } + slf.MoreUnitList = arr + } + + return +} + +func (slf *LocationProductAmount) BeforeCreate(tx *gorm.DB) (err error) { + if len(slf.MoreUnitList) != 0 { + items := make([]UnitItems, 0) + for k, item := range slf.MoreUnitList { + if item.Unit != "" && !item.Amount.IsZero() { + items = append(items, slf.MoreUnitList[k]) + } + } + + str, err := json.Marshal(items) + if err != nil { + return err + } + slf.MoreUnitValue = string(str) + } + return } func NewLocationProductAmountSearch() *LocationProductAmountSearch { @@ -138,6 +174,11 @@ return slf } +func (slf *LocationProductAmountSearch) SetCategoryIds(ids []int) *LocationProductAmountSearch { + slf.CategoryIds = ids + return slf +} + func (slf *LocationProductAmountSearch) build() *gorm.DB { var db = slf.Orm.Model(&LocationProductAmount{}) @@ -149,6 +190,10 @@ } if slf.Keyword != "" { + if !slf.JoinedMaterials { + db = db.Joins("left join material on wms_location_product_amount.product_id = material.id") + slf.JoinedMaterials = true + } db = db.Joins("left join wms_location on wms_location_product_amount.location_id = wms_location.id"). Joins("left join material on wms_location_product_amount.product_id = material.id"). Joins("left join wms_product_category on wms_location_product_amount.product_category_id = wms_product_category.id"). @@ -190,6 +235,14 @@ if slf.Fields != "" { db = db.Select(slf.Fields) + } + + if len(slf.CategoryIds) > 0 { + if !slf.JoinedMaterials { + db = db.Joins("left join material on wms_location_product_amount.product_id = material.id") + slf.JoinedMaterials = true + } + db = db.Where("material.category_id in ?", slf.CategoryIds) } return db @@ -281,6 +334,19 @@ return records, nil } +func (slf *LocationProductAmountSearch) FindAll() ([]*LocationProductAmount, error) { + var ( + records = make([]*LocationProductAmount, 0) + db = slf.build() + ) + + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find records err: %v", err) + } + + return records, nil +} + func (slf *LocationProductAmountSearch) FirstRes() (*LocationProductAmount, *gorm.DB) { var ( record = new(LocationProductAmount) -- Gitblit v1.8.0