feat: 删除植物和删除养护事项
This commit is contained in:
@@ -24,6 +24,7 @@ type MyPlant struct {
|
||||
|
||||
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:养护记录"`
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package plant
|
||||
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/utils/timer"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CarePlan 养护计划
|
||||
@@ -13,3 +16,30 @@ type CarePlan struct {
|
||||
Name string `json:"name"`
|
||||
Period int `json:"period" form:"period" gorm:"column:period;comment:周期"`
|
||||
}
|
||||
|
||||
// AfterUpdate 钩子函数 修改计划后重新生成任务
|
||||
func (p *CarePlan) AfterUpdate(tx *gorm.DB) error {
|
||||
//1.删除旧任务
|
||||
err := tx.Where("plan_id = ?", p.Id).Unscoped().Delete(&CareTask{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//2.创建新任务
|
||||
today := timer.GetZeroTime()
|
||||
dueDate := today.AddDate(0, 0, p.Period)
|
||||
task := CareTask{
|
||||
UserId: p.UserId,
|
||||
PlantId: p.Id,
|
||||
PlanId: p.Id,
|
||||
Name: p.Name,
|
||||
Icon: p.Icon,
|
||||
DueDate: dueDate,
|
||||
Status: 1,
|
||||
}
|
||||
err = tx.Create(&task).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
@@ -19,15 +19,23 @@ type CreateMyPlant struct {
|
||||
CarePlans []*CarePlan `json:"carePlans"` // 养护计划
|
||||
}
|
||||
|
||||
type UpdatePlan struct {
|
||||
Id string `json:"id" binding:"required"`
|
||||
Icon string `json:"icon"` // icon信息
|
||||
Name string `json:"name"` // 农事名称
|
||||
Period int `json:"period"` // 周期
|
||||
}
|
||||
|
||||
// UpdateMyPlant 修改植物
|
||||
type UpdateMyPlant struct {
|
||||
Id string `json:"id" binding:"required"`
|
||||
Name string `json:"name"` // 植物名称
|
||||
PotMaterial string `json:"potMaterial"` // 花盆材质
|
||||
PotSize string `json:"potSize"` // 花盆大小 如直径 20cm × 高度 18cm
|
||||
Placement string `json:"placement"` // 摆放位置
|
||||
Sunlight string `json:"sunlight"` // 光照条件如每日12小时
|
||||
PlantingMaterial string `json:"plantingMaterial"` // 植料(即土的材质)
|
||||
Id string `json:"id" binding:"required"`
|
||||
Name string `json:"name"` // 植物名称
|
||||
PotMaterial string `json:"potMaterial"` // 花盆材质
|
||||
PotSize string `json:"potSize"` // 花盆大小 如直径 20cm × 高度 18cm
|
||||
Placement string `json:"placement"` // 摆放位置
|
||||
Sunlight string `json:"sunlight"` // 光照条件如每日12小时
|
||||
PlantingMaterial string `json:"plantingMaterial"` // 植料(即土的材质)
|
||||
CarePlans []*UpdatePlan `json:"carePlans"`
|
||||
}
|
||||
|
||||
type CompleteTask struct {
|
||||
|
||||
Reference in New Issue
Block a user