package models import ( "fmt" "github.com/shopspring/decimal" "gorm.io/gorm" "wms/pkg/mysqlx" ) type ( ReorderRule struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` ProductId string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"` LocationId int `json:"locationId" gorm:"type:int;not null;comment:位置id"` //位置id Location Location `json:"location" gorm:"foreignKey:LocationId;references:id"` Route string `json:"route" gorm:"type:varchar(255);comment:路线"` //路线 MinInventory decimal.Decimal `json:"minInventory" gorm:"type:decimal(35,18);comment:最小库存"` //最小库存 MaxInventory decimal.Decimal `json:"maxInventory" gorm:"type:decimal(35,18);comment:最大库存"` //最大库存 OrderNumber decimal.Decimal `json:"orderNumber" gorm:"type:decimal(35,18);comment:订购数量"` //订购数量 Unit string `json:"unit" gorm:"type:varchar(100);comment:单位"` //单位 Amount decimal.Decimal `json:"amount" gorm:"-"` //在库数量 Prediction decimal.Decimal `json:"prediction" gorm:"-"` //预测数量 SupplierId int64 `json:"supplierId" gorm:"-"` //供应商id } ReorderRuleSearch struct { ReorderRule PageNum int PageSize int Keyword string Orm *gorm.DB Preload bool } ) func (slf *ReorderRule) TableName() string { return "wms_reorder_rule" } func NewReorderRuleSearch() *ReorderRuleSearch { return &ReorderRuleSearch{Orm: mysqlx.GetDB()} } func (slf *ReorderRuleSearch) SetOrm(tx *gorm.DB) *ReorderRuleSearch { slf.Orm = tx return slf } func (slf *ReorderRuleSearch) SetPage(page, size int) *ReorderRuleSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *ReorderRuleSearch) SetID(ID int) *ReorderRuleSearch { slf.Id = ID return slf } func (slf *ReorderRuleSearch) SetKeyword(keyword string) *ReorderRuleSearch { slf.Keyword = keyword return slf } func (slf *ReorderRuleSearch) SetProductId(productId string) *ReorderRuleSearch { slf.ProductId = productId return slf } func (slf *ReorderRuleSearch) SetLocationId(locationId int) *ReorderRuleSearch { slf.LocationId = locationId return slf } func (slf *ReorderRuleSearch) SetPreload(preload bool) *ReorderRuleSearch { slf.Preload = preload return slf } func (slf *ReorderRuleSearch) build() *gorm.DB { var db = slf.Orm.Model(&ReorderRule{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if slf.ProductId != "" { db = db.Where("product_id = ?", slf.ProductId) } if slf.LocationId != 0 { db = db.Where("location_id = ?", slf.LocationId) } if slf.Preload { db = db.Preload("Product").Preload("Location") } return db } // Create 单条插入 func (slf *ReorderRuleSearch) Create(record *ReorderRule) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return err } return nil } func (slf *ReorderRuleSearch) Update(record *ReorderRule) error { var db = slf.build() if err := db.Omit("CreatedAt").Updates(record).Error; err != nil { return fmt.Errorf("update err: %v, record: %+v", err, record) } return nil } func (slf *ReorderRuleSearch) Count() (int64, error) { var ( total int64 db = slf.build() ) err := db.Count(&total).Error return total, err } func (slf *ReorderRuleSearch) First() (*ReorderRule, error) { var ( record = new(ReorderRule) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *ReorderRuleSearch) Find() ([]*ReorderRule, int64, error) { var ( records = make([]*ReorderRule, 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.Order("created_at desc").Find(&records).Error; err != nil { return records, total, fmt.Errorf("find records err: %v", err) } return records, total, nil } func (slf *ReorderRuleSearch) Delete() error { var db = slf.build() return db.Delete(&ReorderRule{}).Error }