|
@@ -6,7 +6,6 @@ import ( |
|
@@ -6,7 +6,6 @@ import ( |
6
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
6
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
|
8
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
|
8
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/command"
|
9
|
- "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
10
|
)
|
9
|
)
|
11
|
|
10
|
|
12
|
type RoleService struct {
|
11
|
type RoleService struct {
|
|
@@ -17,122 +16,122 @@ func NewRoleService() *RoleService { |
|
@@ -17,122 +16,122 @@ func NewRoleService() *RoleService { |
17
|
return newRoleService
|
16
|
return newRoleService
|
18
|
}
|
17
|
}
|
19
|
|
18
|
|
20
|
-// Create 创建
|
|
|
21
|
-func (rs *RoleService) Create(in *command.CreateRoleCommand) (interface{}, error) {
|
|
|
22
|
- transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
23
|
- if err != nil {
|
|
|
24
|
- return nil, err
|
|
|
25
|
- }
|
|
|
26
|
- defer func() {
|
|
|
27
|
- transactionContext.RollbackTransaction()
|
|
|
28
|
- }()
|
|
|
29
|
- roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
30
|
-
|
|
|
31
|
- // 检测名称重复
|
|
|
32
|
- count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
|
|
|
33
|
- if err != nil {
|
|
|
34
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
35
|
- }
|
|
|
36
|
- if count > 0 {
|
|
|
37
|
- return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
38
|
- }
|
|
|
39
|
- newRole := &domain.Role{
|
|
|
40
|
- Id: 0,
|
|
|
41
|
- Name: in.Name,
|
|
|
42
|
- Type: domain.RoleTypeCommon,
|
|
|
43
|
- Description: in.Description,
|
|
|
44
|
- CompanyId: in.CompanyId,
|
|
|
45
|
- }
|
|
|
46
|
- role, err := roleRepository.Insert(newRole)
|
|
|
47
|
- if err != nil {
|
|
|
48
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
49
|
- }
|
|
|
50
|
- if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
51
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
52
|
- }
|
|
|
53
|
- return role, nil
|
|
|
54
|
-
|
|
|
55
|
-}
|
|
|
56
|
-
|
|
|
57
|
-func (rs *RoleService) Update(in *command.UpdateRoleCommand) (interface{}, error) {
|
|
|
58
|
- transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
59
|
- if err != nil {
|
|
|
60
|
- return nil, err
|
|
|
61
|
- }
|
|
|
62
|
- defer func() {
|
|
|
63
|
- transactionContext.RollbackTransaction()
|
|
|
64
|
- }()
|
|
|
65
|
-
|
|
|
66
|
- roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
67
|
-
|
|
|
68
|
- // 检测名称重复(排除自己)
|
|
|
69
|
- count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
|
|
|
70
|
- if err != nil {
|
|
|
71
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
72
|
- }
|
|
|
73
|
- if count > 0 {
|
|
|
74
|
- return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
75
|
- }
|
|
|
76
|
-
|
|
|
77
|
- role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
78
|
- if err != nil {
|
|
|
79
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
80
|
- }
|
|
|
81
|
-
|
|
|
82
|
- role.Name = in.Name
|
|
|
83
|
- role.Description = in.Description
|
|
|
84
|
-
|
|
|
85
|
- role, err = roleRepository.Insert(role)
|
|
|
86
|
- if err != nil {
|
|
|
87
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
88
|
- }
|
|
|
89
|
- if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
90
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
91
|
- }
|
|
|
92
|
- return role, nil
|
|
|
93
|
-}
|
|
|
94
|
-
|
|
|
95
|
-func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
|
|
|
96
|
- transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
97
|
- if err != nil {
|
|
|
98
|
- return nil, err
|
|
|
99
|
- }
|
|
|
100
|
- defer func() {
|
|
|
101
|
- transactionContext.RollbackTransaction()
|
|
|
102
|
- }()
|
|
|
103
|
-
|
|
|
104
|
- roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
105
|
- roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
106
|
-
|
|
|
107
|
- role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
108
|
- if err != nil {
|
|
|
109
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
110
|
- }
|
|
|
111
|
- if _, err := roleRepository.Remove(role); err != nil {
|
|
|
112
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
113
|
- }
|
|
|
114
|
-
|
|
|
115
|
- // 获取角色所有关联的用户,并删除
|
|
|
116
|
- _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
|
|
|
117
|
- if err != nil {
|
|
|
118
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
119
|
- }
|
|
|
120
|
- ids := make([]int64, 0)
|
|
|
121
|
- for i := range roleUsers {
|
|
|
122
|
- ids = append(ids, roleUsers[i].Id)
|
|
|
123
|
- }
|
|
|
124
|
- if len(ids) > 0 {
|
|
|
125
|
- err := roleUserRepository.BatchDeleteById(ids)
|
|
|
126
|
- if err != nil {
|
|
|
127
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
128
|
- }
|
|
|
129
|
- }
|
|
|
130
|
-
|
|
|
131
|
- if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
132
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
133
|
- }
|
|
|
134
|
- return role, nil
|
|
|
135
|
-}
|
19
|
+//// Create 创建
|
|
|
20
|
+//func (rs *RoleService) Create(in *command.CreateRoleCommand) (interface{}, error) {
|
|
|
21
|
+// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
22
|
+// if err != nil {
|
|
|
23
|
+// return nil, err
|
|
|
24
|
+// }
|
|
|
25
|
+// defer func() {
|
|
|
26
|
+// transactionContext.RollbackTransaction()
|
|
|
27
|
+// }()
|
|
|
28
|
+// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
29
|
+//
|
|
|
30
|
+// // 检测名称重复
|
|
|
31
|
+// count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
|
|
|
32
|
+// if err != nil {
|
|
|
33
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
34
|
+// }
|
|
|
35
|
+// if count > 0 {
|
|
|
36
|
+// return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
37
|
+// }
|
|
|
38
|
+// newRole := &domain.Role{
|
|
|
39
|
+// Id: 0,
|
|
|
40
|
+// Name: in.Name,
|
|
|
41
|
+// Type: domain.RoleTypeCommon,
|
|
|
42
|
+// Description: in.Description,
|
|
|
43
|
+// CompanyId: in.CompanyId,
|
|
|
44
|
+// }
|
|
|
45
|
+// role, err := roleRepository.Insert(newRole)
|
|
|
46
|
+// if err != nil {
|
|
|
47
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
48
|
+// }
|
|
|
49
|
+// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
50
|
+// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
51
|
+// }
|
|
|
52
|
+// return role, nil
|
|
|
53
|
+//
|
|
|
54
|
+//}
|
|
|
55
|
+//
|
|
|
56
|
+//func (rs *RoleService) Update(in *command.UpdateRoleCommand) (interface{}, error) {
|
|
|
57
|
+// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
58
|
+// if err != nil {
|
|
|
59
|
+// return nil, err
|
|
|
60
|
+// }
|
|
|
61
|
+// defer func() {
|
|
|
62
|
+// transactionContext.RollbackTransaction()
|
|
|
63
|
+// }()
|
|
|
64
|
+//
|
|
|
65
|
+// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
66
|
+//
|
|
|
67
|
+// // 检测名称重复(排除自己)
|
|
|
68
|
+// count, err := roleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
|
|
|
69
|
+// if err != nil {
|
|
|
70
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
71
|
+// }
|
|
|
72
|
+// if count > 0 {
|
|
|
73
|
+// return nil, application.ThrowError(application.BUSINESS_ERROR, "角色名称已存在")
|
|
|
74
|
+// }
|
|
|
75
|
+//
|
|
|
76
|
+// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
77
|
+// if err != nil {
|
|
|
78
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
79
|
+// }
|
|
|
80
|
+//
|
|
|
81
|
+// role.Name = in.Name
|
|
|
82
|
+// role.Description = in.Description
|
|
|
83
|
+//
|
|
|
84
|
+// role, err = roleRepository.Insert(role)
|
|
|
85
|
+// if err != nil {
|
|
|
86
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
87
|
+// }
|
|
|
88
|
+// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
89
|
+// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
90
|
+// }
|
|
|
91
|
+// return role, nil
|
|
|
92
|
+//}
|
|
|
93
|
+//
|
|
|
94
|
+//func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
|
|
|
95
|
+// transactionContext, err := factory.ValidateStartTransaction(in)
|
|
|
96
|
+// if err != nil {
|
|
|
97
|
+// return nil, err
|
|
|
98
|
+// }
|
|
|
99
|
+// defer func() {
|
|
|
100
|
+// transactionContext.RollbackTransaction()
|
|
|
101
|
+// }()
|
|
|
102
|
+//
|
|
|
103
|
+// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
104
|
+// roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
|
|
|
105
|
+//
|
|
|
106
|
+// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
|
|
|
107
|
+// if err != nil {
|
|
|
108
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
109
|
+// }
|
|
|
110
|
+// if _, err := roleRepository.Remove(role); err != nil {
|
|
|
111
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
112
|
+// }
|
|
|
113
|
+//
|
|
|
114
|
+// // 获取角色所有关联的用户,并删除
|
|
|
115
|
+// _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
|
|
|
116
|
+// if err != nil {
|
|
|
117
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
118
|
+// }
|
|
|
119
|
+// ids := make([]int64, 0)
|
|
|
120
|
+// for i := range roleUsers {
|
|
|
121
|
+// ids = append(ids, roleUsers[i].Id)
|
|
|
122
|
+// }
|
|
|
123
|
+// if len(ids) > 0 {
|
|
|
124
|
+// err := roleUserRepository.BatchDeleteById(ids)
|
|
|
125
|
+// if err != nil {
|
|
|
126
|
+// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
127
|
+// }
|
|
|
128
|
+// }
|
|
|
129
|
+//
|
|
|
130
|
+// if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
131
|
+// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
132
|
+// }
|
|
|
133
|
+// return role, nil
|
|
|
134
|
+//}
|
136
|
|
135
|
|
137
|
func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
|
136
|
func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
|
138
|
transactionContext, err := factory.StartTransaction()
|
137
|
transactionContext, err := factory.StartTransaction()
|