diff --git a/domain/vo/resp/message_resp.go b/domain/vo/resp/message_resp.go new file mode 100644 index 0000000..12b2f0a --- /dev/null +++ b/domain/vo/resp/message_resp.go @@ -0,0 +1,15 @@ +package resp + +import "time" + +type MessageResp struct { + ID int64 `json:"id"` + Content string `json:"content"` + ReplyMsgID int64 `json:"reply_msg_id"` + GapCount int32 `json:"gap_count"` + Type int32 `json:"type"` + CreateTime time.Time `json:"create_time"` + // 发送者信息 + UserName string `json:"user_name"` + UserAvatar string `json:"user_avatar"` +} diff --git a/pkg/cursor/cursor_utils.go b/pkg/cursor/cursor_utils.go index 3240d9a..3885a47 100644 --- a/pkg/cursor/cursor_utils.go +++ b/pkg/cursor/cursor_utils.go @@ -20,12 +20,12 @@ type PageResp struct { // Paginate 是通用的游标分页函数 // TODO: select部分字段 -func Paginate(db *gorm.DB, params PageReq, result interface{}, cursorFieldName string, isAsc bool, conditions ...string) (*PageResp, error) { +func Paginate(db *gorm.DB, params PageReq, result interface{}, cursorFieldName string, isAsc bool, conditions ...interface{}) (*PageResp, error) { var resp PageResp query := db if len(conditions) > 0 { - query = query.Where(conditions[0], conditions[1:]) + query = query.Where(conditions[0], conditions[1:]...) } if params.Cursor != nil { diff --git a/routes/init_router.go b/routes/init_router.go index ca2c2e0..c74e80e 100644 --- a/routes/init_router.go +++ b/routes/init_router.go @@ -66,6 +66,7 @@ func initGin() { apiContact.Use(jwt.JWT()) { apiContact.GET("getContactList", service.GetContactListService) + apiContact.GET("getContactDetail", service.GetContactDetailService) } apiMsg := router.Group("/api/msg") diff --git a/service/contact_service.go b/service/contact_service.go index b085c97..01e565c 100644 --- a/service/contact_service.go +++ b/service/contact_service.go @@ -5,6 +5,7 @@ import ( "DiTing-Go/dal/model" "DiTing-Go/domain/dto" "DiTing-Go/domain/enum" + domainResp "DiTing-Go/domain/vo/resp" "DiTing-Go/global" cursorUtils "DiTing-Go/pkg/cursor" "DiTing-Go/pkg/resp" @@ -43,7 +44,7 @@ func GetContactListService(c *gin.Context) { func GetContactList(uid int64, pageRequest cursorUtils.PageReq) (*cursorUtils.PageResp, error) { db := dal.DB contact := make([]model.Contact, 0) - condition := []string{"uid=?", strconv.FormatInt(uid, 10)} + condition := []interface{}{"uid=?", strconv.FormatInt(uid, 10)} pageResp, err := cursorUtils.Paginate(db, pageRequest, &contact, "active_time", false, condition...) if err != nil { log.Printf("查询会话列表失败: %s", err.Error()) @@ -113,3 +114,88 @@ func getContactDto(contact model.Contact) (*dto.ContactDto, error) { // TODO: 群聊 return contactDto, nil } + +func GetContactDetailService(c *gin.Context) { + + roomIdString, _ := c.GetQuery("room_id") + roomId, _ := strconv.ParseInt(roomIdString, 10, 64) + //TODO:参数校验 + // 游标翻页 + // 默认值 + var cursor *string = nil + var pageSize int = 20 + pageRequest := cursorUtils.PageReq{ + Cursor: cursor, + PageSize: pageSize, + } + if err := c.ShouldBindQuery(&pageRequest); err != nil { //ShouldBind()会自动推导 + resp.ErrorResponse(c, "参数错误") + c.Abort() + return + } + pageResp, err := GetContactDetail(roomId, pageRequest) + if err != nil { + resp.ErrorResponse(c, "系统正忙,请稍后再试") + c.Abort() + return + } + resp.SuccessResponse(c, pageResp) + c.Abort() + return +} + +func GetContactDetail(roomID int64, pageRequest cursorUtils.PageReq) (*cursorUtils.PageResp, error) { + // 查询消息 + db := dal.DB + msgs := make([]model.Message, 0) + // TODO: 抽象成常量 + condition := []interface{}{"room_id=? AND status=?", strconv.FormatInt(roomID, 10), "0"} + pageResp, err := cursorUtils.Paginate(db, pageRequest, &msgs, "create_time", false, condition...) + if err != nil { + log.Printf("查询消息失败: %s", err.Error()) + return nil, err + } + msgList := pageResp.Data.(*[]model.Message) + userIdMap := make(map[int64]*int64) + for _, msg := range *msgList { + if userIdMap[msg.FromUID] == nil { + userIdMap[msg.FromUID] = &msg.FromUID + } + } + // 转换成列表 + userIdList := make([]int64, 0) + for _, uid := range userIdMap { + userIdList = append(userIdList, *uid) + } + // 查询用户信息 + ctx := context.Background() + user := global.Query.User + userQ := user.WithContext(ctx) + users, err := userQ.Where(user.ID.In(userIdList...)).Find() + if err != nil { + log.Printf("查询用户失败: %s", err.Error()) + return nil, err + } + userMap := make(map[int64]*model.User) + for _, user := range users { + userMap[user.ID] = user + } + + // 拼装结果 + msgRespList := make([]domainResp.MessageResp, 0) + for _, msg := range *msgList { + msgResp := domainResp.MessageResp{ + ID: msg.ID, + Content: msg.Content, + ReplyMsgID: msg.ReplyMsgID, + GapCount: msg.GapCount, + Type: msg.Type, + CreateTime: msg.CreateTime, + UserName: userMap[msg.FromUID].Name, + UserAvatar: userMap[msg.FromUID].Avatar, + } + msgRespList = append(msgRespList, msgResp) + } + pageResp.Data = msgRespList + return pageResp, nil +} diff --git a/service/user_service.go b/service/user_service.go index 1b76d55..243b72f 100644 --- a/service/user_service.go +++ b/service/user_service.go @@ -360,7 +360,7 @@ func GetApplyList(c *gin.Context) { // 获取 UserApply 表中 TargetID 等于 uid(登录用户ID)的用户ID集合,采用游标分页 db := dal.DB userApplys := make([]model.UserApply, 0) - condition := []string{"target_id=?", strconv.FormatInt(uid, 10)} + condition := []interface{}{"target_id=?", strconv.FormatInt(uid, 10)} pageResp, err := cursorUtils.Paginate(db, pageRequest, &userApplys, "create_time", false, condition...) if err != nil { // todo 添加日志系统