node_task_service.go 1.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/utils"
	"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())
	}

	//ttaffAssessRepository := factory.CreateStaffAssessTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
	//
	//for i := range tasks {
	//
	//	staffAssessService := service.NewStaffAssessServeice()
	//	//staffAssessService.CreateStaffAssessTask()
	//
	//	//task := tasks[i]
	//
	//}

	for i := range tasks {
		task := tasks[i]
		// 下一次发送时间
		nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle)
		task.NextSentAt = &nextTime
		// 如果超出截至时间,则周期置空
		if task.NextSentAt.After(*task.EndAt) {
			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

}