92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
// 物品分类
|
|
CategoryFood = "food"
|
|
CategoryMedicine = "medicine"
|
|
CategoryCosmetic = "cosmetic"
|
|
CategoryOther = "other"
|
|
)
|
|
|
|
const (
|
|
// 物品状态
|
|
StatusNormal = "normal"
|
|
StatusExpiring = "expiring"
|
|
StatusExpired = "expired"
|
|
StatusUsed = "used"
|
|
StatusDiscarded = "discarded"
|
|
)
|
|
|
|
var DefaultRemindDays = []int{7, 3, 1}
|
|
|
|
// ExpiryItem 表示保质期物品数据模型。
|
|
type ExpiryItem struct {
|
|
ID uint `json:"id" gorm:"primaryKey;comment:主键ID"`
|
|
UserID uint `json:"user_id" gorm:"not null;index:idx_user_expiry,priority:1;index:idx_user_category,priority:1;index:idx_user_status,priority:1;comment:用户ID"`
|
|
MiniProgramID uint `json:"mini_program_id" gorm:"not null;comment:小程序ID"`
|
|
Name string `json:"name" gorm:"size:100;not null;comment:物品名称"`
|
|
Category string `json:"category" gorm:"size:20;not null;index:idx_user_category,priority:2;comment:分类"`
|
|
ProductionDate *time.Time `json:"production_date" gorm:"type:date;comment:生产日期"`
|
|
ExpiryDate time.Time `json:"expiry_date" gorm:"type:date;not null;index:idx_user_expiry,priority:2;comment:过期日期"`
|
|
ShelfLifeDays *int `json:"shelf_life_days" gorm:"comment:保质期天数"`
|
|
Quantity int `json:"quantity" gorm:"default:1;comment:数量"`
|
|
Location string `json:"location" gorm:"size:50;comment:存放位置"`
|
|
Remark string `json:"remark" gorm:"size:255;comment:备注"`
|
|
Status string `json:"status" gorm:"size:20;default:'normal';index:idx_user_status,priority:2;comment:状态"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"comment:创建时间"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"comment:更新时间"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index:idx_deleted_at;comment:删除时间"`
|
|
}
|
|
|
|
func (ExpiryItem) TableName() string {
|
|
return "expiry_items"
|
|
}
|
|
|
|
// CalculateDaysLeft 计算当前日期与过期日期之间的天数差。
|
|
func (item *ExpiryItem) CalculateDaysLeft() int {
|
|
now := DateOnly(time.Now())
|
|
expiry := DateOnly(item.ExpiryDate)
|
|
return int(expiry.Sub(now).Hours() / 24)
|
|
}
|
|
|
|
// CalculateStatus 根据过期日期动态计算状态。
|
|
// 若已被业务标记为 used/discarded,则保留该状态。
|
|
func (item *ExpiryItem) CalculateStatus() string {
|
|
if item.Status == StatusUsed || item.Status == StatusDiscarded {
|
|
return item.Status
|
|
}
|
|
|
|
daysLeft := item.CalculateDaysLeft()
|
|
if daysLeft < 0 {
|
|
return StatusExpired
|
|
}
|
|
if daysLeft <= 7 {
|
|
return StatusExpiring
|
|
}
|
|
return StatusNormal
|
|
}
|
|
|
|
// ExpiryUserSettings 表示用户提醒设置。
|
|
type ExpiryUserSettings struct {
|
|
ID uint `json:"id" gorm:"primaryKey;comment:主键ID"`
|
|
UserID uint `json:"user_id" gorm:"not null;uniqueIndex;comment:用户ID"`
|
|
RemindDays []int `json:"remind_days" gorm:"type:json;serializer:json;comment:提醒天数"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"comment:创建时间"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"comment:更新时间"`
|
|
}
|
|
|
|
func (ExpiryUserSettings) TableName() string {
|
|
return "expiry_user_settings"
|
|
}
|
|
|
|
func DateOnly(t time.Time) time.Time {
|
|
year, month, day := t.In(time.Local).Date()
|
|
return time.Date(year, month, day, 0, 0, 0, 0, time.Local)
|
|
}
|