feat: 添加和删除养护事项

This commit is contained in:
Blizzard
2026-02-10 14:57:53 +08:00
parent 556ab6baff
commit 01f8306be2
6 changed files with 172 additions and 4 deletions
+93 -3
View File
@@ -112,6 +112,11 @@ func (s *MyPlantService) PlantDetail(id string) (p plant.MyPlant, err error) {
func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
var myPlant plant.MyPlant
err := tx.Where("id = ?", req.Id).First(&myPlant).Error
if err != nil {
return err
}
// 以map形式更新 先构建map
updateMap := map[string]interface{}{
"name": req.Name,
@@ -122,19 +127,40 @@ func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
"sunlight": req.Sunlight,
}
//1.修改基本信息
err := tx.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error
err = tx.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error
if err != nil {
return err
}
//2.修改计划
if len(req.CarePlans) > 0 {
today := timer.GetZeroTime()
for _, plan := range req.CarePlans {
err = tx.Model(&plant.CarePlan{}).Where("id = ?", plan.Id).Updates(map[string]interface{}{
"icon": plan.Icon,
"name": plan.Name,
"period": plan.Period,
}).Error
//3.重新生成任务 使用钩子函数自动执行
if err != nil {
return err
}
//3.重新生成任务 CarePlans 结构体中使用钩子函数自动执行
//3.1 删除旧任务
err = tx.Where("plan_id = ?", plan.Id).Unscoped().Delete(&plant.CareTask{}).Error
if err != nil {
return err
}
//3.2 创建新任务
dueDate := today.AddDate(0, 0, plan.Period)
task := plant.CareTask{
UserId: myPlant.UserId,
PlantId: myPlant.Id,
PlanId: plan.Id,
Name: plan.Name,
Icon: plan.Icon,
DueDate: dueDate,
Status: 1,
}
err = tx.Create(&task).Error
if err != nil {
return err
}
@@ -142,7 +168,6 @@ func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
}
return nil
})
}
// TodayTask 今日任务
@@ -285,3 +310,68 @@ func (s *MyPlantService) DeletePlans(req common.IdsReq) error {
return nil
})
}
// AddCarePlan 添加CarePlan
func (s *MyPlantService) AddCarePlan(req plantReq.AddPlans) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
for _, plan := range req.CarePlan {
var myPlant plant.MyPlant
err := tx.Where("id = ?", plan.PlantId).First(&myPlant).Error
if err != nil {
return err
}
//1.新增计划
newPlan := plant.CarePlan{
UserId: myPlant.UserId,
PlantId: myPlant.Id,
Name: plan.Name,
Icon: plan.Icon,
Period: plan.Period,
}
err = tx.Create(&newPlan).Error
if err != nil {
return err
}
//2.新增任务
today := timer.GetZeroTime()
dueDate := today.AddDate(0, 0, plan.Period)
task := plant.CareTask{
UserId: myPlant.UserId,
PlantId: myPlant.Id,
PlanId: newPlan.Id,
Name: plan.Name,
Icon: plan.Icon,
DueDate: dueDate,
Status: 1,
}
err = tx.Create(&task).Error
if err != nil {
return err
}
}
return nil
})
}
// DeletePlan 删除CarePlan
func (s *MyPlantService) DeletePlan(id string) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
var plan plant.CarePlan
if err := tx.Where("id = ?", id).First(&plan).Error; err != nil {
return err
}
var tasks []plant.CareTask
if err := tx.Where("plan_id = ?", plan.Id).Find(&tasks).Error; err != nil {
return err
}
err := tx.Unscoped().Delete(&plan).Error
if err != nil {
return err
}
err = tx.Unscoped().Delete(&tasks).Error
if err != nil {
return err
}
return nil
})
}