permission_service.go
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type PermissionService struct {
}
func NewPermissionService() *PermissionService {
newPermissionService := &PermissionService{}
return newPermissionService
}
func (rs *PermissionService) Update(in *command.UpdatePermissionCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
// 获取权限配置
_, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(permissions) == 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "权限配置数据错误")
}
permission := permissions[0]
permission.OptHrScore = in.OptHrScore
permission.OptEvalScore = in.OptEvalScore
permission, err = permissionRepository.Insert(permission)
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 permission, nil
}
func (rs *PermissionService) Get(in *command.GetPermissionCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
// 获取权限配置
_, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var permission *domain.Permission
if len(permissions) == 0 { // 不存在时,新增权限配置
value := &domain.Permission{
Id: 0,
CompanyId: in.CompanyId,
OptHrScore: domain.PermissionOff,
OptEvalScore: domain.PermissionOff,
}
permission, err = permissionRepository.Insert(value)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
} else {
permission = permissions[0]
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return permission, nil
}