ot_plugins.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-admin-team/go-admin-core/sdk/service"
  6. "gorm.io/gorm"
  7. "go-admin/app/admin/models"
  8. "go-admin/app/admin/service/dto"
  9. "go-admin/common/actions"
  10. cDto "go-admin/common/dto"
  11. )
  12. type OtPlugins struct {
  13. service.Service
  14. }
  15. // GetPage 获取OtPlugins列表
  16. func (e *OtPlugins) GetPage(c *dto.OtPluginsGetPageReq, p *actions.DataPermission, list *[]models.OtPlugins, count *int64) error {
  17. var err error
  18. var data models.OtPlugins
  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("OtPluginsService GetPage error:%s \r\n", err)
  29. return err
  30. }
  31. return nil
  32. }
  33. // Get 获取OtPlugins对象
  34. func (e *OtPlugins) Get(d *dto.OtPluginsGetReq, p *actions.DataPermission, model *models.OtPlugins) error {
  35. var data models.OtPlugins
  36. err := e.Orm.Model(&data).
  37. Scopes(
  38. actions.Permission(data.TableName(), p),
  39. ).
  40. First(model, d.GetId()).Error
  41. if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
  42. err = errors.New("查看对象不存在或无权查看")
  43. e.Log.Errorf("Service GetOtPlugins error:%s \r\n", err)
  44. return err
  45. }
  46. if err != nil {
  47. e.Log.Errorf("db error:%s", err)
  48. return err
  49. }
  50. return nil
  51. }
  52. // Insert 创建OtPlugins对象
  53. func (e *OtPlugins) Insert(c *dto.OtPluginsInsertReq) error {
  54. var err error
  55. var data models.OtPlugins
  56. c.Generate(&data)
  57. err = e.Orm.Create(&data).Error
  58. if err != nil {
  59. e.Log.Errorf("OtPluginsService Insert error:%s \r\n", err)
  60. return err
  61. }
  62. return nil
  63. }
  64. // Update 修改OtPlugins对象
  65. func (e *OtPlugins) Update(c *dto.OtPluginsUpdateReq, p *actions.DataPermission) error {
  66. var err error
  67. var data = models.OtPlugins{}
  68. e.Orm.Scopes(
  69. actions.Permission(data.TableName(), p),
  70. ).First(&data, c.GetId())
  71. c.Generate(&data)
  72. db := e.Orm.Save(&data)
  73. if err = db.Error; err != nil {
  74. e.Log.Errorf("OtPluginsService Save error:%s \r\n", err)
  75. return err
  76. }
  77. if db.RowsAffected == 0 {
  78. return errors.New("无权更新该数据")
  79. }
  80. return nil
  81. }
  82. // Remove 删除OtPlugins
  83. func (e *OtPlugins) Remove(d *dto.OtPluginsDeleteReq, p *actions.DataPermission) error {
  84. var data models.OtPlugins
  85. db := e.Orm.Model(&data).
  86. Scopes(
  87. actions.Permission(data.TableName(), p),
  88. ).Delete(&data, d.GetId())
  89. if err := db.Error; err != nil {
  90. e.Log.Errorf("Service RemoveOtPlugins error:%s \r\n", err)
  91. return err
  92. }
  93. if db.RowsAffected == 0 {
  94. return errors.New("无权删除该数据")
  95. }
  96. return nil
  97. }
  98. func (e *OtPlugins) GetPluginByAlias(m *models.OtPlugins) error {
  99. fmt.Println("modelplugin: ", m.Alias)
  100. err := e.Orm.Model(m).
  101. Where("alias = ?", m.Alias).
  102. First(m).Error
  103. if err != nil {
  104. if errors.Is(err, gorm.ErrRecordNotFound) {
  105. err = errors.New("查看对象不存在")
  106. e.Log.Errorf("Service GetOtPluginByAlias error:%s \r\n", err)
  107. return err
  108. } else {
  109. e.Log.Errorf("db error:%s", err)
  110. return err
  111. }
  112. }
  113. return nil
  114. }