You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.3 KiB
155 lines
4.3 KiB
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"health-ai/internal/api/middleware"
|
|
"health-ai/internal/service"
|
|
"health-ai/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ConstitutionHandler struct {
|
|
constitutionService *service.ConstitutionService
|
|
}
|
|
|
|
func NewConstitutionHandler() *ConstitutionHandler {
|
|
return &ConstitutionHandler{
|
|
constitutionService: service.NewConstitutionService(),
|
|
}
|
|
}
|
|
|
|
// GetQuestions 获取体质问卷题目
|
|
// @Summary 获取体质问卷题目
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=[]model.QuestionBank}
|
|
// @Router /api/constitution/questions [get]
|
|
func (h *ConstitutionHandler) GetQuestions(c *gin.Context) {
|
|
questions, err := h.constitutionService.GetQuestions()
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, questions)
|
|
}
|
|
|
|
// GetQuestionsGrouped 获取分组的体质问卷题目
|
|
// @Summary 获取分组的体质问卷题目
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=[]service.QuestionGroup}
|
|
// @Router /api/constitution/questions/grouped [get]
|
|
func (h *ConstitutionHandler) GetQuestionsGrouped(c *gin.Context) {
|
|
groups, err := h.constitutionService.GetQuestionsGrouped()
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, groups)
|
|
}
|
|
|
|
// SubmitAssessment 提交体质测评
|
|
// @Summary 提交体质测评答案
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Param request body service.SubmitAssessmentRequest true "测评答案"
|
|
// @Success 200 {object} response.Response{data=service.AssessmentResult}
|
|
// @Router /api/constitution/submit [post]
|
|
func (h *ConstitutionHandler) SubmitAssessment(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
|
|
var req service.SubmitAssessmentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
|
|
if len(req.Answers) == 0 {
|
|
response.BadRequest(c, "请至少回答一道问题")
|
|
return
|
|
}
|
|
|
|
result, err := h.constitutionService.SubmitAssessment(userID, &req)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// GetResult 获取最新体质测评结果
|
|
// @Summary 获取最新体质测评结果
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=service.AssessmentResult}
|
|
// @Router /api/constitution/result [get]
|
|
func (h *ConstitutionHandler) GetResult(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
|
|
result, err := h.constitutionService.GetLatestResult(userID)
|
|
if err != nil {
|
|
response.Error(c, 404, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// GetHistory 获取体质测评历史
|
|
// @Summary 获取体质测评历史
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Param limit query int false "返回数量限制,默认10"
|
|
// @Success 200 {object} response.Response{data=[]service.AssessmentResult}
|
|
// @Router /api/constitution/history [get]
|
|
func (h *ConstitutionHandler) GetHistory(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
|
|
limit := 10
|
|
if limitStr := c.Query("limit"); limitStr != "" {
|
|
if l, err := strconv.Atoi(limitStr); err == nil && l > 0 {
|
|
limit = l
|
|
}
|
|
}
|
|
|
|
results, err := h.constitutionService.GetHistory(userID, limit)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, results)
|
|
}
|
|
|
|
// GetRecommendations 获取体质调养建议
|
|
// @Summary 获取体质调养建议
|
|
// @Tags 体质辨识
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=map[string]map[string]string}
|
|
// @Router /api/constitution/recommendations [get]
|
|
func (h *ConstitutionHandler) GetRecommendations(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
|
|
recommendations, err := h.constitutionService.GetRecommendations(userID)
|
|
if err != nil {
|
|
response.Error(c, 404, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, recommendations)
|
|
}
|
|
|