package service import ( "strconv" "github.com/linmadan/egglib-go/core/application" "github.com/linmadan/egglib-go/utils/tool_funs" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" ) type RoleUserService struct { } func NewRoleUserService() *RoleUserService { newRoleUserService := &RoleUserService{} return newRoleUserService } // Create 创建 func (rs *RoleUserService) Create(in *command.UserRoleCreateCommand) (interface{}, error) { transactionContext, err := factory.ValidateStartTransaction(in) if err != nil { return nil, err } defer func() { transactionContext.RollbackTransaction() }() roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext}) //int64Array := make([]int64, 0) //for i := range in.UserIds { // int64Num, _ := strconv.ParseInt(in.UserIds[i], 10, 64) // int64Array = append(int64Array, int64Num) //} // 检测已存在的关联用户 _, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userIds": in.UserIds, "limit": int64(9999999)}) if err != nil { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) } existUserIds := make(map[int64]int64) for i := range rus { existUserIds[rus[i].UserId] = rus[i].UserId } for i := range in.UserIds { int64Num, _ := strconv.ParseInt(in.UserIds[i], 10, 64) // 不存在关联关系时,新增数据 if _, ok := existUserIds[int64Num]; !ok { newRoleUser := &domain.RoleUser{ Id: 0, RoleId: in.RoleId, UserId: int64Num, CompanyId: in.CompanyId, } _, err := roleUserRepository.Insert(newRoleUser) if err != nil { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) } } } if err := transactionContext.CommitTransaction(); err != nil { return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) } return nil, nil } func (rs *RoleUserService) Remove(in *command.UserRoleDeleteCommand) (interface{}, error) { transactionContext, err := factory.ValidateStartTransaction(in) if err != nil { return nil, err } defer func() { transactionContext.RollbackTransaction() }() roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext}) // 检测已存在的关联用户 _, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userId": in.UserId}) if err != nil { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) } if len(rus) == 0 { return nil, application.ThrowError(application.BUSINESS_ERROR, "数据不存在") } if _, err := roleUserRepository.Remove(rus[0]); err != nil { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) } if err := transactionContext.CommitTransaction(); err != nil { return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) } return rus, nil } func (rs *RoleUserService) ListRole(in *command.UserRoleQueryCommand) (interface{}, error) { transactionContext, err := factory.ValidateStartTransaction(in) if err != nil { return nil, err } defer func() { transactionContext.RollbackTransaction() }() ruRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext}) total, tempList, err := ruRepository.FindAllContainUser(in.PageNumber, in.PageSize, in.CompanyId, in.RoleId) if err != nil { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) } if err := transactionContext.CommitTransaction(); err != nil { return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) } return tool_funs.SimpleWrapGridMap(total, tempList), nil } // GetHrBp 当前操作人是否拥有HR-BP权限 (1表示有权限) func GetHrBp(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) { roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext}) roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext}) _, roles, err := roleRepository.Find(map[string]interface{}{"type": domain.RoleTypeSystem, "companyId": companyId}) if err != nil { return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error()) } if len(roles) == 0 { return -1, nil } _, userRoles, err := roleUserRepository.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId}) if err != nil { return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error()) } if len(userRoles) == 0 { return -1, nil } hrBp := -1 loopFinish: for _, userRole := range userRoles { for _, role := range roles { if userRole.RoleId == role.Id { hrBp = domain.RoleTypeSystem break loopFinish } } } return hrBp, nil } // GetSuperAdmin 当前操作人是否拥有超级管理员权限 (2表示有权限) func GetSuperAdmin(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) { roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext}) roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext}) _, roles, err := roleRepository.Find(map[string]interface{}{"type": domain.RoleTypeSuperAdmin, "companyId": companyId}) if err != nil { return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error()) } if len(roles) == 0 { return -1, nil } _, userRoles, err := roleUserRepository.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId}) if err != nil { return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error()) } if len(userRoles) == 0 { return -1, nil } superAdmin := -1 loopFinish: for _, userRole := range userRoles { for _, role := range roles { if userRole.RoleId == role.Id { superAdmin = domain.RoleTypeSuperAdmin break loopFinish } } } return superAdmin, nil }