group.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "github.com/lejianwen/rustdesk-api/v2/model"
  4. "gorm.io/gorm"
  5. )
  6. type GroupService struct {
  7. }
  8. // InfoById 根据用户id取用户信息
  9. func (us *GroupService) InfoById(id uint) *model.Group {
  10. u := &model.Group{}
  11. DB.Where("id = ?", id).First(u)
  12. return u
  13. }
  14. func (us *GroupService) List(page, pageSize uint, where func(tx *gorm.DB)) (res *model.GroupList) {
  15. res = &model.GroupList{}
  16. res.Page = int64(page)
  17. res.PageSize = int64(pageSize)
  18. tx := DB.Model(&model.Group{})
  19. if where != nil {
  20. where(tx)
  21. }
  22. tx.Count(&res.Total)
  23. tx.Scopes(Paginate(page, pageSize))
  24. tx.Find(&res.Groups)
  25. return
  26. }
  27. // Create 创建
  28. func (us *GroupService) Create(u *model.Group) error {
  29. res := DB.Create(u).Error
  30. return res
  31. }
  32. func (us *GroupService) Delete(u *model.Group) error {
  33. return DB.Delete(u).Error
  34. }
  35. // Update 更新
  36. func (us *GroupService) Update(u *model.Group) error {
  37. return DB.Model(u).Updates(u).Error
  38. }
  39. // DeviceGroupInfoById 根据用户id取用户信息
  40. func (us *GroupService) DeviceGroupInfoById(id uint) *model.DeviceGroup {
  41. u := &model.DeviceGroup{}
  42. DB.Where("id = ?", id).First(u)
  43. return u
  44. }
  45. func (us *GroupService) DeviceGroupList(page, pageSize uint, where func(tx *gorm.DB)) (res *model.DeviceGroupList) {
  46. res = &model.DeviceGroupList{}
  47. res.Page = int64(page)
  48. res.PageSize = int64(pageSize)
  49. tx := DB.Model(&model.DeviceGroup{})
  50. if where != nil {
  51. where(tx)
  52. }
  53. tx.Count(&res.Total)
  54. tx.Scopes(Paginate(page, pageSize))
  55. tx.Find(&res.DeviceGroups)
  56. return
  57. }
  58. func (us *GroupService) DeviceGroupCreate(u *model.DeviceGroup) error {
  59. res := DB.Create(u).Error
  60. return res
  61. }
  62. func (us *GroupService) DeviceGroupDelete(u *model.DeviceGroup) error {
  63. return DB.Delete(u).Error
  64. }
  65. func (us *GroupService) DeviceGroupUpdate(u *model.DeviceGroup) error {
  66. return DB.Model(u).Updates(u).Error
  67. }