作者 zhuangmx
@@ -60,6 +60,7 @@ type ( @@ -60,6 +60,7 @@ type (
60 Page int `json:"page"` 60 Page int `json:"page"`
61 Size int `json:"size"` 61 Size int `json:"size"`
62 IncludeRootCompany bool `json:"includeRootCompany,optional"` // 包含公司(把公司当作部门作为顶级节点 部门ID:0) 62 IncludeRootCompany bool `json:"includeRootCompany,optional"` // 包含公司(把公司当作部门作为顶级节点 部门ID:0)
  63 + IncludeDefaultDepartment bool `json:"includeDefaultDepartment,optional"` // 包含默认分组
63 } 64 }
64 65
65 DepartmentListResponse { 66 DepartmentListResponse {
@@ -286,6 +286,7 @@ type( @@ -286,6 +286,7 @@ type(
286 ParentId int64 `json:"parentId"` // 父级ID 286 ParentId int64 `json:"parentId"` // 父级ID
287 Name string `json:"name"` // 部门名称 287 Name string `json:"name"` // 部门名称
288 UserIds []int64 `json:"userIds"` // 部门下的用户 288 UserIds []int64 `json:"userIds"` // 部门下的用户
  289 + TotalUser int `json:"totalUser"` // 累计用户
289 } 290 }
290 UserSearchRequest{ 291 UserSearchRequest{
291 Page int `json:"page,optional"` 292 Page int `json:"page,optional"`
@@ -478,7 +479,7 @@ type( @@ -478,7 +479,7 @@ type(
478 Phone string `json:"phone,optional"` // 手机号 唯一 479 Phone string `json:"phone,optional"` // 手机号 唯一
479 Position string `json:"position,optional"` // 职位 480 Position string `json:"position,optional"` // 职位
480 Enable int `json:"enable,optional"` // 启用状态 1:启用 2:禁用 481 Enable int `json:"enable,optional"` // 启用状态 1:启用 2:禁用
481 - DepartmentId int64 `json:"departmentId,optional"` // 所属部门 482 + DepartmentId *int64 `json:"departmentId,optional"` // 所属部门
482 } 483 }
483 SystemUserSearchResponse{ 484 SystemUserSearchResponse{
484 List []SystemUser `json:"list"` 485 List []SystemUser `json:"list"`
@@ -2,6 +2,7 @@ package department @@ -2,6 +2,7 @@ package department
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "github.com/samber/lo"
5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
@@ -39,6 +40,28 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty @@ -39,6 +40,28 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty
39 Total: total, 40 Total: total,
40 List: make([]types.Department, 0), 41 List: make([]types.Department, 0),
41 } 42 }
  43 +
  44 + _, users, _ := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  45 + MustWithKV("companyId", userToken.CompanyId).
  46 + WithKV("auditStatus", []int{domain.UserAuditStatusPassed}))
  47 + var counterByDepartment = make(map[int64]int)
  48 + lo.ForEach(users, func(item *domain.User, index int) {
  49 + if len(item.Departments) == 0 {
  50 + if _, ok := counterByDepartment[domain.DefaultDepartmentId]; ok {
  51 + counterByDepartment[domain.DefaultDepartmentId]++
  52 + } else {
  53 + counterByDepartment[domain.DefaultDepartmentId] = 1
  54 + }
  55 + return
  56 + }
  57 + for _, dep := range item.Departments {
  58 + if _, ok := counterByDepartment[dep]; ok {
  59 + counterByDepartment[dep]++
  60 + } else {
  61 + counterByDepartment[dep] = 1
  62 + }
  63 + }
  64 + })
42 if req.IncludeRootCompany { 65 if req.IncludeRootCompany {
43 company, _ := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId) 66 company, _ := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId)
44 if company != nil { 67 if company != nil {
@@ -47,9 +70,23 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty @@ -47,9 +70,23 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty
47 CompanyId: company.Id, 70 CompanyId: company.Id,
48 ParentId: -1, 71 ParentId: -1,
49 Name: company.Name, 72 Name: company.Name,
  73 + TotalUser: len(users),
50 }) 74 })
51 } 75 }
  76 + if req.IncludeDefaultDepartment {
  77 + to := types.Department{
  78 + Id: domain.DefaultDepartmentId,
  79 + CompanyId: company.Id,
  80 + ParentId: 0,
  81 + Name: "默认分组",
  82 + }
  83 + if v, ok := counterByDepartment[domain.DefaultDepartmentId]; ok {
  84 + to.TotalUser = v
  85 + }
  86 + resp.List = append(resp.List, to)
  87 + }
52 } 88 }
  89 +
