审查视图

pkg/application/role/role_user_service.go 6.4 KB
1 2 3
package service

import (
4 5
	"strconv"
6
	"github.com/linmadan/egglib-go/core/application"
郑周 authored
7
	"github.com/linmadan/egglib-go/utils/tool_funs"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	"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})
郑周 authored
32 33 34 35 36
	//int64Array := make([]int64, 0)
	//for i := range in.UserIds {
	//	int64Num, _ := strconv.ParseInt(in.UserIds[i], 10, 64)
	//	int64Array = append(int64Array, int64Num)
	//}
37
	// 检测已存在的关联用户
郑周 authored
38
	_, rus, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.RoleId, "companyId": in.CompanyId, "userIds": in.UserIds, "limit": int64(9999999)})
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	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
}
郑周 authored
98
func (rs *RoleUserService) ListRole(in *command.UserRoleQueryCommand) (interface{}, error) {
郑周 authored
99
	transactionContext, err := factory.ValidateStartTransaction(in)
郑周 authored
100 101 102 103 104 105 106 107
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	ruRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
108
	total, tempList, err := ruRepository.FindAllContainUser(in.PageNumber, in.PageSize, in.CompanyId, in.RoleId)
郑周 authored
109 110 111 112 113 114
	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())
	}
115
	return tool_funs.SimpleWrapGridMap(total, tempList), nil
郑周 authored
116
}
郑周 authored
117
118 119
// GetHrBp 当前操作人是否拥有HR-BP权限 (1表示有权限)
func GetHrBp(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) {
120 121 122
	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})
郑周 authored
123 124 125
	if err != nil {
		return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
	}
126 127 128 129 130
	if len(roles) == 0 {
		return -1, nil
	}

	_, userRoles, err := roleUserRepository.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId})
郑周 authored
131 132 133
	if err != nil {
		return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
	}
134 135 136 137
	if len(userRoles) == 0 {
		return -1, nil
	}
138 139
	hrBp := -1
loopFinish:
140 141
	for _, userRole := range userRoles {
		for _, role := range roles {
142 143 144
			if userRole.RoleId == role.Id {
				hrBp = domain.RoleTypeSystem
				break loopFinish
郑周 authored
145 146
			}
		}
147 148 149 150 151 152
	}
	return hrBp, nil
}

// GetSuperAdmin 当前操作人是否拥有超级管理员权限 (2表示有权限)
func GetSuperAdmin(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) {
153 154 155
	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})
156 157 158
	if err != nil {
		return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
	}
159 160 161 162 163
	if len(roles) == 0 {
		return -1, nil
	}

	_, userRoles, err := roleUserRepository.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId})
164 165 166
	if err != nil {
		return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
	}
167 168 169 170
	if len(userRoles) == 0 {
		return -1, nil
	}
171 172
	superAdmin := -1
loopFinish:
173 174
	for _, userRole := range userRoles {
		for _, role := range roles {
175 176 177 178
			if userRole.RoleId == role.Id {
				superAdmin = domain.RoleTypeSuperAdmin
				break loopFinish
			}
郑周 authored
179 180
		}
	}
181
	return superAdmin, nil
郑周 authored
182
}