| | |
| | | package models |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | |
| | | 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 { |
| | |
| | | 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 { |
| | |
| | | |
| | | 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 { |
| | |
| | | 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{}) |
| | | |
| | |
| | | } |
| | | |
| | | 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"). |
| | |
| | | |
| | | 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 |
| | |
| | | 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) |