diff --git a/api/v1/plant/my_plant.go b/api/v1/plant/my_plant.go index 0baaa06..f064a5f 100644 --- a/api/v1/plant/my_plant.go +++ b/api/v1/plant/my_plant.go @@ -206,3 +206,47 @@ func (a *MyPlantApi) DeletePlans(c *gin.Context) { } response.OkWithMsg("删除任务成功", c) } + +// AddCarePlan 添加养护事项 +// @Tags 我的植物 +// @Summary 添加养护事项 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body plantReq.CreateCarePlan true "添加养护事项" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}" +// @Router /plant/plan/add [post] +func (a *MyPlantApi) AddCarePlan(c *gin.Context) { + var req plantReq.AddPlans + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = plantService.AddCarePlan(req) + if err != nil { + global.Logger.Error("添加任务失败", zap.Error(err)) + response.FailWithMsg("添加任务失败", c) + return + } + response.OkWithMsg("添加任务成功", c) +} + +// DeletePlan 删除养护事项 +// @Tags 我的植物 +// @Summary 删除养护事项 +// @Security BearerAuth +// @Produce application/json +// @Param id query string true "id" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" +// @Router /plant/plan/delete [get] +func (a *MyPlantApi) DeletePlan(c *gin.Context) { + id := c.Query("id") + err := plantService.DeletePlan(id) + if err != nil { + global.Logger.Error("删除任务失败", zap.Error(err)) + response.FailWithMsg("删除任务失败", c) + return + } + response.OkWithMsg("删除任务成功", c) +} diff --git a/api/v1/system/sys_user.go b/api/v1/system/sys_user.go index a0e4be4..c0f6824 100644 --- a/api/v1/system/sys_user.go +++ b/api/v1/system/sys_user.go @@ -6,6 +6,7 @@ import ( "sundynix-go/model/commom/response" "sundynix-go/model/system" systemReq "sundynix-go/model/system/request" + "sundynix-go/utils/auth" "github.com/gin-gonic/gin" "go.uber.org/zap" @@ -14,6 +15,24 @@ import ( type UserApi struct { } +// CurrentUser +// @tags 用户管理 +// @Summary 当前登录用户 +// @Security ApiKeyAuth +// @Produce json +// @Success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "添加成功"}" +// @Router /user/info [get] +func (u *UserApi) CurrentUser(c *gin.Context) { + userId := auth.GetUserId(c) + user, err := userService.GetUserById(userId) + if err != nil { + global.Logger.Error("获取用户信息失败!", zap.Error(err)) + response.FailWithMsg(err.Error(), c) + return + } + response.OkWithData(user, c) +} + // SaveUser // @tags 用户管理 // @Summary 新增用户 diff --git a/model/plant/request/my_plant.go b/model/plant/request/my_plant.go index b656211..88088dd 100644 --- a/model/plant/request/my_plant.go +++ b/model/plant/request/my_plant.go @@ -5,6 +5,15 @@ type CarePlan struct { Name string `json:"name"` // 农事名称 Period int `json:"period"` // 周期 } +type CreateCarePlan struct { + PlantId string `json:"plantId" binding:"required"` + Icon string `json:"icon"` // icon信息 + Name string `json:"name"` // 农事名称 + Period int `json:"period"` // 周期 +} +type AddPlans struct { + CarePlan []CreateCarePlan `json:"carePlan"` +} // CreateMyPlant 创建植物 type CreateMyPlant struct { diff --git a/router/plant/plant_router.go b/router/plant/plant_router.go index 161d9ea..f37922c 100644 --- a/router/plant/plant_router.go +++ b/router/plant/plant_router.go @@ -12,10 +12,15 @@ func (c *MyPlantRouter) InitPlantRouter(Router *gin.RouterGroup) { myPlantRouter.POST("add", myPlantApi.AddPlant) // 添加植物 myPlantRouter.POST("page", myPlantApi.PlantPage) // 分页获取我的植物 myPlantRouter.GET("detail", myPlantApi.PlantDetail) // 获取植物详情 - myPlantRouter.POST("update", myPlantApi.UpdatePlant) // 修改基本信息 + myPlantRouter.POST("update", myPlantApi.UpdatePlant) // 修改植物信息(可以修改养护事项) myPlantRouter.POST("deletePlant", myPlantApi.DeletePlants) // 删除植物 myPlantRouter.POST("deletePlan", myPlantApi.DeletePlans) // 删除计划 + //养护事项 + myPlantRouter.POST("/plan/add", myPlantApi.AddCarePlan) //添加养护计划 + myPlantRouter.GET("/plan/delete", myPlantApi.DeletePlan) //删除事项 + + //任务 myPlantRouter.GET("todayTask", myPlantApi.TodayTask) // 获取今日任务 myPlantRouter.POST("completeTask", myPlantApi.CompleteTask) // 完成任务 diff --git a/router/system/user_router.go b/router/system/user_router.go index c7fc885..adf1b9e 100644 --- a/router/system/user_router.go +++ b/router/system/user_router.go @@ -10,6 +10,7 @@ type UserRouter struct { func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) { userRouter := Router.Group("user") { + userRouter.POST("info", userApi.CurrentUser) userRouter.POST("save", userApi.SaveUser) userRouter.POST("update", userApi.UpdateUser) userRouter.POST("getUserList", userApi.GetUserList) diff --git a/service/plant/my_plant.go b/service/plant/my_plant.go index 1c03e92..6a02887 100644 --- a/service/plant/my_plant.go +++ b/service/plant/my_plant.go @@ -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 + }) +}