审查视图

pkg/application/node_task/node_task_service.go 5.1 KB
郑周 authored
1 2 3
package service

import (
4 5
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
Your Name authored
6 7 8
	"strconv"
	"time"
郑周 authored
9 10
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
郑周 authored
11 12
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/service"
郑周 authored
13
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
郑周 authored
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
)

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()
33
郑周 authored
34 35 36
		if err := recover(); err != nil {
			log.Logger.Error(application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("定时发送评估任务异常:%s", err)).Error())
		}
郑周 authored
37 38 39 40 41 42
	}()
	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())
	}
郑周 authored
43 44 45 46 47
	if len(tasks) == 0 {
		return nil
	}
	projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})
	cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
郑周 authored
48
郑周 authored
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	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)
	}
郑周 authored
65 66 67 68 69
	_, 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})
郑周 authored
70 71 72
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
73
郑周 authored
74 75 76
	for i := range projects {
		projectIdsMap[projects[i].Id] = projects[i]
	}
郑周 authored
77 78 79
	for i := range cycles {
		cycleIdsMap[cycles[i].Id] = cycles[i]
	}
郑周 authored
80
郑周 authored
81
	staffAssessService := service.NewStaffAssessServeice()
郑周 authored
82 83
	for i := range tasks {
		task := tasks[i]
84 85
		project, ok := projectIdsMap[task.ProjectId] // 项目
		if ok && project != nil {
郑周 authored
86
			// 环节截止时间
郑周 authored
87
			maxTime := task.TimeEnd.Local()
郑周 authored
88 89

			// 当前周起始时间和截止时间
郑周 authored
90 91
			var cycleTimeStart = task.NextSentAt.Local()
			var cycleTimeEnd time.Time
郑周 authored
92 93

			// 下个周期起始时间
郑周 authored
94
			nextTime := utils.NextTimeInc(cycleTimeStart, task.KpiCycle)
郑周 authored
95 96 97 98 99 100 101 102 103
			// 超过截止时间
			if nextTime.After(maxTime) {
				task.NextSentAt = nil
			} else {
				task.NextSentAt = &nextTime
			}

			// 下个周期的起始时间=当前周期的截止时间
			if task.NextSentAt == nil {
郑周 authored
104
				cycleTimeEnd = maxTime
郑周 authored
105
			} else {
郑周 authored
106
				cycleTimeEnd = task.NextSentAt.Local()
郑周 authored
107 108 109
			}

			// 格式化周期的起始和截止时间
郑周 authored
110 111
			fmCycleStartTime := cycleTimeStart.Format("2006-1-2 15:04:05")
			fmCycleTimeEnd := cycleTimeEnd.Format("2006-1-2 15:04:05")
郑周 authored
112
郑周 authored
113 114 115 116 117 118 119 120 121 122 123
			csat := &command.CreateStaffAssessTask{
				CompanyId:             int(project.CompanyId),
				EvaluationProjectId:   int(project.Id),
				EvaluationProjectName: project.Name,
				CycleId:               project.CycleId,
				StepList:              make([]command.AssessTaskStep, 0),
			}

			// 周期名称
			if cycle, ok := cycleIdsMap[project.CycleId]; ok {
				csat.CycleName = cycle.Name
郑周 authored
124 125
			}
郑周 authored
126 127 128 129 130
			// 接收人
			csat.ExecutorId = make([]int, 0)
			for rIndex := range project.Recipients {
				vInt, _ := strconv.Atoi(project.Recipients[rIndex])
				csat.ExecutorId = append(csat.ExecutorId, vInt)
郑周 authored
131
			}
郑周 authored
132 133 134

			csat.BeginTime = fmCycleStartTime
			csat.EndTime = fmCycleTimeEnd
郑周 authored
135
			csat.StepList = append(csat.StepList, command.AssessTaskStep{
郑周 authored
136
				SortBy:       task.NodeSort,
郑周 authored
137 138 139 140 141 142
				LinkNodeId:   int(task.NodeId),
				LinkNodeName: task.NodeName,
				LinkNodeType: task.NodeType,
				BeginTime:    fmCycleStartTime,
				EndTime:      fmCycleTimeEnd,
			})
郑周 authored
143 144 145 146 147 148

			// 创建发送任务
			_, err := staffAssessService.CreateStaffAssessTask(transactionContext, csat)
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
郑周 authored
149
		} else {
郑周 authored
150
			task.NextSentAt = nil // 项目不存在,取消周期任务发送
郑周 authored
151
		}
郑周 authored
152
郑周 authored
153 154 155 156 157 158 159 160 161 162 163 164 165
		task, err := taskRepository.Insert(task)
		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

}