Browse Source

fix:save 16

jimmyyem 5 months ago
parent
commit
3a5713165f

+ 20 - 0
desc/wechat/chat_records.api

@@ -58,6 +58,22 @@ type (
         // ChatRecords information | ChatRecords数据
         Data ChatRecordsInfo `json:"data"`
     }
+
+	ChatRecommendReq {
+		SessionId  *uint64 `json:"sessionId"`
+	}
+
+	ChatRecommendResp {
+		BaseDataInfo
+
+		// The API list data | ChatRecords列表数据
+		Data  ChatRecommend  `json:"data"`
+	}
+
+	ChatRecommend {
+		Data []string `json:"data"`
+		SessionId uint64 `json:"sessionId"`
+	}
 )
 
 @server(
@@ -74,6 +90,10 @@ service Wechat {
 	// Get chat records list | 获取ChatRecords列表
 	@handler getApiChatList
 	post /api/chat/list (ChatRecordsListReq) returns (ChatRecordsListResp)
+
+	// Get chat records list | 获取ChatRecords列表
+	@handler getApiRecommendChat
+	post /api/chat/recommmend (ChatRecommendReq) returns (ChatRecommendResp)
 }
 
 @server(

+ 44 - 0
internal/handler/chatrecords/get_api_recommend_chat_handler.go

@@ -0,0 +1,44 @@
+package chatrecords
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/chatrecords"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /api/chat/recommmend chatrecords GetApiRecommendChat
+//
+// Get chat records list | 获取ChatRecords列表
+//
+// Get chat records list | 获取ChatRecords列表
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: ChatRecommendReq
+//
+// Responses:
+//  200: ChatRecommendResp
+
+func GetApiRecommendChatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.ChatRecommendReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := chatrecords.NewGetApiRecommendChatLogic(r.Context(), svcCtx)
+		resp, err := l.GetApiRecommendChat(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

@@ -1023,6 +1023,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/api/chat/list",
 					Handler: chatrecords.GetApiChatListHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/api/chat/recommmend",
+					Handler: chatrecords.GetApiRecommendChatHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 71 - 0
internal/logic/chatrecords/get_api_recommend_chat_logic.go

@@ -0,0 +1,71 @@
+package chatrecords
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/ent"
+	"wechat-api/ent/chatrecords"
+	"wechat-api/ent/chatsession"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetApiRecommendChatLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetApiRecommendChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiRecommendChatLogic {
+	return &GetApiRecommendChatLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetApiRecommendChatLogic) GetApiRecommendChat(req *types.ChatRecommendReq) (*types.ChatRecommendResp, error) {
+	recommendCount := 5
+
+	if req.SessionId != nil && *req.SessionId > 0 {
+		userId := l.ctx.Value("userId").(uint64)
+
+		_, err := l.svcCtx.DB.ChatSession.Query().Where(
+			chatsession.UserID(userId),
+			chatsession.ID(*req.SessionId),
+		).Only(l.ctx)
+		if err != nil {
+			if ent.IsNotFound(err) {
+				return nil, errorx.NewInvalidArgumentError("sessionId not found")
+			}
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+
+		resp := types.ChatRecommendResp{}
+		resp.BaseDataInfo.Code = 0
+		resp.BaseDataInfo.Msg = errormsg.Success
+
+		list, err := l.svcCtx.DB.ChatRecords.Query().Where(chatrecords.SessionID(*req.SessionId)).Limit(recommendCount).All(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+
+		recommendList := make([]string, 0, recommendCount)
+		for _, val := range list {
+			recommendList = append(recommendList, val.Content)
+		}
+
+		//TODO 这里根据历史记录返回推荐内容
+
+		resp.Data.SessionId = *req.SessionId
+		resp.Data.Data = recommendList
+
+		return &resp, nil
+	} else {
+		return nil, errorx.NewInvalidArgumentError("sessionId cannot be null")
+	}
+}

+ 17 - 0
internal/types/types.go

@@ -2181,6 +2181,23 @@ type ChatRecordsInfoResp struct {
 	Data ChatRecordsInfo `json:"data"`
 }
 
+// swagger:model ChatRecommendReq
+type ChatRecommendReq struct {
+	SessionId *uint64 `json:"sessionId"`
+}
+
+// swagger:model ChatRecommendResp
+type ChatRecommendResp struct {
+	BaseDataInfo
+	// The API list data | ChatRecords列表数据
+	Data ChatRecommend `json:"data"`
+}
+
+type ChatRecommend struct {
+	Data      []string `json:"data"`
+	SessionId uint64   `json:"sessionId"`
+}
+
 // The data of chat session information | ChatSession信息
 // swagger:model ChatSessionInfo
 type ChatSessionInfo struct {