Files
sundynix-plant-be/model/plant/my_plant.go
T
2026-02-11 10:40:21 +08:00

53 lines
2.3 KiB
Go

package plant
import (
"sundynix-go/global"
"sundynix-go/model/system"
"sundynix-go/utils/timer"
"time"
"gorm.io/gorm"
)
// MyPlant 我的植物
type MyPlant struct {
global.BaseModel
UserId string `json:"userId" form:"userId" gorm:"size:50;column:user_id;comment:用户id"`
Name string `json:"name" form:"name" gorm:"size:20;column:name;comment:名称"`
PlantTime time.Time `json:"plantTime" form:"plantTime" gorm:"column:plant_time;comment:种植时间"`
Status int `json:"status" form:"status" gorm:"column:status;comment:生长状态"`
Placement string `json:"placement" form:"placement" gorm:"size:50;column:placement;comment:摆放位置"`
PotMaterial string `json:"potMaterial" form:"potMaterial" gorm:"column:pot_material;size:50;comment:花盆材质"`
PotSize string `json:"potSize" form:"potSize" gorm:"column:pot_size;size:50;comment:花盆大小"` // 花盆大小 如直径 20cm × 高度 18cm
Sunlight string `json:"sunlight" form:"sunlight" gorm:"column:sunlight;size:50;comment:光照条件"` // 如每日12小时
PlantingMaterial string `json:"plantingMaterial" form:"plantingMaterial" gorm:"column:planting_material;size:50;comment:植料"`
ImgList []*system.Oss `json:"imgList" form:"imgList" gorm:"many2many:my_plant_oss;comment:图片列表"`
CarePlans []*CarePlan `json:"carePlans" form:"carePlans" gorm:"foreignKey:PlantId;comment:养护计划"`
CareTasks []*CareTask `json:"careTasks" form:"careTasks" gorm:"foreignKey:PlantId;comment:养护任务"`
CareRecords []*CareRecord `json:"careRecords" form:"careRecords" gorm:"foreignKey:PlantId;comment:养护记录"`
GrowthRecords []*GrowthRecord `json:"growthRecords" form:"growthRecords" gorm:"foreignKey:PlantId;comment:成长记录"`
}
// AfterCreate 钩子函数 创建plant时自动创建careTask
func (p *MyPlant) AfterCreate(tx *gorm.DB) (err error) {
today := timer.GetZeroTime()
for _, v := range p.CarePlans {
dueDate := today.AddDate(0, 0, v.Period)
task := CareTask{
UserId: p.UserId,
PlantId: p.Id,
PlanId: v.Id,
Name: v.Name,
Icon: v.Icon,
DueDate: dueDate,
Status: 1,
}
err = tx.Create(&task).Error
if err != nil {
return err
}
}
return
}