Skip to content

Commit

Permalink
feat: 添加获取会话详情
Browse files Browse the repository at this point in the history
fix: 游标分页多条件修复
  • Loading branch information
danmuking committed Apr 11, 2024
1 parent c218b15 commit f2c0113
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
15 changes: 15 additions & 0 deletions domain/vo/resp/message_resp.go
Original file line number Diff line number Diff line change
@@ -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"`
}
4 changes: 2 additions & 2 deletions pkg/cursor/cursor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions routes/init_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
88 changes: 87 additions & 1 deletion service/contact_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 添加日志系统
Expand Down

0 comments on commit f2c0113

Please sign in to comment.