作者 yangfu

chore: 批量导入用户所属组织使用操作的组织,管理员角色返回所有权限

@@ -66,7 +66,7 @@ func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Comm @@ -66,7 +66,7 @@ func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Comm
66 "transactionContext": transactionContext, 66 "transactionContext": transactionContext,
67 }) 67 })
68 var failRows []*domain.BatchAddUserItem 68 var failRows []*domain.BatchAddUserItem
69 - if failRows, err = batchAddUserService.BatchAddUser2(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil { 69 + if failRows, err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
70 return batchAddCommand.Users, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 70 return batchAddCommand.Users, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
71 } 71 }
72 if len(failRows) != 0 { 72 if len(failRows) != 0 {
@@ -3,6 +3,8 @@ package domain @@ -3,6 +3,8 @@ package domain
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
  6 + "strconv"
  7 + "strings"
6 "time" 8 "time"
7 ) 9 )
8 10
@@ -197,6 +199,17 @@ func (org *Org) ID() string { @@ -197,6 +199,17 @@ func (org *Org) ID() string {
197 return org.GetFullPath() 199 return org.GetFullPath()
198 } 200 }
199 201
  202 +func (org *Org) IsChild(pid int64) bool {
  203 + paths := strings.Split(org.ParentPath, PathSegment)
  204 + pidStr := strconv.FormatInt(pid, 10)
  205 + for _, v := range paths {
  206 + if strings.EqualFold(pidStr, v) {
  207 + return true
  208 + }
  209 + }
  210 + return false
  211 +}
  212 +
200 /***** 2.缓存模块 *****/ 213 /***** 2.缓存模块 *****/
201 214
202 func (m *Org) CacheKeyFunc() string { 215 func (m *Org) CacheKeyFunc() string {
@@ -4,6 +4,5 @@ import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" @@ -4,6 +4,5 @@ import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
4 4
5 // PgBatchAddUserService 批量添加用户服务 5 // PgBatchAddUserService 批量添加用户服务
6 type PgBatchAddUserService interface { 6 type PgBatchAddUserService interface {
7 - BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error  
8 - BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) 7 + BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error)
9 } 8 }
@@ -18,71 +18,71 @@ type PgBatchAddUserService struct { @@ -18,71 +18,71 @@ type PgBatchAddUserService struct {
18 // optUser 操作用户 18 // optUser 操作用户
19 // users 待添加用户列表数据 19 // users 待添加用户列表数据
20 // password 密码 20 // password 密码
21 -func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error {  
22 - var (  
23 - err error  
24 - )  
25 - orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)  
26 - if err != nil {  
27 - return err  
28 - }  
29 - _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})  
30 - if err != nil {  
31 - return err  
32 - }  
33 - var mapOrg = make(map[int64]*domain.Org)  
34 - for i := range orgs {  
35 - mapOrg[orgs[i].OrgId] = orgs[i]  
36 - }  
37 -  
38 - createUserService, _ := NewPgCreateUserService(ptr.transactionContext)  
39 - for i := range users {  
40 - user := users[i]  
41 - if err = ptr.preCheck(user); err != nil {  
42 - return err  
43 - }  
44 - newUser := &domain.User{  
45 - CompanyId: user.CompanyId,  
46 - UserType: user.UserType,  
47 - UserCode: user.UserCode,  
48 - OrganizationId: user.OrganizationId,  
49 - DepartmentId: user.DepartmentId,  
50 - UserOrg: []*domain.Org{},  
51 - UserRole: []*domain.Role{},  
52 - FavoriteMenus: []string{},  
53 - CooperationInfo: user.CooperationInfo,  
54 - UserInfo: user.UserInfo,  
55 - EnableStatus: int(domain.UserStatusEnable),  
56 - Ext: &domain.Ext{  
57 - Phone: user.UserInfo.Phone,  
58 - UserName: user.UserInfo.UserName,  
59 - },  
60 - CreatedAt: time.Now(),  
61 - UpdatedAt: time.Now(),  
62 - }  
63 - if user.OrganizationId > 0 {  
64 - if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId {  
65 - newUser.Ext.OrgName = v.OrgName  
66 - } else {  
67 - return fmt.Errorf("导入的组织机构不存在")  
68 - }  
69 - }  
70 - if user.DepartmentId > 0 {  
71 - if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId {  
72 - newUser.Ext.DepName = v.OrgName  
73 - } else {  
74 - return fmt.Errorf("导入的所属部门不存在")  
75 - }  
76 - }  
77 -  
78 - if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {  
79 - return err  
80 - }  
81 - }  
82 - return nil  
83 -} 21 +//func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.User, password string) error {
  22 +// var (
  23 +// err error
  24 +// )
  25 +// orgRepository, err := repository.NewOrgRepository(ptr.transactionContext)
  26 +// if err != nil {
  27 +// return err
  28 +// }
  29 +// _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId})
  30 +// if err != nil {
  31 +// return err
  32 +// }
  33 +// var mapOrg = make(map[int64]*domain.Org)
  34 +// for i := range orgs {
  35 +// mapOrg[orgs[i].OrgId] = orgs[i]
  36 +// }
  37 +//
  38 +// createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
  39 +// for i := range users {
  40 +// user := users[i]
  41 +// if err = ptr.preCheck(user); err != nil {
  42 +// return err
  43 +// }
  44 +// newUser := &domain.User{
  45 +// CompanyId: user.CompanyId,
  46 +// UserType: user.UserType,
  47 +// UserCode: user.UserCode,
  48 +// OrganizationId: user.OrganizationId,
  49 +// DepartmentId: user.DepartmentId,
  50 +// UserOrg: []*domain.Org{},
  51 +// UserRole: []*domain.Role{},
  52 +// FavoriteMenus: []string{},
  53 +// CooperationInfo: user.CooperationInfo,
  54 +// UserInfo: user.UserInfo,
  55 +// EnableStatus: int(domain.UserStatusEnable),
  56 +// Ext: &domain.Ext{
  57 +// Phone: user.UserInfo.Phone,
  58 +// UserName: user.UserInfo.UserName,
  59 +// },
  60 +// CreatedAt: time.Now(),
  61 +// UpdatedAt: time.Now(),
  62 +// }
  63 +// if user.OrganizationId > 0 {
  64 +// if v, ok := mapOrg[user.OrganizationId]; ok && v.CompanyId == user.CompanyId {
  65 +// newUser.Ext.OrgName = v.OrgName
  66 +// } else {
  67 +// return fmt.Errorf("导入的组织机构不存在")
  68 +// }
  69 +// }
  70 +// if user.DepartmentId > 0 {
  71 +// if v, ok := mapOrg[user.DepartmentId]; ok && v.CompanyId == user.CompanyId {
  72 +// newUser.Ext.DepName = v.OrgName
  73 +// } else {
  74 +// return fmt.Errorf("导入的所属部门不存在")
  75 +// }
  76 +// }
  77 +//
  78 +// if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
  79 +// return err
  80 +// }
  81 +// }
  82 +// return nil
  83 +//}
