diff --git a/api/v1/plant/my_plant.go b/api/v1/plant/my_plant.go index c24c770..6a12dae 100644 --- a/api/v1/plant/my_plant.go +++ b/api/v1/plant/my_plant.go @@ -158,3 +158,51 @@ func (a *MyPlantApi) CompleteTask(c *gin.Context) { } response.OkWithMsg("完成任务成功", c) } + +// DeletePlants +// @Tags 我的植物 +// @Summary 删除植物 +// @Security ApiKeyAuth +// @accept json +// @Produce application/json +// @Param data body request.IdsReq true "删除植物" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" +// @Router /plant/delete/ [post] +func (a *MyPlantApi) DeletePlants(c *gin.Context) { + var req request.IdsReq + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = plantService.DeletePlants(req) + if err != nil { + global.Logger.Error("删除植物失败", zap.Error(err)) + response.FailWithMsg("删除植物失败", c) + } + response.OkWithMsg("删除植物成功", c) +} + +// DeletePlans DeletePlants +// @Tags 我的植物 +// @Summary 删除任务 +// @Security ApiKeyAuth +// @accept json +// @Produce application/json +// @Param data body request.IdsReq true "删除植物" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" +// @Router /plant/deletePlant [post] +func (a *MyPlantApi) DeletePlans(c *gin.Context) { + var req request.IdsReq + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = plantService.DeletePlans(req) + if err != nil { + global.Logger.Error("删除任务失败", zap.Error(err)) + response.FailWithMsg("删除任务失败", c) + } + response.OkWithMsg("删除任务成功", c) +} diff --git a/model/plant/my_plant.go b/model/plant/my_plant.go index 4a38aaf..5ca670f 100644 --- a/model/plant/my_plant.go +++ b/model/plant/my_plant.go @@ -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:养护记录"` } diff --git a/model/plant/my_plant_care_plan.go b/model/plant/my_plant_care_plan.go index df10daa..a7d9882 100644 --- a/model/plant/my_plant_care_plan.go +++ b/model/plant/my_plant_care_plan.go @@ -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 + +} diff --git a/model/plant/request/my_plant.go b/model/plant/request/my_plant.go index 1b5b00c..b656211 100644 --- a/model/plant/request/my_plant.go +++ b/model/plant/request/my_plant.go @@ -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 { diff --git a/router/plant/plant_router.go b/router/plant/plant_router.go index 61b6a20..161d9ea 100644 --- a/router/plant/plant_router.go +++ b/router/plant/plant_router.go @@ -9,10 +9,12 @@ type MyPlantRouter struct{} func (c *MyPlantRouter) InitPlantRouter(Router *gin.RouterGroup) { myPlantRouter := Router.Group("plant") { - myPlantRouter.POST("add", myPlantApi.AddPlant) // 添加植物 - myPlantRouter.POST("page", myPlantApi.PlantPage) // 分页获取植物 - myPlantRouter.GET("detail", myPlantApi.PlantDetail) // 获取植物详情 - myPlantRouter.POST("update", myPlantApi.UpdatePlant) // 修改基本信息 + myPlantRouter.POST("add", myPlantApi.AddPlant) // 添加植物 + myPlantRouter.POST("page", myPlantApi.PlantPage) // 分页获取我的植物 + myPlantRouter.GET("detail", myPlantApi.PlantDetail) // 获取植物详情 + myPlantRouter.POST("update", myPlantApi.UpdatePlant) // 修改基本信息 + myPlantRouter.POST("deletePlant", myPlantApi.DeletePlants) // 删除植物 + myPlantRouter.POST("deletePlan", myPlantApi.DeletePlans) // 删除计划 myPlantRouter.GET("todayTask", myPlantApi.TodayTask) // 获取今日任务 myPlantRouter.POST("completeTask", myPlantApi.CompleteTask) // 完成任务 diff --git a/service/plant/my_plant.go b/service/plant/my_plant.go index a3d8474..7a089bc 100644 --- a/service/plant/my_plant.go +++ b/service/plant/my_plant.go @@ -111,16 +111,39 @@ func (s *MyPlantService) PlantDetail(id string) (p plant.MyPlant, err error) { } func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error { - // 以map形式更新 先构建map - updateMap := map[string]interface{}{ - "name": req.Name, - "placement": req.Placement, - "planting_material": req.PlantingMaterial, - "pot_material": req.PotMaterial, - "pot_size": req.PotSize, - "sunlight": req.Sunlight, - } - return global.DB.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error + return global.DB.Transaction(func(tx *gorm.DB) error { + // 以map形式更新 先构建map + updateMap := map[string]interface{}{ + "name": req.Name, + "placement": req.Placement, + "planting_material": req.PlantingMaterial, + "pot_material": req.PotMaterial, + "pot_size": req.PotSize, + "sunlight": req.Sunlight, + } + //1.修改基本信息 + err := tx.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error + if err != nil { + return err + } + //2.修改计划 + if len(req.CarePlans) > 0 { + //删除已经存在的计划 生成新的计划 + 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 + } + } + } + return nil + }) + } // TodayTask 今日任务 @@ -222,3 +245,44 @@ func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask) error { return nil }) } + +// DeletePlants 删除植物 +func (s *MyPlantService) DeletePlants(req common.IdsReq) error { + return global.DB.Transaction(func(tx *gorm.DB) error { + var plants []plant.MyPlant + if err := tx.Where("id in ?", req.Ids).Find(&plants).Error; err != nil { + return err + } + // 删除图片 养护计划 养护任务 养护记录 成长记录 + err := tx.Select("ImgList", "CarePlans", "CareTasks", "CareRecords").Unscoped().Delete(&plants).Error + if err != nil { + return err + } + return nil + }) +} + +// DeletePlans 删除任务 +func (s *MyPlantService) DeletePlans(req common.IdsReq) error { + return global.DB.Transaction(func(tx *gorm.DB) error { + var plans []plant.CarePlan + if err := tx.Where("id in ?", req.Ids).Find(&plans).Error; err != nil { + return err + } + var tasks []plant.CareTask + if err := tx.Where("plan_id in ?", req.Ids).Find(&tasks).Error; err != nil { + return err + } + //1.删除计划 + err := tx.Unscoped().Delete(&plans).Error + if err != nil { + return err + } + //2.删除任务 + err = tx.Unscoped().Delete(&tasks).Error + if err != nil { + return err + } + return nil + }) +}