sms_staff_assess.go 2.2 KB
package notify

import (
	"time"

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

// 每日自评短信通知
type notifyStaffAssess struct {
}

var _ notifySendOrNot = (*notifyStaffAssess)(nil)

func (notices notifyStaffAssess) from() string {
	return "StaffAssess"
}

// makeNotify 生成待执行的短信通知内容
func (notices notifyStaffAssess) makeNotify(param *domain.StaffAssess) *domain.LogSms {
	newSms := domain.LogSms{
		Id:         0,
		Phone:      param.Executor.Account,
		TemplateId: 5475050,
		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(),
	}
	// 每日自评 结束前30 分钟
	// newSms.ExecuteAt = param.EndTime.Add(-1800 * time.Second)
	//改为 固定在截止时间 那天的 前一天晚上10
	y, m, d := param.EndTime.Local().Date()
	dayTime := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
	newSms.ExecuteAt = dayTime.Add(-2 * time.Hour)
	return &newSms
}

// ifSend 确认是否发送通知
func (notices notifyStaffAssess) ifSend(index int) (bool, error) {
	nowDay := time.Now().Weekday()
	if nowDay == time.Sunday || nowDay == time.Saturday {
		//周末不发
		return false, nil
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return false, err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return false, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	staffAssessRepo := factory.CreateStaffAssessRepository(map[string]interface{}{"transactionContext": transactionContext})
	assessData, err := staffAssessRepo.FindOne(map[string]interface{}{"id": index})
	if err != nil {
		return false, err
	}
	//还未完成评估填写,时发送短信
	if assessData.Status == domain.StaffAssessUncompleted {
		return true, nil
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return false, err
	}
	return false, nil
}