package models import ( "fmt" "github.com/shopspring/decimal" "gorm.io/gorm" "wms/constvar" "wms/pkg/mysqlx" ) type ( LocationProductAmount struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` LocationProductId int `json:"locationProductId" gorm:"type:int;not null;comment:上架规则id"` //上架规则id LocationProduct LocationProduct `json:"locationProduct" gorm:"foreignKey:LocationProductId;references:Id"` Amount decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:库存数量"` //库存数量 CreateDate string `json:"createDate" gorm:"type:varchar(63);comment:日期"` //日期 } LocationProductAmountSearch struct { LocationProductAmount Order string PageNum int PageSize int Keyword string Orm *gorm.DB Preload bool } LocationProductAmountWithOperation struct { //LocationProductAmount LocationProductAmount `json:"locationProductAmount"` LocationProductAmountId int `json:"locationProductAmountId" gorm:"location_product_amount_id"` LocationId int `json:"locationId" gorm:"column:location_id"` LocationName string `json:"locationName" gorm:"column:location_name"` ProductId string `json:"productId" gorm:"column:product_id"` ProductName string `json:"productName" gorm:"column:product_name"` Amount decimal.Decimal `json:"amount" gorm:"column:amount"` Unit string `json:"unit" gorm:"column:unit"` CreateDate string `json:"createDate" gorm:"column:create_date"` AdjustAmount decimal.Decimal `json:"adjustAmount" gorm:"column:adjust_amount"` DifferenceAmount decimal.Decimal `json:"differenceAmount" gorm:"-"` OperationId int `json:"operationId" gorm:"column:operation_id"` Status constvar.OperationStatus `json:"status" gorm:"status"` } ) func (slf *LocationProductAmount) TableName() string { return "wms_location_product_amount" } func NewLocationProductAmountSearch() *LocationProductAmountSearch { return &LocationProductAmountSearch{Orm: mysqlx.GetDB()} } func (slf *LocationProductAmountSearch) SetOrm(tx *gorm.DB) *LocationProductAmountSearch { slf.Orm = tx return slf } func (slf *LocationProductAmountSearch) SetPage(page, size int) *LocationProductAmountSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *LocationProductAmountSearch) SetOrder(order string) *LocationProductAmountSearch { slf.Order = order return slf } func (slf *LocationProductAmountSearch) SetID(id int) *LocationProductAmountSearch { slf.Id = id return slf } func (slf *LocationProductAmountSearch) SetKeyword(keyword string) *LocationProductAmountSearch { slf.Keyword = keyword return slf } func (slf *LocationProductAmountSearch) SetPreload(preload bool) *LocationProductAmountSearch { slf.Preload = preload return slf } func (slf *LocationProductAmountSearch) SetLocationProductId(id int) *LocationProductAmountSearch { slf.LocationProductId = id return slf } func (slf *LocationProductAmountSearch) build() *gorm.DB { var db = slf.Orm.Model(&LocationProductAmount{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if slf.Order != "" { db = db.Order(slf.Order) } if slf.Keyword != "" { db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) } if slf.Preload { db = db.Model(&LocationProductAmount{}).Preload("LocationProduct").Preload("LocationProduct.Location").Preload("LocationProduct.Product") } if slf.LocationProductId != 0 { db = db.Where("location_product_id=?", slf.LocationProductId) } return db } // Create 单条插入 func (slf *LocationProductAmountSearch) Create(record *LocationProductAmount) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return err } return nil } // CreateBatch 批量插入 func (slf *LocationProductAmountSearch) CreateBatch(records []*LocationProductAmount) error { var db = slf.build() if err := db.Create(&records).Error; err != nil { return fmt.Errorf("create batch err: %v, records: %+v", err, records) } return nil } func (slf *LocationProductAmountSearch) Update(record *LocationProductAmount) error { var db = slf.build() if err := db.Omit("CreatedAt").Updates(record).Error; err != nil { return fmt.Errorf("save err: %v, record: %+v", err, record) } return nil } func (slf *LocationProductAmountSearch) Delete() error { var db = slf.build() return db.Unscoped().Delete(&LocationProductAmount{}).Error } func (slf *LocationProductAmountSearch) First() (*LocationProductAmount, error) { var ( record = new(LocationProductAmount) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *LocationProductAmountSearch) FindByPage() ([]*LocationProductAmount, int64, error) { var ( records = make([]*LocationProductAmount, 0) total int64 db = slf.build() ) if err := db.Count(&total).Error; err != nil { return records, total, fmt.Errorf("find count err: %v", err) } if slf.PageNum*slf.PageSize > 0 { db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) } if err := db.Find(&records).Error; err != nil { return records, total, fmt.Errorf("find records err: %v", err) } return records, total, nil } func (slf *LocationProductAmountSearch) Find() ([]*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 }