package system import ( "errors" "xps/datamodels" "xps/datasource" "xps/repositories/system" "xps/viewmodels" ) type GroupService interface { GetByList(m map[string]interface{})([]viewmodels.GroupInfo, error) GetById(ocId int64 ,groupId int64)(*viewmodels.GroupInfo, error) Create(group *datamodels.Group) error Update(group *datamodels.Group) error Delete(m map[string]interface{}) error } type groupService struct { groupRepo *system.GroupRepo groupMemberRepo *system.GroupMemberRepo groupMemberPositionRepo *system.GroupMemberPositionRepo //groupMemberPositionGrantRepo *base.GroupMemberPositionGrantRepo } func NewGroupService() GroupService{ return &groupService{ groupRepo: system.NewGroupRepo(datasource.InstanceMaster()), groupMemberRepo : system.NewGroupMemberRepo(datasource.InstanceMaster()), groupMemberPositionRepo : system.NewGroupMemberPositionRepo(datasource.InstanceMaster()), //groupMemberPositionGrantRepo : base.NewGroupMemberPositionGrantRepo(datasource.InstanceMaster()), } } func (s *groupService) GetByList(m map[string]interface{})([]viewmodels.GroupInfo, error) { return s.groupRepo.GetByList(m) } func (s *groupService) GetById(ocId int64 ,gid int64)(*viewmodels.GroupInfo, error) { return s.groupRepo.GetById(ocId ,gid) } func (s *groupService) Update(a *datamodels.Group) error{ return s.groupRepo.Update(a) } // Create the group func (s *groupService) Create(g *datamodels.Group) error { ocId := g.OcId parentId := g.ParentId var parentNodeRight = int64(0) var parentNodeLevel = int64(0) parent, err := s.groupRepo.GetById(ocId, parentId) if err != nil { return err } if parent != nil { parentNodeRight = parent.NodeRight parentNodeLevel = parent.NodeLevel err := s.groupRepo.IncrementGroupRight(ocId ,2 ,parentNodeRight) if err != nil { return err } err = s.groupRepo.IncrementGroupLeft(ocId ,2 ,parentNodeRight) if err != nil { return err } // refresh parent node as non-leaf status err = s.groupRepo.RefreshLeafStatus(ocId ,parentId, false) if err != nil { return err } } g.GroupId = NewID() g.NodeLeft = parentNodeRight g.NodeRight = parentNodeRight + 1 g.NodeLevel = parentNodeLevel + 1 g.RootId = ocId g.IsLeaf = 1 return s.groupRepo.Create(g) } func (s *groupService) Delete(m map[string]interface{}) error { ocId := m["ocId"].(int64) groupId := m["groupId"].(int64) group, err := s.groupRepo.GetById(ocId ,groupId) if err != nil{ return err } if group.IsFixed == 1 { return errors.New("不能删除锁定群组") } nodeLeft := group.NodeLeft nodeRight := group.NodeRight parentId := group.ParentId // delete the groups err = s.groupRepo.BatchDelete(ocId ,nodeLeft ,nodeRight) if err != nil { return err } steps := nodeRight - nodeLeft + 1 // update the left err = s.groupRepo.DecrementGroupLeft(ocId ,steps ,nodeLeft) if err != nil { return err } // update the right err = s.groupRepo.DecrementGroupRight(ocId ,steps ,nodeRight) if err != nil { return err } // update the group parent leaf status countOfBrothers, err := s.groupRepo.CountOfChildren(ocId ,parentId) if err != nil { return err } if (countOfBrothers < 1){ err = s.groupRepo.RefreshLeafStatus(ocId ,parentId, true) if err != nil { return err } } // delete the group members err = s.groupMemberRepo.DeleteByGroupId(ocId ,groupId) if err != nil { return err } // delete the group member position err = s.groupMemberPositionRepo.DeleteByGroupId(ocId ,groupId) if err != nil { return err } // delete the group member position grant //err = s.groupMemberPositionGrantRepo.DeleteByGroupId(ocId ,gid) return err }