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 }