作者 yangfu

批量删除角色

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type BatchDeleteRoleCommand struct {
  12 + // 用户ID
  13 + UserId int64 `cname:"用户ID" json:"userId"`
  14 + // 组织ID
  15 + OrgId int64 `cname:"组织ID" json:"orgId"`
  16 + // 角色ID
  17 + RoleIds []int64 `cname:"角色ID" json:"roleIds" valid:"Required"`
  18 +}
  19 +
  20 +func (removeRoleCommand *BatchDeleteRoleCommand) Valid(validation *validation.Validation) {
  21 + //validation.SetError("CustomValid", "未实现的自定义认证")
  22 +}
  23 +
  24 +func (removeRoleCommand *BatchDeleteRoleCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(removeRoleCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(removeRoleCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
@@ -10,9 +10,9 @@ import ( @@ -10,9 +10,9 @@ import (
10 10
11 type CreateRoleCommand struct { 11 type CreateRoleCommand struct {
12 // 用户ID 12 // 用户ID
13 - UserId int64 `cname:"用户ID" json:"userId" valid:"Required` 13 + UserId int64 `cname:"用户ID" json:"userId" valid:"Required"`
14 // 组织ID 14 // 组织ID
15 - OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required` 15 + OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
16 // 角色名称 16 // 角色名称
17 RoleName string `cname:"角色名称" json:"roleName" valid:"Required"` 17 RoleName string `cname:"角色名称" json:"roleName" valid:"Required"`
18 // 描述 18 // 描述
@@ -314,6 +314,53 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole @@ -314,6 +314,53 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
314 } 314 }
315 } 315 }
316 316
  317 +// 批量移除角色
  318 +func (roleService *RoleService) BatchDeleteRole(removeRoleCommand *command.BatchDeleteRoleCommand) (interface{}, error) {
  319 + if err := removeRoleCommand.ValidateCommand(); err != nil {
  320 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  321 + }
  322 + transactionContext, err := factory.CreateTransactionContext(nil)
  323 + if err != nil {
  324 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  325 + }
  326 + if err := transactionContext.StartTransaction(); err != nil {
  327 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  328 + }
  329 + defer func() {
  330 + transactionContext.RollbackTransaction()
  331 + }()
  332 + var roleRepository domain.RoleRepository
  333 + if value, err := factory.CreateRoleRepository(map[string]interface{}{
  334 + "transactionContext": transactionContext,
  335 + }); err != nil {
  336 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  337 + } else {
  338 + roleRepository = value
  339 + }
  340 + for i := range removeRoleCommand.RoleIds {
  341 + roleId := removeRoleCommand.RoleIds[i]
  342 + role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})
  343 + if err != nil {
  344 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  345 + }
  346 + if role == nil {
  347 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%v", roleId))
  348 + }
  349 + if role.RoleType == domain.RoleTypeAdmin {
  350 + return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")
  351 + }
  352 + role.DeletedAt = time.Now()
  353 + if _, err := roleRepository.Remove(role); err != nil {
  354 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  355 + }
  356 + }
  357 +
  358 + if err := transactionContext.CommitTransaction(); err != nil {
  359 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  360 + }
  361 + return struct{}{}, nil
  362 +}
  363 +
317 // 取消用户分配的角色 364 // 取消用户分配的角色
318 func (roleService *RoleService) UnAssginRoleToUsers(unAssignRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) { 365 func (roleService *RoleService) UnAssginRoleToUsers(unAssignRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
319 if err := unAssignRoleToUsersCommand.ValidateCommand(); err != nil { 366 if err := unAssignRoleToUsersCommand.ValidateCommand(); err != nil {
@@ -409,10 +456,20 @@ func (roleService *RoleService) UpdateRoleAccessMenus(updateRoleAccessMenusComma @@ -409,10 +456,20 @@ func (roleService *RoleService) UpdateRoleAccessMenus(updateRoleAccessMenusComma
409 defer func() { 456 defer func() {
410 transactionContext.RollbackTransaction() 457 transactionContext.RollbackTransaction()
411 }() 458 }()
  459 +
  460 + roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleAccessMenusCommand.RoleId)
  461 + if err != nil {
  462 + return nil, err
  463 + }
  464 + role.AccessMenus = updateRoleAccessMenusCommand.AccessMenus
  465 + if role, err = roleRepository.Save(role); err != nil {
  466 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  467 + }
  468 +
412 if err := transactionContext.CommitTransaction(); err != nil { 469 if err := transactionContext.CommitTransaction(); err != nil {
413 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 470 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
414 } 471 }
415 - return nil, nil 472 + return role, nil
416 } 473 }
417 474
418 func NewRoleService(options map[string]interface{}) *RoleService { 475 func NewRoleService(options map[string]interface{}) *RoleService {
@@ -61,6 +61,7 @@ func (controller *CompanyController) ListCompanyCustomizeMenus() { @@ -61,6 +61,7 @@ func (controller *CompanyController) ListCompanyCustomizeMenus() {
61 controller.Unmarshal(listCompanyCustomizeMenusCommand) 61 controller.Unmarshal(listCompanyCustomizeMenusCommand)
62 companyId, _ := controller.GetInt64(":companyId") 62 companyId, _ := controller.GetInt64(":companyId")
63 listCompanyCustomizeMenusCommand.CompanyId = companyId 63 listCompanyCustomizeMenusCommand.CompanyId = companyId
  64 + listCompanyCustomizeMenusCommand.MenuCategory = controller.GetString("menuCategory")
64 data, err := companyService.ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand) 65 data, err := companyService.ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand)
65 controller.Response(data, err) 66 controller.Response(data, err)
66 } 67 }
@@ -48,6 +48,14 @@ func (controller *RoleController) RemoveRole() { @@ -48,6 +48,14 @@ func (controller *RoleController) RemoveRole() {
48 controller.Response(data, err) 48 controller.Response(data, err)
49 } 49 }
50 50
  51 +func (controller *RoleController) BatchDeleteRole() {
  52 + roleService := service.NewRoleService(nil)
  53 + removeRoleCommand := &command.BatchDeleteRoleCommand{}
  54 + controller.Unmarshal(removeRoleCommand)
  55 + data, err := roleService.BatchDeleteRole(removeRoleCommand)
  56 + controller.Response(data, err)
  57 +}
  58 +
51 func (controller *RoleController) ListRole() { 59 func (controller *RoleController) ListRole() {
52 roleService := service.NewRoleService(nil) 60 roleService := service.NewRoleService(nil)
53 listRoleQuery := &query.ListRoleQuery{} 61 listRoleQuery := &query.ListRoleQuery{}
@@ -110,3 +118,18 @@ func (controller *RoleController) UnAssginRoleToUsers() { @@ -110,3 +118,18 @@ func (controller *RoleController) UnAssginRoleToUsers() {
110 data, err := roleService.UnAssginRoleToUsers(unAssginRoleToUsersCommand) 118 data, err := roleService.UnAssginRoleToUsers(unAssginRoleToUsersCommand)
111 controller.Response(data, err) 119 controller.Response(data, err)
112 } 120 }
  121 +
  122 +/***** 1.适配web模块 *****/
  123 +func (controller *RoleController) WebSearchRole() {
  124 + roleService := service.NewRoleService(nil)
  125 + listRoleQuery := &query.ListRoleQuery{}
  126 + Must(controller.Unmarshal(listRoleQuery))
  127 + listRoleQuery.Limit = 20
  128 + listRoleQuery.CompanyId = 23
  129 + data, err := roleService.ListRole(listRoleQuery)
  130 + webData := map[string]interface{}{
  131 + "list": data.(map[string]interface{})["roles"],
  132 + "total": data.(map[string]interface{})["count"],
  133 + }
  134 + ResponseGrid(controller.BaseController, webData, err)
  135 +}
@@ -16,4 +16,16 @@ func init() { @@ -16,4 +16,16 @@ func init() {
16 web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus") 16 web.Router("/role/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus")
17 web.Router("/role/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers") 17 web.Router("/role/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers")
18 web.Router("/role/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers") 18 web.Router("/role/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers")
  19 + web.Router("/role/batch-delete", &controllers.RoleController{}, "Post:BatchDeleteRole")
  20 +
  21 + web.Router("/v1/web/roles/", &controllers.RoleController{}, "Post:CreateRole")
  22 + web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Put:UpdateRole")
  23 + web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Get:GetRole")
  24 + web.Router("/v1/web/roles/:roleId", &controllers.RoleController{}, "Delete:RemoveRole")
  25 + web.Router("/v1/web/roles/search", &controllers.RoleController{}, "Post:WebSearchRole")
  26 + web.Router("/v1/web/roles/:roleId/related-user", &controllers.RoleController{}, "Get:GetRoleRelatedUsers")
  27 + web.Router("/v1/web/roles/:roleId/access-menus", &controllers.RoleController{}, "Get:GetRoleAccessMenus")
  28 + web.Router("/v1/web/roles/:roleId/access-menus", &controllers.RoleController{}, "Put:UpdateRoleAccessMenus")
  29 + web.Router("/v1/web/roles/assign", &controllers.RoleController{}, "Post:AssginRoleToUsers")
  30 + web.Router("/v1/web/roles/unassign", &controllers.RoleController{}, "Post:UnAssginRoleToUsers")
19 } 31 }