作者 tangxuhui
@@ -94,3 +94,11 @@ func CreateBatchAddUserService(options map[string]interface{}) (service.PgBatchA @@ -94,3 +94,11 @@ func CreateBatchAddUserService(options map[string]interface{}) (service.PgBatchA
94 } 94 }
95 return domainService.NewPgBatchAddUserService(transactionContext) 95 return domainService.NewPgBatchAddUserService(transactionContext)
96 } 96 }
  97 +
  98 +func CreatePgBatchRemoveRoleService(options map[string]interface{}) (service.PgBatchRemoveRoleService, error) {
  99 + var transactionContext *pgTransaction.TransactionContext
  100 + if value, ok := options["transactionContext"]; ok {
  101 + transactionContext = value.(*pgTransaction.TransactionContext)
  102 + }
  103 + return domainService.NewPgBatchRemoveRoleService(transactionContext)
  104 +}
@@ -290,34 +290,17 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole @@ -290,34 +290,17 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
290 defer func() { 290 defer func() {
291 transactionContext.RollbackTransaction() 291 transactionContext.RollbackTransaction()
292 }() 292 }()
293 - // TODO:数据权限  
294 - var roleRepository domain.RoleRepository  
295 - if value, err := factory.CreateRoleRepository(map[string]interface{}{ 293 + batchRemoveRoleService, _ := factory.CreatePgBatchRemoveRoleService(map[string]interface{}{
296 "transactionContext": transactionContext, 294 "transactionContext": transactionContext,
297 - }); err != nil {  
298 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
299 - } else {  
300 - roleRepository = value  
301 - }  
302 - role, err := roleRepository.FindOne(map[string]interface{}{"roleId": removeRoleCommand.RoleId})  
303 - if err != nil { 295 + })
  296 +
  297 + if _, err := batchRemoveRoleService.BatchRemove(removeRoleCommand.OperateInfo, []int64{removeRoleCommand.RoleId}); err != nil {
304 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 298 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
305 } 299 }
306 - if role == nil {  
307 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))  
308 - }  
309 - if role.RoleType == domain.RoleTypeAdmin {  
310 - return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")  
311 - }  
312 - role.DeletedAt = time.Now()  
313 - if role, err := roleRepository.Remove(role); err != nil {  
314 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
315 - } else {  
316 - if err := transactionContext.CommitTransaction(); err != nil {  
317 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
318 - }  
319 - return role, nil 300 + if err := transactionContext.CommitTransaction(); err != nil {
  301 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
320 } 302 }
  303 + return struct{}{}, nil
