作者 tangxvhui

暂存

@@ -216,3 +216,18 @@ func CreateMessagePersonalRepository(options map[string]interface{}) domain.Mess @@ -216,3 +216,18 @@ func CreateMessagePersonalRepository(options map[string]interface{}) domain.Mess
216 } 216 }
217 return repository.NewMessagePersonalRepository(transactionContext) 217 return repository.NewMessagePersonalRepository(transactionContext)
218 } 218 }
  219 +func CreateTaskRepository(options map[string]interface{}) domain.TaskRepository {
  220 + var transactionContext *pg.TransactionContext
  221 + if value, ok := options["transactionContext"]; ok {
  222 + transactionContext = value.(*pg.TransactionContext)
  223 + }
  224 + return repository.NewTaskRepository(transactionContext)
  225 +}
  226 +
  227 +func CreateTaskStageRepository(options map[string]interface{}) domain.TaskStageRepository {
  228 + var transactionContext *pg.TransactionContext
  229 + if value, ok := options["transactionContext"]; ok {
  230 + transactionContext = value.(*pg.TransactionContext)
  231 + }
  232 + return repository.NewTaskStageRepository(transactionContext)
  233 +}
  1 +package command
  2 +
  3 +type CreateTaskCommand struct {
  4 + Name string `json:"name"` //任务名称
  5 + LeaderId int `json:"leaderId"` //赋值人id
  6 +}
  1 +package command
  2 +
  3 +type RunTaskCommand struct{}
  1 +package command
  2 +
  3 +type StopTaskCommand struct{}
  1 +package command
  2 +
  3 +type UpdateTaskCommand struct {
  4 + Id int `json:"id"`
  5 + Name string `json:"name"` //任务名称
  6 + LeaderId int `json:"leaderId"` //赋值人id
  7 + LevelName string `json:"levelName"` //优先级
  8 + StageList []struct {
  9 + Id int `json:"id"`
  10 + Name string `json:"name"` //里程碑名称
  11 + PlanCompletedAt string `json:"planCompletedAt"` //计划完成时间, 例:2006-01-02
  12 + } `json:"stageList"`
  13 +}
  1 +package service
  2 +
  3 +import (
  4 + "time"
  5 +
  6 + "github.com/linmadan/egglib-go/core/application"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 +)
  11 +
  12 +type TaskService struct{}
  13 +
  14 +func dayEndTime(t time.Time) time.Time {
  15 + y, m, d := t.Local().Date()
  16 + t2 := time.Date(y, m, d, 23, 59, 59, 0, time.Local)
  17 + return t2
  18 +}
  19 +
  20 +func (srv TaskService) CreateTask(transactionContext application.TransactionContext, param *command.CreateTaskCommand) error {
  21 + taskRepo := factory.CreateTaskRepository(map[string]interface{}{
  22 + "transactionContext": transactionContext,
  23 + })
  24 + taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
  25 + "transactionContext": transactionContext,
  26 + })
  27 + cnt, _, err := taskRepo.Find(map[string]interface{}{
  28 + "name": param.Name,
  29 + "leaderId": param.LeaderId,
  30 + })
  31 + if err != nil {
  32 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务失败:"+err.Error())
  33 + }
  34 + if cnt > 0 {
  35 + //任务已存在
  36 + return nil
  37 + }
  38 + nowTime := time.Now()
  39 + newTask := domain.Task{
  40 + Id: 0,
  41 + Name: param.Name,
  42 + Alias: param.Name,
  43 + Status: domain.TaskRunning,
  44 + Level: 0,
  45 + LevalName: "",
  46 + RelatedUser: []int{},
  47 + RunAt: nowTime.Unix(),
  48 + }
  49 + newTask.ApplyLevelName("")
  50 + err = taskRepo.Save(&newTask)
  51 + if err != nil {
  52 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
  53 + }
  54 +
  55 + nowEndTime := dayEndTime(nowTime)
  56 +
  57 + newTaskStage := []*domain.TaskStage{
  58 + {
  59 + Id: 0,
  60 + TaskId: newTask.Id,
  61 + Name: "里程碑1",
  62 + SortBy: 1,
  63 + Status: domain.TaskStageUncompleted,
  64 + PlanCompletedAt: nowEndTime.Add(30 * 24 * time.Hour).Unix(),
  65 + RealCompletedAt: 0,
  66 + },
  67 + {
  68 + Id: 0,
  69 + TaskId: newTask.Id,
  70 + Name: "里程碑2",
  71 + SortBy: 2,
  72 + Status: domain.TaskStageUncompleted,
  73 + PlanCompletedAt: nowEndTime.Add(60 * 24 * time.Hour).Unix(),
  74 + RealCompletedAt: 0,
  75 + },
  76 + {
  77 + Id: 0,
  78 + TaskId: newTask.Id,
  79 + Name: "里程碑3",
  80 + SortBy: 3,
  81 + Status: domain.TaskStageUncompleted,
  82 + PlanCompletedAt: nowEndTime.Add(90 * 24 * time.Hour).Unix(),
  83 + RealCompletedAt: 0,
  84 + },
  85 + }
  86 + err = taskStageRepo.Save(newTaskStage)
  87 + if err != nil {
  88 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
  89 + }
  90 + return nil
  91 +}
  92 +
  93 +func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]interface{}, error) {
  94 + transactionContext, err := factory.CreateTransactionContext(nil)
  95 + if err != nil {
  96 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  97 + }
  98 + if err := transactionContext.StartTransaction(); err != nil {
  99 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  100 + }
  101 + defer func() {
  102 + _ = transactionContext.RollbackTransaction()
  103 + }()
  104 + taskRepo := factory.CreateTaskRepository(map[string]interface{}{
  105 + "transactionContext": transactionContext,
  106 + })
  107 + taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
  108 + "transactionContext": transactionContext,
  109 + })
  110 + taskData, err := taskRepo.FindOne(map[string]interface{}{
  111 + "id": param.Id,
  112 + })
  113 + if err != nil {
  114 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  115 + }
  116 + _, stageList, err := taskStageRepo.Find(map[string]interface{}{
  117 + "taskId": param.Id,
  118 + })
  119 + _ = taskData
  120 + _ = stageList
  121 +
  122 + if err := transactionContext.CommitTransaction(); err != nil {
  123 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  124 + }
  125 + return nil, nil
  126 +}
  127 +
  128 +func (srv TaskService) RunTask() error {
  129 + return nil
  130 +}
  131 +
  132 +func (srv TaskService) StopTask() error {
  133 + return nil
  134 +}
