作者 郑周

定时器 通知

... ... @@ -371,14 +371,17 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
for i := range project.Template.LinkNodes {
node := project.Template.LinkNodes[i]
task := &domain.NodeTask{
Id: 0,
CompanyId: project.CompanyId,
CycleId: project.CycleId,
ProjectId: project.Id,
NodeId: node.Id,
KpiCycle: node.KpiCycle,
BeginAt: node.TimeStart,
EndAt: node.TimeEnd,
Id: 0,
CompanyId: project.CompanyId,
CycleId: project.CycleId,
ProjectId: project.Id,
NodeId: node.Id,
NodeType: node.Type,
NodeName: node.Name,
NodeDescribe: node.Describe,
TimeStart: node.TimeStart,
TimeEnd: node.TimeEnd,
KpiCycle: node.KpiCycle,
}
// 在当前时间之前,则计算下一个周期时间
if node.TimeStart.Before(now) {
... ...
... ... @@ -3,8 +3,11 @@ 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"
)
... ... @@ -26,13 +29,15 @@ func (rs *NodeTaskService) SendEvaluationNode() error {
transactionContext.RollbackTransaction()
}()
taskRepository := factory.CreateNodeTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
projectRepository := factory.CreateEvaluationProjectRepository(map[string]interface{}{"transactionContext": transactionContext})
//cycleRepository := factory.CreateEvaluationCycleRepository(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})
//ttaffAssessRepository := factory.CreateStaffAssessTaskRepository(map[string]interface{}{"transactionContext": transactionContext})
//
... ... @@ -53,49 +58,114 @@ func (rs *NodeTaskService) SendEvaluationNode() error {
cycleIds = append(cycleIds, k)
}
_, projects, err := projectRepository.Find(map[string]interface{}{"ids": projectIds})
_, 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())
}
//_, 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()
staffAssessTaskMap := map[int64]*command.CreateStaffAssessTask{}
for i := range tasks {
task := tasks[i]
//if project, ok := projectIdsMap[task.ProjectId]; ok {
//csat := &command.CreateStaffAssessTask{
// CompanyId: project.CompanyId,
// EvaluationProjectId: project.Id,
// EvaluationProjectName: project.Name,
// CycleId: project.CycleId,
//}
//
//staffAssessService.CreateStaffAssessTask(csat)
//}
// 下一次发送时间
nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle)
task.NextSentAt = &nextTime
// 如果超出截至时间,则周期置空
if task.NextSentAt.After(*task.EndAt) {
// 项目
project, ok := projectIdsMap[task.ProjectId]
if ok {
// 环节截止时间
maxTime := time.Date(task.TimeEnd.Year(), task.TimeEnd.Month(), task.TimeEnd.Day(), task.TimeEnd.Hour(), task.TimeEnd.Minute(), 0, 0, time.Local)
// 当前周起始时间和截止时间
var cycleTimeStart = task.NextSentAt
var cycleTimeEnd *time.Time
// 下个周期起始时间
nextTime := utils.NextTimeInc(task.NextSentAt, task.KpiCycle)
// 超过截止时间
if nextTime.After(maxTime) {
task.NextSentAt = nil
} else {
task.NextSentAt = &nextTime
}
// 下个周期的起始时间=当前周期的截止时间
if task.NextSentAt == nil {
cycleTimeEnd = &maxTime
} else {
cycleTimeEnd = task.NextSentAt
}
// 格式化周期的起始和截止时间
fmCycleStartTime := cycleTimeStart.Format("2006-1-2 15:04:05")
fmCycleTimeEnd := cycleTimeEnd.Format("2006-1-2 15:04:05")
var csat, staffOk = staffAssessTaskMap[task.ProjectId]
if !staffOk {
csat = &command.CreateStaffAssessTask{
CompanyId: int(project.CompanyId),
EvaluationProjectId: int(project.Id),
EvaluationProjectName: project.Name,
CycleId: project.CycleId,
}
// 周期名称
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)
}
// FIXME 这里设置时间可能会有歧义,目前原型设计时间均一致,所以直接选取第一个获取到的周期时间,作为本周期的起止和截止时间
csat.BeginTime = fmCycleStartTime
csat.EndTime = fmCycleTimeEnd
staffAssessTaskMap[task.ProjectId] = csat
}
if csat.StepList == nil {
csat.StepList = make([]command.AssessTaskStep, 0)
}
csat.StepList = append(csat.StepList, command.AssessTaskStep{
SortBy: i,
LinkNodeId: int(task.NodeId),
LinkNodeName: task.NodeName,
LinkNodeType: task.NodeType,
BeginTime: fmCycleStartTime,
EndTime: fmCycleTimeEnd,
})
} else {
task.NextSentAt = nil
}
task, err := taskRepository.Insert(task)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
staffAssessService := service.NewStaffAssessServeice()
for i := range staffAssessTaskMap {
staffAssessTask := staffAssessTaskMap[i]
_, err := staffAssessService.CreateStaffAssessTask(staffAssessTask)
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())
}
... ...
... ... @@ -5,18 +5,21 @@ import (
)
type NodeTask struct {
Id int64 `json:"id,string" comment:"ID"`
CompanyId int64 `json:"companyId,string" comment:"公司ID"`
CycleId int64 `json:"cycleId,string" comment:"周期ID"`
ProjectId int64 `json:"projectId,string" comment:"项目ID"`
NodeId int64 `json:"nodeId,string" comment:"节点ID"`
KpiCycle int `json:"kpiCycle" comment:"周期"`
BeginAt *time.Time `json:"beginAt" comment:"起始时间"`
EndAt *time.Time `json:"endAt" comment:"截至时间"`
NextSentAt *time.Time `json:"nextSentAt" comment:"下一次发送时间"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
Id int64 `json:"id,string" comment:"ID"`
CompanyId int64 `json:"companyId,string" comment:"公司ID"`
CycleId int64 `json:"cycleId,string" comment:"周期ID"`
ProjectId int64 `json:"projectId,string" comment:"项目ID"`
NodeId int64 `json:"nodeId,string" comment:"节点ID"`
NodeType int `json:"nodeType" comment:"环节类型"`
NodeName string `json:"nodeName" comment:"环节名称"`
NodeDescribe string `json:"nodeDescribe" comment:"环节描述"`
TimeStart *time.Time `json:"timeStart" comment:"起始时间"`
TimeEnd *time.Time `json:"timeEnd" comment:"截至时间"`
KpiCycle int `json:"kpiCycle" comment:"考核周期(1日、2周、3月)"`
NextSentAt *time.Time `json:"nextSentAt" comment:"下一次发送时间"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
type NodeTaskRepository interface {
... ...
... ... @@ -5,17 +5,20 @@ import (
)
type NodeTask struct {
tableName struct{} `comment:"环节任务" pg:"node_task"`
Id int64 `comment:"ID" pg:"pk:id"`
CompanyId int64 `comment:"公司ID"`
CycleId int64 `comment:"周期ID"`
ProjectId int64 `comment:"项目ID"`
NodeId int64 `comment:"环节ID"`
KpiCycle int `comment:"周期"`
BeginAt *time.Time `comment:"起始时间"`
EndAt *time.Time `comment:"截止时间"`
NextSentAt *time.Time `comment:"下一次发送时间"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
tableName struct{} `comment:"环节任务" pg:"node_task"`
Id int64 `comment:"ID" pg:"pk:id"`
CompanyId int64 `comment:"公司ID"`
CycleId int64 `comment:"周期ID"`
ProjectId int64 `comment:"项目ID"`
NodeId int64 `comment:"环节ID"`
NodeType int `comment:"环节类型"`
NodeName string `comment:"环节名称"`
NodeDescribe string `comment:"环节描述"`
TimeStart *time.Time `comment:"起始时间"`
TimeEnd *time.Time `comment:"截至时间"`
KpiCycle int `comment:"考核周期(1日、2周、3月)"`
NextSentAt *time.Time `comment:"下一次发送时间"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
}
... ...
... ... @@ -21,35 +21,41 @@ func NewNodeTaskRepository(transactionContext *pgTransaction.TransactionContext)
func (repo *NodeTaskRepository) TransformToDomain(m *models.NodeTask) domain.NodeTask {
return domain.NodeTask{
Id: m.Id,
CompanyId: m.CompanyId,
CycleId: m.CycleId,
ProjectId: m.ProjectId,
NodeId: m.NodeId,
KpiCycle: m.KpiCycle,
BeginAt: m.BeginAt,
EndAt: m.EndAt,
NextSentAt: m.NextSentAt,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
Id: m.Id,
CompanyId: m.CompanyId,
CycleId: m.CycleId,
ProjectId: m.ProjectId,
NodeId: m.NodeId,
NodeType: m.NodeType,
NodeName: m.NodeName,
NodeDescribe: m.NodeDescribe,
TimeStart: m.TimeStart,
TimeEnd: m.TimeEnd,
KpiCycle: m.KpiCycle,
NextSentAt: m.NextSentAt,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
}
}
func (repo *NodeTaskRepository) TransformToModel(d *domain.NodeTask) models.NodeTask {
return models.NodeTask{
Id: d.Id,
CompanyId: d.CompanyId,
CycleId: d.CycleId,
ProjectId: d.ProjectId,
NodeId: d.NodeId,
KpiCycle: d.KpiCycle,
BeginAt: d.BeginAt,
EndAt: d.EndAt,
NextSentAt: d.NextSentAt,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
Id: d.Id,
CompanyId: d.CompanyId,
CycleId: d.CycleId,
ProjectId: d.ProjectId,
NodeId: d.NodeId,
NodeType: d.NodeType,
NodeName: d.NodeName,
NodeDescribe: d.NodeDescribe,
TimeStart: d.TimeStart,
TimeEnd: d.TimeEnd,
KpiCycle: d.KpiCycle,
NextSentAt: d.NextSentAt,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
}
}
... ... @@ -121,6 +127,7 @@ func (repo *NodeTaskRepository) Find(queryOptions map[string]interface{}) ([]*do
query := tx.Model(&m).Where("deleted_at isnull")
if v, ok := queryOptions["now"].(time.Time); ok {
query.Where("next_sent_at isnull")
query.Where("next_sent_at <= ?", v)
}
... ...