作者 郑周

1. 修复BUG,项目更新截止时间时,未同步更新表node_task中的任务时间!

... ... @@ -155,79 +155,28 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp
if end.After(maxTime) {
return nil, application.ThrowError(application.BUSINESS_ERROR, "评估截至时间不能超出周期截至时间")
}
// 更新项目模板中的环节时间
if project.Template != nil {
for i := range project.Template.LinkNodes {
node := project.Template.LinkNodes[i]
node.TimeEnd = &end
}
}
// 项目起始截止时间(暂时环节中的时间未分开)
// 更新项目截止时间(暂时环节中的时间未分开)
project.EndTime = end
project, err = projectRepository.Insert(project)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 查看任务过程,重新日计算任务截至期
taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
tasks, err := taskRepository.Find(map[string]interface{}{"projectId": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
now := time.Now().Local()
year, month, day := now.Date()
nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻
for i := range tasks {
task := tasks[i]
task.TimeEnd = &end
// 重新计算
if task.NextSentAt == nil {
// 环节起始和截止本地时间
startLocal := task.TimeStart.Local()
sY, sM, sD := startLocal.Date()
startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算
endLocal := task.TimeEnd.Local()
// 在当前时间之前,则计算下一个周期时间
if startLocal.Before(nowO) {
nextTime := utils.NextTime(nowO, startLocal, task.KpiCycle)
task.NextSentAt = &nextTime
} else {
task.NextSentAt = &startLocal
}
// 注.最后一次发送时间和重新计算后的时间相同时,继续获取下一个周期
if task.LastSentAt != nil {
nextY, nextM, nextD := task.NextSentAt.Local().Date()
lastY, lastM, lastD := task.LastSentAt.Local().Date()
if nextY == lastY && nextM == lastM && nextD == lastD {
nextTime := utils.NextTimeInc(task.NextSentAt.Local(), task.KpiCycle)
task.NextSentAt = &nextTime
}
}
// 如果超出截至时间,则周期置空
if task.NextSentAt.After(endLocal) {
task.NextSentAt = nil
}
} else {
// 新的截止时间在下一次发送周期任务之前,则结束
if end.Before(task.NextSentAt.Local()) {
task.NextSentAt = nil
} else {
// do nothing
}
}
// 项目调整截止时间,同步更新任务时间
if err := rs.updateTaskTime(transactionContext, in.Id, end); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return project, nil
} else {
_, projects, err := projectRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "cycleId": in.CycleId}, "template")
if err != nil {
... ... @@ -314,6 +263,62 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp
}
}
// 更新项目截止时间,同步任务截止时间
func (rs *EvaluationProjectService) updateTaskTime(context application.TransactionContext, projectId int64, end time.Time) error {
// 查看任务过程,重新日计算任务截至期
taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": context})
tasks, err := taskRepository.Find(map[string]interface{}{"projectId": projectId})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
now := time.Now().Local()
year, month, day := now.Date()
nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻
for i := range tasks {
task := tasks[i]
task.TimeEnd = &end // 先赋值
if task.NextSentAt == nil { // 重新计算
// 环节起始和截止本地时间
startLocal := task.TimeStart.Local()
sY, sM, sD := startLocal.Date()
startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算
// 在当前时间之前,则计算下一个周期时间
if startLocal.Before(nowO) {
nextTime := utils.NextTime(nowO, startLocal, task.KpiCycle)
task.NextSentAt = &nextTime
} else {
task.NextSentAt = &startLocal
}
// 注.最后一次发送时间和重新计算后的时间相同时,继续获取下一个周期
if task.LastSentAt != nil {
nextY, nextM, nextD := task.NextSentAt.Local().Date()
lastY, lastM, lastD := task.LastSentAt.Local().Date()
if nextY == lastY && nextM == lastM && nextD == lastD {
nextTime := utils.NextTimeInc(task.NextSentAt.Local(), task.KpiCycle)
task.NextSentAt = &nextTime
}
}
}
// 如果超出截至时间,则周期置空
if task.NextSentAt.Local().After(task.TimeEnd.Local()) {
task.NextSentAt = nil
}
// 更新任务
task, err := taskRepository.Insert(task)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
return nil
}
func (rs *EvaluationProjectService) Get(in *command.GetProjectCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
... ...