project_module.go
7.1 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
193
194
195
196
197
198
199
200
201
package project_module
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/project_module"
"time"
)
type ProjectModuleService struct {
}
func (svr *ProjectModuleService) CreateProjectModule(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
)
rsp = &protocolx.CreateProjectModuleResponse{}
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()
}()
newProjectModule := &domain.ProjectModule{
ProjectName: request.ProjectName,
ProjectKey: request.ProjectKey,
Description: request.Description,
Status: 0,
CreateTime: time.Now(),
UpdateTime: time.Now(),
}
var ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
if _, err = ProjectModuleRepository.FindOne(map[string]interface{}{"projectKey": request.ProjectKey, "status": domain.StatusNormal}); err == nil {
err = protocol.NewCustomMessage(1, "项目键值已存在:"+request.ProjectKey)
return
}
if m, err := ProjectModuleRepository.Save(newProjectModule); err != nil {
return nil, err
} else {
rsp = m
}
if _, err = ProjectModuleVersionRepository.Save(&domain.ProjectModuleVersion{
ProjectModuleId: newProjectModule.Id,
Version: "latest",
Description: "latest version",
Status: domain.StatusNormal,
CreateTime: time.Now(),
UpdateTime: time.Now(),
}); err != nil {
return
}
err = transactionContext.CommitTransaction()
return
}
func (svr *ProjectModuleService) UpdateProjectModule(header *protocol.RequestHeader, request *protocolx.UpdateProjectModuleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.UpdateProjectModuleResponse{}
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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
var projectModule *domain.ProjectModule
if projectModule, err = ProjectModuleRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
err = protocol.NewCustomMessage(1, "模板项目不存在")
return
}
if projectModule.ProjectKey != request.ProjectKey {
if _, err = ProjectModuleRepository.FindOne(map[string]interface{}{"projectKey": request.ProjectKey, "status": domain.StatusNormal}); err == nil {
err = protocol.NewCustomMessage(1, "项目键值已存在:"+request.ProjectKey)
return
}
}
if err = projectModule.Update(common.ObjectToMap(request)); err != nil {
return
}
if projectModule, err = ProjectModuleRepository.Save(projectModule); err != nil {
return
}
err = transactionContext.CommitTransaction()
return
}
func (svr *ProjectModuleService) GetProjectModule(header *protocol.RequestHeader, request *protocolx.GetProjectModuleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.GetProjectModuleResponse{}
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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
var projectModule *domain.ProjectModule
if projectModule, err = ProjectModuleRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = protocol.NewCustomMessage(1, "模板项目不存在")
return
}
rsp = utils.LoadCustomField(projectModule, "Id", "ProjectName", "ProjectKey", "Description", "Status", "CreateTime", "UpdateTime")
err = transactionContext.CommitTransaction()
return
}
func (svr *ProjectModuleService) DeleteProjectModule(header *protocol.RequestHeader, request *protocolx.DeleteProjectModuleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.DeleteProjectModuleResponse{}
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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
var projectModule *domain.ProjectModule
if projectModule, err = ProjectModuleRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
return
}
if err = projectModule.Update(map[string]interface{}{"status": 2}); err != nil {
return
}
if projectModule, err = ProjectModuleRepository.Save(projectModule); err != nil {
return
}
rsp = projectModule
err = transactionContext.CommitTransaction()
return
}
func (svr *ProjectModuleService) ListProjectModule(header *protocol.RequestHeader, request *protocolx.ListProjectModuleRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
//listProjectModuleResponse := &protocolx.ListProjectModuleResponse{}
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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
var projectModule []*domain.ProjectModule
var total int64
if total, projectModule, err = ProjectModuleRepository.Find(map[string]interface{}{"offset": (request.PageNumber - 1) * request.PageSize, "limit": request.PageSize, "sortByCreateTime": "DESC", "status": domain.StatusNormal, "searchByText": request.ProjectName}); err != nil {
return
}
rsp = map[string]interface{}{
"totalRow": total,
"pageNumber": request.PageNumber,
"lists": utils.LoadCustomField(projectModule, "Id", "ProjectName", "ProjectKey", "Description", "Status", "CreateTime", "UpdateTime"), //listProjectModuleResponse.LoadDto(projectModule),
}
err = transactionContext.CommitTransaction()
return
}
func NewProjectModuleService(options map[string]interface{}) *ProjectModuleService {
svr := &ProjectModuleService{}
return svr
}