Browse Source

set user_id=0 at peers, when the user is deleted

Tao Chen 1 year ago
parent
commit
d85a799d0b
2 changed files with 10 additions and 0 deletions
  1. 5 0
      service/peer.go
  2. 5 0
      service/user.go

+ 5 - 0
service/peer.go

@@ -58,6 +58,11 @@ func (ps *PeerService) UuidUnbindUserId(uuid string, userId uint) {
58 58
 	}
59 59
 }
60 60
 
61
+// EraseUserId 清除用户id, 用于用户删除
62
+func (ps *PeerService) EraseUserId(userId uint) error {
63
+	return global.DB.Model(&model.Peer{}).Where("user_id = ?", userId).Update("user_id", 0).Error
64
+}
65
+
61 66
 // ListByUserIds 根据用户id取列表
62 67
 func (ps *PeerService) ListByUserIds(userIds []uint, page, pageSize uint) (res *model.PeerList) {
63 68
 	res = &model.PeerList{}

+ 5 - 0
service/user.go

@@ -207,6 +207,11 @@ func (us *UserService) Delete(u *model.User) error {
207 207
 		return err
208 208
 	}
209 209
 	tx.Commit()
210
+	// 删除关联的peer
211
+	if err := AllService.PeerService.EraseUserId(u.Id); err != nil {
212
+		tx.Rollback()
213
+		return err
214
+	}
210 215
 	return nil
211 216
 }
212 217