peer.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package service
  2. import (
  3. "Gwen/global"
  4. "Gwen/model"
  5. "gorm.io/gorm"
  6. )
  7. type PeerService struct {
  8. }
  9. // FindById 根据id查找
  10. func (ps *PeerService) FindById(id string) *model.Peer {
  11. p := &model.Peer{}
  12. global.DB.Where("id = ?", id).First(p)
  13. return p
  14. }
  15. func (ps *PeerService) FindByUuid(uuid string) *model.Peer {
  16. p := &model.Peer{}
  17. global.DB.Where("uuid = ?", uuid).First(p)
  18. return p
  19. }
  20. func (ps *PeerService) InfoByRowId(id uint) *model.Peer {
  21. p := &model.Peer{}
  22. global.DB.Where("row_id = ?", id).First(p)
  23. return p
  24. }
  25. // UuidBindUserId 绑定用户id
  26. func (ps *PeerService) UuidBindUserId(uuid string, userId uint) {
  27. peer := ps.FindByUuid(uuid)
  28. if peer.RowId > 0 {
  29. peer.UserId = userId
  30. ps.Update(peer)
  31. }
  32. }
  33. // ListByUserIds 根据用户id取列表
  34. func (ps *PeerService) ListByUserIds(userIds []uint, page, pageSize uint) (res *model.PeerList) {
  35. res = &model.PeerList{}
  36. res.Page = int64(page)
  37. res.PageSize = int64(pageSize)
  38. tx := global.DB.Model(&model.Peer{})
  39. tx.Where("user_id in (?)", userIds)
  40. tx.Count(&res.Total)
  41. tx.Scopes(Paginate(page, pageSize))
  42. tx.Find(&res.Peers)
  43. return
  44. }
  45. func (ps *PeerService) List(page, pageSize uint, where func(tx *gorm.DB)) (res *model.PeerList) {
  46. res = &model.PeerList{}
  47. res.Page = int64(page)
  48. res.PageSize = int64(pageSize)
  49. tx := global.DB.Model(&model.Peer{})
  50. if where != nil {
  51. where(tx)
  52. }
  53. tx.Count(&res.Total)
  54. tx.Scopes(Paginate(page, pageSize))
  55. tx.Find(&res.Peers)
  56. return
  57. }
  58. // Create 创建
  59. func (ps *PeerService) Create(u *model.Peer) error {
  60. res := global.DB.Create(u).Error
  61. return res
  62. }
  63. func (ps *PeerService) Delete(u *model.Peer) error {
  64. return global.DB.Delete(u).Error
  65. }
  66. // BatchDelete
  67. func (ps *PeerService) BatchDelete(ids []uint) error {
  68. return global.DB.Where("row_id in (?)", ids).Delete(&model.Peer{}).Error
  69. }
  70. // Update 更新
  71. func (ps *PeerService) Update(u *model.Peer) error {
  72. return global.DB.Model(u).Updates(u).Error
  73. }