node_task_service.go
5.4 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
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/service"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
"strconv"
"time"
)
type NodeTaskService struct {
}
func NewNodeTaskService() *NodeTaskService {
newRoleService := &NodeTaskService{}
return newRoleService
}
// SendEvaluationNode 发送评估环节
func (rs *NodeTaskService) SendEvaluationNode() error {
transactionContext, err := factory.StartTransaction()
if err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
tasks, err := taskRepository.Find(map[string]interface{}{"now": time.Now().Local()})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(tasks) == 0 {
return nil
}
projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
//ttaffAssessRepository := factory.CreateStaffAssessTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
//
projectIdsMap := map[int64]*domain.EvaluationProject{}
cycleIdsMap := map[int64]*domain.EvaluationCycle{}
for i := range tasks {
task := tasks[i]
projectIdsMap[task.ProjectId] = nil
cycleIdsMap[task.CycleId] = nil
}
projectIds := make([]int64, 0)
cycleIds := make([]int64, 0)
for k := range projectIdsMap {
projectIds = append(projectIds, k)
}
for k := range cycleIdsMap {
cycleIds = append(cycleIds, k)
}
_, projects, err := projectRepository.Find(map[string]interface{}{"ids": projectIds}, "template")
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, cycles, err := cycleRepository.Find(map[string]interface{}{"ids": cycleIds})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range projects {
projectIdsMap[projects[i].Id] = projects[i]
}
for i := range cycles {
cycleIdsMap[cycles[i].Id] = cycles[i]
}
staffAssessTaskMap := map[int64]*command.CreateStaffAssessTask{}
for i := range tasks {
task := tasks[i]
// 项目
project, ok := projectIdsMap[task.ProjectId]
if ok {
// 环节截止时间
maxTime := time.Date(task.TimeEnd.Year(), task.TimeEnd.Month(), task.TimeEnd.Day(), task.TimeEnd.Hour(), task.TimeEnd.Minute(), 0, 0, time.Local)
// 当前周起始时间和截止时间
var cycleTimeStart = task.NextSentAt
var cycleTimeEnd *time.Time
// 下个周期起始时间
nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle)
// 超过截止时间
if nextTime.After(maxTime) {
task.NextSentAt = nil
} else {
task.NextSentAt = &nextTime
}
// 下个周期的起始时间=当前周期的截止时间
if task.NextSentAt == nil {
cycleTimeEnd = &maxTime
} else {
cycleTimeEnd = task.NextSentAt
}
// 格式化周期的起始和截止时间
fmCycleStartTime := cycleTimeStart.Format("2006-1-2 15:04:05")
fmCycleTimeEnd := cycleTimeEnd.Format("2006-1-2 15:04:05")
var csat, staffOk = staffAssessTaskMap[task.ProjectId]
if !staffOk {
csat = &command.CreateStaffAssessTask{
CompanyId: int(project.CompanyId),
EvaluationProjectId: int(project.Id),
EvaluationProjectName: project.Name,
CycleId: project.CycleId,
}
// 周期名称
if cycle, ok := cycleIdsMap[project.CycleId]; ok {
csat.CycleName = cycle.Name
}
// 接收人
csat.ExecutorId = make([]int, 0)
for rIndex := range project.Recipients {
vInt, _ := strconv.Atoi(project.Recipients[rIndex])
csat.ExecutorId = append(csat.ExecutorId, vInt)
}
// FIXME 这里设置时间可能会有歧义,目前原型设计时间均一致,所以直接选取第一个获取到的周期时间,作为本周期的起止和截止时间
csat.BeginTime = fmCycleStartTime
csat.EndTime = fmCycleTimeEnd
staffAssessTaskMap[task.ProjectId] = csat
}
if csat.StepList == nil {
csat.StepList = make([]command.AssessTaskStep, 0)
}
csat.StepList = append(csat.StepList, command.AssessTaskStep{
SortBy: i,
LinkNodeId: int(task.NodeId),
LinkNodeName: task.NodeName,
LinkNodeType: task.NodeType,
BeginTime: fmCycleStartTime,
EndTime: fmCycleTimeEnd,
})
} else {
task.NextSentAt = nil
}
task, err := taskRepository.Insert(task)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
staffAssessService := service.NewStaffAssessServeice()
for i := range staffAssessTaskMap {
staffAssessTask := staffAssessTaskMap[i]
_, err := staffAssessService.CreateStaffAssessTask(staffAssessTask)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}