| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package service
- import (
- "errors"
- "time"
- "xps/cache"
- "xps/datamodels"
- "xps/datasource"
- "xps/repositories"
- "xps/viewmodels"
- )
- type BizObjectService interface {
- GetList(m map[string]interface{}) ([]viewmodels.BizObjectInfo, error)
- GetPage(m map[string]interface{}) (*viewmodels.PageResult, error)
- GetById(bizId, objectId int64) (*viewmodels.BizObjectInfo, error)
- Create(d *datamodels.BizObject) error
- Update(d *datamodels.BizObject) error
- Delete(m map[string]interface{}) error
- GetObjectDataById(bizId, objectId int64) (*viewmodels.BizObjectDataInfo, error)
- CreateObjectData(d *viewmodels.BizObjectData) error
- UpdateObjectData(d *viewmodels.BizObjectData) error
- }
- type bizObjectService struct {
- bizObjectRepo *repositories.BizObjectRepo
- bizObjectAttrRepo *repositories.BizObjectAttrRepo
- }
- func NewBizObjectService() BizObjectService {
- return &bizObjectService{
- bizObjectRepo: repositories.NewBizObjectRepo(datasource.InstanceMaster()),
- bizObjectAttrRepo: repositories.NewBizObjectAttrRepo(datasource.InstanceMaster()),
- }
- }
- func (s *bizObjectService) GetList(m map[string]interface{}) ([]viewmodels.BizObjectInfo, error) {
- return s.bizObjectRepo.GetList(m)
- }
- func (s *bizObjectService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
- return s.bizObjectRepo.GetPage(m)
- }
- func (s *bizObjectService) GetById(bizId, objectId int64) (*viewmodels.BizObjectInfo, error) {
- return s.bizObjectRepo.GetById(bizId, objectId)
- }
- func (s *bizObjectService) Create(Object *datamodels.BizObject) error {
- Object.ObjectId = NewID()
- return s.bizObjectRepo.Create(Object)
- }
- func (s *bizObjectService) Update(Object *datamodels.BizObject) error {
- return s.bizObjectRepo.Update(Object)
- }
- func (s *bizObjectService) Delete(m map[string]interface{}) error {
- bizId := m["bizId"].(int64)
- objectId := m["objectId"].(int64)
- err := s.bizObjectRepo.Delete(m)
- if err != nil {
- return err
- }
- return s.bizObjectAttrRepo.DeleteByObjectId(bizId, objectId)
- }
- func (s *bizObjectService) GetObjectDataById(bizId, objectId int64) (*viewmodels.BizObjectDataInfo, error) {
- objectInfo, err := s.bizObjectRepo.GetById(bizId, objectId)
- if err != nil {
- return nil, err
- }
-
- attrs, err := s.bizObjectAttrRepo.GetListById(bizId, objectId)
- if err != nil {
- return nil, err
- }
-
- objectDataInfo := &viewmodels.BizObjectDataInfo{
- BizId: objectInfo.BizId,
- ObjectId: objectInfo.ObjectId,
- ObjectType: objectInfo.ObjectType,
- ObjectTitle: objectInfo.ObjectTitle,
- ObjectCode: objectInfo.ObjectCode,
- Comment: objectInfo.Comment,
- IsFixed: objectInfo.IsFixed,
- Attrs: attrs,
- }
-
- return objectDataInfo, nil
- }
- func (s *bizObjectService) CreateObjectData(o *viewmodels.BizObjectData) error {
- bizId := o.BizId
- objectId := NewID()
- createdBy := o.CreatedBy
- object := &datamodels.BizObject{
- BizId: bizId,
- ObjectId: objectId,
- ObjectType: o.ObjectType,
- ObjectTitle: o.ObjectTitle,
- ObjectCode: o.ObjectCode,
- Comment: o.Comment,
- IsFixed: 0,
- CreatedBy: createdBy,
- CreatedAt: time.Now(),
- }
-
- err := s.bizObjectRepo.Create(object)
- if err != nil {
- return err
- }
-
- // cache object
- attrsCache := make([]viewmodels.BizObjectAttrInfo, 0)
- for _, a := range o.Attrs {
- newId := NewID()
- objectAttr := &datamodels.BizObjectAttr{
- BizId: bizId,
- ObjectId: objectId,
- AttrId: newId,
- AttrTitle: a.AttrTitle,
- AttrCode: a.AttrCode,
- AttrType: a.AttrType,
- AttrLength: a.AttrLength,
- AttrDecimals: a.AttrDecimals,
- Comment: a.Comment,
- IsNotNull: a.IsNotNull,
- CreatedAt: time.Now(),
- CreatedBy: createdBy,
- }
-
- err = s.bizObjectAttrRepo.Create(objectAttr)
- if err != nil {
- return err
- }
-
- attrCache := viewmodels.BizObjectAttrInfo{
- BizId: bizId,
- ObjectId: objectId,
- AttrId: newId,
- AttrTitle: a.AttrTitle,
- AttrCode: a.AttrCode,
- AttrType: a.AttrType,
- AttrLength: a.AttrLength,
- AttrDecimals: a.AttrDecimals,
- Comment: a.Comment,
- IsNotNull: a.IsNotNull,
- }
-
- attrsCache = append(attrsCache, attrCache)
- }
-
- objectDataCache := &viewmodels.BizObjectDataInfo{
- BizId: o.BizId,
- BizTitle: o.BizTitle,
- ObjectId: o.ObjectId,
- ObjectType: o.ObjectType,
- ObjectTitle: o.ObjectTitle,
- ObjectCode: o.ObjectCode,
- Comment: o.Comment,
- IsFixed: o.IsFixed,
- Attrs: attrsCache,
- }
-
- // 缓存数据
- err = cache.SetBizObjectData(o.ObjectCode, objectDataCache)
- if err != nil {
- return errors.New("Redis缓存Biz数据失败")
- }
-
- return nil
- }
- func (s *bizObjectService) UpdateObjectData(o *viewmodels.BizObjectData) error {
- bizId := o.BizId
- objectId := o.ObjectId
- updatedBy := o.UpdatedBy
- object := &datamodels.BizObject{
- BizId: bizId,
- ObjectId: objectId,
- ObjectType: o.ObjectType,
- ObjectTitle: o.ObjectTitle,
- ObjectCode: o.ObjectCode,
- Comment: o.Comment,
- IsFixed: 0,
- UpdatedBy: updatedBy,
- }
-
- err := s.bizObjectRepo.Update(object)
- if err != nil {
- return err
- }
-
- err = s.bizObjectAttrRepo.DeleteByObjectId(bizId, objectId)
- if err != nil {
- return err
- }
-
- // cache object
- attrsCache := make([]viewmodels.BizObjectAttrInfo, 0)
- for _, a := range o.Attrs {
- newId := NewID()
- objectAttr := &datamodels.BizObjectAttr{
- BizId: bizId,
- ObjectId: objectId,
- AttrId: newId,
- AttrTitle: a.AttrTitle,
- AttrCode: a.AttrCode,
- AttrType: a.AttrType,
- AttrLength: a.AttrLength,
- AttrDecimals: a.AttrDecimals,
- Comment: a.Comment,
- IsNotNull: a.IsNotNull,
- CreatedBy: updatedBy,
- }
-
- err = s.bizObjectAttrRepo.Create(objectAttr)
- if err != nil {
- return err
- }
-
- attrCache := viewmodels.BizObjectAttrInfo{
- BizId: bizId,
- ObjectId: objectId,
- AttrId: newId,
- AttrTitle: a.AttrTitle,
- AttrCode: a.AttrCode,
- AttrType: a.AttrType,
- AttrLength: a.AttrLength,
- AttrDecimals: a.AttrDecimals,
- Comment: a.Comment,
- IsNotNull: a.IsNotNull,
- }
-
- attrsCache = append(attrsCache, attrCache)
- }
-
- objectDataCache := &viewmodels.BizObjectDataInfo{
- BizId: o.BizId,
- BizTitle: o.BizTitle,
- ObjectId: o.ObjectId,
- ObjectType: o.ObjectType,
- ObjectTitle: o.ObjectTitle,
- ObjectCode: o.ObjectCode,
- Comment: o.Comment,
- IsFixed: o.IsFixed,
- Attrs: attrsCache,
- }
-
- err = cache.SetBizObjectData(o.ObjectCode, objectDataCache)
- if err != nil {
- return errors.New("Redis缓存Model数据失败")
- }
-
- return nil
- }
|