84 84
85 -func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) { 85 +func (ptr *PgBatchAddUserService) BatchAddUser(optUser *domain.OperateInfo, users []*domain.BatchAddUserItem, password string) ([]*domain.BatchAddUserItem, error) {
86 var ( 86 var (
87 err error 87 err error
88 ) 88 )
@@ -99,6 +99,10 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use @@ -99,6 +99,10 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
99 for i := range orgs { 99 for i := range orgs {
100 mapOrg[orgs[i].OrgCode] = orgs[i] 100 mapOrg[orgs[i].OrgCode] = orgs[i]
101 } 101 }
  102 + optUserOrg, err := orgRepository.FindOne(map[string]interface{}{"orgId": optUser.OrgId})
  103 + if err != nil {
  104 + return failRows, err
  105 + }
102 106
103 createUserService, _ := NewPgCreateUserService(ptr.transactionContext) 107 createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
104 for i := range users { 108 for i := range users {
@@ -110,13 +114,19 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use @@ -110,13 +114,19 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
110 } 114 }
111 var org, dep *domain.Org 115 var org, dep *domain.Org
112 var ok bool 116 var ok bool
113 - if org, ok = mapOrg[user.Org]; !ok {  
114 - user.FailReason = "导入的组织机构不存在:" + user.Org 117 + // 使用导入用户的组织作为默认组织
  118 + //if org, ok = mapOrg[user.Org]; !ok {
  119 + // user.FailReason = "导入的组织机构不存在:" + user.Org
  120 + // failRows = append(failRows, user)
  121 + // continue
  122 + //}
  123 + if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
  124 + user.FailReason = "导入的所属部门不存在:" + user.Department
115 failRows = append(failRows, user) 125 failRows = append(failRows, user)
116 continue 126 continue
117 } 127 }
118 - if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {  
119 - user.FailReason = "导入的所属部门不存在:" + user.Department 128 + if dep != nil && !dep.IsChild(optUser.OrgId) {
  129 + user.FailReason = fmt.Sprintf("导入的所属部门不是当前登录组织(%v)的子部门:%v", optUserOrg.OrgName, user.Department)
120 failRows = append(failRows, user) 130 failRows = append(failRows, user)
121 continue 131 continue
122 } 132 }
@@ -124,7 +134,7 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use @@ -124,7 +134,7 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
124 CompanyId: user.CompanyId, 134 CompanyId: user.CompanyId,
125 UserType: user.UserType, 135 UserType: user.UserType,
126 UserCode: user.UserCode, 136 UserCode: user.UserCode,
127 - OrganizationId: org.OrgId, 137 + OrganizationId: optUser.OrgId,
128 UserOrg: []*domain.Org{}, 138 UserOrg: []*domain.Org{},
129 UserRole: []*domain.Role{}, 139 UserRole: []*domain.Role{},
130 FavoriteMenus: []string{}, 140 FavoriteMenus: []string{},
@@ -172,24 +182,24 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use @@ -172,24 +182,24 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
172 return failRows, nil 182 return failRows, nil
173 } 183 }
174 184
175 -func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {  
176 - if len(user.UserCode) == 0 {  
177 - return fmt.Errorf("导入的用户编码为空值")  
178 - }  
179 - if len(user.UserInfo.UserName) == 0 {  
180 - return fmt.Errorf("导入的用户姓名为空值")  
181 - }  
182 - if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {  
183 - return fmt.Errorf("导入的手机号不是有效手机号")  
184 - }  
185 - if user.OrganizationId == 0 {  
186 - return fmt.Errorf("导入的组织机构不存在")  
187 - }  
188 - if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {  
189 - return fmt.Errorf("导入的所属部门不存在")  
190 - }  
191 - return nil  
192 -} 185 +//func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
  186 +// if len(user.UserCode) == 0 {
  187 +// return fmt.Errorf("导入的用户编码为空值")
  188 +// }
  189 +// if len(user.UserInfo.UserName) == 0 {
  190 +// return fmt.Errorf("导入的用户姓名为空值")
  191 +// }
  192 +// if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {
  193 +// return fmt.Errorf("导入的手机号不是有效手机号")
  194 +// }
  195 +// if user.OrganizationId == 0 {
  196 +// return fmt.Errorf("导入的组织机构不存在")
  197 +// }
  198 +// if user.DepartmentId == 0 && user.UserType == domain.UserTypeEmployee {
  199 +// return fmt.Errorf("导入的所属部门不存在")
  200 +// }
  201 +// return nil
  202 +//}
