作者 郑周

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

@@ -155,79 +155,28 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp @@ -155,79 +155,28 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp
155 if end.After(maxTime) { 155 if end.After(maxTime) {
156 return nil, application.ThrowError(application.BUSINESS_ERROR, "评估截至时间不能超出周期截至时间") 156 return nil, application.ThrowError(application.BUSINESS_ERROR, "评估截至时间不能超出周期截至时间")
157 } 157 }
  158 + // 更新项目模板中的环节时间
158 if project.Template != nil { 159 if project.Template != nil {
159 for i := range project.Template.LinkNodes { 160 for i := range project.Template.LinkNodes {
160 node := project.Template.LinkNodes[i] 161 node := project.Template.LinkNodes[i]
161 node.TimeEnd = &end 162 node.TimeEnd = &end
162 } 163 }
163 } 164 }
164 - // 项目起始截止时间(暂时环节中的时间未分开) 165 + // 更新项目截止时间(暂时环节中的时间未分开)
165 project.EndTime = end 166 project.EndTime = end
166 project, err = projectRepository.Insert(project) 167 project, err = projectRepository.Insert(project)
167 if err != nil { 168 if err != nil {
168 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 169 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
169 } 170 }
170 -  
171 - // 查看任务过程,重新日计算任务截至期  
172 - taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext})  
173 - tasks, err := taskRepository.Find(map[string]interface{}{"projectId": in.Id})  
174 - if err != nil {  
175 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
176 - }  
177 -  
178 - now := time.Now().Local()  
179 - year, month, day := now.Date()  
180 - nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻  
181 -  
182 - for i := range tasks {  
183 - task := tasks[i]  
184 - task.TimeEnd = &end  
185 -  
186 - // 重新计算  
187 - if task.NextSentAt == nil {  
188 - // 环节起始和截止本地时间  
189 - startLocal := task.TimeStart.Local()  
190 - sY, sM, sD := startLocal.Date()  
191 - startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算  
192 - endLocal := task.TimeEnd.Local()  
193 -  
194 - // 在当前时间之前,则计算下一个周期时间  
195 - if startLocal.Before(nowO) {  
196 - nextTime := utils.NextTime(nowO, startLocal, task.KpiCycle)  
197 - task.NextSentAt = &nextTime  
198 - } else {  
199 - task.NextSentAt = &startLocal  
200 - }  
201 -  
202 - // 注.最后一次发送时间和重新计算后的时间相同时,继续获取下一个周期  
203 - if task.LastSentAt != nil {  
204 - nextY, nextM, nextD := task.NextSentAt.Local().Date()  
205 - lastY, lastM, lastD := task.LastSentAt.Local().Date()  
206 - if nextY == lastY && nextM == lastM && nextD == lastD {  
207 - nextTime := utils.NextTimeInc(task.NextSentAt.Local(), task.KpiCycle)  
208 - task.NextSentAt = &nextTime  
209 - }  
210 - }  
211 -  
212 - // 如果超出截至时间,则周期置空  
213 - if task.NextSentAt.After(endLocal) {  
214 - task.NextSentAt = nil  
215 - }  
216 - } else {  
217 - // 新的截止时间在下一次发送周期任务之前,则结束  
218 - if end.Before(task.NextSentAt.Local()) {  
219 - task.NextSentAt = nil  
220 - } else {  
221 - // do nothing  
222 - }  
223 - } 171 + // 项目调整截止时间,同步更新任务时间
  172 + if err := rs.updateTaskTime(transactionContext, in.Id, end); err != nil {
  173 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
224 } 174 }
225 175
226 if err := transactionContext.CommitTransaction(); err != nil { 176 if err := transactionContext.CommitTransaction(); err != nil {
227 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 177 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
228 } 178 }
229 return project, nil 179 return project, nil
230 -  
231 } else { 180 } else {
232 _, projects, err := projectRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "cycleId": in.CycleId}, "template") 181 _, projects, err := projectRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "cycleId": in.CycleId}, "template")
233 if err != nil { 182 if err != nil {
@@ -314,6 +263,62 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp @@ -314,6 +263,62 @@ func (rs *EvaluationProjectService) UpdateTemplate(in *command.UpdateProjectTemp
314 } 263 }
315 } 264 }
316 265
  266 +// 更新项目截止时间,同步任务截止时间
  267 +func (rs *EvaluationProjectService) updateTaskTime(context application.TransactionContext, projectId int64, end time.Time) error {
  268 + // 查看任务过程,重新日计算任务截至期
  269 + taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": context})
  270 + tasks, err := taskRepository.Find(map[string]interface{}{"projectId": projectId})
  271 + if err != nil {
  272 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  273 + }
  274 +
  275 + now := time.Now().Local()
  276 + year, month, day := now.Date()
  277 + nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻
  278 +
  279 + for i := range tasks {
  280 + task := tasks[i]
  281 + task.TimeEnd = &end // 先赋值
  282 +
  283 + if task.NextSentAt == nil { // 重新计算
  284 + // 环节起始和截止本地时间
  285 + startLocal := task.TimeStart.Local()
  286 + sY, sM, sD := startLocal.Date()
  287 + startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算
  288 +
  289 + // 在当前时间之前,则计算下一个周期时间
  290 + if startLocal.Before(nowO) {
  291 + nextTime := utils.NextTime(nowO, startLocal, task.KpiCycle)
  292 + task.NextSentAt = &nextTime
  293 + } else {
  294 + task.NextSentAt = &startLocal
  295 + }
  296 +
  297 + // 注.最后一次发送时间和重新计算后的时间相同时,继续获取下一个周期
  298 + if task.LastSentAt != nil {
  299 + nextY, nextM, nextD := task.NextSentAt.Local().Date()
  300 + lastY, lastM, lastD := task.LastSentAt.Local().Date()
  301 + if nextY == lastY && nextM == lastM && nextD == lastD {
  302 + nextTime := utils.NextTimeInc(task.NextSentAt.Local(), task.KpiCycle)
  303 + task.NextSentAt = &nextTime
  304 + }
  305 + }
  306 + }
  307 +
  308 + // 如果超出截至时间,则周期置空
  309 + if task.NextSentAt.Local().After(task.TimeEnd.Local()) {
  310 + task.NextSentAt = nil
  311 + }
  312 +
  313 + // 更新任务
  314 + task, err := taskRepository.Insert(task)
  315 + if err != nil {
  316 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  317 + }
  318 + }
  319 + return nil
  320 +}
  321 +
317 func (rs *EvaluationProjectService) Get(in *command.GetProjectCommand) (interface{}, error) { 322 func (rs *EvaluationProjectService) Get(in *command.GetProjectCommand) (interface{}, error) {
318 transactionContext, err := factory.ValidateStartTransaction(in) 323 transactionContext, err := factory.ValidateStartTransaction(in)
319 if err != nil { 324 if err != nil {