53 for _, item := range list { 90 for _, item := range list {
54 to := types.Department{ 91 to := types.Department{
55 Id: item.Id, 92 Id: item.Id,
@@ -57,6 +94,9 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty @@ -57,6 +94,9 @@ func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *ty
57 ParentId: item.ParentId, 94 ParentId: item.ParentId,
58 Name: item.Name, 95 Name: item.Name,
59 } 96 }
  97 + if v, ok := counterByDepartment[item.Id]; ok {
  98 + to.TotalUser = v
  99 + }
60 resp.List = append(resp.List, to) 100 resp.List = append(resp.List, to)
61 } 101 }
62 return resp, nil 102 return resp, nil
@@ -61,7 +61,7 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs @@ -61,7 +61,7 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs
61 groupUserSet := collection.NewSet() 61 groupUserSet := collection.NewSet()
62 for _, user := range users { 62 for _, user := range users {
63 // 未分配部门的归类到公司底下 63 // 未分配部门的归类到公司底下
64 - if len(user.Departments) == 0 { 64 + if item.Id == 0 && len(user.Departments) == 0 {
65 if !groupUserSet.Contains(user.Id) { 65 if !groupUserSet.Contains(user.Id) {
66 group.Users = append(group.Users, &domain.User{ 66 group.Users = append(group.Users, &domain.User{
67 Id: user.Id, 67 Id: user.Id,
@@ -41,8 +41,11 @@ func (l *SystemUserSearchLogic) SystemUserSearch(req *types.SystemUserSearchRequ @@ -41,8 +41,11 @@ func (l *SystemUserSearchLogic) SystemUserSearch(req *types.SystemUserSearchRequ
41 WithKV("likePhone", req.Phone). 41 WithKV("likePhone", req.Phone).
42 WithKV("position", req.Position). 42 WithKV("position", req.Position).
43 WithKV("enable", req.Enable). 43 WithKV("enable", req.Enable).
44 - WithKV("departmentId", req.DepartmentId).  
45 WithKV("auditStatus", []int{domain.UserAuditStatusPassed}) 44 WithKV("auditStatus", []int{domain.UserAuditStatusPassed})
  45 +
  46 + if req.DepartmentId != nil {
  47 + queryOptions.MustWithKV("departmentId", *req.DepartmentId)
  48 + }
46 if total, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, queryOptions); err != nil { 49 if total, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, queryOptions); err != nil {
47 return nil, xerr.NewErr(err) 50 return nil, xerr.NewErr(err)
48 } 51 }
@@ -659,6 +659,7 @@ type Department struct { @@ -659,6 +659,7 @@ type Department struct {
659 ParentId int64 `json:"parentId"` // 父级ID 659 ParentId int64 `json:"parentId"` // 父级ID
660 Name string `json:"name"` // 部门名称 660 Name string `json:"name"` // 部门名称
661 UserIds []int64 `json:"userIds"` // 部门下的用户 661 UserIds []int64 `json:"userIds"` // 部门下的用户
  662 + TotalUser int `json:"totalUser"` // 累计用户
662 } 663 }
663 664
664 type UserSearchRequest struct { 665 type UserSearchRequest struct {
@@ -808,7 +809,7 @@ type SystemUserSearchRequest struct { @@ -808,7 +809,7 @@ type SystemUserSearchRequest struct {
808 Phone string `json:"phone,optional"` // 手机号 唯一 809 Phone string `json:"phone,optional"` // 手机号 唯一
809 Position string `json:"position,optional"` // 职位 810 Position string `json:"position,optional"` // 职位
810 Enable int `json:"enable,optional"` // 启用状态 1:启用 2:禁用 811 Enable int `json:"enable,optional"` // 启用状态 1:启用 2:禁用
811 - DepartmentId int64 `json:"departmentId,optional"` // 所属部门 812 + DepartmentId *int64 `json:"departmentId,optional"` // 所属部门
812 } 813 }
813 814
814 type SystemUserSearchResponse struct { 815 type SystemUserSearchResponse struct {
@@ -1770,9 +1771,10 @@ type DepartmentUpdateRequest struct { @@ -1770,9 +1771,10 @@ type DepartmentUpdateRequest struct {
1770 } 1771 }
1771 1772
1772 type DepartmentListRequest struct { 1773 type DepartmentListRequest struct {
1773 - Page int `json:"page"`  
1774 - Size int `json:"size"`  
1775 - IncludeRootCompany bool `json:"includeRootCompany,optional"` // 包含公司(把公司当作部门作为顶级节点 部门ID:0) 1774 + Page int `json:"page"`
  1775 + Size int `json:"size"`
  1776 + IncludeRootCompany bool `json:"includeRootCompany,optional"` // 包含公司(把公司当作部门作为顶级节点 部门ID:0)
  1777 + IncludeDefaultDepartment bool `json:"includeDefaultDepartment,optional"` // 包含默认分组
1776 } 1778 }
1777 1779
1778 type DepartmentListResponse struct { 1780 type DepartmentListResponse struct {
@@ -174,7 +174,12 @@ func (repository *UserRepository) Find(ctx context.Context, conn transaction.Con @@ -174,7 +174,12 @@ func (repository *UserRepository) Find(ctx context.Context, conn transaction.Con
174 tx.Where("position = ? ", v) 174 tx.Where("position = ? ", v)
175 } 175 }
176 if v, ok := queryOptions["departmentId"]; ok { 176 if v, ok := queryOptions["departmentId"]; ok {
177 - tx.Where(fmt.Sprintf("departments @>'[%v]'", v)) 177 + // 未分组用户部门ID=0
  178 + if vi, okVi := v.(int64); okVi && vi == domain.DefaultDepartmentId {
  179 + tx.Where(fmt.Sprintf("departments = '[]'"))
  180 + } else {
  181 + tx.Where(fmt.Sprintf("departments @>'[%v]'", v))
  182 + }
178 } 183 }
179 if v, ok := queryOptions["roleId"]; ok { 184 if v, ok := queryOptions["roleId"]; ok {
180 tx.Where(fmt.Sprintf("roles @>'[%v]'", v)) 185 tx.Where(fmt.Sprintf("roles @>'[%v]'", v))
@@ -31,3 +31,5 @@ func (m *Department) Identify() interface{} { @@ -31,3 +31,5 @@ func (m *Department) Identify() interface{} {
31 } 31 }
32 return m.Id 32 return m.Id
33 } 33 }
  34 +
  35 +const DefaultDepartmentId = -1