| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package service
- import (
- "errors"
- "time"
- "xps/cache"
- "xps/constant"
- "xps/datamodel"
- "xps/datasource"
- "xps/repositories"
- "xps/viewmodel"
- )
- type BizObjectOAMService interface {
- GetList(m map[string]interface{}) ([]viewmodel.BizObjectOAMInfo, error)
- GetListById(bizId, objectId int64) ([]viewmodel.BizObjectOAMInfo, error)
- GetById(bizId, objectId, oamId int64) (*viewmodel.BizObjectOAMInfo, error)
- Create(d *datamodel.BizObjectOAM) error
- Update(d *datamodel.BizObjectOAM) error
- Delete(m map[string]interface{}) error
-
- CacheBizObjectOAM(bizId, objectId, oamId int64) error
- CacheApiData(apiId int64) error
- }
- type bizObjectOAMService struct {
- bizObjectOAMRepo *repositories.BizObjectOAMRepo
- apiRepo *repositories.ApiRepo
- }
- func NewBizObjectOAMService() BizObjectOAMService {
- return &bizObjectOAMService{
- bizObjectOAMRepo: repositories.NewBizObjectOAMRepo(datasource.InstanceMaster()),
- apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
- }
- }
- func (s *bizObjectOAMService) GetList(m map[string]interface{}) ([]viewmodel.BizObjectOAMInfo, error) {
- return s.bizObjectOAMRepo.GetList(m)
- }
- func (s *bizObjectOAMService) GetListById(bizId, objectId int64) ([]viewmodel.BizObjectOAMInfo, error) {
- return s.bizObjectOAMRepo.GetListById(bizId, objectId)
- }
- func (s *bizObjectOAMService) GetById(bizId, objectId, oamId int64) (*viewmodel.BizObjectOAMInfo, error) {
- return s.bizObjectOAMRepo.GetById(bizId, objectId, oamId)
- }
- func (s *bizObjectOAMService) Create(oam *datamodel.BizObjectOAM) error {
- oamId := NewID()
- oam.OamId = oamId
-
- // 1.新增Object OAM
- err1 := s.bizObjectOAMRepo.Create(oam)
- if err1 != nil {
- return err1
- }
-
- // 2.Redis缓存Object OAM
- bizId := oam.BizId
- objectId := oam.ObjectId
- err2 := s.CacheBizObjectOAM(bizId, objectId, oamId)
- if err2 != nil {
- return err2
- }
-
- // 3.生成API
- apiId := oamId
- apiMethod := s.GetApiMethodByOAMType(oam.OamType)
- api := &datamodel.Api{
- ApiId: apiId,
- ApiMethod: apiMethod,
- ApiType: apiMethod,
- ApiTitle: oam.OamTitle,
- ApiDesc: "",
- ApiParams: "",
- ApiCode: oam.OamCode,
- BizId: oam.BizId,
- ObjectId: oam.ObjectId,
- OamId: oamId,
- Status: 1,
- CreatedAt: time.Now(),
- CreatedBy: oam.CreatedBy,
- }
- err3 := s.apiRepo.Create(api)
- if err3 != nil {
- return err3
- }
-
- // 4.缓存API
- return s.CacheApiData(apiId)
- }
- func (s *bizObjectOAMService) Update(oam *datamodel.BizObjectOAM) error {
-
- // 1. Update Object OAM
- err := s.bizObjectOAMRepo.Update(oam)
- if err != nil {
- return err
- }
-
- // 2.缓存Object OAM
- bizId := oam.BizId
- objectId := oam.ObjectId
- oamId := oam.OamId
- err2 := s.CacheBizObjectOAM(bizId, objectId, oamId)
- if err2 != nil {
- return err2
- }
-
- // 3.更新API
- apiId := oam.OamId
- apiMethod := s.GetApiMethodByOAMType(oam.OamType)
- api := &datamodel.Api {
- ApiId: apiId,
- ApiMethod: apiMethod,
- ApiType: apiMethod,
- ApiTitle: oam.OamTitle,
- ApiDesc: "",
- ApiParams: "",
- ApiCode: oam.OamCode,
- BizId: oam.BizId,
- ObjectId: oam.ObjectId,
- OamId: oamId,
- Status: 1,
- UpdatedAt: time.Now(),
- UpdatedBy: oam.UpdatedBy,
- }
- err3 := s.apiRepo.Update(api)
- if err3 != nil {
- return err3
- }
-
- // 4.更新API缓存数据
- return s.CacheApiData(apiId)
- }
- func (s *bizObjectOAMService) Delete(m map[string]interface{}) error {
- // 1.删除Object OAM
- err1 := s.bizObjectOAMRepo.Delete(m)
- if err1 != nil {
- return err1
- }
-
- // 2.删除API
- m["apiId"] = m["oamId"]
- err2 := s.apiRepo.Delete(m)
- if err2 != nil {
- return err2
- }
- return nil
- }
- func (s *bizObjectOAMService) CacheBizObjectOAM(bizId, objectId, oamId int64) error {
- oam, err1 := s.bizObjectOAMRepo.GetById(bizId, objectId, oamId)
- if err1 != nil {
- return err1
- }
-
- err3 := cache.SetBizObjectOAM(oam.ObjectCode, oam.OamCode, oam)
- if err3 != nil {
- return errors.New("Redis缓存Model数据失败")
- }
- return nil
- }
- func (s *bizObjectOAMService) CacheApiData(apiId int64) error {
- api, err1 := s.apiRepo.GetById(apiId)
- if err1 != nil {
- return err1
- }
-
- objectCode := api.ObjectCode
- oamCode := api.OamCode
- err3 := cache.SetApiData(objectCode, oamCode, api)
- if err3 != nil {
- return errors.New("Redis缓存Api数据失败")
- }
- return nil
- }
- func (s *bizObjectOAMService) GetApiMethodByOAMType(oamType int64) int64 {
- if oamType == constant.OAM_GET_ONE ||
- oamType == constant.OAM_GET_LIST ||
- oamType == constant.OAM_GET_PAGE {
- return constant.API_METHOD_GET
- }
-
- if oamType == constant.OAM_CREATE {
- return constant.API_METHOD_POST
- }
-
- if oamType == constant.OAM_UPDATE {
- return constant.API_METHOD_PUT
- }
-
- if oamType == constant.OAM_PATCH {
- return constant.API_METHOD_PATCH
- }
-
- if oamType == constant.OAM_DELETE {
- return constant.API_METHOD_DELETE
- }
-
- return constant.API_METHOD_GET
- }
|