group_service.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "errors"
  4. "xps/datamodels"
  5. "xps/datasource"
  6. "xps/repositories"
  7. "xps/viewmodels"
  8. )
  9. type GroupService interface {
  10. GetList(m map[string]interface{})([]viewmodels.GroupInfo, error)
  11. GetById(ocId int64 ,groupId int64)(*viewmodels.GroupInfo, error)
  12. Create(group *datamodels.Group) error
  13. Update(group *datamodels.Group) error
  14. Delete(m map[string]interface{}) error
  15. }
  16. type groupService struct {
  17. groupRepo *repositories.GroupRepo
  18. groupMemberRepo *repositories.GroupMemberRepo
  19. groupMemberPositionRepo *repositories.GroupMemberPositionRepo
  20. //groupMemberPositionGrantRepo *base.GroupMemberPositionGrantRepo
  21. }
  22. func NewGroupService() GroupService {
  23. return &groupService{
  24. groupRepo: repositories.NewGroupRepo(datasource.InstanceMaster()),
  25. groupMemberRepo : repositories.NewGroupMemberRepo(datasource.InstanceMaster()),
  26. groupMemberPositionRepo : repositories.NewGroupMemberPositionRepo(datasource.InstanceMaster()),
  27. //groupMemberPositionGrantRepo : base.NewGroupMemberPositionGrantRepo(datasource.InstanceMaster()),
  28. }
  29. }
  30. func (s *groupService) GetList(m map[string]interface{})([]viewmodels.GroupInfo, error) {
  31. return s.groupRepo.GetList(m)
  32. }
  33. func (s *groupService) GetById(ocId int64 ,gid int64)(*viewmodels.GroupInfo, error) {
  34. return s.groupRepo.GetById(ocId ,gid)
  35. }
  36. func (s *groupService) Update(a *datamodels.Group) error{
  37. return s.groupRepo.Update(a)
  38. }
  39. // Create the group
  40. func (s *groupService) Create(g *datamodels.Group) error {
  41. ocId := g.OcId
  42. parentId := g.ParentId
  43. var parentNodeRight = int64(0)
  44. var parentNodeLevel = int64(0)
  45. parent, err := s.groupRepo.GetById(ocId, parentId)
  46. if err != nil {
  47. return err
  48. }
  49. if parent != nil {
  50. parentNodeRight = parent.NodeRight
  51. parentNodeLevel = parent.NodeLevel
  52. err := s.groupRepo.IncrementGroupRight(ocId ,2 ,parentNodeRight)
  53. if err != nil {
  54. return err
  55. }
  56. err = s.groupRepo.IncrementGroupLeft(ocId ,2 ,parentNodeRight)
  57. if err != nil {
  58. return err
  59. }
  60. // refresh parent node as non-leaf status
  61. err = s.groupRepo.RefreshLeafStatus(ocId ,parentId, false)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. g.GroupId = NewID()
  67. g.NodeLeft = parentNodeRight
  68. g.NodeRight = parentNodeRight + 1
  69. g.NodeLevel = parentNodeLevel + 1
  70. g.RootId = ocId
  71. g.IsLeaf = 1
  72. return s.groupRepo.Create(g)
  73. }
  74. func (s *groupService) Delete(m map[string]interface{}) error {
  75. ocId := m["ocId"].(int64)
  76. groupId := m["groupId"].(int64)
  77. group, err := s.groupRepo.GetById(ocId ,groupId)
  78. if err != nil{
  79. return err
  80. }
  81. if group.IsFixed == 1 {
  82. return errors.New("不能删除锁定群组")
  83. }
  84. nodeLeft := group.NodeLeft
  85. nodeRight := group.NodeRight
  86. parentId := group.ParentId
  87. // delete the groups
  88. err = s.groupRepo.BatchDelete(ocId ,nodeLeft ,nodeRight)
  89. if err != nil {
  90. return err
  91. }
  92. steps := nodeRight - nodeLeft + 1
  93. // update the left
  94. err = s.groupRepo.DecrementGroupLeft(ocId ,steps ,nodeLeft)
  95. if err != nil {
  96. return err
  97. }
  98. // update the right
  99. err = s.groupRepo.DecrementGroupRight(ocId ,steps ,nodeRight)
  100. if err != nil {
  101. return err
  102. }
  103. // update the group parent leaf status
  104. countOfBrothers, err := s.groupRepo.CountOfChildren(ocId ,parentId)
  105. if err != nil {
  106. return err
  107. }
  108. if (countOfBrothers < 1){
  109. err = s.groupRepo.RefreshLeafStatus(ocId ,parentId, true)
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. // delete the group members
  115. err = s.groupMemberRepo.DeleteByGroupId(ocId ,groupId)
  116. if err != nil {
  117. return err
  118. }
  119. // delete the group member position
  120. err = s.groupMemberPositionRepo.DeleteByGroupId(ocId ,groupId)
  121. if err != nil {
  122. return err
  123. }
  124. // delete the group member position grant
  125. //err = s.groupMemberPositionGrantRepo.DeleteByGroupId(ocId ,gid)
  126. return err
  127. }