package service import ( "errors" "xps/cache" "xps/datamodels" "xps/datasource" "xps/repositories" "xps/viewmodels" ) type BizObjectMappingService interface { GetList(m map[string]interface{}) ([]viewmodels.BizObjectMappingInfo, error) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) GetById(bizId, objectId, mappingId int64) (*viewmodels.BizObjectMappingInfo, error) GetListById(bizId, objectId int64) ([]viewmodels.BizObjectMappingInfo, error) Create(d *datamodels.BizObjectMapping) error Update(d *datamodels.BizObjectMapping) error Delete(m map[string]interface{}) error CacheBizObjectMapping(bizId, objectId int64) error } type bizObjectMappingService struct { repo *repositories.BizObjectMappingRepo } func NewBizObjectMappingService() BizObjectMappingService { return &bizObjectMappingService{ repo: repositories.NewBizObjectMappingRepo(datasource.InstanceMaster()), } } func (s *bizObjectMappingService) GetList(m map[string]interface{}) ([]viewmodels.BizObjectMappingInfo, error) { return s.repo.GetList(m) } func (s *bizObjectMappingService) GetListById(bizId, objectId int64) ([]viewmodels.BizObjectMappingInfo, error) { return s.repo.GetListById(bizId, objectId) } func (s *bizObjectMappingService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) { return s.repo.GetPage(m) } func (s *bizObjectMappingService) GetById(bizId, objectId, attrId int64) (*viewmodels.BizObjectMappingInfo, error) { return s.repo.GetById(bizId, objectId, attrId) } func (s *bizObjectMappingService) Create(attr *datamodels.BizObjectMapping) error { attr.MappingId = NewID() err := s.repo.Create(attr) if err != nil { return nil } bizId := attr.BizId objectId := attr.ObjectId return s.CacheBizObjectMapping(bizId, objectId) } func (s *bizObjectMappingService) Update(attr *datamodels.BizObjectMapping) error { err := s.repo.Update(attr) if err != nil { return nil } bizId := attr.BizId objectId := attr.ObjectId return s.CacheBizObjectMapping(bizId, objectId) } func (s *bizObjectMappingService) Delete(m map[string]interface{}) error { return s.repo.Delete(m) } func (s *bizObjectMappingService) CacheBizObjectMapping(bizId, objectId int64) error { mappingList, err1 := s.repo.GetListById(bizId, objectId) if err1 != nil { return err1 } if len(mappingList) <= 0 { return errors.New("biz Object Mapping Not Exist") } objectCode := mappingList[0].ObjectCode err2 := cache.SetBizObjectMapping(objectCode, mappingList) if err2 != nil { return errors.New("Redis缓存Model数据失败") } return nil }