139 lines
4.3 KiB
Go
139 lines
4.3 KiB
Go
package user
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/service"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||
"github.com/gin-gonic/gin"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type PetAssistantApi struct{}
|
||
|
||
var petChatService = service.ServiceGroupApp.PetServiceGroup.PetChatService
|
||
|
||
// GetAssistantHistory 获取宠物助手对话历史
|
||
// @Tags PetAssistant
|
||
// @Summary 获取宠物助手对话历史记录(简化版本,只返回必要字段)
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param sessionId query string false "会话ID"
|
||
// @Param page query int false "页码" default(1)
|
||
// @Param pageSize query int false "每页数量" default(20)
|
||
// @Success 200 {object} response.Response{data=[]petResponse.ChatHistoryItem,msg=string} "获取成功"
|
||
// @Router /api/v1/pet/user/assistant/history [get]
|
||
func (p *PetAssistantApi) GetAssistantHistory(ctx *gin.Context) {
|
||
// 创建业务用Context
|
||
businessCtx := ctx.Request.Context()
|
||
|
||
// 获取用户ID
|
||
userId := utils.GetAppUserID(ctx)
|
||
if userId == 0 {
|
||
global.GVA_LOG.Error("获取用户ID失败")
|
||
response.FailWithMessage("用户认证失败", ctx)
|
||
return
|
||
}
|
||
|
||
// 获取查询参数
|
||
sessionId := ctx.Query("sessionId")
|
||
pageStr := ctx.DefaultQuery("page", "1")
|
||
pageSizeStr := ctx.DefaultQuery("pageSize", "20")
|
||
|
||
page, err := strconv.Atoi(pageStr)
|
||
if err != nil || page < 1 {
|
||
page = 1
|
||
}
|
||
|
||
pageSize, err := strconv.Atoi(pageSizeStr)
|
||
if err != nil || pageSize < 1 || pageSize > 100 {
|
||
pageSize = 20
|
||
}
|
||
|
||
// 计算limit
|
||
limit := pageSize
|
||
|
||
// 调用服务层
|
||
conversations, err := petChatService.GetChatHistory(businessCtx, userId, sessionId, limit)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取聊天历史失败", zap.Error(err), zap.Uint("userId", userId))
|
||
response.FailWithMessage("获取聊天历史失败: "+err.Error(), ctx)
|
||
return
|
||
}
|
||
|
||
response.OkWithDetailed(conversations, "获取成功", ctx)
|
||
}
|
||
|
||
// ClearAssistantHistory 清空宠物助手对话历史
|
||
// @Tags PetAssistant
|
||
// @Summary 清空宠物助手对话历史记录
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param sessionId query string false "会话ID,不传则清空所有会话"
|
||
// @Success 200 {object} response.Response{msg=string} "清空成功"
|
||
// @Router /api/v1/pet/user/assistant/clear-history [delete]
|
||
func (p *PetAssistantApi) ClearAssistantHistory(ctx *gin.Context) {
|
||
// 创建业务用Context
|
||
businessCtx := ctx.Request.Context()
|
||
|
||
// 获取用户ID
|
||
userId := utils.GetAppUserID(ctx)
|
||
if userId == 0 {
|
||
global.GVA_LOG.Error("获取用户ID失败")
|
||
response.FailWithMessage("用户认证失败", ctx)
|
||
return
|
||
}
|
||
|
||
// 获取会话ID(可选)
|
||
sessionId := ctx.Query("sessionId")
|
||
|
||
// 调用服务层
|
||
err := petChatService.ClearChatHistory(businessCtx, userId, sessionId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("清空聊天历史失败", zap.Error(err), zap.Uint("userId", userId))
|
||
response.FailWithMessage("清空聊天历史失败: "+err.Error(), ctx)
|
||
return
|
||
}
|
||
|
||
if sessionId != "" {
|
||
response.OkWithMessage("指定会话历史清空成功", ctx)
|
||
} else {
|
||
response.OkWithMessage("所有聊天历史清空成功", ctx)
|
||
}
|
||
}
|
||
|
||
// GetAssistantSessions 获取宠物助手会话列表
|
||
// @Tags PetAssistant
|
||
// @Summary 获取用户的宠物助手会话列表
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=[]map[string]interface{},msg=string} "获取成功"
|
||
// @Router /api/v1/pet/user/assistant/sessions [get]
|
||
func (p *PetAssistantApi) GetAssistantSessions(ctx *gin.Context) {
|
||
// 创建业务用Context
|
||
businessCtx := ctx.Request.Context()
|
||
|
||
// 获取用户ID
|
||
userId := utils.GetAppUserID(ctx)
|
||
if userId == 0 {
|
||
global.GVA_LOG.Error("获取用户ID失败")
|
||
response.FailWithMessage("用户认证失败", ctx)
|
||
return
|
||
}
|
||
|
||
// 调用服务层
|
||
sessions, err := petChatService.GetChatSessions(businessCtx, userId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取会话列表失败", zap.Error(err), zap.Uint("userId", userId))
|
||
response.FailWithMessage("获取会话列表失败: "+err.Error(), ctx)
|
||
return
|
||
}
|
||
|
||
response.OkWithDetailed(sessions, "获取成功", ctx)
|
||
}
|