1
|
package service
|
1
|
package service
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "strconv"
|
4
|
"time"
|
5
|
"time"
|
5
|
|
6
|
|
6
|
"github.com/linmadan/egglib-go/core/application"
|
7
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
8
|
+ "github.com/linmadan/egglib-go/utils/tool_funs"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
9
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
|
|
10
|
+ "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/adapter"
|
8
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
|
9
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
12
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
10
|
)
|
13
|
)
|
|
@@ -107,6 +110,9 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] |
|
@@ -107,6 +110,9 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] |
107
|
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
110
|
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
108
|
"transactionContext": transactionContext,
|
111
|
"transactionContext": transactionContext,
|
109
|
})
|
112
|
})
|
|
|
113
|
+ userRepo := factory.CreateUserRepository(map[string]interface{}{
|
|
|
114
|
+ "transactionContext": transactionContext,
|
|
|
115
|
+ })
|
110
|
taskData, err := taskRepo.FindOne(map[string]interface{}{
|
116
|
taskData, err := taskRepo.FindOne(map[string]interface{}{
|
111
|
"id": param.Id,
|
117
|
"id": param.Id,
|
112
|
})
|
118
|
})
|
|
@@ -116,19 +122,300 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] |
|
@@ -116,19 +122,300 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] |
116
|
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
|
122
|
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
|
117
|
"taskId": param.Id,
|
123
|
"taskId": param.Id,
|
118
|
})
|
124
|
})
|
119
|
- _ = taskData
|
|
|
120
|
- _ = stageList
|
125
|
+ if err != nil {
|
|
|
126
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
127
|
+ }
|
|
|
128
|
+ leaderData, err := userRepo.FindOne(map[string]interface{}{
|
|
|
129
|
+ "id": param.LeaderId,
|
|
|
130
|
+ })
|
|
|
131
|
+ if err != nil {
|
|
|
132
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
133
|
+ }
|
|
|
134
|
+ taskData.Alias = param.Alias
|
|
|
135
|
+ taskData.Leader = domain.TaskLeader{
|
|
|
136
|
+ Id: leaderData.Id,
|
|
|
137
|
+ Account: leaderData.Account,
|
|
|
138
|
+ Name: leaderData.Name,
|
|
|
139
|
+ }
|
|
|
140
|
+ taskData.ApplyLevelName(param.LevelName)
|
|
|
141
|
+
|
|
|
142
|
+ nowTime := time.Now()
|
|
|
143
|
+
|
|
|
144
|
+ stageMap := map[int]*domain.TaskStage{}
|
|
|
145
|
+ for _, val := range stageList {
|
|
|
146
|
+ val.DeletedAt = &nowTime
|
|
|
147
|
+ stageMap[val.Id] = val
|
|
|
148
|
+ }
|
|
|
149
|
+ lastPlanCompletedAt := time.Time{}
|
|
|
150
|
+ newStageList := []*domain.TaskStage{}
|
|
|
151
|
+ for i, val2 := range param.StageList {
|
|
|
152
|
+ t, err := time.ParseInLocation("2006-01-02", val2.PlanCompletedAt, time.Local)
|
|
|
153
|
+ if err != nil {
|
|
|
154
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "请填写里程碑完成时间")
|
|
|
155
|
+ }
|
|
|
156
|
+ if i > 0 {
|
|
|
157
|
+ if lastPlanCompletedAt.After(t) {
|
|
|
158
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑完成时间填写错误")
|
|
|
159
|
+ }
|
|
|
160
|
+ }
|
|
|
161
|
+ lastPlanCompletedAt = t
|
|
|
162
|
+ if val2.Id == 0 {
|
|
|
163
|
+ newStage := domain.TaskStage{
|
|
|
164
|
+ Id: 0,
|
|
|
165
|
+ TaskId: taskData.Id,
|
|
|
166
|
+ Name: val2.Name,
|
|
|
167
|
+ SortBy: i + 1,
|
|
|
168
|
+ Status: domain.TaskStageUncompleted,
|
|
|
169
|
+ PlanCompletedAt: t.Unix(),
|
|
|
170
|
+ RealCompletedAt: 0,
|
|
|
171
|
+ }
|
|
|
172
|
+ newStageList = append(newStageList, &newStage)
|
|
|
173
|
+ continue
|
|
|
174
|
+ }
|
|
|
175
|
+ oldStage, ok := stageMap[val2.Id]
|
|
|
176
|
+ if !ok {
|
|
|
177
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑数据被其他人变更")
|
|
|
178
|
+ }
|
|
|
179
|
+ oldStage.DeletedAt = nil
|
|
|
180
|
+ oldStage.SortBy = i + 1
|
|
|
181
|
+ oldStage.PlanCompletedAt = t.Unix()
|
|
|
182
|
+ oldStage.Name = val2.Name
|
|
|
183
|
+ }
|
|
|
184
|
+ stageList = append(stageList, newStageList...)
|
121
|
|
185
|
|
|
|
186
|
+ err = taskStageRepo.Save(stageList)
|
|
|
187
|
+ if err != nil {
|
|
|
188
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
189
|
+ }
|
|
|
190
|
+ err = taskRepo.Save(taskData)
|
|
|
191
|
+ if err != nil {
|
|
|
192
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
193
|
+ }
|
122
|
if err := transactionContext.CommitTransaction(); err != nil {
|
194
|
if err := transactionContext.CommitTransaction(); err != nil {
|
123
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
195
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
124
|
}
|
196
|
}
|
125
|
- return nil, nil
|
197
|
+ return map[string]interface{}{
|
|
|
198
|
+ "id": param.Id,
|
|
|
199
|
+ }, nil
|
126
|
}
|
200
|
}
|
127
|
|
201
|
|
128
|
-func (srv TaskService) RunTask() error {
|
|
|
129
|
- return nil
|
202
|
+// 获取任务详情
|
|
|
203
|
+func (srv TaskService) GetTaskInfo(param *command.GetTaskCommand) (*adapter.TaskInfoAdapter, error) {
|
|
|
204
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
205
|
+ if err != nil {
|
|
|
206
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
207
|
+ }
|
|
|
208
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
209
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
210
|
+ }
|
|
|
211
|
+ defer func() {
|
|
|
212
|
+ _ = transactionContext.RollbackTransaction()
|
|
|
213
|
+ }()
|
|
|
214
|
+ taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
215
|
+ "transactionContext": transactionContext,
|
|
|
216
|
+ })
|
|
|
217
|
+ taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
|
|
218
|
+ "transactionContext": transactionContext,
|
|
|
219
|
+ })
|
|
|
220
|
+ userRepo := factory.CreateUserRepository(map[string]interface{}{
|
|
|
221
|
+ "transactionContext": transactionContext,
|
|
|
222
|
+ })
|
|
|
223
|
+ taskData, err := taskRepo.FindOne(map[string]interface{}{
|
|
|
224
|
+ "id": param.TaskId,
|
|
|
225
|
+ })
|
|
|
226
|
+ if err != nil {
|
|
|
227
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
228
|
+ }
|
|
|
229
|
+ _, stageList, err := taskStageRepo.Find(map[string]interface{}{
|
|
|
230
|
+ "taskId": param.TaskId,
|
|
|
231
|
+ })
|
|
|
232
|
+ if err != nil {
|
|
|
233
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
234
|
+ }
|
|
|
235
|
+
|
|
|
236
|
+ relatedUserId := taskData.RelatedUser
|
|
|
237
|
+ _, relatedUserList, err := userRepo.Find(map[string]interface{}{
|
|
|
238
|
+ "ids": relatedUserId,
|
|
|
239
|
+ })
|
|
|
240
|
+ if err != nil {
|
|
|
241
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取相关人员"+err.Error())
|
|
|
242
|
+ }
|
|
|
243
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
244
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ result := adapter.TaskInfoAdapter{
|
|
|
248
|
+ Id: taskData.Id,
|
|
|
249
|
+ Name: taskData.Name,
|
|
|
250
|
+ Alias: taskData.Alias,
|
|
|
251
|
+ Leader: adapter.UserData{
|
|
|
252
|
+ Id: taskData.Leader.Id,
|
|
|
253
|
+ Account: taskData.Leader.Account,
|
|
|
254
|
+ Name: taskData.Leader.Name,
|
|
|
255
|
+ },
|
|
|
256
|
+ Status: int(taskData.Status),
|
|
|
257
|
+ Level: taskData.Level,
|
|
|
258
|
+ LevalName: taskData.LevalName,
|
|
|
259
|
+ RelatedUserId: []string{},
|
|
|
260
|
+ RelatedUser: []adapter.UserData{},
|
|
|
261
|
+ StageList: []adapter.TaskStage{},
|
|
|
262
|
+ }
|
|
|
263
|
+ for _, val := range relatedUserList {
|
|
|
264
|
+ idStr := strconv.Itoa(int(val.Id))
|
|
|
265
|
+ result.RelatedUserId = append(result.RelatedUserId, idStr)
|
|
|
266
|
+ result.RelatedUser = append(result.RelatedUser, adapter.UserData{
|
|
|
267
|
+ Id: val.Id,
|
|
|
268
|
+ Account: val.Account,
|
|
|
269
|
+ Name: val.Name,
|
|
|
270
|
+ })
|
|
|
271
|
+ }
|
|
|
272
|
+ for _, val := range stageList {
|
|
|
273
|
+ var t string
|
|
|
274
|
+ if val.PlanCompletedAt > 0 {
|
|
|
275
|
+ t = time.Unix(val.PlanCompletedAt, 0).Local().Format("2006-01-02")
|
|
|
276
|
+ }
|
|
|
277
|
+ result.StageList = append(result.StageList, adapter.TaskStage{
|
|
|
278
|
+ Id: val.Id,
|
|
|
279
|
+ Name: val.Name,
|
|
|
280
|
+ PlanCompletedAt: t,
|
|
|
281
|
+ })
|
|
|
282
|
+ }
|
|
|
283
|
+ return &result, nil
|
130
|
}
|
284
|
}
|
131
|
|
285
|
|
132
|
-func (srv TaskService) StopTask() error {
|
|
|
133
|
- return nil
|
286
|
+// RunTask 启动任务
|
|
|
287
|
+func (srv TaskService) RunTask(param *command.RunTaskCommand) (map[string]interface{}, error) {
|
|
|
288
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
289
|
+ if err != nil {
|
|
|
290
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
291
|
+ }
|
|
|
292
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
293
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
294
|
+ }
|
|
|
295
|
+ defer func() {
|
|
|
296
|
+ _ = transactionContext.RollbackTransaction()
|
|
|
297
|
+ }()
|
|
|
298
|
+ taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
299
|
+ "transactionContext": transactionContext,
|
|
|
300
|
+ })
|
|
|
301
|
+ taskData, err := taskRepo.FindOne(map[string]interface{}{
|
|
|
302
|
+ "id": param.TaskId,
|
|
|
303
|
+ })
|
|
|
304
|
+ if err != nil {
|
|
|
305
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
306
|
+ }
|
|
|
307
|
+ if taskData.Status == domain.TaskRunning {
|
|
|
308
|
+ return map[string]interface{}{"id": param.TaskId}, nil
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ taskData.Status = domain.TaskRunning
|
|
|
312
|
+ taskData.RunAt = time.Now().Unix()
|
|
|
313
|
+ err = taskRepo.Save(taskData)
|
|
|
314
|
+ if err != nil {
|
|
|
315
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务数据"+err.Error())
|
|
|
316
|
+ }
|
|
|
317
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
318
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
319
|
+ }
|
|
|
320
|
+ return map[string]interface{}{"id": param.TaskId}, nil
|
|
|
321
|
+}
|
|
|
322
|
+
|
|
|
323
|
+// RunTask 停止任务
|
|
|
324
|
+func (srv TaskService) StopTask(param *command.StopTaskCommand) (map[string]interface{}, error) {
|
|
|
325
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
326
|
+ if err != nil {
|
|
|
327
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
328
|
+ }
|
|
|
329
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
330
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
331
|
+ }
|
|
|
332
|
+ defer func() {
|
|
|
333
|
+ _ = transactionContext.RollbackTransaction()
|
|
|
334
|
+ }()
|
|
|
335
|
+ taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
336
|
+ "transactionContext": transactionContext,
|
|
|
337
|
+ })
|
|
|
338
|
+ taskData, err := taskRepo.FindOne(map[string]interface{}{
|
|
|
339
|
+ "id": param.TaskId,
|
|
|
340
|
+ })
|
|
|
341
|
+ if err != nil {
|
|
|
342
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
343
|
+ }
|
|
|
344
|
+ if taskData.Status == domain.TaskStop {
|
|
|
345
|
+ return map[string]interface{}{"id": param.TaskId}, nil
|
|
|
346
|
+ }
|
|
|
347
|
+
|
|
|
348
|
+ taskData.Status = domain.TaskStop
|
|
|
349
|
+ taskData.StopAt = time.Now().Unix()
|
|
|
350
|
+ err = taskRepo.Save(taskData)
|
|
|
351
|
+ if err != nil {
|
|
|
352
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务数据"+err.Error())
|
|
|
353
|
+ }
|
|
|
354
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
355
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
356
|
+ }
|
|
|
357
|
+ return map[string]interface{}{"id": param.TaskId}, nil
|
|
|
358
|
+}
|
|
|
359
|
+
|
|
|
360
|
+// ListTask 任务列表
|
|
|
361
|
+func (srv TaskService) ListTask(param *command.ListTaskCommand) (map[string]interface{}, error) {
|
|
|
362
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
363
|
+ if err != nil {
|
|
|
364
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
365
|
+ }
|
|
|
366
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
367
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
368
|
+ }
|
|
|
369
|
+ defer func() {
|
|
|
370
|
+ _ = transactionContext.RollbackTransaction()
|
|
|
371
|
+ }()
|
|
|
372
|
+ taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
373
|
+ "transactionContext": transactionContext,
|
|
|
374
|
+ })
|
|
|
375
|
+ taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
|
|
376
|
+ "transactionContext": transactionContext,
|
|
|
377
|
+ })
|
|
|
378
|
+ //
|
|
|
379
|
+ condition := map[string]interface{}{
|
|
|
380
|
+ "offset": 0,
|
|
|
381
|
+ "limit": 20,
|
|
|
382
|
+ "companyId": param.CompanyId,
|
|
|
383
|
+ }
|
|
|
384
|
+ if len(param.SearchWord) > 0 {
|
|
|
385
|
+ condition["aliasOrLeader"] = "%" + param.SearchWord + "%"
|
|
|
386
|
+ }
|
|
|
387
|
+ if param.PageSize > 0 {
|
|
|
388
|
+ condition["limit"] = param.PageSize
|
|
|
389
|
+ if param.PageNumber > 0 {
|
|
|
390
|
+ condition["offset"] = (param.PageNumber - 1) * param.PageSize
|
|
|
391
|
+ }
|
|
|
392
|
+ }
|
|
|
393
|
+
|
|
|
394
|
+ taskCnt, taskList, err := taskRepo.Find(condition)
|
|
|
395
|
+ if err != nil {
|
|
|
396
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务数据"+err.Error())
|
|
|
397
|
+ }
|
|
|
398
|
+ taskIdList := []int{}
|
|
|
399
|
+ for _, val := range taskList {
|
|
|
400
|
+ taskIdList = append(taskIdList, val.Id)
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+ _, stageList, err := taskStageRepo.Find(map[string]interface{}{
|
|
|
404
|
+ "taskIdList": taskIdList,
|
|
|
405
|
+ })
|
|
|
406
|
+ if err != nil {
|
|
|
407
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务阶段数据"+err.Error())
|
|
|
408
|
+ }
|
|
|
409
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
410
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
411
|
+ }
|
|
|
412
|
+
|
|
|
413
|
+ taskResult := []*adapter.TaskItem{}
|
|
|
414
|
+ _ = stageList
|
|
|
415
|
+ // taskMapResult := map[int]*adapter.TaskItem{}
|
|
|
416
|
+ // for _, val := range taskList {
|
|
|
417
|
+
|
|
|
418
|
+ // }
|
|
|
419
|
+ result := tool_funs.SimpleWrapGridMap(int64(taskCnt), taskResult)
|
|
|
420
|
+ return result, nil
|
134
|
} |
421
|
} |