sys_post.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "Medical_OAuth/common/actions"
  4. cDto "Medical_OAuth/common/dto"
  5. "Medical_OAuth/common/global"
  6. "errors"
  7. "gogs.baozhida.cn/zoie/OAuth-core/service"
  8. "gorm.io/gorm"
  9. "Medical_OAuth/app/admin/model"
  10. "Medical_OAuth/app/admin/service/dto"
  11. )
  12. type SysPost struct {
  13. service.Service
  14. }
  15. // GetPage 获取SysPost列表
  16. func (e *SysPost) GetPage(c *dto.SysPostPageReq, list *[]model.SysPost, count *int64, p *actions.DataPermission) error {
  17. var err error
  18. var data model.SysPost
  19. err = e.Orm.Model(&data).
  20. Scopes(
  21. cDto.MakeCondition(c.GetNeedSearch()),
  22. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  23. actions.Permission(data.TableName(), p),
  24. ).
  25. Find(list).Limit(-1).Offset(-1).
  26. Count(count).Error
  27. if err != nil {
  28. e.Log.Errorf("db error: %s \r", err)
  29. return global.GetFailedErr
  30. }
  31. return nil
  32. }
  33. // Get 获取SysPost对象
  34. func (e *SysPost) Get(d *dto.SysPostGetReq, postModel *model.SysPost, p *actions.DataPermission) error {
  35. err := e.Orm.
  36. Scopes(actions.Permission(postModel.TableName(), p)).
  37. First(postModel, d.GetId()).Error
  38. if err != nil {
  39. e.Log.Errorf("db error: %s", err)
  40. if errors.Is(err, gorm.ErrRecordNotFound) {
  41. return global.GetNotFoundOrNoPermissionErr
  42. }
  43. return global.GetFailedErr
  44. }
  45. return nil
  46. }
  47. // Insert 创建SysPost对象
  48. func (e *SysPost) Insert(c *dto.SysPostInsertReq) error {
  49. var err error
  50. var data model.SysPost
  51. c.Generate(&data)
  52. err = e.Orm.Create(&data).Error
  53. if err != nil {
  54. e.Log.Errorf("db error: %s", err)
  55. return global.CreateFailedErr
  56. }
  57. c.Id = data.Id
  58. return nil
  59. }
  60. // Update 修改SysPost对象
  61. func (e *SysPost) Update(c *dto.SysPostUpdateReq) error {
  62. var postModel = model.SysPost{}
  63. err := e.Orm.First(&postModel, c.GetId()).Error
  64. if err != nil {
  65. e.Log.Errorf("db error: %s", err)
  66. if errors.Is(err, gorm.ErrRecordNotFound) {
  67. return global.UpdateNotFoundOrNoPermissionErr
  68. }
  69. return global.UpdateFailedErr
  70. }
  71. c.Generate(&postModel)
  72. err = e.Orm.Save(&postModel).Error
  73. if err != nil {
  74. e.Log.Errorf("db error: %s", err)
  75. return global.UpdateFailedErr
  76. }
  77. return nil
  78. }
  79. // Remove 删除SysPost
  80. func (e *SysPost) Remove(d *dto.SysPostDeleteReq) error {
  81. var err error
  82. var data model.SysPost
  83. db := e.Orm.Model(&data).Delete(&data, d.GetId())
  84. if err = db.Error; err != nil {
  85. e.Log.Errorf("Delete error: %s", err)
  86. return global.DeleteFailedErr
  87. }
  88. if db.RowsAffected == 0 {
  89. return global.DeleteNotFoundOrNoPermissionErr
  90. }
  91. return nil
  92. }