group.go 1.9 KB

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