diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 2e53e5bb9..0272aa7d3 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -45,8 +45,8 @@ func NewFriendServer(port int) *friendServer { } //mysql init var mysql relation.Mysql - var model relation.Friend - err := mysql.InitConn().AutoMigrateModel(&model) + var model relation.FriendGorm + err := mysql.InitConn().AutoMigrateModel(&relation.FriendModel{}) if err != nil { panic("db init err:" + err.Error()) } diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 1f5670f6a..d4c498b65 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -117,11 +117,11 @@ type FriendDatabaseInterface interface { } type FriendDatabase struct { - sqlDB *relation.Friend + sqlDB relation.FriendDB } func NewFriendDatabase(db *gorm.DB) *FriendDatabase { - sqlDB := relation.NewFriendDB(db) + sqlDB := relation.NewFriendGorm(db) database := &FriendDatabase{ sqlDB: sqlDB, } diff --git a/pkg/common/db/relation/friend_model_k.go b/pkg/common/db/relation/friend_model_k.go index d581e1d25..5355dcc9b 100644 --- a/pkg/common/db/relation/friend_model_k.go +++ b/pkg/common/db/relation/friend_model_k.go @@ -8,7 +8,16 @@ import ( "time" ) -type Friend struct { +type FriendDB interface { + Create(ctx context.Context, friends []*FriendModel) (err error) + Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) + UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, friends []*FriendModel) (err error) + UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) + FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error) +} + +type FriendModel struct { OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"` FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"` Remark string `gorm:"column:remark;size:255"` @@ -16,77 +25,77 @@ type Friend struct { AddSource int32 `gorm:"column:add_source"` OperatorUserID string `gorm:"column:operator_user_id;size:64"` Ex string `gorm:"column:ex;size:1024"` - DB *gorm.DB `gorm:"-"` +} +type FriendGorm struct { + DB *gorm.DB `gorm:"-"` +} + +func NewFriendGorm(DB *gorm.DB) *FriendGorm { + return &FriendGorm{DB: DB} } type FriendUser struct { - Friend + FriendGorm Nickname string `gorm:"column:name;size:255"` } -func NewFriendDB(db *gorm.DB) *Friend { - var friend Friend - friend.DB = db - return &friend -} - -func (f *Friend) Create(ctx context.Context, friends []*Friend) (err error) { +func (f *FriendGorm) Create(ctx context.Context, friends []*FriendModel) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends) }() return utils.Wrap(f.DB.Create(&friends).Error, "") } -func (f *Friend) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { +func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs) }() - err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&Friend{}).Error, "") + err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&FriendModel{}).Error, "") return err } -func (f *Friend) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { +func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "args", args) }() return utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "") } -func (f *Friend) Update(ctx context.Context, friends []*Friend) (err error) { +func (f *FriendGorm) Update(ctx context.Context, friends []*FriendModel) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends) }() return utils.Wrap(f.DB.Updates(&friends).Error, "") } -func (f *Friend) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { +func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark) }() - return utils.Wrap(f.DB.Model(f).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "") + return utils.Wrap(f.DB.Model(&FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "") } -func (f *Friend) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*Friend, err error) { +func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends) }() return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "") } -func (f *Friend) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*Friend, err error) { +func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends) }() return friends, utils.Wrap(f.DB.Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "") } -func (f *Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) { - friend = &Friend{} +func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendModel, err error) { + friend = &FriendModel{} defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend) return friend, utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "") } -func (f *Friend) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*Friend, err error) { +func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "userID1", userID1, "userID2", userID2) }()