feat: 快捷养护接口
This commit is contained in:
@@ -277,3 +277,28 @@ func (a *MyPlantApi) AddGrowthRecord(c *gin.Context) {
|
||||
}
|
||||
response.OkWithMsg("添加成长记录成功", c)
|
||||
}
|
||||
|
||||
// QuickCare 快捷养护记录
|
||||
// @Tags 我的植物
|
||||
// @Summary 快捷养护记录(无需预设任务)
|
||||
// @Security BearerAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body plantReq.QuickCare true "快捷养护"
|
||||
// @Router /plant/quickCare [post]
|
||||
func (a *MyPlantApi) QuickCare(c *gin.Context) {
|
||||
var req plantReq.QuickCare
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMsg("请求参数错误", c)
|
||||
return
|
||||
}
|
||||
userId := auth.GetUserId(c)
|
||||
err = plantService.QuickCare(req, userId)
|
||||
if err != nil {
|
||||
global.Logger.Error("快捷养护记录失败", zap.Error(err))
|
||||
response.FailWithMsg(err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("养护记录已保存", c)
|
||||
}
|
||||
|
||||
@@ -63,3 +63,11 @@ type CreateGrowthRecord struct {
|
||||
Desc string `json:"desc"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// QuickCare 快捷养护记录(无需预设任务)
|
||||
type QuickCare struct {
|
||||
PlantId string `json:"plantId" binding:"required"`
|
||||
Name string `json:"name" binding:"required"` // 养护名称(如 浇水、施肥、喷雾...)
|
||||
Icon string `json:"icon"` // icon JSON(和 CarePlan 的 icon 格式一致)
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ func (c *MyPlantRouter) InitPlantRouter(Router *gin.RouterGroup) {
|
||||
//成长记录
|
||||
myPlantRouter.POST("/growth/add", myPlantApi.AddGrowthRecord) // 添加成长记录
|
||||
|
||||
//快捷养护
|
||||
myPlantRouter.POST("quickCare", myPlantApi.QuickCare) // 快捷养护记录
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -552,6 +552,71 @@ func (s *MyPlantService) AddGrowthRecord(req plantReq.CreateGrowthRecord, userId
|
||||
})
|
||||
}
|
||||
|
||||
// QuickCare 快捷养护记录(无需预设任务,直接创建养护记录)
|
||||
func (s *MyPlantService) QuickCare(req plantReq.QuickCare, userId string) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// 1.验证植物存在且属于该用户
|
||||
var myPlant plant.MyPlant
|
||||
if err := tx.Where("id = ? AND user_id = ?", req.PlantId, userId).First(&myPlant).Error; err != nil {
|
||||
return errors.New("植物不存在")
|
||||
}
|
||||
// 2.直接创建养护记录
|
||||
record := plant.CareRecord{
|
||||
UserId: userId,
|
||||
PlantId: req.PlantId,
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
Remark: req.Remark,
|
||||
}
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 3.更新用户 profile 对应计数(尝试从 icon JSON 解析 targetAction)
|
||||
column := "care_count"
|
||||
if req.Icon != "" {
|
||||
// 解析 icon JSON 中的 id 字段来判断动作类型
|
||||
actionMap := map[string]string{
|
||||
"water": "water_count", "fertilize": "fertilize_count",
|
||||
"prune": "prune_count", "repot": "repot_count",
|
||||
}
|
||||
// 简单提取: 不引入 encoding/json 解析,用名称匹配
|
||||
nameMap := map[string]string{
|
||||
"浇水": "water_count", "施肥": "fertilize_count",
|
||||
"修剪": "prune_count", "换盆": "repot_count",
|
||||
}
|
||||
if col, ok := nameMap[req.Name]; ok {
|
||||
column = col
|
||||
} else {
|
||||
// 尝试从 actionMap (icon.id) 匹配
|
||||
for key, col := range actionMap {
|
||||
if len(req.Icon) > 0 && contains(req.Icon, key) {
|
||||
column = col
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := tx.Model(&plant.UserProfile{}).Where("user_id = ?", userId).
|
||||
Update(column, gorm.Expr(column+" + 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsStr(s, substr))
|
||||
}
|
||||
|
||||
func containsStr(s, sub string) bool {
|
||||
for i := 0; i <= len(s)-len(sub); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 完成任务后异步执行 更新用户等级 sunlight
|
||||
func (s *MyPlantService) handleCompleteTaskThenUpdateProfile(ctx context.Context, userId string) {
|
||||
//5.更新用户profile
|
||||
|
||||
Reference in New Issue
Block a user