From eae158f8d95df5f49c4e36d5b9ad00b62dbad9ec Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 29 七月 2024 21:51:47 +0800 Subject: [PATCH] 产品库存增加多单位存储,完成出入库操作时更改多单位剩余数量 --- models/location_product_amount.go | 36 +++++++++++++++++++++++++++++++++++- 1 files changed, 35 insertions(+), 1 deletions(-) diff --git a/models/location_product_amount.go b/models/location_product_amount.go index 2c59969..81cc55b 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 { @@ -64,6 +67,37 @@ 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 { return &LocationProductAmountSearch{Orm: mysqlx.GetDB()} } -- Gitblit v1.8.0