321 } 304 }
322 305
323 // 批量移除角色 306 // 批量移除角色
@@ -335,31 +318,13 @@ func (roleService *RoleService) BatchDeleteRole(removeRoleCommand *command.Batch @@ -335,31 +318,13 @@ func (roleService *RoleService) BatchDeleteRole(removeRoleCommand *command.Batch
335 defer func() { 318 defer func() {
336 transactionContext.RollbackTransaction() 319 transactionContext.RollbackTransaction()
337 }() 320 }()
338 - // TODO:数据权限  
339 - var roleRepository domain.RoleRepository  
340 - if value, err := factory.CreateRoleRepository(map[string]interface{}{ 321 +
  322 + batchRemoveRoleService, _ := factory.CreatePgBatchRemoveRoleService(map[string]interface{}{
341 "transactionContext": transactionContext, 323 "transactionContext": transactionContext,
342 - }); err != nil { 324 + })
  325 +
  326 + if _, err := batchRemoveRoleService.BatchRemove(removeRoleCommand.OperateInfo, removeRoleCommand.RoleIds); err != nil {
343 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 327 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
344 - } else {  
345 - roleRepository = value  
346 - }  
347 - for i := range removeRoleCommand.RoleIds {  
348 - roleId := removeRoleCommand.RoleIds[i]  
349 - role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})  
350 - if err != nil {  
351 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
352 - }  
353 - if role == nil {  
354 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%v", roleId))  
355 - }  
356 - if role.RoleType == domain.RoleTypeAdmin {  
357 - return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")  
358 - }  
359 - role.DeletedAt = time.Now()  
360 - if _, err := roleRepository.Remove(role); err != nil {  
361 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
362 - }  
363 } 328 }
364 329
365 if err := transactionContext.CommitTransaction(); err != nil { 330 if err := transactionContext.CommitTransaction(); err != nil {
@@ -423,7 +388,6 @@ func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRole @@ -423,7 +388,6 @@ func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRole
423 defer func() { 388 defer func() {
424 transactionContext.RollbackTransaction() 389 transactionContext.RollbackTransaction()
425 }() 390 }()
426 - // TODO:数据权限  
427 roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleCommand.RoleId) 391 roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleCommand.RoleId)
428 if err != nil { 392 if err != nil {
429 return nil, err 393 return nil, err
@@ -110,6 +110,14 @@ type AccessMenusOptions struct { @@ -110,6 +110,14 @@ type AccessMenusOptions struct {
110 CompanyId int64 110 CompanyId int64
111 } 111 }
112 112
  113 +func (role *Role) IsRemoved() bool {
  114 + if !role.DeletedAt.IsZero() {
  115 + return false
  116 + }
  117 + role.DeletedAt = time.Now()
  118 + return true
  119 +}
  120 +
113 /***** 1.自定义函数模块 *****/ 121 /***** 1.自定义函数模块 *****/
114 /*1.1 拷贝简单的角色信息*/ 122 /*1.1 拷贝简单的角色信息*/
115 123
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +// PgAuthAccountDestroyService 账号注销服务
  6 +type PgBatchRemoveRoleService interface {
  7 + BatchRemove(optUser *domain.OperateInfo, roleIds []int64) (interface{}, error)
  8 +}
@@ -215,7 +215,7 @@ func (user *User) RemoveUserRole(roleId int64) bool { @@ -215,7 +215,7 @@ func (user *User) RemoveUserRole(roleId int64) bool {
215 var res bool = false 215 var res bool = false
216 if user.ExistsUserRole(roleId) { 216 if user.ExistsUserRole(roleId) {
217 217
218 - var roles []*Role 218 + var roles = make([]*Role, 0)
219 for i := range user.UserRole { 219 for i := range user.UserRole {
220 if user.UserRole[i].RoleId != roleId { 220 if user.UserRole[i].RoleId != roleId {
221 roles = append(roles, user.UserRole[i]) 221 roles = append(roles, user.UserRole[i])
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
  8 +)
  9 +
  10 +// PgCreateRoleService 传教角色服务
  11 +type PgBatchRemoveRoleService struct {
  12 + transactionContext *pgTransaction.TransactionContext
  13 +}
  14 +
  15 +// CreateRole 批量删除角色
  16 +//
  17 +// optUser 操作用户
  18 +// roleInfo 角色信息
  19 +func (ptr *PgBatchRemoveRoleService) BatchRemove(optUser *domain.OperateInfo, roleIds []int64) (interface{}, error) {
  20 +
  21 + roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
  22 + userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
  23 +
  24 + for i := range roleIds {
  25 + roleId := roleIds[i]
  26 + role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})
  27 + if err != nil {
  28 + return nil, err
  29 + }
  30 + if role.RoleType == domain.RoleTypeAdmin {
  31 + return nil, fmt.Errorf("主管理员角色不可删除")
  32 + }
  33 + if ok := role.IsRemoved(); !ok {
  34 + continue
  35 + }
  36 +
  37 + // 1.移除角色
  38 + if _, err := roleRepository.Remove(role); err != nil {
  39 + return nil, err
  40 + }
  41 +
  42 + // 2.移除用户中有配置该角色的数据
  43 + var offset, limit int = 0, repository.MaxRowLimit
  44 + for {
  45 + _, users, e := userRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId, "roleId": roleId, "limit": limit, "offset": offset})
  46 + for i := 0; i < len(users); i++ {
  47 + if !users[i].RemoveUserRole(roleId) {
  48 + continue
  49 + }
  50 + if _, err := userRepository.Save(users[i]); err != nil {
  51 + return nil, err
  52 + }
  53 + }
  54 + if len(users) == 0 || len(users) < repository.MaxRowLimit || e != nil {
  55 + break
  56 + }
  57 + offset += limit
  58 + }
  59 + }
  60 +
  61 + return struct{}{}, nil
  62 +}
  63 +
  64 +func NewPgBatchRemoveRoleService(transactionContext *pgTransaction.TransactionContext) (*PgBatchRemoveRoleService, error) {
  65 + if transactionContext == nil {
  66 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  67 + } else {
  68 + return &PgBatchRemoveRoleService{
  69 + transactionContext: transactionContext,
  70 + }, nil
  71 + }
  72 +}
1 package repository 1 package repository
2 2
3 //func PGConten 3 //func PGConten
  4 +
  5 +const MaxRowLimit = 100
@@ -202,6 +202,8 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int @@ -202,6 +202,8 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int
202 } 202 }
203 query.SetWhereByQueryOption("user_base_id=?", "userBaseId") 203 query.SetWhereByQueryOption("user_base_id=?", "userBaseId")
204 query.SetWhereByQueryOption("(user_type & ?)>0", "userType") 204 query.SetWhereByQueryOption("(user_type & ?)>0", "userType")
  205 + query.SetWhereByQueryOption(fmt.Sprintf(`user_role @> '[{"roleId":%v}]'`, queryOptions["roleId"]), "roleId")
  206 +
205 if v, ok := queryOptions["depName"]; ok && len(v.(string)) > 0 { 207 if v, ok := queryOptions["depName"]; ok && len(v.(string)) > 0 {
206 query.Where(fmt.Sprintf(`ext->>'depName' like '%%%v%%'`, v)) 208 query.Where(fmt.Sprintf(`ext->>'depName' like '%%%v%%'`, v))
207 } 209 }