@@ -14,12 +14,16 @@ type Task struct { @@ -14,12 +14,16 @@ type Task struct {
14 Id int `json:"id"` 14 Id int `json:"id"`
15 CreatedAt time.Time `json:"createdAt"` 15 CreatedAt time.Time `json:"createdAt"`
16 UpdatedAt time.Time `json:"updatedAt"` 16 UpdatedAt time.Time `json:"updatedAt"`
17 - DeletedAt time.Time `json:"deletedAt"` 17 + DeletedAt *time.Time `json:"deletedAt"`
18 Name string `json:"name"` // 任务名称 18 Name string `json:"name"` // 任务名称
  19 + Alias string `json:"alias"` // 任务别名
19 Leader TaskLeader `json:"leader"` // 任务负责人 20 Leader TaskLeader `json:"leader"` // 任务负责人
20 Status TaskState `json:"status"` // 任务的状态 21 Status TaskState `json:"status"` // 任务的状态
21 Level int `json:"level"` // 优先级,值越小优先级越高 22 Level int `json:"level"` // 优先级,值越小优先级越高
22 LevalName string `json:"levalName"` // 优先级名称 23 LevalName string `json:"levalName"` // 优先级名称
  24 + RelatedUser []int `json:"relatedUser"` // 相关的员工id
  25 + RunAt int64 `json:"runAt"` // 启动的时间戳,秒
  26 + StopAt int64 `json:"stopAt"` // 停止的时间戳,秒
23 } 27 }
24 28
25 type TaskLeader struct { 29 type TaskLeader struct {
@@ -15,12 +15,12 @@ type TaskStage struct { @@ -15,12 +15,12 @@ type TaskStage struct {
15 TaskId int `json:"taskId"` 15 TaskId int `json:"taskId"`
16 CreatedAt time.Time `json:"createdAt"` 16 CreatedAt time.Time `json:"createdAt"`
17 UpdatedAt time.Time `json:"updatedAt"` 17 UpdatedAt time.Time `json:"updatedAt"`
18 - DeletedAt time.Time `json:"deletedAt"` 18 + DeletedAt *time.Time `json:"deletedAt"`
19 Name string `json:"name"` //里程碑名称 19 Name string `json:"name"` //里程碑名称
20 SortBy int `json:"sortBy"` //排序 20 SortBy int `json:"sortBy"` //排序
21 Status TaskStageState `json:"status"` //里程碑完成情况 21 Status TaskStageState `json:"status"` //里程碑完成情况
22 - PlanCompletedAt int `json:"planCompletedAt"` //计划完成时间  
23 - RealCompletedAt int `json:"realCompletedAt"` //时间完成时间 22 + PlanCompletedAt int64 `json:"planCompletedAt"` //计划完成时间戳,秒
  23 + RealCompletedAt int64 `json:"realCompletedAt"` //时间完成时间戳,秒
24 } 24 }
25 type TaskStageRepository interface { 25 type TaskStageRepository interface {
26 Save(param []*TaskStage) error 26 Save(param []*TaskStage) error
@@ -12,11 +12,14 @@ type Task struct { @@ -12,11 +12,14 @@ type Task struct {
12 Id int `pg:"id,pk"` 12 Id int `pg:"id,pk"`
13 CreatedAt time.Time `pg:"created_at"` 13 CreatedAt time.Time `pg:"created_at"`
14 UpdatedAt time.Time `pg:"updated_at"` 14 UpdatedAt time.Time `pg:"updated_at"`
15 - DeletedAt time.Time `pg:"deleted_at"` 15 + DeletedAt *time.Time `pg:"deleted_at"`
16 Name string `pg:"name"` // 任务名称 16 Name string `pg:"name"` // 任务名称
  17 + Alias string `pg:"alias"`
17 Leader domain.TaskLeader `pg:"leader"` // 任务负责人 18 Leader domain.TaskLeader `pg:"leader"` // 任务负责人
18 Status int `pg:"status"` // 任务的状态 19 Status int `pg:"status"` // 任务的状态
19 - StageList []domain.TaskStage `pg:"stage_list"` // 里程碑  
20 Level int `pg:"level"` // 优先级,值越小优先级越高 20 Level int `pg:"level"` // 优先级,值越小优先级越高
21 LevalName string `pg:"leval_name"` // 优先级名称 21 LevalName string `pg:"leval_name"` // 优先级名称
  22 + RelatedUser []int `pg:"related_user"` //
  23 + RunAt int64 `pg:"run_at"` // 启动的时间
  24 + StopAt int64 `pg:"stop_at"` // 停止的时间
22 } 25 }
@@ -9,10 +9,10 @@ type TaskStage struct { @@ -9,10 +9,10 @@ type TaskStage struct {
9 TaskId int `pg:"task_id"` 9 TaskId int `pg:"task_id"`
10 CreatedAt time.Time `pg:"created_at"` 10 CreatedAt time.Time `pg:"created_at"`
11 UpdatedAt time.Time `pg:"updated_at"` 11 UpdatedAt time.Time `pg:"updated_at"`
12 - DeletedAt time.Time `pg:"deleted_at"` 12 + DeletedAt *time.Time `pg:"deleted_at"`
13 Name string `pg:"name"` //里程碑名称 13 Name string `pg:"name"` //里程碑名称
14 SortBy int `pg:"sort_by"` //排序 14 SortBy int `pg:"sort_by"` //排序
15 Status int `pg:"status"` //里程碑完成情况 15 Status int `pg:"status"` //里程碑完成情况
16 - PlanCompletedAt int `pg:"plan_completed_at"` //计划完成时间  
17 - RealCompletedAt int `pg:"real_completed_at"` //时间完成时间 16 + PlanCompletedAt int64 `pg:"plan_completed_at"` //计划完成时间
  17 + RealCompletedAt int64 `pg:"real_completed_at"` //时间完成时间
18 } 18 }
@@ -19,6 +19,10 @@ type TaskRepository struct { @@ -19,6 +19,10 @@ type TaskRepository struct {
19 19
20 var _ domain.TaskRepository = (*TaskRepository)(nil) 20 var _ domain.TaskRepository = (*TaskRepository)(nil)
21 21
  22 +func NewTaskRepository(transactionContext *pgTransaction.TransactionContext) *TaskRepository {
  23 + return &TaskRepository{transactionContext: transactionContext}
  24 +}
  25 +
22 func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task { 26 func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task {
23 return &domain.Task{ 27 return &domain.Task{
24 Id: d.Id, 28 Id: d.Id,
@@ -26,10 +30,14 @@ func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task { @@ -26,10 +30,14 @@ func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task {
26 UpdatedAt: d.UpdatedAt, 30 UpdatedAt: d.UpdatedAt,
27 DeletedAt: d.DeletedAt, 31 DeletedAt: d.DeletedAt,
28 Name: d.Name, 32 Name: d.Name,
  33 + Alias: d.Alias,
29 Leader: d.Leader, 34 Leader: d.Leader,
30 Status: domain.TaskState(d.Status), 35 Status: domain.TaskState(d.Status),
31 Level: d.Level, 36 Level: d.Level,
32 LevalName: d.LevalName, 37 LevalName: d.LevalName,
  38 + RelatedUser: d.RelatedUser,
  39 + RunAt: d.RunAt,
  40 + StopAt: d.StopAt,
33 } 41 }
34 } 42 }
35 43
@@ -45,10 +53,14 @@ func (repo *TaskRepository) Save(param *domain.Task) error { @@ -45,10 +53,14 @@ func (repo *TaskRepository) Save(param *domain.Task) error {
45 UpdatedAt: param.UpdatedAt, 53 UpdatedAt: param.UpdatedAt,
46 DeletedAt: param.DeletedAt, 54 DeletedAt: param.DeletedAt,
47 Name: param.Name, 55 Name: param.Name,
  56 + Alias: param.Alias,
48 Leader: param.Leader, 57 Leader: param.Leader,
49 Status: int(param.Status), 58 Status: int(param.Status),
50 Level: param.Level, 59 Level: param.Level,
51 LevalName: param.LevalName, 60 LevalName: param.LevalName,
  61 + RelatedUser: param.RelatedUser,
  62 + RunAt: param.RunAt,
  63 + StopAt: param.StopAt,
52 } 64 }
53 db := repo.transactionContext.PgTx 65 db := repo.transactionContext.PgTx
54 if m.Id == 0 { 66 if m.Id == 0 {
@@ -102,7 +114,12 @@ func (repo *TaskRepository) Find(queryOptions map[string]interface{}) (int, []*d @@ -102,7 +114,12 @@ func (repo *TaskRepository) Find(queryOptions map[string]interface{}) (int, []*d
102 query := tx.Model(&m). 114 query := tx.Model(&m).
103 Where("deleted_at isnull"). 115 Where("deleted_at isnull").
104 Limit(20) 116 Limit(20)
105 - 117 + if val, ok := queryOptions["name"]; ok {
  118 + query.Where("task.name like ?", val)
  119 + }
  120 + if val, ok := queryOptions["leaderId"]; ok {
  121 + query.Where("task.leader->>'userId'='?'", val)
  122 + }
106 query.Order("id desc") 123 query.Order("id desc")
107 count, err := query.SelectAndCount() 124 count, err := query.SelectAndCount()
108 if err != nil { 125 if err != nil {
@@ -19,6 +19,10 @@ type TaskStageRepository struct { @@ -19,6 +19,10 @@ type TaskStageRepository struct {
19 19
20 var _ domain.TaskStageRepository = (*TaskStageRepository)(nil) 20 var _ domain.TaskStageRepository = (*TaskStageRepository)(nil)
21 21
  22 +func NewTaskStageRepository(transactionContext *pgTransaction.TransactionContext) *TaskStageRepository {
  23 + return &TaskStageRepository{transactionContext: transactionContext}
  24 +}
  25 +
22 func (repo *TaskStageRepository) TransformToDomain(d *models.TaskStage) *domain.TaskStage { 26 func (repo *TaskStageRepository) TransformToDomain(d *models.TaskStage) *domain.TaskStage {
23 return &domain.TaskStage{ 27 return &domain.TaskStage{
24 Id: d.Id, 28 Id: d.Id,
@@ -102,8 +106,10 @@ func (repo *TaskStageRepository) Find(queryOptions map[string]interface{}) (int, @@ -102,8 +106,10 @@ func (repo *TaskStageRepository) Find(queryOptions map[string]interface{}) (int,
102 query := tx.Model(&m). 106 query := tx.Model(&m).
103 Where("deleted_at isnull"). 107 Where("deleted_at isnull").
104 Limit(20) 108 Limit(20)
105 -  
106 - query.Order("id desc") 109 + if val, ok := queryOptions["taskId"]; ok {
  110 + query.Where("task_stage.task_id = ?", val)
  111 + }
  112 + query.Order("sort_by")
107 count, err := query.SelectAndCount() 113 count, err := query.SelectAndCount()
108 if err != nil { 114 if err != nil {
109 return 0, nil, err 115 return 0, nil, err