role.go
5.8 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package role
import (
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/role"
"time"
)
type RoleService struct {
}
func (svr *RoleService) CreateRole(header *protocol.RequestHeader, request *protocolx.CreateRoleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.CreateRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
newRole := &domain.Role{
RoleName: request.RoleName,
ParentId: request.ParentId,
CreateTime: time.Now(),
UpdateTime: time.Now(),
}
var RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
if request.ParentId > 0 {
if role, e := RoleRepository.FindOne(map[string]interface{}{"id": request.ParentId}); e != nil || role == nil {
err = protocol.NewCustomMessage(1, "父角色不存在")
return
}
}
if m, err := RoleRepository.Save(newRole); err != nil {
return nil, err
} else {
rsp = utils.LoadCustomField(m, "Id", "RoleName", "ParentId")
}
err = transactionContext.CommitTransaction()
return
}
func (svr *RoleService) UpdateRole(header *protocol.RequestHeader, request *protocolx.UpdateRoleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.UpdateRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
var role *domain.Role
if role, err = RoleRepository.FindOne(common.ObjectToMap(request)); err != nil {
return
}
if role.Id == request.ParentId {
err = protocol.NewCustomMessage(1, "父角色不能设为自己")
return
}
if request.ParentId > 0 {
if role, e := RoleRepository.FindOne(map[string]interface{}{"id": request.ParentId}); e != nil || role == nil {
err = protocol.NewCustomMessage(1, "父角色不存在")
return
}
}
if err = role.Update(map[string]interface{}{"roleName": request.RoleName, "parentId": request.ParentId}); err != nil {
return
}
if role, err = RoleRepository.Save(role); err != nil {
return
}
err = transactionContext.CommitTransaction()
return
}
func (svr *RoleService) GetRole(header *protocol.RequestHeader, request *protocolx.GetRoleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
transactionContext.SetTransactionClose()
rsp = &protocolx.GetRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
var role *domain.Role
if role, err = RoleRepository.FindOne(common.ObjectToMap(request)); err != nil {
return
}
rsp = map[string]interface{}{"role": utils.LoadCustomField(role, "Id", "RoleName", "ParentId")}
err = transactionContext.CommitTransaction()
return
}
func (svr *RoleService) DeleteRole(header *protocol.RequestHeader, request *protocolx.DeleteRoleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.DeleteRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
var role *domain.Role
if role, err = RoleRepository.FindOne(common.ObjectToMap(request)); err != nil {
return
}
if count, _, e := RoleRepository.Find(map[string]interface{}{"parentId": role.Id}); e == nil && count > 0 {
err = protocol.NewCustomMessage(2, "存在子角色,不可删除")
return
}
if role, err = RoleRepository.Remove(role); err != nil {
return
}
rsp = utils.LoadCustomField(role, "Id", "RoleName", "ParentId")
err = transactionContext.CommitTransaction()
return
}
func (svr *RoleService) ListRole(header *protocol.RequestHeader, request *protocolx.ListRoleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.ListRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
var role []*domain.Role
var total int64
if _, role, err = RoleRepository.Find(map[string]interface{}{"orderById": "DESC"}); err != nil {
return
}
rsp = map[string]interface{}{
"total": total,
"list": utils.LoadCustomField(role, "Id", "RoleName", "ParentId"),
}
err = transactionContext.CommitTransaction()
return
}
func NewRoleService(options map[string]interface{}) *RoleService {
svr := &RoleService{}
return svr
}