作者 tangxvhui
@@ -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 {
@@ -14,14 +14,19 @@ info( @@ -14,14 +14,19 @@ info(
14 jwt: SystemAuth 14 jwt: SystemAuth
15 ) 15 )
16 service Core { 16 service Core {
  17 + @doc "角色详情"
17 @handler systemGetRole 18 @handler systemGetRole
18 get /system/role/:id (RoleGetRequest) returns (RoleGetResponse) 19 get /system/role/:id (RoleGetRequest) returns (RoleGetResponse)
  20 + @doc "角色新增"
19 @handler systemSaveRole 21 @handler systemSaveRole
20 post /system/role (RoleSaveRequest) returns (RoleSaveResponse) 22 post /system/role (RoleSaveRequest) returns (RoleSaveResponse)
  23 + @doc "角色删除"
21 @handler systemDeleteRole 24 @handler systemDeleteRole
22 delete /system/role/:id (RoleDeleteRequest) returns (RoleDeleteResponse) 25 delete /system/role/:id (RoleDeleteRequest) returns (RoleDeleteResponse)
  26 + @doc "角色更新"
23 @handler systemUpdateRole 27 @handler systemUpdateRole
24 put /system/role/:id (RoleUpdateRequest) returns (RoleUpdateResponse) 28 put /system/role/:id (RoleUpdateRequest) returns (RoleUpdateResponse)
  29 + @doc "角色列表搜索"
25 @handler systemSearchRole 30 @handler systemSearchRole
26 post /system/role/search (RoleSearchRequest) returns (RoleSearchResponse) 31 post /system/role/search (RoleSearchRequest) returns (RoleSearchResponse)
27 } 32 }
@@ -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)
  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 {
55 return err 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)
@@ -33,9 +33,10 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs @@ -33,9 +33,10 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs
33 userToken = contextdata.GetUserTokenFromCtx(l.ctx) 33 userToken = contextdata.GetUserTokenFromCtx(l.ctx)
34 users []*domain.User 34 users []*domain.User
35 departments []*domain.Department 35 departments []*domain.Department
  36 + groups = make([]DepartmentUser, 0)
36 ) 37 )
37 resp = map[string]interface{}{ 38 resp = map[string]interface{}{
38 - "list": make([]DepartmentUser, 0), 39 + "list": groups,
39 } 40 }
40 _, departments, err = l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly()) 41 _, departments, err = l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithFindOnly())
41 if err != nil { 42 if err != nil {
@@ -60,11 +61,15 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs @@ -60,11 +61,15 @@ func (l *MiniUserDepartmentUsersLogic) MiniUserDepartmentUsers(req *types.MiniUs
60 group.Users = append(group.Users, &domain.User{ 61 group.Users = append(group.Users, &domain.User{
61 Id: user.Id, 62 Id: user.Id,
62 Name: user.Name, 63 Name: user.Name,
  64 + PinYinName: user.PinYinName,
63 }) 65 })
64 } 66 }
65 } 67 }
  68 + groups = append(groups, group)
66 }) 69 })
67 - 70 + resp = map[string]interface{}{
  71 + "list": groups,
  72 + }
68 return 73 return
69 } 74 }
70 75
@@ -2,6 +2,7 @@ package user @@ -2,6 +2,7 @@ package user
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/interanl/pkg/db/transaction" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
@@ -33,6 +34,9 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System @@ -33,6 +34,9 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System
33 users []*domain.User 34 users []*domain.User
34 userToken = contextdata.GetUserTokenFromCtx(l.ctx) 35 userToken = contextdata.GetUserTokenFromCtx(l.ctx)
35 ) 36 )
  37 + if !lo.Contains([]int{domain.UserEnable, domain.UserDisable}, req.Status) {
  38 + return nil, xerr.NewErrMsg("启用状态有误")
  39 + }
36 if _, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().MustWithKV("ids", req.UserIds).WithFindOnly()); err != nil { 40 if _, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().MustWithKV("ids", req.UserIds).WithFindOnly()); err != nil {
37 return nil, xerr.NewErrMsgErr("公司不存在", err) 41 return nil, xerr.NewErrMsgErr("公司不存在", err)
38 } 42 }
@@ -50,5 +54,6 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System @@ -50,5 +54,6 @@ func (l *SystemUserAccountEnableLogic) SystemUserAccountEnable(req *types.System
50 }, true); err != nil { 54 }, true); err != nil {
51 return nil, xerr.NewErrMsgErr("更新启用状态失败", err) 55 return nil, xerr.NewErrMsgErr("更新启用状态失败", err)
52 } 56 }
  57 + resp = &types.SystemUserAccountEnableResponse{}
53 return 58 return
54 } 59 }
@@ -916,6 +916,7 @@ type DepartmentGetResponse struct { @@ -916,6 +916,7 @@ type DepartmentGetResponse struct {
916 type DepartmentUpdateRequest struct { 916 type DepartmentUpdateRequest struct {
917 Id int64 `path:"id"` 917 Id int64 `path:"id"`
918 Name string `json:"name"` 918 Name string `json:"name"`
  919 + Ids []int64 `json:"ids"` // 用户ID
919 } 920 }
920 921
921 type DepartmentListRequest struct { 922 type DepartmentListRequest struct {
@@ -211,8 +211,8 @@ func (repository *UserRepository) FindDepartmentUsers(ctx context.Context, conn @@ -211,8 +211,8 @@ func (repository *UserRepository) FindDepartmentUsers(ctx context.Context, conn
211 total int64 211 total int64
212 ) 212 )
213 queryFunc := func() (interface{}, error) { 213 queryFunc := func() (interface{}, error) {
214 - tx = tx.Model(&ms).Order("id desc")  
215 - tx.Select("id", "name", "departments") 214 + tx = tx.Model(&ms).Order("pin_yin_name asc")
  215 + tx.Select("id", "name", "departments", "pin_yin_name")
216 tx.Where("company_id = ?", companyId) 216 tx.Where("company_id = ?", companyId)
217 tx.Where("audit_status in (?)", domain.UserAuditStatusPassed) 217 tx.Where("audit_status in (?)", domain.UserAuditStatusPassed)
218 tx.Where("enable = ?", domain.UserEnable) 218 tx.Where("enable = ?", domain.UserEnable)