作者 郑周

定时器 通知

@@ -376,9 +376,12 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) @@ -376,9 +376,12 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
376 CycleId: project.CycleId, 376 CycleId: project.CycleId,
377 ProjectId: project.Id, 377 ProjectId: project.Id,
378 NodeId: node.Id, 378 NodeId: node.Id,
  379 + NodeType: node.Type,
  380 + NodeName: node.Name,
  381 + NodeDescribe: node.Describe,
  382 + TimeStart: node.TimeStart,
  383 + TimeEnd: node.TimeEnd,
379 KpiCycle: node.KpiCycle, 384 KpiCycle: node.KpiCycle,
380 - BeginAt: node.TimeStart,  
381 - EndAt: node.TimeEnd,  
382 } 385 }
383 // 在当前时间之前,则计算下一个周期时间 386 // 在当前时间之前,则计算下一个周期时间
384 if node.TimeStart.Before(now) { 387 if node.TimeStart.Before(now) {
@@ -3,8 +3,11 @@ package service @@ -3,8 +3,11 @@ package service
3 import ( 3 import (
4 "github.com/linmadan/egglib-go/core/application" 4 "github.com/linmadan/egglib-go/core/application"
5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" 5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/service"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" 9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  10 + "strconv"
8 "time" 11 "time"
9 ) 12 )
10 13
@@ -26,13 +29,15 @@ func (rs *NodeTaskService) SendEvaluationNode() error { @@ -26,13 +29,15 @@ func (rs *NodeTaskService) SendEvaluationNode() error {
26 transactionContext.RollbackTransaction() 29 transactionContext.RollbackTransaction()
27 }() 30 }()
28 taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext}) 31 taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
29 - projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})  
30 - //cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})  
31 -  
32 tasks, err := taskRepository.Find(map[string]interface{}{"now": time.Now().Local()}) 32 tasks, err := taskRepository.Find(map[string]interface{}{"now": time.Now().Local()})
33 if err != nil { 33 if err != nil {
34 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 34 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
35 } 35 }
  36 + if len(tasks) == 0 {
  37 + return nil
  38 + }
  39 + projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})
  40 + cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
36 41
37 //ttaffAssessRepository := factory.CreateStaffAssessTaskRepository(map[string]interface{}{"transactionContext": transactionContext}) 42 //ttaffAssessRepository := factory.CreateStaffAssessTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
38 // 43 //
@@ -53,49 +58,114 @@ func (rs *NodeTaskService) SendEvaluationNode() error { @@ -53,49 +58,114 @@ func (rs *NodeTaskService) SendEvaluationNode() error {
53 cycleIds = append(cycleIds, k) 58 cycleIds = append(cycleIds, k)
54 } 59 }
55 60
56 - _, projects, err := projectRepository.Find(map[string]interface{}{"ids": projectIds}) 61 + _, projects, err := projectRepository.Find(map[string]interface{}{"ids": projectIds}, "template")
  62 + if err != nil {
  63 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  64 + }
  65 + _, cycles, err := cycleRepository.Find(map[string]interface{}{"ids": cycleIds})
57 if err != nil { 66 if err != nil {
58 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 67 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
59 } 68 }
60 - //_, cycles, err := cycleRepository.Find(map[string]interface{}{"ids": cycleIds})  
61 - //if err != nil {  
62 - // return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
63 - //}  
64 69
65 for i := range projects { 70 for i := range projects {
66 projectIdsMap[projects[i].Id] = projects[i] 71 projectIdsMap[projects[i].Id] = projects[i]
67 } 72 }
  73 + for i := range cycles {
  74 + cycleIdsMap[cycles[i].Id] = cycles[i]
  75 + }
