diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index aa7bb2b7e..32fb465e6 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -116,7 +116,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR utils.CopyStructFields(&groupInfo, req.GroupInfo) groupInfo.CreatorUserID = req.OpUserID groupInfo.GroupID = groupId - + groupInfo.CreateTime = time.Now() if groupInfo.NotificationUpdateTime.Unix() < 0 { groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0) } @@ -1375,7 +1375,7 @@ func (s *groupServer) GetGroups(_ context.Context, req *pbGroup.GetGroupsReq) (* groupInfo.MemberCount = uint32(memberNum) resp.CMSGroups = append(resp.CMSGroups, &pbGroup.CMSGroup{GroupInfo: groupInfo, GroupOwnerUserName: groupMember.Nickname, GroupOwnerUserID: groupMember.UserID}) } else { - groups, err := imdb.GetGroupsByName(req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber) + groups, count, err := imdb.GetGroupsByName(req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber) if err != nil { log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupsByName error", req.String(), req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber) } @@ -1392,13 +1392,7 @@ func (s *groupServer) GetGroups(_ context.Context, req *pbGroup.GetGroupsReq) (* group.GroupOwnerUserName = groupMember.Nickname resp.CMSGroups = append(resp.CMSGroups, group) } - resp.GroupNum, err = imdb.GetGroupsCountNum(db.Group{GroupName: req.GroupName}) - if err != nil { - log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupsCountNum error", err.Error()) - resp.CommonResp.ErrCode = constant.ErrDB.ErrCode - resp.CommonResp.ErrMsg = err.Error() - return resp, nil - } + resp.GroupNum = int32(count) } log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "GetGroups resp", resp.String()) return resp, nil diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_model.go index 9f9b10d3c..6f3b230f4 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_model.go @@ -53,11 +53,16 @@ type GroupWithNum struct { MemberCount int `gorm:"column:num"` } -func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, error) { +func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, int64, error) { var groups []GroupWithNum - err := db.DB.MysqlDB.DefaultGormDB().Table("groups").Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num"). - Where(" name like ? and status != ?", fmt.Sprintf("%%%s%%", groupName), constant.GroupStatusDismissed).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groups).Error - return groups, err + var count int64 + sql := db.DB.MysqlDB.DefaultGormDB().Table("groups").Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num"). + Where(" name like ? and status != ?", fmt.Sprintf("%%%s%%", groupName), constant.GroupStatusDismissed) + if err := sql.Count(&count).Error; err != nil { + return nil, 0, err + } + err := sql.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groups).Error + return groups, count, err } func GetGroups(pageNumber, showNumber int) ([]GroupWithNum, error) {