Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode

This commit is contained in:
wangchuxiao 2023-01-17 14:40:24 +08:00
commit df6bd5719d
6 changed files with 415 additions and 394 deletions

View File

@ -7,18 +7,7 @@ import (
) )
func CopyAny(from, to interface{}) { func CopyAny(from, to interface{}) {
t := reflect.ValueOf(to) copyAny(reflect.ValueOf(from), reflect.Indirect(reflect.ValueOf(to)))
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if !t.CanSet() {
return
}
f := reflect.ValueOf(from)
if isBaseNil(f) {
return
}
copyAny(f, t)
} }
func copyAny(from, to reflect.Value) { func copyAny(from, to reflect.Value) {
@ -97,13 +86,19 @@ func getBaseZeroValue(t reflect.Type) reflect.Value {
l++ l++
t = t.Elem() t = t.Elem()
} }
v := reflect.Zero(t) v := reflect.New(t)
for i := 0; i < l; i++ { if l == 0 {
t := reflect.New(v.Type()) v = v.Elem()
t.Elem().Set(v) } else {
v = t for i := 1; i < l; i++ {
t := reflect.New(v.Type())
t.Elem().Set(v)
v = t
}
} }
return v r := reflect.New(v.Type()).Elem()
r.Set(v)
return r
} }
func isBaseNil(v reflect.Value) bool { func isBaseNil(v reflect.Value) bool {
@ -163,15 +158,35 @@ func copySlice(from, to reflect.Value) {
temp := reflect.MakeSlice(to.Type(), 0, size) temp := reflect.MakeSlice(to.Type(), 0, size)
elemTo := to.Type().Elem() elemTo := to.Type().Elem()
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
itemTo := getBaseZeroValue(elemTo) var itemTo reflect.Value
copyAny(from.Index(i), itemTo) if item := from.Index(i); isBaseNil(item) {
itemTo = reflect.Zero(elemTo)
} else {
itemTo = getBaseZeroValue(elemTo)
copyAny(from.Index(i), itemTo)
}
temp = reflect.Append(temp, itemTo) temp = reflect.Append(temp, itemTo)
} }
to.Set(temp) to.Set(temp)
} }
func copyMap(from, to reflect.Value) { func copyMap(from, to reflect.Value) {
// todo copy map to.Set(reflect.MakeMap(to.Type()))
toTypeKey := to.Type().Key()
toTypeVal := to.Type().Elem()
for r := from.MapRange(); r.Next(); {
key := getBaseZeroValue(toTypeKey)
copyAny(r.Key(), key)
var val reflect.Value
fVal := r.Value()
if isBaseNil(fVal) {
val = reflect.Zero(toTypeVal)
} else {
val = getBaseZeroValue(toTypeVal)
copyAny(fVal, val)
}
to.SetMapIndex(key, val)
}
} }
func toString(value reflect.Value) string { func toString(value reflect.Value) string {

View File

@ -206,7 +206,33 @@ func (db *DBGroupRequest) convert() (*sdk.GroupRequest, error) {
return dst, nil return dst, nil
} }
func UserOpenIMCopyDB(dst *imdb.User, src *open_im_sdk.UserInfo) { type DBUser struct {
*imdb
}
type PBUser struct {
*sdk.UserInfo
}
func (pb *PBUser) convert() (*DBUser, error) {
dst := &DBUser{}
utils.CopyStructFields(dst, pb)
utils.CopyStructFields(dst, src)
dst.Birth, _ = utils.TimeStringToTime(src.BirthStr)
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))
return dst, nil
}
func (db *DBUser) convert() (*PBUser, error) {
dst := &sdk.GroupRequest{}
utils.CopyStructFields(dst, db)
dst.ReqTime = uint32(db.ReqTime.Unix())
dst.HandleTime = uint32(db.HandledTime.Unix())
return dst, nil
}
func UserOpenIMCopyDB(dst *imdb.User, src *sdk.UserInfo) {
utils.CopyStructFields(dst, src) utils.CopyStructFields(dst, src)
dst.Birth, _ = utils.TimeStringToTime(src.BirthStr) dst.Birth, _ = utils.TimeStringToTime(src.BirthStr)
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime)) dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))

