api_service.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "errors"
  4. "xps/cache"
  5. "xps/datamodels"
  6. "xps/datasource"
  7. "xps/repositories"
  8. "xps/viewmodels"
  9. )
  10. type ApiService interface {
  11. GetList(m map[string]interface{}) ([]viewmodels.ApiInfo, error)
  12. GetPage(m map[string]interface{}) (*viewmodels.PageResult, error)
  13. GetById(modId, apiId int64) (*viewmodels.ApiInfo, error)
  14. Create(d *datamodels.Api) error
  15. Update(d *datamodels.Api) error
  16. ChangeStatus(modId, apiId, status int64) error
  17. Delete(m map[string]interface{}) error
  18. CacheApiData(modId, apiId int64) error
  19. }
  20. type apiService struct {
  21. apiRepo *repositories.ApiRepo
  22. transRepo *repositories.ApiTransUnitRepo
  23. }
  24. func NewApiService() ApiService {
  25. return &apiService{
  26. apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
  27. transRepo: repositories.NewApiTransUnitRepo(datasource.InstanceMaster()),
  28. }
  29. }
  30. func (s *apiService) GetList(m map[string]interface{}) ([]viewmodels.ApiInfo, error) {
  31. return s.apiRepo.GetList(m)
  32. }
  33. func (s *apiService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
  34. return s.apiRepo.GetPage(m)
  35. }
  36. func (s *apiService) GetById(modId, apiId int64) (*viewmodels.ApiInfo, error) {
  37. return s.apiRepo.GetById(modId, apiId)
  38. }
  39. func (s *apiService) Create(api *datamodels.Api) error {
  40. api.ApiId = NewID()
  41. err := s.apiRepo.Create(api)
  42. if err != nil {
  43. return err
  44. }
  45. modId := api.ModId
  46. apiId := api.ApiId
  47. return s.CacheApiData(modId, apiId)
  48. }
  49. func (s *apiService) Update(api *datamodels.Api) error {
  50. err := s.apiRepo.Update(api)
  51. if err != nil {
  52. return err
  53. }
  54. modId := api.ModId
  55. apiId := api.ApiId
  56. return s.CacheApiData(modId, apiId)
  57. }
  58. func (s *apiService) ChangeStatus(modId, apiId, status int64) error {
  59. data := &datamodels.Api{
  60. ModId: modId,
  61. ApiId: apiId,
  62. Status: status,
  63. }
  64. err := s.apiRepo.Update(data)
  65. if err != nil {
  66. return err
  67. }
  68. return s.CacheApiData(modId, apiId)
  69. }
  70. func (s *apiService) Delete(m map[string]interface{}) error {
  71. return s.apiRepo.Delete(m)
  72. }
  73. func (s *apiService) CacheApiData(modId, apiId int64) error {
  74. api, err := s.apiRepo.GetById(modId, apiId)
  75. if err != nil {
  76. return nil
  77. }
  78. trans, err := s.transRepo.GetListById(apiId)
  79. if err != nil {
  80. return nil
  81. }
  82. cacheData := &viewmodels.ApiDataInfo{
  83. ModId: api.ModId,
  84. ModTitle: api.ModTitle,
  85. ApiId: api.ApiId,
  86. ApiCode: api.ApiCode,
  87. ApiTitle: api.ApiTitle,
  88. ApiType: api.ApiType,
  89. ApiParams: api.ApiParams,
  90. ApiDesc: api.ApiDesc,
  91. Status: api.Status,
  92. TransUnits: trans,
  93. }
  94. // 缓存数据
  95. err = cache.SetApiData(api.ApiCode, cacheData)
  96. if err != nil {
  97. return errors.New("Redis缓存Model数据失败")
  98. }
  99. return nil
  100. }