193 203
194 func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error { 204 func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error {
195 if len(user.UserCode) == 0 { 205 if len(user.UserCode) == 0 {
@@ -22,6 +22,7 @@ type PgRoleAccessMenusService struct { @@ -22,6 +22,7 @@ type PgRoleAccessMenusService struct {
22 func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleIds []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) { 22 func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleIds []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
23 var err error 23 var err error
24 var menus []*domain.Menu 24 var menus []*domain.Menu
  25 + var hasAdminRole bool = false
25 menuIdSet := hashset.New() 26 menuIdSet := hashset.New()
26 if len(roleIds) == 0 { 27 if len(roleIds) == 0 {
27 return menus, nil 28 return menus, nil
@@ -41,6 +42,9 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro @@ -41,6 +42,9 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
41 for i := 0; i < len(role.AccessMenus); i++ { 42 for i := 0; i < len(role.AccessMenus); i++ {
42 menuIdSet.Add(role.AccessMenus[i]) 43 menuIdSet.Add(role.AccessMenus[i])
43 } 44 }
  45 + if role.RoleType&domain.RoleTypeAdmin > 0 {
  46 + hasAdminRole = true
  47 + }
44 } 48 }
45 49
46 // 2.获取所有公开的菜单 50 // 2.获取所有公开的菜单
@@ -66,7 +70,7 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro @@ -66,7 +70,7 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
66 if err != nil { 70 if err != nil {
67 return nil, err 71 return nil, err
68 } 72 }
69 - _, alias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId}) 73 + _, menuAlias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
70 if err != nil { 74 if err != nil {
71 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 75 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
72 } 76 }
@@ -74,18 +78,25 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro @@ -74,18 +78,25 @@ func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, ro
74 // 4. 设置别名、返回有效的菜单列表 78 // 4. 设置别名、返回有效的菜单列表
75 var mapMenus = make(map[int64]*domain.Menu) 79 var mapMenus = make(map[int64]*domain.Menu)
76 for i := range menus { 80 for i := range menus {
  81 + if menus[i].EnableStatus == domain.MenuStatusEnable {
77 menus[i].EnableStatus = domain.MenuStatusDisable 82 menus[i].EnableStatus = domain.MenuStatusDisable
  83 + if hasAdminRole {
  84 + menus[i].EnableStatus = domain.MenuStatusEnable //管理员角色,返回所有权限
  85 + }
  86 + }
78 mapMenus[menus[i].MenuId] = menus[i] 87 mapMenus[menus[i].MenuId] = menus[i]
79 } 88 }
80 - for i := range alias {  
81 - if v, ok := mapMenus[alias[i].MenuId]; ok {  
82 - v.MenuName = alias[i].MenuAlias // 设置别名  
83 - v.Sort = alias[i].Sort 89 + // 4.1.设置别名
  90 + for i := range menuAlias {
  91 + if v, ok := mapMenus[menuAlias[i].MenuId]; ok {
  92 + v.MenuName = menuAlias[i].MenuAlias
  93 + v.Sort = menuAlias[i].Sort
84 } 94 }
85 } 95 }
  96 + // 4.2.设置菜单权限状态
86 values := menuIdSet.Values() 97 values := menuIdSet.Values()
87 for i := range values { 98 for i := range values {
88 - menuId := (values[i]).(int64) // 设置菜单权限状态 99 + menuId := (values[i]).(int64)
89 if v, ok := mapMenus[menuId]; ok { 100 if v, ok := mapMenus[menuId]; ok {
90 v.EnableStatus = domain.MenuStatusEnable 101 v.EnableStatus = domain.MenuStatusEnable
91 } 102 }