|
|
package service
|
|
|
|
|
|
import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
)
|
|
|
|
|
|
type TaskService struct{}
|
|
|
|
|
|
func dayEndTime(t time.Time) time.Time {
|
|
|
y, m, d := t.Local().Date()
|
|
|
t2 := time.Date(y, m, d, 23, 59, 59, 0, time.Local)
|
|
|
return t2
|
|
|
}
|
|
|
|
|
|
func (srv TaskService) CreateTask(transactionContext application.TransactionContext, param *command.CreateTaskCommand) error {
|
|
|
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
cnt, _, err := taskRepo.Find(map[string]interface{}{
|
|
|
"name": param.Name,
|
|
|
"leaderId": param.LeaderId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务失败:"+err.Error())
|
|
|
}
|
|
|
if cnt > 0 {
|
|
|
//任务已存在
|
|
|
return nil
|
|
|
}
|
|
|
nowTime := time.Now()
|
|
|
newTask := domain.Task{
|
|
|
Id: 0,
|
|
|
Name: param.Name,
|
|
|
Alias: param.Name,
|
|
|
Status: domain.TaskRunning,
|
|
|
Level: 0,
|
|
|
LevalName: "",
|
|
|
RelatedUser: []int{},
|
|
|
RunAt: nowTime.Unix(),
|
|
|
}
|
|
|
newTask.ApplyLevelName("")
|
|
|
err = taskRepo.Save(&newTask)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
|
|
|
}
|
|
|
|
|
|
nowEndTime := dayEndTime(nowTime)
|
|
|
|
|
|
newTaskStage := []*domain.TaskStage{
|
|
|
{
|
|
|
Id: 0,
|
|
|
TaskId: newTask.Id,
|
|
|
Name: "里程碑1",
|
|
|
SortBy: 1,
|
|
|
Status: domain.TaskStageUncompleted,
|
|
|
PlanCompletedAt: nowEndTime.Add(30 * 24 * time.Hour).Unix(),
|
|
|
RealCompletedAt: 0,
|
|
|
},
|
|
|
{
|
|
|
Id: 0,
|
|
|
TaskId: newTask.Id,
|
|
|
Name: "里程碑2",
|
|
|
SortBy: 2,
|
|
|
Status: domain.TaskStageUncompleted,
|
|
|
PlanCompletedAt: nowEndTime.Add(60 * 24 * time.Hour).Unix(),
|
|
|
RealCompletedAt: 0,
|
|
|
},
|
|
|
{
|
|
|
Id: 0,
|
|
|
TaskId: newTask.Id,
|
|
|
Name: "里程碑3",
|
|
|
SortBy: 3,
|
|
|
Status: domain.TaskStageUncompleted,
|
|
|
PlanCompletedAt: nowEndTime.Add(90 * 24 * time.Hour).Unix(),
|
|
|
RealCompletedAt: 0,
|
|
|
},
|
|
|
}
|
|
|
err = taskStageRepo.Save(newTaskStage)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]interface{}, 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()
|
|
|
}()
|
|
|
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
taskData, err := taskRepo.FindOne(map[string]interface{}{
|
|
|
"id": param.Id,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
|
|
|
"taskId": param.Id,
|
|
|
})
|
|
|
_ = taskData
|
|
|
_ = stageList
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func (srv TaskService) RunTask() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (srv TaskService) StopTask() error {
|
|
|
return nil
|
|
|
} |
...
|
...
|
|