作者 tangxvhui

添加定时任务

@@ -17,6 +17,7 @@ import ( @@ -17,6 +17,7 @@ import (
17 func main() { 17 func main() {
18 startNodeTask() 18 startNodeTask()
19 startSummaryEvaluation() 19 startSummaryEvaluation()
  20 + // startConfirmEvaluationScore()
20 go notify.RunTaskSmsNotify() 21 go notify.RunTaskSmsNotify()
21 go consumer.Run() 22 go consumer.Run()
22 web.Run() 23 web.Run()
@@ -63,3 +64,23 @@ func startSummaryEvaluation() { @@ -63,3 +64,23 @@ func startSummaryEvaluation() {
63 } 64 }
64 }() 65 }()
65 } 66 }
  67 +
  68 +// 定时自动确认周期评估考核结果
  69 +func startConfirmEvaluationScore() {
  70 + go func() {
  71 + var duration time.Duration
  72 + if constant.Env == "prd" {
  73 + duration = time.Minute * 5
  74 + } else {
  75 + duration = time.Minute * 1
  76 + }
  77 + timer := time.NewTimer(duration)
  78 + for {
  79 + <-timer.C
  80 + if err := serviceSummary.TaskConfirmScore(); err != nil {
  81 + log.Logger.Error(err.Error())
  82 + }
  83 + timer.Reset(duration) // 重置定时
  84 + }
  85 + }()
  86 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "strconv"
  6 + "time"
  7 +
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/command"
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
  12 +)
  13 +
  14 +// 定时自动确认周期评估考核结果
  15 +func TaskConfirmScore() error {
  16 + nowTime := time.Now()
  17 + defer func() {
  18 + str := fmt.Sprintf("自动确认周期评估考核结果耗时%.2f s", time.Since(nowTime).Seconds())
  19 + log.Logger.Info(str)
  20 + }()
  21 + for {
  22 + evaluationList, err := catchEvaluation()
  23 + if err != nil {
  24 + log.Logger.Error(fmt.Sprintf("获取周期考核结果%s", err.Error()))
  25 + }
  26 + if len(evaluationList) == 0 {
  27 + return nil
  28 + }
  29 + evluationSrv := NewSummaryEvaluationService()
  30 + for _, val := range evaluationList {
  31 + targetUserId, err := strconv.Atoi(val.TargetUserId)
  32 + if err != nil {
  33 + log.Logger.Error(fmt.Sprintf("获取员工id%s", err))
  34 + continue
  35 + }
  36 + err = evluationSrv.ConfirmScoreEvaluation(&command.ConfirmScore{
  37 + SummaryEvaluationId: val.SummaryEvaluationId,
  38 + UserId: targetUserId,
  39 + })
  40 + if err != nil {
  41 + log.Logger.Error(fmt.Sprintf("确认周期考核结果%s", err.Error()))
  42 + }
  43 + }
  44 + }
  45 +}
  46 +
  47 +func catchEvaluation() ([]dao.SummaryEvaluationData1, error) {
  48 + transactionContext, err := factory.CreateTransactionContext(nil)
  49 + if err != nil {
  50 + return nil, err
  51 + }
  52 + if err := transactionContext.StartTransaction(); err != nil {
  53 + return nil, err
  54 + }
  55 + defer func() {
  56 + _ = transactionContext.RollbackTransaction()
  57 + }()
  58 + evaluationDao := dao.NewSummaryEvaluationDao(map[string]interface{}{
  59 + "transactionContext": transactionContext,
  60 + })
  61 + evaluationList, err := evaluationDao.ListEvaluationFinishNoResult()
  62 + if err != nil {
  63 + return nil, fmt.Errorf("获取综合评估数据%s", err)
  64 + }
  65 + if err := transactionContext.CommitTransaction(); err != nil {
  66 + return nil, err
  67 + }
  68 + return evaluationList, nil
  69 +}
@@ -151,3 +151,27 @@ func (d *SummaryEvaluationDao) UpdateBeginTime(ids []int, beginTime time.Time) e @@ -151,3 +151,27 @@ func (d *SummaryEvaluationDao) UpdateBeginTime(ids []int, beginTime time.Time) e
151 _, err := tx.Exec(sqlStr, condition...) 151 _, err := tx.Exec(sqlStr, condition...)
152 return err 152 return err
153 } 153 }
  154 +
  155 +type SummaryEvaluationData1 struct {
  156 + SummaryEvaluationId int `pg:"summary_evaluation_id"`
  157 + TargetUserId string `pg:"target_user_id"`
  158 +}
  159 +
  160 +// 查询周期考核结果
  161 +// 条件:已过截止时间,并且还没确认周期考核结果
  162 +func (d *SummaryEvaluationDao) ListEvaluationFinishNoResult() ([]SummaryEvaluationData1, error) {
  163 + sqlStr := `select
  164 + summary_evaluation.id as summary_evaluation_id,
  165 + summary_evaluation.target_user ->>'userName' as target_user_name
  166 + from summary_evaluation
  167 + where 1=1
  168 + and summary_evaluation."types" = 5
  169 + and summary_evaluation.status ='completed'
  170 + and summary_evaluation.check_result ='uncompleted'
  171 + and summary_evaluation.end_time <=now()
  172 + limit 10`
  173 + result := []SummaryEvaluationData1{}
  174 + tx := d.transactionContext.PgTx
  175 + _, err := tx.Query(&result, sqlStr)
  176 + return result, err
  177 +}