package service import ( "errors" "xps/cmd/cache" "xps/cmd/datasource" "xps/pkg/api/datamodel" "xps/pkg/api/repository" "xps/pkg/api/viewmodel" "xps/pkg/base" ) type ApiService interface { GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error) GetPage(m map[string]interface{}) (*base.PageResult, error) GetById(apiId int64) (*viewmodel.ApiInfo, error) Create(d *datamodel.Api) error Update(d *datamodel.Api) error ChangeStatus(apiId, status int64) error Delete(m map[string]interface{}) error CacheApiData(apiId int64) error } type apiService struct { apiRepo *repository.ApiRepo } func NewApiService() ApiService { return &apiService{ apiRepo: repository.NewApiRepo(datasource.InstanceMaster()), } } func (s *apiService) GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error) { return s.apiRepo.GetList(m) } func (s *apiService) GetPage(m map[string]interface{}) (*base.PageResult, error) { return s.apiRepo.GetPage(m) } func (s *apiService) GetById(apiId int64) (*viewmodel.ApiInfo, error) { return s.apiRepo.GetById(apiId) } func (s *apiService) Create(api *datamodel.Api) error { api.ApiId = NewID() err := s.apiRepo.Create(api) if err != nil { return err } apiId := api.ApiId return s.CacheApiData(apiId) } func (s *apiService) Update(api *datamodel.Api) error { err := s.apiRepo.Update(api) if err != nil { return err } apiId := api.ApiId return s.CacheApiData(apiId) } func (s *apiService) ChangeStatus(apiId, status int64) error { data := &datamodel.Api{ ApiId: apiId, Status: status, } err := s.apiRepo.Update(data) if err != nil { return err } return s.CacheApiData(apiId) } func (s *apiService) Delete(m map[string]interface{}) error { return s.apiRepo.Delete(m) } func (s *apiService) CacheApiData(apiId int64) error { api, err := s.apiRepo.GetById(apiId) if err != nil { return nil } // 缓存数据 err = cache.SetApiData(api.ObjectCode, api.OamCode, api) if err != nil { return errors.New("Redis缓存Model数据失败") } return nil }