View File

@ -0,0 +1,23 @@
package model
import (
"Open_IM/pkg/common/db/mysql"
"context"
)
type UserModel struct {
db *mysql.User
}
func NewGroupUser(ctx context.Context) {
var userModel UserModel
userModel.db = mysql.NewUserDB()
}
func (u *UserModel) Find(ctx context.Context, userIDs []string) (users []*mysql.User, err error) {
return u.db.Find(ctx, userIDs)
}
func (u *UserModel) Create(ctx context.Context, users []*mysql.User) error {
return u.db.Create(ctx, users)
}

View File

@ -8,8 +8,6 @@ import (
"time" "time"
) )
var userDB *gorm.DB
type User struct { type User struct {
UserID string `gorm:"column:user_id;primary_key;size:64"` UserID string `gorm:"column:user_id;primary_key;size:64"`
Nickname string `gorm:"column:name;size:255"` Nickname string `gorm:"column:name;size:255"`
@ -23,44 +21,51 @@ type User struct {
AppMangerLevel int32 `gorm:"column:app_manger_level"` AppMangerLevel int32 `gorm:"column:app_manger_level"`
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"` GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"`
status int32 `gorm:"column:status"` status int32 `gorm:"column:status"`
DB *gorm.DB `gorm:"-" json:"-"`
} }
func (*User) Create(ctx context.Context, users []*User) (err error) { func NewUserDB() *User {
var user User
user.DB = initMysqlDB(&user)
return &user
}
func (u *User) Create(ctx context.Context, users []*User) (err error) {
defer func() { defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users) trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}() }()
err = utils.Wrap(userDB.Create(&users).Error, "") err = utils.Wrap(u.DB.Create(&users).Error, "")
return err return err
} }
func (*User) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) { func (u *User) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
defer func() { defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args) trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "args", args)
}() }()
return utils.Wrap(userDB.Where("user_id = ?", userID).Updates(args).Error, "") return utils.Wrap(u.DB.Where("user_id = ?", userID).Updates(args).Error, "")
} }
func (*User) Update(ctx context.Context, users []*User) (err error) { func (u *User) Update(ctx context.Context, users []*User) (err error) {
defer func() { defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users) trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
}() }()
return utils.Wrap(userDB.Updates(&users).Error, "") return utils.Wrap(u.DB.Updates(&users).Error, "")
} }
func (*User) Find(ctx context.Context, userIDs []string) (users []*User, err error) { func (u *User) Find(ctx context.Context, userIDs []string) (users []*User, err error) {
defer func() { defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users) trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userIDs", userIDs, "users", users)
}() }()
err = utils.Wrap(userDB.Where("user_id in (?)", userIDs).Find(&users).Error, "") err = utils.Wrap(u.DB.Where("user_id in (?)", userIDs).Find(&users).Error, "")
return users, err return users, err
} }
func (*User) Take(ctx context.Context, userID string) (user *User, err error) { func (u *User) Take(ctx context.Context, userID string) (user *User, err error) {
user = &User{} user = &User{}
defer func() { defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user) trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "user", *user)
}() }()
err = utils.Wrap(userDB.Where("user_id = ?", userID).Take(&user).Error, "") err = utils.Wrap(u.DB.Where("user_id = ?", userID).Take(&user).Error, "")
return user, err return user, err
} }

File diff suppressed because it is too large Load Diff

View File

@ -79,7 +79,7 @@ message UserInfo{
uint32 createTime = 9; uint32 createTime = 9;
int32 appMangerLevel = 10; int32 appMangerLevel = 10;
int32 globalRecvMsgOpt = 11; int32 globalRecvMsgOpt = 11;
string birthStr = 12; int64 birthday = 13;
} }
message FriendInfo{ message FriendInfo{