feat: 删除植物和删除养护事项
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,6 +19,13 @@ 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"`
|
||||
@@ -28,6 +35,7 @@ type UpdateMyPlant struct {
|
||||
Placement string `json:"placement"` // 摆放位置
|
||||
Sunlight string `json:"sunlight"` // 光照条件如每日12小时
|
||||
PlantingMaterial string `json:"plantingMaterial"` // 植料(即土的材质)
|
||||
CarePlans []*UpdatePlan `json:"carePlans"`
|
||||
}
|
||||
|
||||
type CompleteTask struct {
|
||||
|
||||
@@ -10,9 +10,11 @@ func (c *MyPlantRouter) InitPlantRouter(Router *gin.RouterGroup) {
|
||||
myPlantRouter := Router.Group("plant")
|
||||
{
|
||||
myPlantRouter.POST("add", myPlantApi.AddPlant) // 添加植物
|
||||
myPlantRouter.POST("page", myPlantApi.PlantPage) // 分页获取植物
|
||||
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) // 完成任务
|
||||
|
||||
@@ -111,6 +111,7 @@ 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 {
|
||||
// 以map形式更新 先构建map
|
||||
updateMap := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
@@ -120,7 +121,29 @@ func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
|
||||
"pot_size": req.PotSize,
|
||||
"sunlight": req.Sunlight,
|
||||
}
|
||||
return global.DB.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error
|
||||
//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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user