68 76
69 - //staffAssessService := service.NewStaffAssessServeice()  
70 - 77 + staffAssessTaskMap := map[int64]*command.CreateStaffAssessTask{}
71 for i := range tasks { 78 for i := range tasks {
72 task := tasks[i] 79 task := tasks[i]
  80 + // 项目
  81 + project, ok := projectIdsMap[task.ProjectId]
  82 + if ok {
73 83
74 - //if project, ok := projectIdsMap[task.ProjectId]; ok { 84 + // 环节截止时间
  85 + maxTime := time.Date(task.TimeEnd.Year(), task.TimeEnd.Month(), task.TimeEnd.Day(), task.TimeEnd.Hour(), task.TimeEnd.Minute(), 0, 0, time.Local)
75 86
76 - //csat := &command.CreateStaffAssessTask{  
77 - // CompanyId: project.CompanyId,  
78 - // EvaluationProjectId: project.Id,  
79 - // EvaluationProjectName: project.Name,  
80 - // CycleId: project.CycleId,  
81 - //}  
82 - //  
83 - //staffAssessService.CreateStaffAssessTask(csat)  
84 - //} 87 + // 当前周起始时间和截止时间
  88 + var cycleTimeStart = task.NextSentAt
  89 + var cycleTimeEnd *time.Time
85 90
86 - // 下一次发送时间 91 + // 下个周期起始时间
87 nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle) 92 nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle)
  93 + // 超过截止时间
  94 + if nextTime.After(maxTime) {
  95 + task.NextSentAt = nil
  96 + } else {
88 task.NextSentAt = &nextTime 97 task.NextSentAt = &nextTime
89 - // 如果超出截至时间,则周期置空  
90 - if task.NextSentAt.After(*task.EndAt) { 98 + }
  99 +
  100 + // 下个周期的起始时间=当前周期的截止时间
  101 + if task.NextSentAt == nil {
  102 + cycleTimeEnd = &maxTime
  103 + } else {
  104 + cycleTimeEnd = task.NextSentAt
  105 + }
  106 +
  107 + // 格式化周期的起始和截止时间
  108 + fmCycleStartTime := cycleTimeStart.Format("2006-1-2 15:04:05")
  109 + fmCycleTimeEnd := cycleTimeEnd.Format("2006-1-2 15:04:05")
  110 +
  111 + var csat, staffOk = staffAssessTaskMap[task.ProjectId]
  112 + if !staffOk {
  113 + csat = &command.CreateStaffAssessTask{
  114 + CompanyId: int(project.CompanyId),
  115 + EvaluationProjectId: int(project.Id),
  116 + EvaluationProjectName: project.Name,
  117 + CycleId: project.CycleId,
  118 + }
  119 +
  120 + // 周期名称
  121 + if cycle, ok := cycleIdsMap[project.CycleId]; ok {
  122 + csat.CycleName = cycle.Name
  123 + }
  124 +
  125 + // 接收人
  126 + csat.ExecutorId = make([]int, 0)
  127 + for rIndex := range project.Recipients {
  128 + vInt, _ := strconv.Atoi(project.Recipients[rIndex])
  129 + csat.ExecutorId = append(csat.ExecutorId, vInt)
  130 + }
  131 +
  132 + // FIXME 这里设置时间可能会有歧义,目前原型设计时间均一致,所以直接选取第一个获取到的周期时间,作为本周期的起止和截止时间
  133 + csat.BeginTime = fmCycleStartTime
  134 + csat.EndTime = fmCycleTimeEnd
  135 +
  136 + staffAssessTaskMap[task.ProjectId] = csat
  137 + }
  138 +
  139 + if csat.StepList == nil {
  140 + csat.StepList = make([]command.AssessTaskStep, 0)
  141 + }
  142 + csat.StepList = append(csat.StepList, command.AssessTaskStep{
  143 + SortBy: i,
  144 + LinkNodeId: int(task.NodeId),
  145 + LinkNodeName: task.NodeName,
  146 + LinkNodeType: task.NodeType,
  147 + BeginTime: fmCycleStartTime,
  148 + EndTime: fmCycleTimeEnd,
  149 + })
  150 + } else {
91 task.NextSentAt = nil 151 task.NextSentAt = nil
92 } 152 }
  153 +
93 task, err := taskRepository.Insert(task) 154 task, err := taskRepository.Insert(task)
94 if err != nil { 155 if err != nil {
95 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 156 return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
96 } 157 }
97 } 158 }
98 159
  160 + staffAssessService := service.NewStaffAssessServeice()
  161 + for i := range staffAssessTaskMap {
  162 + staffAssessTask := staffAssessTaskMap[i]
  163 + _, err := staffAssessService.CreateStaffAssessTask(staffAssessTask)
  164 + if err != nil {
  165 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  166 + }
  167 + }
  168 +
