group.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package service
  2. import (
  3. "context"
  4. "icloudapp.cn/tools/service/dependency"
  5. "icloudapp.cn/tools/service/entity"
  6. "icloudapp.cn/tools/service/model"
  7. )
  8. type Group struct {
  9. ctx context.Context
  10. dependency.IGroupRes
  11. query *model.MGroup
  12. }
  13. func NewGroup(ctx context.Context) *Group {
  14. group := &Group{ctx: ctx}
  15. group.query = model.Group
  16. return group
  17. }
  18. // Info 查询单个group信息
  19. func (g *Group) Info(groupId int64) (*entity.Group, error) {
  20. query := model.Group
  21. group, err := query.WithContext(g.ctx).ReadDB().Where(query.GroupID.Eq(groupId)).First()
  22. if err != nil {
  23. return nil, err
  24. }
  25. return group, nil
  26. }
  27. // Infos 批量查询 group 信息
  28. func (g *Group) Infos(groupsID ...int64) (map[int64]*entity.Group, error) {
  29. groups := make(map[int64]*entity.Group, len(groupsID))
  30. groupsInfo, err := g.query.WithContext(g.ctx).ReadDB().Where(g.query.GroupID.In(groupsID...)).Find()
  31. if err != nil {
  32. return groups, err
  33. }
  34. for _, val := range groupsInfo {
  35. groups[val.GroupID] = val
  36. }
  37. for _, groupID := range groupsID {
  38. if _, ok := groups[groupID]; !ok {
  39. groups[groupID] = nil
  40. }
  41. }
  42. return groups, nil
  43. }