作者 tangxvhui

添加定时任务

... ... @@ -17,6 +17,7 @@ import (
func main() {
startNodeTask()
startSummaryEvaluation()
// startConfirmEvaluationScore()
go notify.RunTaskSmsNotify()
go consumer.Run()
web.Run()
... ... @@ -63,3 +64,23 @@ func startSummaryEvaluation() {
}
}()
}
// 定时自动确认周期评估考核结果
func startConfirmEvaluationScore() {
go func() {
var duration time.Duration
if constant.Env == "prd" {
duration = time.Minute * 5
} else {
duration = time.Minute * 1
}
timer := time.NewTimer(duration)
for {
<-timer.C
if err := serviceSummary.TaskConfirmScore(); err != nil {
log.Logger.Error(err.Error())
}
timer.Reset(duration) // 重置定时
}
}()
}
... ...
package service
import (
"fmt"
"strconv"
"time"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)
// 定时自动确认周期评估考核结果
func TaskConfirmScore() error {
nowTime := time.Now()
defer func() {
str := fmt.Sprintf("自动确认周期评估考核结果耗时%.2f s", time.Since(nowTime).Seconds())
log.Logger.Info(str)
}()
for {
evaluationList, err := catchEvaluation()
if err != nil {
log.Logger.Error(fmt.Sprintf("获取周期考核结果%s", err.Error()))
}
if len(evaluationList) == 0 {
return nil
}
evluationSrv := NewSummaryEvaluationService()
for _, val := range evaluationList {
targetUserId, err := strconv.Atoi(val.TargetUserId)
if err != nil {
log.Logger.Error(fmt.Sprintf("获取员工id%s", err))
continue
}
err = evluationSrv.ConfirmScoreEvaluation(&command.ConfirmScore{
SummaryEvaluationId: val.SummaryEvaluationId,
UserId: targetUserId,
})
if err != nil {
log.Logger.Error(fmt.Sprintf("确认周期考核结果%s", err.Error()))
}
}
}
}
func catchEvaluation() ([]dao.SummaryEvaluationData1, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, err
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
evaluationDao := dao.NewSummaryEvaluationDao(map[string]interface{}{
"transactionContext": transactionContext,
})
evaluationList, err := evaluationDao.ListEvaluationFinishNoResult()
if err != nil {
return nil, fmt.Errorf("获取综合评估数据%s", err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, err
}
return evaluationList, nil
}
... ...
... ... @@ -151,3 +151,27 @@ func (d *SummaryEvaluationDao) UpdateBeginTime(ids []int, beginTime time.Time) e
_, err := tx.Exec(sqlStr, condition...)
return err
}
type SummaryEvaluationData1 struct {
SummaryEvaluationId int `pg:"summary_evaluation_id"`
TargetUserId string `pg:"target_user_id"`
}
// 查询周期考核结果
// 条件:已过截止时间,并且还没确认周期考核结果
func (d *SummaryEvaluationDao) ListEvaluationFinishNoResult() ([]SummaryEvaluationData1, error) {
sqlStr := `select
summary_evaluation.id as summary_evaluation_id,
summary_evaluation.target_user ->>'userName' as target_user_name
from summary_evaluation
where 1=1
and summary_evaluation."types" = 5
and summary_evaluation.status ='completed'
and summary_evaluation.check_result ='uncompleted'
and summary_evaluation.end_time <=now()
limit 10`
result := []SummaryEvaluationData1{}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr)
return result, err
}
... ...