product_job.go
13.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
)
// 工位服务
type ProductJobService struct {
}
// 创建工位服务
func (productJobService *ProductJobService) CreateProductJob(operateInfo *domain.OperateInfo, cmd *command.CreateProductJobCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//var workshopRepository domain.WorkshopRepository
var workStation *domain.WorkStation
_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var productJobRepository domain.ProductJobRepository
var productJob *domain.ProductJob
productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, 0)
if job, err := productJobRepository.FindOne(map[string]interface{}{
"jobName": cmd.JobName,
"workStationId": workStation.WorkStationId,
}); err == nil && job != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
}
productJob = &domain.ProductJob{
CompanyId: operateInfo.CompanyId,
OrgId: operateInfo.OrgId,
JobName: cmd.JobName,
ProcessName: cmd.ProcessName,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
WorkStation: workStation,
RelatedDevices: cmd.RelatedDevices,
}
if productJob, err := productJobRepository.Save(productJob); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return productJob, nil
}
}
// 返回工位服务
func (productJobService *ProductJobService) GetProductJob(getProductJobQuery *query.GetProductJobQuery) (interface{}, error) {
if err := getProductJobQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//var productJobRepository domain.ProductJobRepository
_, productJob, err := factory.FastPgProductJob(transactionContext, getProductJobQuery.ProductJobId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, workshop, err := factory.FastPgWorkshop(transactionContext, productJob.WorkStation.WorkshopId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
productJob.WorkStation, _ = workshop.FindWorkStation(productJob.WorkStation.WorkshopId, productJob.WorkStation.LineId, productJob.WorkStation.SectionId)
newJobDto := &dto.ProductJobDto{}
newJobDto.LoadDto(productJob, 0)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return newJobDto, nil
}
// 返回工位服务列表
func (productJobService *ProductJobService) ListProductJob(listProductJobQuery *query.ListProductJobQuery) (interface{}, error) {
if err := listProductJobQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var productJobRepository domain.ProductJobRepository
if value, err := factory.CreateProductJobRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productJobRepository = value
}
if count, productJobs, err := productJobRepository.Find(tool_funs.SimpleStructToMap(listProductJobQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"productJobs": productJobs,
}, nil
}
}
// 移除工位服务
func (productJobService *ProductJobService) RemoveProductJob(removeProductJobCommand *command.RemoveProductJobCommand) (interface{}, error) {
if err := removeProductJobCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var productJobRepository domain.ProductJobRepository
if value, err := factory.CreateProductJobRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productJobRepository = value
}
productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": removeProductJobCommand.ProductJobId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productJob == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductJobCommand.ProductJobId)))
}
if productJob, err := productJobRepository.Remove(productJob); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return productJob, nil
}
}
// 移除工位服务
func (productJobService *ProductJobService) BatchRemoveProductJob(cmd *command.BatchRemoveProductJobCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var productJobRepository domain.ProductJobRepository
if value, err := factory.CreateProductJobRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productJobRepository = value
}
for i := range cmd.IdList {
id := cmd.IdList[i]
productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productJob == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", id))
}
if _, err := productJobRepository.Remove(productJob); 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 struct {
}{}, nil
}
// 更新工位服务
func (productJobService *ProductJobService) UpdateProductJob(cmd *command.UpdateProductJobCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//var workshopRepository domain.WorkshopRepository
var workStation *domain.WorkStation
_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var productJobRepository domain.ProductJobRepository
var productJob *domain.ProductJob
var checkDuplicateJobName = false
productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, cmd.ProductJobId)
if cmd.JobName != productJob.JobName {
checkDuplicateJobName = true
}
if workStation.WorkStationId != productJob.WorkStation.WorkStationId {
checkDuplicateJobName = true
}
if checkDuplicateJobName {
if job, err := productJobRepository.FindOne(map[string]interface{}{
"jobName": cmd.JobName,
"companyId": productJob.CompanyId,
"orgId": productJob.OrgId,
"workStationId": workStation.WorkStationId,
}); err == nil && job != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
}
}
productJob.WorkStation = workStation
productJob.JobName = cmd.JobName
productJob.RelatedDevices = cmd.RelatedDevices
productJob.UpdatedAt = time.Now()
if productJob, err := productJobRepository.Save(productJob); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return productJob, nil
}
}
// 搜索工位服务列表
func (productJobService *ProductJobService) SearchProductJob(operateInfo *domain.OperateInfo, cmd *query.SearchProductJobQuery) (int64, interface{}, error) {
if err := cmd.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var productJobRepository domain.ProductJobRepository
if value, err := factory.CreateProductJobRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productJobRepository = value
}
workshops, _ := factory.FastPgWorkshops(transactionContext, operateInfo.CompanyId)
queryOptions := utils.ObjectToMap(cmd)
queryOptions = workshops.FindByNameWithQuery(queryOptions, cmd.WorkshopName, cmd.LineName, cmd.SectionName)
count, productJobs, err := productJobRepository.Find(queryOptions)
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var result = make([]*dto.ProductJobDto, 0)
for i := range productJobs {
item := productJobs[i]
item.WorkStation = workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
newJobDto := &dto.ProductJobDto{}
newJobDto.LoadDto(item, operateInfo.OrgId)
result = append(result, newJobDto)
}
return count, result, nil
}
func NewProductJobService(options map[string]interface{}) *ProductJobService {
newProductJobService := &ProductJobService{}
return newProductJobService
}