99 if err := transactionContext.CommitTransaction(); err != nil { 169 if err := transactionContext.CommitTransaction(); err != nil {
100 return application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 170 return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
101 } 171 }
@@ -10,9 +10,12 @@ type NodeTask struct { @@ -10,9 +10,12 @@ type NodeTask struct {
10 CycleId int64 `json:"cycleId,string" comment:"周期ID"` 10 CycleId int64 `json:"cycleId,string" comment:"周期ID"`
11 ProjectId int64 `json:"projectId,string" comment:"项目ID"` 11 ProjectId int64 `json:"projectId,string" comment:"项目ID"`
12 NodeId int64 `json:"nodeId,string" comment:"节点ID"` 12 NodeId int64 `json:"nodeId,string" comment:"节点ID"`
13 - KpiCycle int `json:"kpiCycle" comment:"周期"`  
14 - BeginAt *time.Time `json:"beginAt" comment:"起始时间"`  
15 - EndAt *time.Time `json:"endAt" comment:"截至时间"` 13 + NodeType int `json:"nodeType" comment:"环节类型"`
  14 + NodeName string `json:"nodeName" comment:"环节名称"`
  15 + NodeDescribe string `json:"nodeDescribe" comment:"环节描述"`
  16 + TimeStart *time.Time `json:"timeStart" comment:"起始时间"`
  17 + TimeEnd *time.Time `json:"timeEnd" comment:"截至时间"`
  18 + KpiCycle int `json:"kpiCycle" comment:"考核周期(1日、2周、3月)"`
16 NextSentAt *time.Time `json:"nextSentAt" comment:"下一次发送时间"` 19 NextSentAt *time.Time `json:"nextSentAt" comment:"下一次发送时间"`
17 CreatedAt time.Time `json:"createdAt" comment:"创建时间"` 20 CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
18 UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` 21 UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
@@ -11,9 +11,12 @@ type NodeTask struct { @@ -11,9 +11,12 @@ type NodeTask struct {
11 CycleId int64 `comment:"周期ID"` 11 CycleId int64 `comment:"周期ID"`
12 ProjectId int64 `comment:"项目ID"` 12 ProjectId int64 `comment:"项目ID"`
13 NodeId int64 `comment:"环节ID"` 13 NodeId int64 `comment:"环节ID"`
14 - KpiCycle int `comment:"周期"`  
15 - BeginAt *time.Time `comment:"起始时间"`  
16 - EndAt *time.Time `comment:"截止时间"` 14 + NodeType int `comment:"环节类型"`
  15 + NodeName string `comment:"环节名称"`
  16 + NodeDescribe string `comment:"环节描述"`
  17 + TimeStart *time.Time `comment:"起始时间"`
  18 + TimeEnd *time.Time `comment:"截至时间"`
  19 + KpiCycle int `comment:"考核周期(1日、2周、3月)"`
17 NextSentAt *time.Time `comment:"下一次发送时间"` 20 NextSentAt *time.Time `comment:"下一次发送时间"`
18 CreatedAt time.Time `comment:"创建时间"` 21 CreatedAt time.Time `comment:"创建时间"`
19 UpdatedAt time.Time `comment:"更新时间"` 22 UpdatedAt time.Time `comment:"更新时间"`
@@ -26,9 +26,12 @@ func (repo *NodeTaskRepository) TransformToDomain(m *models.NodeTask) domain.Nod @@ -26,9 +26,12 @@ func (repo *NodeTaskRepository) TransformToDomain(m *models.NodeTask) domain.Nod
26 CycleId: m.CycleId, 26 CycleId: m.CycleId,
27 ProjectId: m.ProjectId, 27 ProjectId: m.ProjectId,
28 NodeId: m.NodeId, 28 NodeId: m.NodeId,
  29 + NodeType: m.NodeType,
  30 + NodeName: m.NodeName,
  31 + NodeDescribe: m.NodeDescribe,
  32 + TimeStart: m.TimeStart,
  33 + TimeEnd: m.TimeEnd,
29 KpiCycle: m.KpiCycle, 34 KpiCycle: m.KpiCycle,
30 - BeginAt: m.BeginAt,  
31 - EndAt: m.EndAt,  
32 NextSentAt: m.NextSentAt, 35 NextSentAt: m.NextSentAt,
33 CreatedAt: m.CreatedAt, 36 CreatedAt: m.CreatedAt,
34 UpdatedAt: m.UpdatedAt, 37 UpdatedAt: m.UpdatedAt,
@@ -43,9 +46,12 @@ func (repo *NodeTaskRepository) TransformToModel(d *domain.NodeTask) models.Node @@ -43,9 +46,12 @@ func (repo *NodeTaskRepository) TransformToModel(d *domain.NodeTask) models.Node
43 CycleId: d.CycleId, 46 CycleId: d.CycleId,
44 ProjectId: d.ProjectId, 47 ProjectId: d.ProjectId,
45 NodeId: d.NodeId, 48 NodeId: d.NodeId,
  49 + NodeType: d.NodeType,
  50 + NodeName: d.NodeName,
  51 + NodeDescribe: d.NodeDescribe,
  52 + TimeStart: d.TimeStart,
  53 + TimeEnd: d.TimeEnd,
46 KpiCycle: d.KpiCycle, 54 KpiCycle: d.KpiCycle,
47 - BeginAt: d.BeginAt,  
48 - EndAt: d.EndAt,  
49 NextSentAt: d.NextSentAt, 55 NextSentAt: d.NextSentAt,
50 CreatedAt: d.CreatedAt, 56 CreatedAt: d.CreatedAt,
51 UpdatedAt: d.UpdatedAt, 57 UpdatedAt: d.UpdatedAt,
@@ -121,6 +127,7 @@ func (repo *NodeTaskRepository) Find(queryOptions map[string]interface{}) ([]*do @@ -121,6 +127,7 @@ func (repo *NodeTaskRepository) Find(queryOptions map[string]interface{}) ([]*do
121 query := tx.Model(&m).Where("deleted_at isnull") 127 query := tx.Model(&m).Where("deleted_at isnull")
122 128
123 if v, ok := queryOptions["now"].(time.Time); ok { 129 if v, ok := queryOptions["now"].(time.Time); ok {
  130 + query.Where("next_sent_at isnull")
124 query.Where("next_sent_at <= ?", v) 131 query.Where("next_sent_at <= ?", v)
125 } 132 }
126 133