sys_dict_data_service.go 2.7 KB

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