biz_object_oam_service.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. "xps/cache"
  6. "xps/constant"
  7. "xps/datamodel"
  8. "xps/datasource"
  9. "xps/repositories"
  10. "xps/viewmodel"
  11. )
  12. type BizObjectOAMService interface {
  13. GetList(m map[string]interface{}) ([]viewmodel.BizObjectOAMInfo, error)
  14. GetListById(bizId, objectId int64) ([]viewmodel.BizObjectOAMInfo, error)
  15. GetById(bizId, objectId, oamId int64) (*viewmodel.BizObjectOAMInfo, error)
  16. Create(d *datamodel.BizObjectOAM) error
  17. Update(d *datamodel.BizObjectOAM) error
  18. Delete(m map[string]interface{}) error
  19. CacheBizObjectOAM(bizId, objectId, oamId int64) error
  20. CacheApiData(apiId int64) error
  21. }
  22. type bizObjectOAMService struct {
  23. bizObjectOAMRepo *repositories.BizObjectOAMRepo
  24. apiRepo *repositories.ApiRepo
  25. }
  26. func NewBizObjectOAMService() BizObjectOAMService {
  27. return &bizObjectOAMService{
  28. bizObjectOAMRepo: repositories.NewBizObjectOAMRepo(datasource.InstanceMaster()),
  29. apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
  30. }
  31. }
  32. func (s *bizObjectOAMService) GetList(m map[string]interface{}) ([]viewmodel.BizObjectOAMInfo, error) {
  33. return s.bizObjectOAMRepo.GetList(m)
  34. }
  35. func (s *bizObjectOAMService) GetListById(bizId, objectId int64) ([]viewmodel.BizObjectOAMInfo, error) {
  36. return s.bizObjectOAMRepo.GetListById(bizId, objectId)
  37. }
  38. func (s *bizObjectOAMService) GetById(bizId, objectId, oamId int64) (*viewmodel.BizObjectOAMInfo, error) {
  39. return s.bizObjectOAMRepo.GetById(bizId, objectId, oamId)
  40. }
  41. func (s *bizObjectOAMService) Create(oam *datamodel.BizObjectOAM) error {
  42. oamId := NewID()
  43. oam.OamId = oamId
  44. // 1.新增Object OAM
  45. err1 := s.bizObjectOAMRepo.Create(oam)
  46. if err1 != nil {
  47. return err1
  48. }
  49. // 2.Redis缓存Object OAM
  50. bizId := oam.BizId
  51. objectId := oam.ObjectId
  52. err2 := s.CacheBizObjectOAM(bizId, objectId, oamId)
  53. if err2 != nil {
  54. return err2
  55. }
  56. // 3.生成API
  57. apiId := oamId
  58. apiMethod := s.GetApiMethodByOAMType(oam.OamType)
  59. api := &datamodel.Api{
  60. ApiId: apiId,
  61. ApiMethod: apiMethod,
  62. ApiType: apiMethod,
  63. ApiTitle: oam.OamTitle,
  64. ApiDesc: "",
  65. ApiParams: "",
  66. ApiCode: oam.OamCode,
  67. BizId: oam.BizId,
  68. ObjectId: oam.ObjectId,
  69. OamId: oamId,
  70. Status: 1,
  71. CreatedAt: time.Now(),
  72. CreatedBy: oam.CreatedBy,
  73. }
  74. err3 := s.apiRepo.Create(api)
  75. if err3 != nil {
  76. return err3
  77. }
  78. // 4.缓存API
  79. return s.CacheApiData(apiId)
  80. }
  81. func (s *bizObjectOAMService) Update(oam *datamodel.BizObjectOAM) error {
  82. // 1. Update Object OAM
  83. err := s.bizObjectOAMRepo.Update(oam)
  84. if err != nil {
  85. return err
  86. }
  87. // 2.缓存Object OAM
  88. bizId := oam.BizId
  89. objectId := oam.ObjectId
  90. oamId := oam.OamId
  91. err2 := s.CacheBizObjectOAM(bizId, objectId, oamId)
  92. if err2 != nil {
  93. return err2
  94. }
  95. // 3.更新API
  96. apiId := oam.OamId
  97. apiMethod := s.GetApiMethodByOAMType(oam.OamType)
  98. api := &datamodel.Api {
  99. ApiId: apiId,
  100. ApiMethod: apiMethod,
  101. ApiType: apiMethod,
  102. ApiTitle: oam.OamTitle,
  103. ApiDesc: "",
  104. ApiParams: "",
  105. ApiCode: oam.OamCode,
  106. BizId: oam.BizId,
  107. ObjectId: oam.ObjectId,
  108. OamId: oamId,
  109. Status: 1,
  110. UpdatedAt: time.Now(),
  111. UpdatedBy: oam.UpdatedBy,
  112. }
  113. err3 := s.apiRepo.Update(api)
  114. if err3 != nil {
  115. return err3
  116. }
  117. // 4.更新API缓存数据
  118. return s.CacheApiData(apiId)
  119. }
  120. func (s *bizObjectOAMService) Delete(m map[string]interface{}) error {
  121. // 1.删除Object OAM
  122. err1 := s.bizObjectOAMRepo.Delete(m)
  123. if err1 != nil {
  124. return err1
  125. }
  126. // 2.删除API
  127. m["apiId"] = m["oamId"]
  128. err2 := s.apiRepo.Delete(m)
  129. if err2 != nil {
  130. return err2
  131. }
  132. return nil
  133. }
  134. func (s *bizObjectOAMService) CacheBizObjectOAM(bizId, objectId, oamId int64) error {
  135. oam, err1 := s.bizObjectOAMRepo.GetById(bizId, objectId, oamId)
  136. if err1 != nil {
  137. return err1
  138. }
  139. err3 := cache.SetBizObjectOAM(oam.ObjectCode, oam.OamCode, oam)
  140. if err3 != nil {
  141. return errors.New("Redis缓存Model数据失败")
  142. }
  143. return nil
  144. }
  145. func (s *bizObjectOAMService) CacheApiData(apiId int64) error {
  146. api, err1 := s.apiRepo.GetById(apiId)
  147. if err1 != nil {
  148. return err1
  149. }
  150. objectCode := api.ObjectCode
  151. oamCode := api.OamCode
  152. err3 := cache.SetApiData(objectCode, oamCode, api)
  153. if err3 != nil {
  154. return errors.New("Redis缓存Api数据失败")
  155. }
  156. return nil
  157. }
  158. func (s *bizObjectOAMService) GetApiMethodByOAMType(oamType int64) int64 {
  159. if oamType == constant.OAM_GET_ONE ||
  160. oamType == constant.OAM_GET_LIST ||
  161. oamType == constant.OAM_GET_PAGE {
  162. return constant.API_METHOD_GET
  163. }
  164. if oamType == constant.OAM_CREATE {
  165. return constant.API_METHOD_POST
  166. }
  167. if oamType == constant.OAM_UPDATE {
  168. return constant.API_METHOD_PUT
  169. }
  170. if oamType == constant.OAM_PATCH {
  171. return constant.API_METHOD_PATCH
  172. }
  173. if oamType == constant.OAM_DELETE {
  174. return constant.API_METHOD_DELETE
  175. }
  176. return constant.API_METHOD_GET
  177. }