作者 郑周

Merge branch 'fix'

@@ -2,6 +2,7 @@ package service @@ -2,6 +2,7 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis"
5 "strconv" 6 "strconv"
6 "strings" 7 "strings"
7 "time" 8 "time"
@@ -482,6 +483,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter @@ -482,6 +483,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter
482 } 483 }
483 484
484 func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) { 485 func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) {
  486 + lock := xredis.NewLockProjectId(int(in.Id))
  487 + err := lock.Lock()
  488 + if err != nil {
  489 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  490 + }
  491 + defer func() {
  492 + lock.UnLock()
  493 + }()
  494 +
485 transactionContext, err := factory.ValidateStartTransaction(in) 495 transactionContext, err := factory.ValidateStartTransaction(in)
486 if err != nil { 496 if err != nil {
487 return nil, err 497 return nil, err
@@ -562,56 +572,61 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) @@ -562,56 +572,61 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
562 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 572 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
563 } 573 }
564 574
565 - now := time.Now().Local()  
566 - year, month, day := now.Date()  
567 - nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻 575 + tasks, err := taskRepository.Find(map[string]interface{}{"projectId": project.Id})
  576 + if err != nil {
  577 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  578 + }
  579 + if len(tasks) == 0 {
  580 + now := time.Now().Local()
  581 + year, month, day := now.Date()
  582 + nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻
  583 + for i := range project.Template.LinkNodes {
  584 + node := project.Template.LinkNodes[i]
  585 + task := &domain.NodeTask{
  586 + Id: 0,
  587 + CompanyId: project.CompanyId,
  588 + CycleId: project.CycleId,
  589 + ProjectId: project.Id,
  590 + NodeId: node.Id,
  591 + NodeType: node.Type,
  592 + NodeName: node.Name,
  593 + NodeDescribe: node.Describe,
  594 + NodeSort: i + 1,
  595 + TimeStart: node.TimeStart,
  596 + TimeEnd: node.TimeEnd,
  597 + KpiCycle: node.KpiCycle,
  598 + }
568 599
569 - for i := range project.Template.LinkNodes {  
570 - node := project.Template.LinkNodes[i]  
571 - task := &domain.NodeTask{  
572 - Id: 0,  
573 - CompanyId: project.CompanyId,  
574 - CycleId: project.CycleId,  
575 - ProjectId: project.Id,  
576 - NodeId: node.Id,  
577 - NodeType: node.Type,  
578 - NodeName: node.Name,  
579 - NodeDescribe: node.Describe,  
580 - NodeSort: i + 1,  
581 - TimeStart: node.TimeStart,  
582 - TimeEnd: node.TimeEnd,  
583 - KpiCycle: node.KpiCycle,  
584 - }  
585 -  
586 - // 环节起始和截止本地时间  
587 - startLocal := task.TimeStart.Local()  
588 - sY, sM, sD := startLocal.Date()  
589 - startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算  
590 - endLocal := task.TimeEnd.Local()  
591 -  
592 - // 在当前时间之前,则计算下一个周期时间  
593 - if startLocal.Before(nowO) {  
594 - nextTime := utils.NextTime(nowO, startLocal, node.KpiCycle)  
595 - task.NextSentAt = &nextTime  
596 - } else {  
597 - task.NextSentAt = &startLocal  
598 - }  
599 - // 如果超出截至时间,则周期置空  
600 - if task.NextSentAt.After(endLocal) {  
601 - task.NextSentAt = nil 600 + // 环节起始和截止本地时间
  601 + startLocal := task.TimeStart.Local()
  602 + sY, sM, sD := startLocal.Date()
  603 + startLocal = time.Date(sY, sM, sD, 0, 0, 0, 0, time.Local) // 开始时间以0点开始计算
  604 + endLocal := task.TimeEnd.Local()
  605 +
  606 + // 在当前时间之前,则计算下一个周期时间
  607 + if startLocal.Before(nowO) {
  608 + nextTime := utils.NextTime(nowO, startLocal, node.KpiCycle)
  609 + task.NextSentAt = &nextTime
  610 + } else {
  611 + task.NextSentAt = &startLocal
  612 + }
  613 + // 如果超出截至时间,则周期置空
  614 + if task.NextSentAt.After(endLocal) {
  615 + task.NextSentAt = nil
  616 + }
  617 +
  618 + _, err := taskRepository.Insert(task)
  619 + if err != nil {
  620 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  621 + }
602 } 622 }
603 623
604 - _, err := taskRepository.Insert(task) 624 + err = rs.generateEvaluationItemUsed(transactionContext, project)
605 if err != nil { 625 if err != nil {
606 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 626 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
607 } 627 }
608 } 628 }
609 629
610 - err = rs.generateEvaluationItemUsed(transactionContext, project)  
611 - if err != nil {  
612 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
613 - }  
614 -  
615 if err := transactionContext.CommitTransaction(); err != nil { 630 if err := transactionContext.CommitTransaction(); err != nil {
616 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 631 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
617 } 632 }
  1 +package xredis
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/go-redsync/redsync/v4"
  7 +)
  8 +
  9 +type LockProjectId struct {
  10 + m *redsync.Mutex
  11 +}
  12 +
  13 +func NewLockProjectId(id int) *LockProjectId {
  14 + key := fmt.Sprintf("performance:project_id:%d", id)
  15 + return &LockProjectId{
  16 + m: rsync.NewMutex(key),
  17 + }
  18 +}
  19 +
  20 +func (lk *LockProjectId) Lock() error {
  21 + return lk.m.Lock()
  22 +}
  23 +
  24 +func (lk *LockProjectId) UnLock() (bool, error) {
  25 + return lk.m.Unlock()
  26 +}