test-errcode
withchao 2 years ago
parent e0a422fd16
commit 762d6516a5

@ -1,11 +1,11 @@
package relation
package ormutil
import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gorm.io/gorm"
)
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
func GormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
var count int64
if err := db.Count(&count).Error; err != nil {
return 0, nil, utils.Wrap(err, "")
@ -17,7 +17,7 @@ func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, e
return uint32(count), es, nil
}
func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
func GormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
if len(fields) > 0 && value != "" {
value = "%" + value + "%"
if len(fields) == 1 {
@ -30,17 +30,17 @@ func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, s
db = db.Where(t)
}
}
return gormPage[E](db, pageNumber, showNumber)
return GormPage[E](db, pageNumber, showNumber)
}
func gormIn[E any](db **gorm.DB, field string, es []E) {
func GormIn[E any](db **gorm.DB, field string, es []E) {
if len(es) == 0 {
return
}
*db = (*db).Where(field+" in (?)", es)
}
func mapCount(db *gorm.DB, field string) (map[string]uint32, error) {
func MapCount(db *gorm.DB, field string) (map[string]uint32, error) {
var items []struct {
ID string `gorm:"column:id"`
Count uint32 `gorm:"column:count"`

@ -2,6 +2,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
@ -50,7 +51,7 @@ func (b *BlackGorm) FindOwnerBlacks(ctx context.Context, ownerUserID string, pag
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
totalUint32, blacks, err := gormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
totalUint32, blacks, err := ormutil.GormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
total = int64(totalUint32)
return
}

@ -3,6 +3,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gorm.io/gorm"
@ -71,14 +72,14 @@ func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string) (groupM
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, groupList []*relation.GroupMemberModel, err error) {
db := g.DB
gormIn(&db, "group_id", groupIDs)
gormIn(&db, "user_id", userIDs)
gormIn(&db, "role_level", roleLevels)
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
ormutil.GormIn(&db, "group_id", groupIDs)
ormutil.GormIn(&db, "user_id", userIDs)
ormutil.GormIn(&db, "role_level", roleLevels)
return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
}
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string) (count map[string]uint32, err error) {
return mapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
return ormutil.MapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
}
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error) {

@ -2,6 +2,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"gorm.io/gorm"
@ -43,7 +44,7 @@ func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.G
}
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
return gormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
return ormutil.GormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
}
func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {

@ -2,6 +2,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
@ -39,5 +40,5 @@ func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID stri
}
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupRequestModel, err error) {
return gormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
return ormutil.GormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
}

Loading…
Cancel
Save