biz_object_service.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. "xps/cache"
  6. "xps/datamodels"
  7. "xps/datasource"
  8. "xps/repositories"
  9. "xps/viewmodels"
  10. )
  11. type BizObjectService interface {
  12. GetList(m map[string]interface{}) ([]viewmodels.BizObjectInfo, error)
  13. GetPage(m map[string]interface{}) (*viewmodels.PageResult, error)
  14. GetById(bizId, objectId int64) (*viewmodels.BizObjectInfo, error)
  15. Create(d *datamodels.BizObject) error
  16. Update(d *datamodels.BizObject) error
  17. Delete(m map[string]interface{}) error
  18. GetObjectDataById(bizId, objectId int64) (*viewmodels.BizObjectDataInfo, error)
  19. CreateObjectData(d *viewmodels.BizObjectData) error
  20. UpdateObjectData(d *viewmodels.BizObjectData) error
  21. }
  22. type bizObjectService struct {
  23. bizObjectRepo *repositories.BizObjectRepo
  24. bizObjectAttrRepo *repositories.BizObjectAttrRepo
  25. }
  26. func NewBizObjectService() BizObjectService {
  27. return &bizObjectService{
  28. bizObjectRepo: repositories.NewBizObjectRepo(datasource.InstanceMaster()),
  29. bizObjectAttrRepo: repositories.NewBizObjectAttrRepo(datasource.InstanceMaster()),
  30. }
  31. }
  32. func (s *bizObjectService) GetList(m map[string]interface{}) ([]viewmodels.BizObjectInfo, error) {
  33. return s.bizObjectRepo.GetList(m)
  34. }
  35. func (s *bizObjectService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
  36. return s.bizObjectRepo.GetPage(m)
  37. }
  38. func (s *bizObjectService) GetById(bizId, objectId int64) (*viewmodels.BizObjectInfo, error) {
  39. return s.bizObjectRepo.GetById(bizId, objectId)
  40. }
  41. func (s *bizObjectService) Create(Object *datamodels.BizObject) error {
  42. Object.ObjectId = NewID()
  43. return s.bizObjectRepo.Create(Object)
  44. }
  45. func (s *bizObjectService) Update(Object *datamodels.BizObject) error {
  46. return s.bizObjectRepo.Update(Object)
  47. }
  48. func (s *bizObjectService) Delete(m map[string]interface{}) error {
  49. bizId := m["bizId"].(int64)
  50. objectId := m["objectId"].(int64)
  51. err := s.bizObjectRepo.Delete(m)
  52. if err != nil {
  53. return err
  54. }
  55. return s.bizObjectAttrRepo.DeleteByObjectId(bizId, objectId)
  56. }
  57. func (s *bizObjectService) GetObjectDataById(bizId, objectId int64) (*viewmodels.BizObjectDataInfo, error) {
  58. objectInfo, err := s.bizObjectRepo.GetById(bizId, objectId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. attrs, err := s.bizObjectAttrRepo.GetListById(bizId, objectId)
  63. if err != nil {
  64. return nil, err
  65. }
  66. objectDataInfo := &viewmodels.BizObjectDataInfo{
  67. BizId: objectInfo.BizId,
  68. ObjectId: objectInfo.ObjectId,
  69. ObjectType: objectInfo.ObjectType,
  70. ObjectTitle: objectInfo.ObjectTitle,
  71. ObjectCode: objectInfo.ObjectCode,
  72. Comment: objectInfo.Comment,
  73. IsFixed: objectInfo.IsFixed,
  74. Attrs: attrs,
  75. }
  76. return objectDataInfo, nil
  77. }
  78. func (s *bizObjectService) CreateObjectData(o *viewmodels.BizObjectData) error {
  79. bizId := o.BizId
  80. objectId := NewID()
  81. createdBy := o.CreatedBy
  82. object := &datamodels.BizObject{
  83. BizId: bizId,
  84. ObjectId: objectId,
  85. ObjectType: o.ObjectType,
  86. ObjectTitle: o.ObjectTitle,
  87. ObjectCode: o.ObjectCode,
  88. Comment: o.Comment,
  89. IsFixed: 0,
  90. CreatedBy: createdBy,
  91. CreatedAt: time.Now(),
  92. }
  93. err := s.bizObjectRepo.Create(object)
  94. if err != nil {
  95. return err
  96. }
  97. // cache object
  98. attrsCache := make([]viewmodels.BizObjectAttrInfo, 0)
  99. for _, a := range o.Attrs {
  100. newId := NewID()
  101. objectAttr := &datamodels.BizObjectAttr{
  102. BizId: bizId,
  103. ObjectId: objectId,
  104. AttrId: newId,
  105. AttrTitle: a.AttrTitle,
  106. AttrCode: a.AttrCode,
  107. AttrType: a.AttrType,
  108. AttrLength: a.AttrLength,
  109. AttrDecimals: a.AttrDecimals,
  110. Comment: a.Comment,
  111. IsNotNull: a.IsNotNull,
  112. CreatedAt: time.Now(),
  113. CreatedBy: createdBy,
  114. }
  115. err = s.bizObjectAttrRepo.Create(objectAttr)
  116. if err != nil {
  117. return err
  118. }
  119. attrCache := viewmodels.BizObjectAttrInfo{
  120. BizId: bizId,
  121. ObjectId: objectId,
  122. AttrId: newId,
  123. AttrTitle: a.AttrTitle,
  124. AttrCode: a.AttrCode,
  125. AttrType: a.AttrType,
  126. AttrLength: a.AttrLength,
  127. AttrDecimals: a.AttrDecimals,
  128. Comment: a.Comment,
  129. IsNotNull: a.IsNotNull,
  130. }
  131. attrsCache = append(attrsCache, attrCache)
  132. }
  133. objectDataCache := &viewmodels.BizObjectDataInfo{
  134. BizId: o.BizId,
  135. BizTitle: o.BizTitle,
  136. ObjectId: o.ObjectId,
  137. ObjectType: o.ObjectType,
  138. ObjectTitle: o.ObjectTitle,
  139. ObjectCode: o.ObjectCode,
  140. Comment: o.Comment,
  141. IsFixed: o.IsFixed,
  142. Attrs: attrsCache,
  143. }
  144. // 缓存数据
  145. err = cache.SetBizObjectData(o.ObjectCode, objectDataCache)
  146. if err != nil {
  147. return errors.New("Redis缓存Biz数据失败")
  148. }
  149. return nil
  150. }
  151. func (s *bizObjectService) UpdateObjectData(o *viewmodels.BizObjectData) error {
  152. bizId := o.BizId
  153. objectId := o.ObjectId
  154. updatedBy := o.UpdatedBy
  155. object := &datamodels.BizObject{
  156. BizId: bizId,
  157. ObjectId: objectId,
  158. ObjectType: o.ObjectType,
  159. ObjectTitle: o.ObjectTitle,
  160. ObjectCode: o.ObjectCode,
  161. Comment: o.Comment,
  162. IsFixed: 0,
  163. UpdatedBy: updatedBy,
  164. }
  165. err := s.bizObjectRepo.Update(object)
  166. if err != nil {
  167. return err
  168. }
  169. err = s.bizObjectAttrRepo.DeleteByObjectId(bizId, objectId)
  170. if err != nil {
  171. return err
  172. }
  173. // cache object
  174. attrsCache := make([]viewmodels.BizObjectAttrInfo, 0)
  175. for _, a := range o.Attrs {
  176. newId := NewID()
  177. objectAttr := &datamodels.BizObjectAttr{
  178. BizId: bizId,
  179. ObjectId: objectId,
  180. AttrId: newId,
  181. AttrTitle: a.AttrTitle,
  182. AttrCode: a.AttrCode,
  183. AttrType: a.AttrType,
  184. AttrLength: a.AttrLength,
  185. AttrDecimals: a.AttrDecimals,
  186. Comment: a.Comment,
  187. IsNotNull: a.IsNotNull,
  188. CreatedBy: updatedBy,
  189. }
  190. err = s.bizObjectAttrRepo.Create(objectAttr)
  191. if err != nil {
  192. return err
  193. }
  194. attrCache := viewmodels.BizObjectAttrInfo{
  195. BizId: bizId,
  196. ObjectId: objectId,
  197. AttrId: newId,
  198. AttrTitle: a.AttrTitle,
  199. AttrCode: a.AttrCode,
  200. AttrType: a.AttrType,
  201. AttrLength: a.AttrLength,
  202. AttrDecimals: a.AttrDecimals,
  203. Comment: a.Comment,
  204. IsNotNull: a.IsNotNull,
  205. }
  206. attrsCache = append(attrsCache, attrCache)
  207. }
  208. objectDataCache := &viewmodels.BizObjectDataInfo{
  209. BizId: o.BizId,
  210. BizTitle: o.BizTitle,
  211. ObjectId: o.ObjectId,
  212. ObjectType: o.ObjectType,
  213. ObjectTitle: o.ObjectTitle,
  214. ObjectCode: o.ObjectCode,
  215. Comment: o.Comment,
  216. IsFixed: o.IsFixed,
  217. Attrs: attrsCache,
  218. }
  219. err = cache.SetBizObjectData(o.ObjectCode, objectDataCache)
  220. if err != nil {
  221. return errors.New("Redis缓存Model数据失败")
  222. }
  223. return nil
  224. }