46 lines
1.6 KiB
Go
46 lines
1.6 KiB
Go
package model
|
|
|
|
import (
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
// Consumable 耗材
|
|
type Consumable struct {
|
|
ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
|
Name string `gorm:"size:64" json:"name"`
|
|
Category string `gorm:"size:32;index" json:"category"` // lamp_reagent/lamp_consumable/disinfectant/other
|
|
Spec *string `gorm:"size:128" json:"spec,omitempty"`
|
|
Quantity float64 `gorm:"type:float" json:"quantity"`
|
|
Unit *string `gorm:"size:32" json:"unit,omitempty"`
|
|
MinQuantity float64 `gorm:"column:min_quantity;type:float" json:"minQuantity"`
|
|
ExpiryDate *time.Time `gorm:"column:expiry_date;type:timestamptz" json:"expiryDate,omitempty"`
|
|
Supplier *string `gorm:"size:128" json:"supplier,omitempty"`
|
|
Note *string `gorm:"type:text" json:"note,omitempty"`
|
|
CreatedAt time.Time `gorm:"type:timestamptz" json:"createdAt"`
|
|
UpdatedAt time.Time `gorm:"type:timestamptz" json:"updatedAt"`
|
|
}
|
|
|
|
func (Consumable) TableName() string { return "consumables" }
|
|
|
|
// LowStockAlert 库存低于安全阈值
|
|
func (c Consumable) LowStockAlert() bool {
|
|
return c.Quantity < c.MinQuantity
|
|
}
|
|
|
|
// ExpiringAlert 效期在 days 天内到期(含已过期)
|
|
func (c Consumable) ExpiringAlert(days int) bool {
|
|
if c.ExpiryDate == nil {
|
|
return false
|
|
}
|
|
return !c.ExpiryDate.After(time.Now().Add(time.Duration(days) * 24 * time.Hour))
|
|
}
|
|
|
|
// PurchaseSuggestion 建议采购量(低于安全阈值时补足差额,向上取整)
|
|
func (c Consumable) PurchaseSuggestion() float64 {
|
|
if !c.LowStockAlert() {
|
|
return 0
|
|
}
|
|
return math.Ceil(c.MinQuantity - c.Quantity)
|
|
}
|