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

test-errcode
wangchuxiao 2 years ago
commit df6bd5719d

@ -7,18 +7,7 @@ import (
)
func CopyAny(from, to interface{}) {
t := 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)
copyAny(reflect.ValueOf(from), reflect.Indirect(reflect.ValueOf(to)))
}
func copyAny(from, to reflect.Value) {
@ -97,13 +86,19 @@ func getBaseZeroValue(t reflect.Type) reflect.Value {
l++
t = t.Elem()
}
v := reflect.Zero(t)
for i := 0; i < l; i++ {
t := reflect.New(v.Type())
t.Elem().Set(v)
v = t
v := reflect.New(t)
if l == 0 {
v = v.Elem()
} else {
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 {
@ -163,15 +158,35 @@ func copySlice(from, to reflect.Value) {
temp := reflect.MakeSlice(to.Type(), 0, size)
elemTo := to.Type().Elem()
for i := 0; i < size; i++ {
itemTo := getBaseZeroValue(elemTo)
copyAny(from.Index(i), itemTo)
var itemTo reflect.Value
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)
}
to.Set(temp)
}
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 {

@ -206,7 +206,33 @@ func (db *DBGroupRequest) convert() (*sdk.GroupRequest, error) {
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)
dst.Birth, _ = utils.TimeStringToTime(src.BirthStr)
dst.CreateTime = utils.UnixSecondToTime(int64(src.CreateTime))

@ -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)
}

@ -8,8 +8,6 @@ import (
"time"
)
var userDB *gorm.DB
type User struct {
UserID string `gorm:"column:user_id;primary_key;size:64"`
Nickname string `gorm:"column:name;size:255"`
@ -23,44 +21,51 @@ type User struct {
AppMangerLevel int32 `gorm:"column:app_manger_level"`
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"`
status int32 `gorm:"column:status"`
status int32 `gorm:"column:status"`
DB *gorm.DB `gorm:"-" json:"-"`
}
func NewUserDB() *User {
var user User
user.DB = initMysqlDB(&user)
return &user
}
func (*User) Create(ctx context.Context, users []*User) (err error) {
func (u *User) Create(ctx context.Context, users []*User) (err error) {
defer func() {
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
}
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() {
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() {
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() {
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
}
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{}
defer func() {
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
}

File diff suppressed because it is too large Load Diff

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

Loading…
Cancel
Save