feat: 成长记录
This commit is contained in:
@@ -250,3 +250,29 @@ func (a *MyPlantApi) DeletePlan(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
response.OkWithMsg("删除任务成功", c)
|
response.OkWithMsg("删除任务成功", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddGrowthRecord 添加成长记录
|
||||||
|
// @Tags 我的植物
|
||||||
|
// @Summary 添加成长记录
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @accept json
|
||||||
|
// @Produce application/json
|
||||||
|
// @Param data body plantReq.CreateGrowthRecord true "添加成长记录"
|
||||||
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
|
||||||
|
// @Router /plant/growth/add [post]
|
||||||
|
func (a *MyPlantApi) AddGrowthRecord(c *gin.Context) {
|
||||||
|
var req plantReq.CreateGrowthRecord
|
||||||
|
err := c.ShouldBindJSON(&req)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg("请求参数错误", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userId := auth.GetUserId(c)
|
||||||
|
err = plantService.AddGrowthRecord(req, userId)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("添加成长记录失败", zap.Error(err))
|
||||||
|
response.FailWithMsg("添加成长记录失败", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("添加成长记录成功", c)
|
||||||
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ func MigrateTable() {
|
|||||||
plant.CarePlan{}, //植物养护计划
|
plant.CarePlan{}, //植物养护计划
|
||||||
plant.CareTask{}, //植物养护任务
|
plant.CareTask{}, //植物养护任务
|
||||||
plant.CareRecord{}, //植物养护记录
|
plant.CareRecord{}, //植物养护记录
|
||||||
|
plant.GrowthRecord{}, //植物成长记录
|
||||||
plant.Topic{}, //帖子话题
|
plant.Topic{}, //帖子话题
|
||||||
plant.Post{}, //帖子
|
plant.Post{}, //帖子
|
||||||
plant.PostLike{}, //帖子点赞
|
plant.PostLike{}, //帖子点赞
|
||||||
|
|||||||
@@ -22,10 +22,11 @@ type MyPlant struct {
|
|||||||
Sunlight string `json:"sunlight" form:"sunlight" gorm:"column:sunlight;size:50;comment:光照条件"` // 如每日12小时
|
Sunlight string `json:"sunlight" form:"sunlight" gorm:"column:sunlight;size:50;comment:光照条件"` // 如每日12小时
|
||||||
PlantingMaterial string `json:"plantingMaterial" form:"plantingMaterial" gorm:"column:planting_material;size:50;comment:植料"`
|
PlantingMaterial string `json:"plantingMaterial" form:"plantingMaterial" gorm:"column:planting_material;size:50;comment:植料"`
|
||||||
|
|
||||||
ImgList []*system.Oss `json:"imgList" form:"imgList" gorm:"many2many:my_plant_oss;comment:图片列表"`
|
ImgList []*system.Oss `json:"imgList" form:"imgList" gorm:"many2many:my_plant_oss;comment:图片列表"`
|
||||||
CarePlans []*CarePlan `json:"carePlans" form:"carePlans" gorm:"foreignKey:PlantId;comment:养护计划"`
|
CarePlans []*CarePlan `json:"carePlans" form:"carePlans" gorm:"foreignKey:PlantId;comment:养护计划"`
|
||||||
CareTasks []*CareTask `json:"careTasks" form:"careTasks" gorm:"foreignKey:PlantId;comment:养护任务"`
|
CareTasks []*CareTask `json:"careTasks" form:"careTasks" gorm:"foreignKey:PlantId;comment:养护任务"`
|
||||||
CareRecords []*CareRecord `json:"careRecords" form:"careRecords" gorm:"foreignKey:PlantId;comment:养护记录"`
|
CareRecords []*CareRecord `json:"careRecords" form:"careRecords" gorm:"foreignKey:PlantId;comment:养护记录"`
|
||||||
|
GrowthRecords []*GrowthRecord `json:"growthRecords" form:"growthRecords" gorm:"foreignKey:PlantId;comment:成长记录"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AfterCreate 钩子函数 创建plant时自动创建careTask
|
// AfterCreate 钩子函数 创建plant时自动创建careTask
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package plant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sundynix-go/global"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GrowthRecord struct {
|
||||||
|
global.BaseModel
|
||||||
|
PlantId string `json:"plantId" form:"plantId" gorm:"index;column:plant_id;comment:植物id"`
|
||||||
|
UserId string `json:"userId" form:"userId" gorm:"index;column:user_id;comment:用户id"`
|
||||||
|
Name string `json:"name" form:"name" gorm:"column:name;size:50;comment:名称"`
|
||||||
|
Tag string `json:"tag" form:"tag" gorm:"column:tag;size:50;comment:标签"`
|
||||||
|
Desc string `json:"desc" form:"desc" gorm:"column:desc;size:200;comment:描述"`
|
||||||
|
Content string `json:"content" form:"content" gorm:"column:content;size:200;comment:内容"`
|
||||||
|
//图片
|
||||||
|
ImgList []*system.Oss `json:"imgList" form:"imgList" gorm:"many2many:growth_record_oss;comment:图片列表"`
|
||||||
|
}
|
||||||
@@ -51,3 +51,12 @@ type CompleteTask struct {
|
|||||||
TaskId string `json:"taskId" binding:"required"`
|
TaskId string `json:"taskId" binding:"required"`
|
||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateGrowthRecord struct {
|
||||||
|
PlantId string `json:"plantId" binding:"required"`
|
||||||
|
OssIds []string `json:"ossIds"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
Desc string `json:"desc"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ func (c *MyPlantRouter) InitPlantRouter(Router *gin.RouterGroup) {
|
|||||||
myPlantRouter.GET("todayTask", myPlantApi.TodayTask) // 获取今日任务
|
myPlantRouter.GET("todayTask", myPlantApi.TodayTask) // 获取今日任务
|
||||||
myPlantRouter.POST("completeTask", myPlantApi.CompleteTask) // 完成任务
|
myPlantRouter.POST("completeTask", myPlantApi.CompleteTask) // 完成任务
|
||||||
|
|
||||||
|
//成长记录
|
||||||
|
myPlantRouter.POST("/growth/add", myPlantApi.AddGrowthRecord)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,11 @@ func (s *MyPlantService) PlantDetail(id string) (p plant.MyPlant, err error) {
|
|||||||
}).
|
}).
|
||||||
Preload("CarePlans").
|
Preload("CarePlans").
|
||||||
Preload("CareRecords").
|
Preload("CareRecords").
|
||||||
|
Preload("GrowthRecords", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Order("created_at desc")
|
||||||
|
})
|
||||||
|
}).
|
||||||
First(&res).Error
|
First(&res).Error
|
||||||
|
|
||||||
//不存在的时候不要返回错误,而是返回nil
|
//不存在的时候不要返回错误,而是返回nil
|
||||||
@@ -375,3 +380,44 @@ func (s *MyPlantService) DeletePlan(id string) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddGrowthRecord 添加成长记录
|
||||||
|
func (s *MyPlantService) AddGrowthRecord(req plantReq.CreateGrowthRecord, userId string) error {
|
||||||
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
//1.验证图片是否存在
|
||||||
|
var ossList []*system.Oss
|
||||||
|
err := tx.Where("id in ?", req.OssIds).Find(&ossList).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//2.保存记录
|
||||||
|
record := plant.GrowthRecord{
|
||||||
|
UserId: userId,
|
||||||
|
PlantId: req.PlantId,
|
||||||
|
Name: req.Name,
|
||||||
|
Content: req.Content,
|
||||||
|
Desc: req.Desc,
|
||||||
|
Tag: req.Tag,
|
||||||
|
}
|
||||||
|
err = tx.Create(&record).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//3.保存图片关系
|
||||||
|
if len(ossList) > 0 {
|
||||||
|
var relations []map[string]interface{}
|
||||||
|
for _, oss := range ossList {
|
||||||
|
relations = append(relations, map[string]interface{}{
|
||||||
|
"growth_record_id": record.Id,
|
||||||
|
"oss_id": oss.Id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
err = tx.Table("sundynix_growth_record_oss").Create(relations).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user