sms_summary_evaluation.go 1.9 KB
package notify

import (
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)

// 周期评估短信通知
// 条件:周期自评结束前4个小时,且还未完成评估填写
type notifySummaryEvaluation struct {
}

func (notices notifySummaryEvaluation) from() string {
	return "SummaryEvaluation"
}

// makeNotify 生成待执行的短信通知内容
func (notices notifySummaryEvaluation) makeNotify(param *domain.SummaryEvaluation) *domain.LogSms {
	newSms := domain.LogSms{
		Id:         0,
		Phone:      param.Executor.Account,
		TemplateId: 5536232,
		Template:   "您好,#name#,百忙之中不要忘记填写本月综合自评哦",
		Value: map[string]string{
			"name": param.Executor.UserName,
		},
		Result: "",
		Status: domain.SmsWait,
		From:   notices.from(),
		Index:  param.Id,
		// ExecuteAt:  executeAt,
		CreatedAt: time.Now(),
	}
	// 周期自评结束前4个小时,
	newSms.ExecuteAt = param.EndTime.Add(-4 * time.Hour)
	return &newSms
}

// ifSendSms 确认是否发送通知
func (notices notifySummaryEvaluation) ifSend(index int) (bool, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return false, err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return false, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	summaryEvaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	summaryEvaluationData, err := summaryEvaluationRepo.FindOne(map[string]interface{}{"id": index})
	if err != nil {
		return false, err
	}
	//还未完成评估填写,时发送短信
	if summaryEvaluationData.Status == domain.EvaluationUncompleted {
		return true, nil
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return false, err
	}
	return false, nil
}