node_task_service.go 4.8 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/service"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
	"strconv"
	"time"
)

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()
	}()
	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())
	}
	if len(tasks) == 0 {
		return nil
	}
	projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})
	cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})

	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)
	}

	_, 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})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	for i := range projects {
		projectIdsMap[projects[i].Id] = projects[i]
	}
	for i := range cycles {
		cycleIdsMap[cycles[i].Id] = cycles[i]
	}

	staffAssessService := service.NewStaffAssessServeice()
	for i := range tasks {
		task := tasks[i]
		// 项目
		project, ok := projectIdsMap[task.ProjectId]
		if ok {
			// 环节截止时间
			maxTime := task.TimeEnd.Local()

			// 当前周起始时间和截止时间
			var cycleTimeStart = task.NextSentAt.Local()
			var cycleTimeEnd time.Time

			// 下个周期起始时间
			nextTime := utils.NextTimeInc(cycleTimeStart, task.KpiCycle)
			// 超过截止时间
			if nextTime.After(maxTime) {
				task.NextSentAt = nil
			} else {
				task.NextSentAt = &nextTime
			}

			// 下个周期的起始时间=当前周期的截止时间
			if task.NextSentAt == nil {
				cycleTimeEnd = maxTime
			} else {
				cycleTimeEnd = task.NextSentAt.Local()
			}

			// 格式化周期的起始和截止时间
			fmCycleStartTime := cycleTimeStart.Format("2006-1-2 15:04:05")
			fmCycleTimeEnd := cycleTimeEnd.Format("2006-1-2 15:04:05")

			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
			}

			// 接收人
			csat.ExecutorId = make([]int, 0)
			for rIndex := range project.Recipients {
				vInt, _ := strconv.Atoi(project.Recipients[rIndex])
				csat.ExecutorId = append(csat.ExecutorId, vInt)
			}

			csat.BeginTime = fmCycleStartTime
			csat.EndTime = fmCycleTimeEnd
			csat.StepList = append(csat.StepList, command.AssessTaskStep{
				SortBy:       task.NodeSort,
				LinkNodeId:   int(task.NodeId),
				LinkNodeName: task.NodeName,
				LinkNodeType: task.NodeType,
				BeginTime:    fmCycleStartTime,
				EndTime:      fmCycleTimeEnd,
			})

			// 创建发送任务
			_, err := staffAssessService.CreateStaffAssessTask(transactionContext, csat)
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		} else {
			task.NextSentAt = nil // 项目不存在,取消周期任务发送
		}

		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

}