biz_object_oam_service.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "time"
  4. "xps/cmd/constant"
  5. "xps/cmd/datasource"
  6. apiDataModel "xps/pkg/api/datamodel"
  7. apiRepository "xps/pkg/api/repository"
  8. "xps/pkg/biz/datamodel"
  9. "xps/pkg/biz/repository"
  10. "xps/pkg/biz/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. }
  20. type bizObjectOAMService struct {
  21. bizObjectOAMRepo *repository.BizObjectOAMRepo
  22. apiRepo *apiRepository.ApiRepo
  23. }
  24. func NewBizObjectOAMService() BizObjectOAMService {
  25. return &bizObjectOAMService{
  26. bizObjectOAMRepo: repository.NewBizObjectOAMRepo(datasource.InstanceMaster()),
  27. apiRepo: apiRepository.NewApiRepo(datasource.InstanceMaster()),
  28. }
  29. }
  30. func (s *bizObjectOAMService) GetList(m map[string]interface{}) ([]viewmodel.BizObjectOAMInfo, error) {
  31. return s.bizObjectOAMRepo.GetList(m)
  32. }
  33. func (s *bizObjectOAMService) GetListById(bizId, objectId int64) ([]viewmodel.BizObjectOAMInfo, error) {
  34. return s.bizObjectOAMRepo.GetListById(bizId, objectId)
  35. }
  36. func (s *bizObjectOAMService) GetById(bizId, objectId, oamId int64) (*viewmodel.BizObjectOAMInfo, error) {
  37. return s.bizObjectOAMRepo.GetById(bizId, objectId, oamId)
  38. }
  39. func (s *bizObjectOAMService) Create(oam *datamodel.BizObjectOAM) error {
  40. oamId := NewID()
  41. oam.OamId = oamId
  42. // 1.新增Object OAM
  43. err1 := s.bizObjectOAMRepo.Create(oam)
  44. if err1 != nil {
  45. return err1
  46. }
  47. // 2.生成API
  48. apiId := oamId
  49. apiMethod := s.GetApiMethodByOAMType(oam.OamType)
  50. api := &apiDataModel.Api{
  51. ApiId: apiId,
  52. ApiMethod: apiMethod,
  53. ApiType: apiMethod,
  54. ApiTitle: oam.OamTitle,
  55. ApiDesc: "",
  56. ApiParams: "",
  57. ApiCode: oam.OamCode,
  58. BizId: oam.BizId,
  59. ObjectId: oam.ObjectId,
  60. OamId: oamId,
  61. Status: 1,
  62. CreatedAt: time.Now(),
  63. CreatedBy: oam.CreatedBy,
  64. }
  65. err3 := s.apiRepo.Create(api)
  66. if err3 != nil {
  67. return err3
  68. }
  69. return nil
  70. }
  71. func (s *bizObjectOAMService) Update(oam *datamodel.BizObjectOAM) error {
  72. // 1. Update Object OAM
  73. err := s.bizObjectOAMRepo.Update(oam)
  74. if err != nil {
  75. return err
  76. }
  77. bizId := oam.BizId
  78. objectId := oam.ObjectId
  79. oamId := oam.OamId
  80. // 2.更新API
  81. apiId := oam.OamId
  82. apiMethod := s.GetApiMethodByOAMType(oam.OamType)
  83. api := &apiDataModel.Api{
  84. ApiId: apiId,
  85. ApiMethod: apiMethod,
  86. ApiType: apiMethod,
  87. ApiTitle: oam.OamTitle,
  88. ApiDesc: "",
  89. ApiParams: "",
  90. ApiCode: oam.OamCode,
  91. BizId: bizId,
  92. ObjectId: objectId,
  93. OamId: oamId,
  94. Status: 1,
  95. UpdatedAt: time.Now(),
  96. UpdatedBy: oam.UpdatedBy,
  97. }
  98. err3 := s.apiRepo.Update(api)
  99. if err3 != nil {
  100. return err3
  101. }
  102. return nil
  103. }
  104. func (s *bizObjectOAMService) Delete(m map[string]interface{}) error {
  105. // 1.删除Object OAM
  106. err1 := s.bizObjectOAMRepo.Delete(m)
  107. if err1 != nil {
  108. return err1
  109. }
  110. // 2.删除API
  111. m["apiId"] = m["oamId"]
  112. err2 := s.apiRepo.Delete(m)
  113. if err2 != nil {
  114. return err2
  115. }
  116. return nil
  117. }
  118. func (s *bizObjectOAMService) GetApiMethodByOAMType(oamType int64) int64 {
  119. if oamType == constant.OAM_GET_ONE ||
  120. oamType == constant.OAM_GET_LIST ||
  121. oamType == constant.OAM_GET_PAGE {
  122. return constant.API_METHOD_GET
  123. }
  124. if oamType == constant.OAM_CREATE {
  125. return constant.API_METHOD_POST
  126. }
  127. if oamType == constant.OAM_UPDATE {
  128. return constant.API_METHOD_PUT
  129. }
  130. if oamType == constant.OAM_PATCH {
  131. return constant.API_METHOD_PATCH
  132. }
  133. if oamType == constant.OAM_DELETE {
  134. return constant.API_METHOD_DELETE
  135. }
  136. return constant.API_METHOD_GET
  137. }