biz_object_oam_service.go 4.8 KB

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