Enhance smoking tracking features with AI next smoke time suggestions
- Added new API endpoint `GET /api/v1/smoke/next_smoke_time` to provide AI-generated suggestions for the next smoking time based on user data. - Introduced a new database table `fa_smoke_ai_next_smoke` to store structured AI time node suggestions. - Updated smoke handler and service to integrate the new AI next smoke time functionality. - Enhanced documentation to reflect the new API endpoint and its usage, including details on how to generate AI time nodes.
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -17,13 +18,23 @@ type SmokeHandler struct {
|
||||
smokeLogService *smokeservice.SmokeLogService
|
||||
smokeAIAdviceService *smokeservice.SmokeAIAdviceService
|
||||
smokeProfileService *smokeservice.SmokeProfileService
|
||||
smokeNextService *smokeservice.SmokeNextService
|
||||
smokeAINextService *smokeservice.SmokeAINextSmokeService
|
||||
}
|
||||
|
||||
func NewSmokeHandler(smokeLogService *smokeservice.SmokeLogService, smokeAIAdviceService *smokeservice.SmokeAIAdviceService, smokeProfileService *smokeservice.SmokeProfileService) *SmokeHandler {
|
||||
func NewSmokeHandler(
|
||||
smokeLogService *smokeservice.SmokeLogService,
|
||||
smokeAIAdviceService *smokeservice.SmokeAIAdviceService,
|
||||
smokeProfileService *smokeservice.SmokeProfileService,
|
||||
smokeNextService *smokeservice.SmokeNextService,
|
||||
smokeAINextService *smokeservice.SmokeAINextSmokeService,
|
||||
) *SmokeHandler {
|
||||
return &SmokeHandler{
|
||||
smokeLogService: smokeLogService,
|
||||
smokeAIAdviceService: smokeAIAdviceService,
|
||||
smokeProfileService: smokeProfileService,
|
||||
smokeNextService: smokeNextService,
|
||||
smokeAINextService: smokeAINextService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +48,8 @@ type createSmokeLogRequest struct {
|
||||
// 真实抽烟时间(精确到时分秒,可补录)
|
||||
SmokeAt string `json:"smoke_at"`
|
||||
Remark string `json:"remark"`
|
||||
Level int64 `json:"level"`
|
||||
Num int `json:"num"`
|
||||
Level *int64 `json:"level"`
|
||||
Num *int `json:"num"`
|
||||
}
|
||||
|
||||
func (h *SmokeHandler) Create(c *gin.Context) {
|
||||
@@ -54,6 +65,83 @@ func (h *SmokeHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
level := int64(1)
|
||||
if req.Level != nil {
|
||||
if *req.Level < 0 {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "level 不能为负数"))
|
||||
return
|
||||
}
|
||||
level = *req.Level
|
||||
}
|
||||
num := 1
|
||||
if req.Num != nil {
|
||||
if *req.Num < 0 {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "num 不能为负数"))
|
||||
return
|
||||
}
|
||||
num = *req.Num
|
||||
}
|
||||
|
||||
if level < 0 || num < 0 {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "level/num 不能为负数"))
|
||||
return
|
||||
}
|
||||
|
||||
var smokeTime *time.Time
|
||||
if req.SmokeTime != "" {
|
||||
parsed, err := time.ParseInLocation(dateLayout, req.SmokeTime, time.Local)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "smoke_time 格式错误,应为 YYYY-MM-DD"))
|
||||
return
|
||||
}
|
||||
smokeTime = &parsed
|
||||
}
|
||||
|
||||
var smokeAt *time.Time
|
||||
if req.SmokeAt != "" {
|
||||
parsed, err := time.ParseInLocation(dateTimeLayout, req.SmokeAt, time.Local)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "smoke_at 格式错误,应为 YYYY-MM-DD HH:MM:SS"))
|
||||
return
|
||||
}
|
||||
smokeAt = &parsed
|
||||
}
|
||||
|
||||
record, err := h.smokeLogService.Create(c.Request.Context(), int(user.ID), smokeservice.CreateSmokeLogRequest{
|
||||
SmokeTime: smokeTime,
|
||||
SmokeAt: smokeAt,
|
||||
Remark: req.Remark,
|
||||
Level: level,
|
||||
Num: num,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "创建记录失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.Success(record))
|
||||
}
|
||||
|
||||
type resistedSmokeLogRequest struct {
|
||||
SmokeTime string `json:"smoke_time"`
|
||||
SmokeAt string `json:"smoke_at"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// Resist 表示“想抽但忍住了”:在 fa_smoke_log 中写入 level=0,num=0。
|
||||
func (h *SmokeHandler) Resist(c *gin.Context) {
|
||||
user, ok := middleware.CurrentUser(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
|
||||
return
|
||||
}
|
||||
|
||||
var req resistedSmokeLogRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
var smokeTime *time.Time
|
||||
if req.SmokeTime != "" {
|
||||
parsed, err := time.ParseInLocation(dateLayout, req.SmokeTime, time.Local)
|
||||
@@ -78,8 +166,8 @@ func (h *SmokeHandler) Create(c *gin.Context) {
|
||||
SmokeTime: smokeTime,
|
||||
SmokeAt: smokeAt,
|
||||
Remark: req.Remark,
|
||||
Level: req.Level,
|
||||
Num: req.Num,
|
||||
Level: 0,
|
||||
Num: 0,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "创建记录失败,请稍后重试"))
|
||||
@@ -271,6 +359,15 @@ func (h *SmokeHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if req.Level != nil && *req.Level < 0 {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "level 不能为负数"))
|
||||
return
|
||||
}
|
||||
if req.Num != nil && *req.Num < 0 {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "num 不能为负数"))
|
||||
return
|
||||
}
|
||||
|
||||
smokeTimeProvided := req.SmokeTime != nil
|
||||
var smokeTime *time.Time
|
||||
if req.SmokeTime != nil {
|
||||
|
||||
Reference in New Issue
Block a user