scheduled.go
9.2 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package service
import (
"fmt"
"strconv"
"time"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/notify"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)
// sendSummaryEvaluafionTask 根据评估项目设置,确认是否下发评估任务
// project 需要处理的项目
// userList 全体的员工数据 map[id]=>domain.User
// departmetList 全部的部门数据 map[id]=>domain.Department
func sendSummaryEvaluation(project *domain.EvaluationProject,
userMap map[int64]*domain.User, departmentMap map[int64]*domain.Department) ([]domain.SummaryEvaluation, error) {
//自评的时间范围
beginTimeSelf := project.EndTime
endTimeSelf := dayZeroTime(project.EndTime).Add(3*24*time.Hour - time.Second)
//人资、360评估的时间范围
beginTime360 := endTimeSelf
endTime360 := endTimeSelf.Add(2 * 24 * time.Hour)
//上级评估的是时间范围
beginTimeSuper := endTime360
endTimeSuper := endTime360.Add(2 * 24 * time.Hour)
//
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, err
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
cycleData, err := cycleRepo.FindOne(map[string]interface{}{"id": project.CycleId})
if err != nil {
return nil, err
}
//确定 被评估的人
targetUserId := []int64{}
for _, v := range project.Recipients {
id, err := strconv.ParseInt(v, 10, 64)
if err == nil {
targetUserId = append(targetUserId, id)
}
}
if len(targetUserId) == 0 {
return nil, nil
}
//被评估的员工
targetUserMap := map[int64]*domain.User{}
for _, v := range targetUserId {
u, ok := userMap[v]
if ok {
targetUserMap[v] = u
}
}
//确定360评估的执行人
_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{"evaluationProjectId": project.Id, "nodeType": int(domain.LinkNodeSelfAssessment)})
if err != nil {
return nil, err
}
nodeId := 0
executor360Map := map[int64]*domain.User{}
hrbpExist := false
for _, v := range itemList {
nodeId = v.NodeId
if v.EvaluatorId < 0 {
hrbpExist = true
}
if v.EvaluatorId <= 0 {
continue
}
if u, ok := userMap[int64(v.EvaluatorId)]; ok {
executor360Map[u.Id] = u
}
}
//创建周期评估任务
var newEvaluationList []domain.SummaryEvaluation
evaluationTemp := domain.SummaryEvaluation{
Id: 0,
CompanyId: int(project.CompanyId),
EvaluationProjectId: int(project.Id),
EvaluationProjectName: project.Name,
CycleId: cycleData.Id,
CycleName: cycleData.Name,
NodeId: nodeId,
TargetUser: domain.StaffDesc{},
TargetDepartment: []domain.StaffDepartment{},
Executor: domain.StaffDesc{},
Types: 0,
Status: domain.EvaluationUncompleted,
CheckResult: domain.EvaluationCheckUncompleted,
BeginTime: time.Time{},
EndTime: time.Time{},
TotalScore: "",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: nil,
}
//确定周期评估
for _, v := range targetUserMap {
//处理自评
evaluationTemp.TargetUser = domain.StaffDesc{
UserId: int(v.Id),
Account: v.Account,
UserName: v.Name,
}
evaluationTemp.Executor = domain.StaffDesc{
UserId: int(v.Id),
Account: v.Account,
UserName: v.Name,
}
evaluationTemp.BeginTime = beginTimeSelf
evaluationTemp.EndTime = endTimeSelf
evaluationTemp.TargetDepartment = []domain.StaffDepartment{}
evaluationTemp.Types = domain.EvaluationSelf
for _, departId := range v.DepartmentId {
depart, ok := departmentMap[int64(departId)]
if !ok {
continue
}
evaluationTemp.TargetDepartment = append(evaluationTemp.TargetDepartment, domain.StaffDepartment{
DepartmentId: int(depart.Id),
DepartmentName: depart.Name,
})
}
//确定自评
newEvaluationList = append(newEvaluationList, evaluationTemp)
if hrbpExist {
//处理人资评估
evaluationTemp.BeginTime = beginTime360
evaluationTemp.EndTime = endTime360
evaluationTemp.Executor = domain.StaffDesc{}
evaluationTemp.Types = domain.EvaluationHrbp
//确定人资评估
newEvaluationList = append(newEvaluationList, evaluationTemp)
}
//处理360 评估
for _, v2 := range executor360Map {
evaluationTemp.BeginTime = beginTime360
evaluationTemp.EndTime = endTime360
evaluationTemp.Executor = domain.StaffDesc{
UserId: int(v2.Id),
Account: v2.Account,
UserName: v2.Name,
}
evaluationTemp.Types = domain.Evaluation360
//确定人资评估
newEvaluationList = append(newEvaluationList, evaluationTemp)
}
//处理上级评估
superUser, ok := userMap[v.ParentId]
if ok {
evaluationTemp.Types = domain.EvaluationSuper
evaluationTemp.Executor = domain.StaffDesc{
UserId: int(superUser.Id),
Account: superUser.Account,
UserName: superUser.Name,
}
evaluationTemp.BeginTime = beginTimeSuper
evaluationTemp.EndTime = endTimeSuper
//确定上级评估
newEvaluationList = append(newEvaluationList, evaluationTemp)
}
// 确定 考核结果 的评估记录
evaluationTemp.Types = domain.EvaluationFinish
evaluationTemp.Executor = domain.StaffDesc{}
evaluationTemp.BeginTime = endTimeSuper
evaluationTemp.EndTime = endTimeSuper.Add(2 * 24 * time.Hour)
newEvaluationList = append(newEvaluationList, evaluationTemp)
}
summaryEvaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
for i := range newEvaluationList {
err = summaryEvaluationRepo.Save(&newEvaluationList[i])
if err != nil {
return nil, err
}
}
//回填 项目的状态
projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext})
err = projectDao.UpdateSummaryState(project.Id, domain.ProjectSummaryStateYes)
if err != nil {
return nil, err
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, err
}
return newEvaluationList, nil
}
func dayZeroTime(t time.Time) time.Time {
y, m, d := t.Local().Date()
t2 := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
return t2
}
// 下发周期评估
func TaskSendSummaryEvaluation() error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
_, projectList, err := projectRepo.Find(map[string]interface{}{
"endTime": time.Now(),
"summaryState": domain.ProjectSummaryStateNo,
"state": domain.ProjectStateEnable,
}, "template")
if err != nil {
return fmt.Errorf("获取可用的项目数据,%s", err)
}
companyIdMap := map[int64]struct{}{}
for _, v := range projectList {
companyIdMap[v.CompanyId] = struct{}{}
}
var createdEvalationList []domain.SummaryEvaluation
//获取可执行的项目
for companyId := range companyIdMap {
//获取员工数据,部门数据
_, userList, err := userRepo.Find(map[string]interface{}{"companyId": companyId})
if err != nil {
log.Logger.Error("TaskSendSummaryEvaluation 获取员工数据:" + err.Error())
continue
}
_, departmentList, err := departmentRepo.Find(map[string]interface{}{"companyId": companyId})
if err != nil {
log.Logger.Error("TaskSendSummaryEvaluation 获取部门数据:" + err.Error())
continue
}
//转map
userMap := map[int64]*domain.User{}
for _, v := range userList {
userMap[v.Id] = v
}
departmentMap := map[int64]*domain.Department{}
for _, v := range departmentList {
departmentMap[v.Id] = v
}
for _, v := range projectList {
if v.CompanyId != companyId {
continue
}
evalationList, err := sendSummaryEvaluation(v, userMap, departmentMap)
if err != nil {
log.Logger.Error("TaskSendSummaryEvaluation 下发评估任务:" + err.Error())
continue
}
createdEvalationList = append(createdEvalationList, evalationList...)
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
//添加待发送的短信通知
err = sendSmsEvalation(createdEvalationList)
return err
}
// 添加待发送的短信通知
func sendSmsEvalation(param []domain.SummaryEvaluation) error {
for i := range param {
if param[i].Types == domain.EvaluationSelf {
notify.AddNotifySummaryEvaluation(¶m[i])
}
}
return nil
}