| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package service
- import (
- "errors"
- "xps/cache"
- "xps/datamodels"
- "xps/datasource"
- "xps/repositories"
- "xps/viewmodels"
- )
- type ApiService interface {
- GetList(m map[string]interface{}) ([]viewmodels.ApiInfo, error)
- GetPage(m map[string]interface{}) (*viewmodels.PageResult, error)
- GetById(modId, apiId int64) (*viewmodels.ApiInfo, error)
- Create(d *datamodels.Api) error
- Update(d *datamodels.Api) error
- ChangeStatus(modId, apiId, status int64) error
- Delete(m map[string]interface{}) error
- CacheApiData(modId, apiId int64) error
- }
- type apiService struct {
- apiRepo *repositories.ApiRepo
- transRepo *repositories.ApiTransUnitRepo
- }
- func NewApiService() ApiService {
- return &apiService{
- apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
- transRepo: repositories.NewApiTransUnitRepo(datasource.InstanceMaster()),
- }
- }
- func (s *apiService) GetList(m map[string]interface{}) ([]viewmodels.ApiInfo, error) {
- return s.apiRepo.GetList(m)
- }
- func (s *apiService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
- return s.apiRepo.GetPage(m)
- }
- func (s *apiService) GetById(modId, apiId int64) (*viewmodels.ApiInfo, error) {
- return s.apiRepo.GetById(modId, apiId)
- }
- func (s *apiService) Create(api *datamodels.Api) error {
- api.ApiId = NewID()
- err := s.apiRepo.Create(api)
- if err != nil {
- return err
- }
-
- modId := api.ModId
- apiId := api.ApiId
- return s.CacheApiData(modId, apiId)
- }
- func (s *apiService) Update(api *datamodels.Api) error {
- err := s.apiRepo.Update(api)
- if err != nil {
- return err
- }
-
- modId := api.ModId
- apiId := api.ApiId
- return s.CacheApiData(modId, apiId)
- }
- func (s *apiService) ChangeStatus(modId, apiId, status int64) error {
- data := &datamodels.Api{
- ModId: modId,
- ApiId: apiId,
- Status: status,
- }
- err := s.apiRepo.Update(data)
- if err != nil {
- return err
- }
-
- return s.CacheApiData(modId, apiId)
- }
- func (s *apiService) Delete(m map[string]interface{}) error {
- return s.apiRepo.Delete(m)
- }
- func (s *apiService) CacheApiData(modId, apiId int64) error {
- api, err := s.apiRepo.GetById(modId, apiId)
- if err != nil {
- return nil
- }
-
- trans, err := s.transRepo.GetListById(apiId)
- if err != nil {
- return nil
- }
-
- cacheData := &viewmodels.ApiDataInfo{
- ModId: api.ModId,
- ModTitle: api.ModTitle,
- ApiId: api.ApiId,
- ApiCode: api.ApiCode,
- ApiTitle: api.ApiTitle,
- ApiType: api.ApiType,
- ApiParams: api.ApiParams,
- ApiDesc: api.ApiDesc,
- Status: api.Status,
- TransUnits: trans,
- }
-
- // 缓存数据
- err = cache.SetApiData(api.ApiCode, cacheData)
- if err != nil {
- return errors.New("Redis缓存Model数据失败")
- }
-
- return nil
- }
|