作者 郑周

1 部门更新,更新用户ID归属部门

@@ -48,6 +48,7 @@ type ( @@ -48,6 +48,7 @@ type (
48 DepartmentUpdateRequest { 48 DepartmentUpdateRequest {
49 Id int64 `path:"id"` 49 Id int64 `path:"id"`
50 Name string `json:"name"` 50 Name string `json:"name"`
  51 + Ids []int64 `json:"ids"` // 用户ID
51 } 52 }
52 53
53 DepartmentListRequest { 54 DepartmentListRequest {
@@ -31,14 +31,14 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types @@ -31,14 +31,14 @@ func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types
31 var userToken = contextdata.GetUserTokenFromCtx(l.ctx) 31 var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
32 var conn = l.svcCtx.DefaultDBConn() 32 var conn = l.svcCtx.DefaultDBConn()
33 33
34 - _, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().  
35 - WithOffsetLimit(1, 1).WithFindOnly(). 34 + total, _, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  35 + WithCountOnly().
36 WithKV("companyId", userToken.CompanyId). 36 WithKV("companyId", userToken.CompanyId).
37 WithKV("name", req.Name)) 37 WithKV("name", req.Name))
38 if err != nil { 38 if err != nil {
39 return nil, err 39 return nil, err
40 } 40 }
41 - if len(list) > 0 { 41 + if total > 0 {
42 return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)") 42 return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
43 } 43 }
44 44
@@ -31,15 +31,15 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re @@ -31,15 +31,15 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re
31 var userToken = contextdata.GetUserTokenFromCtx(l.ctx) 31 var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
32 var conn = l.svcCtx.DefaultDBConn() 32 var conn = l.svcCtx.DefaultDBConn()
33 33
34 - _, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().  
35 - WithOffsetLimit(1, 1).WithCountOnly(). 34 + total, _, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  35 + WithCountOnly().
36 WithKV("companyId", userToken.CompanyId). 36 WithKV("companyId", userToken.CompanyId).
37 WithKV("notId", req.Id). // 排除自己 37 WithKV("notId", req.Id). // 排除自己
38 WithKV("name", req.Name)) 38 WithKV("name", req.Name))
39 if err != nil { 39 if err != nil {
40 return nil, err 40 return nil, err
41 } 41 }
42 - if len(list) > 0 { 42 + if total > 0 {
43 return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)") 43 return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
44 } 44 }
45 45
@@ -49,10 +49,59 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re @@ -49,10 +49,59 @@ func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (re
49 } 49 }
50 one.Name = req.Name 50 one.Name = req.Name
51 51
  52 + // 更新分组中的用户Id
  53 + var newIdMap = map[int64]int{}
  54 + for i := range req.Ids {
  55 + newIdMap[req.Ids[i]] = 0
  56 + }
  57 +
52 // 更新 58 // 更新
53 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { 59 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
54 _, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one) 60 _, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
55 - return err 61 + if err != nil {
  62 + return err
  63 + }
  64 +
  65 + // 获取公司下的所有用户
  66 + _, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  67 + WithFindOnly().
  68 + WithKV(" companyId", one.CompanyId))
  69 + if err != nil {
  70 + return err
  71 + }
  72 +
  73 + var findIndex = func(ids []int64, id int64) int {
  74 + for i, _ := range ids {
  75 + if ids[i] == id {
  76 + return i
  77 + }
  78 + }
  79 + return -1
  80 + }
  81 + for i := range users {
  82 + var user = users[i]
  83 + if _, ok := newIdMap[user.Id]; ok {
  84 + var targetIndex = findIndex(user.Departments, req.Id)
  85 + if targetIndex == -1 { // 归属分组不存在,则新增
  86 + user.Departments = append(user.Departments)
  87 + _, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
  88 + if err != nil {
  89 + return err
  90 + }
  91 + }
  92 + } else {
  93 + var targetIndex = findIndex(user.Departments, req.Id)
  94 + if targetIndex != -1 { // 归属分组存在,则移除
  95 + user.Departments = append(user.Departments[:targetIndex], user.Departments[targetIndex+1:]...) // 移除分组ID
  96 + _, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
  97 + if err != nil {
  98 + return err
  99 + }
  100 + }
  101 + }
  102 + }
  103 +
  104 + return nil
56 }, true) 105 }, true)
57 if err != nil { 106 if err != nil {
58 return nil, xerr.NewErrMsgErr("分组修改失败", err) 107 return nil, xerr.NewErrMsgErr("分组修改失败", err)
@@ -849,8 +849,9 @@ type DepartmentGetResponse struct { @@ -849,8 +849,9 @@ type DepartmentGetResponse struct {
849 } 849 }
850 850
851 type DepartmentUpdateRequest struct { 851 type DepartmentUpdateRequest struct {
852 - Id int64 `path:"id"`  
853 - Name string `json:"name"` 852 + Id int64 `path:"id"`
  853 + Name string `json:"name"`
  854 + Ids []int64 `json:"ids"` // 用户ID
854 } 855 }
855 856
856 type DepartmentListRequest struct { 857 type DepartmentListRequest struct {