sys_service.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/pkg/utils"
  8. "gogs.baozhida.cn/zoie/OAuth-core/service"
  9. "gorm.io/gorm"
  10. "Medical_OAuth/app/admin/model"
  11. "Medical_OAuth/app/admin/service/dto"
  12. )
  13. type SysService struct {
  14. service.Service
  15. }
  16. // GetPage 获取SysService列表
  17. func (e *SysService) GetPage(c *dto.SysServicePageReq, list *[]model.SysService, count *int64, p *actions.DataPermission) error {
  18. var err error
  19. var data model.SysService
  20. c.No = c.Name
  21. err = e.Orm.Model(&data).
  22. Scopes(
  23. cDto.MakeCondition(c.GetNeedSearch()),
  24. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  25. actions.Permission(data.TableName(), p),
  26. ).
  27. Find(list).Limit(-1).Offset(-1).
  28. Count(count).Error
  29. if err != nil {
  30. e.Log.Errorf("db error: %s \r", err)
  31. return global.GetFailedErr
  32. }
  33. return nil
  34. }
  35. // Get 获取SysService对象
  36. func (e *SysService) Get(d *dto.SysServiceGetReq, postModel *model.SysService, p *actions.DataPermission) error {
  37. err := e.Orm.
  38. Scopes(actions.Permission(postModel.TableName(), p)).
  39. First(postModel, d.GetId()).Error
  40. if err != nil {
  41. e.Log.Errorf("db error: %s", err)
  42. if errors.Is(err, gorm.ErrRecordNotFound) {
  43. return global.GetNotFoundOrNoPermissionErr
  44. }
  45. return global.GetFailedErr
  46. }
  47. return nil
  48. }
  49. // Insert 创建SysService对象
  50. func (e *SysService) Insert(c *dto.SysServiceInsertReq) error {
  51. var err error
  52. var data model.SysService
  53. err = e.Orm.Model(&data).Where("name = ?", c.Name).First(&data).Error
  54. if err == nil {
  55. e.Log.Errorf("db error: %s", err)
  56. return errors.New("服务名称已存在")
  57. }
  58. var no string
  59. for {
  60. no = utils.GetRandString(8, "", 0)
  61. var i int64
  62. err = e.Orm.Model(&data).Where("no = ?", no).Count(&i).Error
  63. if err != nil {
  64. continue
  65. }
  66. if i == 0 {
  67. break
  68. }
  69. }
  70. c.Generate(&data)
  71. data.No = no
  72. err = e.Orm.Create(&data).Error
  73. if err != nil {
  74. e.Log.Errorf("db error: %s", err)
  75. return global.CreateFailedErr
  76. }
  77. c.Id = data.Id
  78. return nil
  79. }
  80. // Update 修改SysService对象
  81. func (e *SysService) Update(c *dto.SysServiceUpdateReq) error {
  82. var postModel = model.SysService{}
  83. var data = model.SysService{}
  84. err := e.Orm.First(&postModel, c.GetId()).Error
  85. if err != nil {
  86. e.Log.Errorf("db error: %s", err)
  87. if errors.Is(err, gorm.ErrRecordNotFound) {
  88. return global.UpdateNotFoundOrNoPermissionErr
  89. }
  90. return global.UpdateFailedErr
  91. }
  92. err = e.Orm.Model(&data).Where("name = ?", c.Name).First(&data).Error
  93. if err == nil && postModel.Name != c.Name {
  94. e.Log.Errorf("db error: %s", err)
  95. return errors.New("服务名称已存在")
  96. }
  97. c.Generate(&postModel)
  98. err = e.Orm.Save(&postModel).Error
  99. if err != nil {
  100. e.Log.Errorf("db error: %s", err)
  101. return global.UpdateFailedErr
  102. }
  103. return nil
  104. }
  105. // Remove 删除SysService
  106. func (e *SysService) Remove(d *dto.SysServiceDeleteReq) error {
  107. var err error
  108. var data model.SysService
  109. db := e.Orm.Model(&data).Delete(&data, d.GetId())
  110. if err = db.Error; err != nil {
  111. e.Log.Errorf("Delete error: %s", err)
  112. return global.DeleteFailedErr
  113. }
  114. if db.RowsAffected == 0 {
  115. return global.DeleteNotFoundOrNoPermissionErr
  116. }
  117. return nil
  118. }
  119. // Remove 删除SysService
  120. func (e *SysService) Discovery(d *dto.SysServiceDiscoveryReq) error {
  121. var err error
  122. var data model.SysService
  123. err = e.Orm.Model(&data).Where("name = ?", d.Name).First(&data).Error
  124. if err != nil {
  125. if err == gorm.ErrRecordNotFound {
  126. var no string
  127. for {
  128. no = utils.GetRandString(8, "", 0)
  129. var i int64
  130. err = e.Orm.Model(&data).Where("no = ?", no).Count(&i).Error
  131. if err != nil {
  132. continue
  133. }
  134. if i == 0 {
  135. break
  136. }
  137. }
  138. d.Generate(&data)
  139. data.No = no
  140. data.Status = 1
  141. err = e.Orm.Create(&data).Error
  142. if err != nil {
  143. e.Log.Errorf("db error: %s", err)
  144. return global.CreateFailedErr
  145. }
  146. d.Id = data.Id
  147. return nil
  148. }
  149. e.Log.Errorf("Delete error: %s", err)
  150. return global.CreateFailedErr
  151. }
  152. // 有则更新
  153. d.Generate(&data)
  154. err = e.Orm.Save(&data).Error
  155. if err != nil {
  156. e.Log.Errorf("db error: %s", err)
  157. return global.UpdateFailedErr
  158. }
  159. d.Id = data.Id
  160. return nil
  161. }