审查视图

pkg/application/notify/sms.go 4.8 KB
1 2
package notify
tangxvhui authored
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import (
	"fmt"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/serviceGateway"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)

type notifySendOrNot interface {
	from() string
	ifSend(index int) (bool, error)
}
19
// 短信通知
tangxvhui authored
20 21 22 23 24 25 26 27
type notifySms struct {
	newSms    chan *domain.LogSms
	interval  time.Duration
	sendOrNot map[string]notifySendOrNot //到点后判断是否真的发送短信消息
}

func (notices *notifySms) init() {
	notices.newSms = make(chan *domain.LogSms, 50)
tangxvhui authored
28
	notices.interval = 5 * time.Minute
tangxvhui authored
29 30 31 32 33 34 35 36 37 38 39
	if constant.Env != "prd" {
		notices.interval = 1 * time.Minute
	}
	notices.sendOrNot = map[string]notifySendOrNot{}
}

func (notices *notifySms) regist(ifsend notifySendOrNot) {
	notices.sendOrNot[ifsend.from()] = ifsend
}

func (notices *notifySms) addTask(task *domain.LogSms) {
40 41 42 43 44 45
	// notices.newSms <- task
	err := notices.addNewSms(task)
	if err != nil {
		e := fmt.Sprintf("添加短信通知任务:%+v %s", task, err)
		log.Logger.Error(e)
	}
tangxvhui authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

// RunTask 执行短信通知任务
func (notices *notifySms) runTask() {
	timer := time.NewTimer(notices.interval)
	for {
		select {
		case newSms := <-notices.newSms:
			err := notices.addNewSms(newSms)
			if err != nil {
				e := fmt.Sprintf("添加短信通知任务:%+v %s", newSms, err)
				log.Logger.Error(e)
			}
		case <-timer.C:
tangxvhui authored
60
			nowTime := time.Now()
tangxvhui authored
61 62 63 64
			err := notices.checkSendSms()
			if err != nil {
				log.Logger.Error("检查发送短信通知任务:" + err.Error())
			}
tangxvhui authored
65
			log.Logger.Info(fmt.Sprintf("检查发送短信通知任务消耗时间:%.2f s", time.Since(nowTime).Seconds()))
tangxvhui authored
66 67 68 69 70
			timer.Reset(notices.interval) // 重置定时
		}
	}
}
tangxvhui authored
71
// addNewSms 添加新的通知消息
tangxvhui authored
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
func (notices *notifySms) addNewSms(newSms *domain.LogSms) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})
	err = logSmsRepo.Save(newSms)
	if err != nil {
		return err
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}
tangxvhui authored
94
// checkSendSms 检查发送短信通知
tangxvhui authored
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
func (notices *notifySms) checkSendSms() error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})
	nowTime := time.Now()
	nowDay := dayZeroTime(nowTime)
	_, logSmsList, err := logSmsRepo.Find(map[string]interface{}{
		"status":         string(domain.SmsWait),
		"executeAtBegin": nowDay,
		"executeAtEnd":   nowTime,
	})
	if err != nil {
		return err
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return err
	}
	for _, v := range logSmsList {
		err = notices.sendSms(v)
		if err != nil {
			e := fmt.Sprintf("发送短信通知:%+v %s", *v, err)
			log.Logger.Error(e)
		}
	}
	return nil
}
tangxvhui authored
131
// sendSms 发送短信消息
tangxvhui authored
132
func (notices *notifySms) sendSms(param *domain.LogSms) error {
133 134 135
	if constant.Env != "prd" {
		return nil
	}
tangxvhui authored
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	//单开处理 数据保存操作,发一条短信更新一条数据
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})

	sendOk := false
	sendOrNot, ok := notices.sendOrNot[param.From]
	if ok {
		ok, err := sendOrNot.ifSend(param.Index)
		if err != nil {
			param.Result = err.Error()
		}
		sendOk = ok
	} else {
		sendOk = true
	}
	if !sendOk {
		param.Status = domain.SmsIgnore
	} else {
		sms := serviceGateway.SmsService{}
		err = sms.SendNoticeSms(param.Phone, param.TemplateId, param.Value)
		if err != nil {
			param.Result = err.Error()
tangxvhui authored
167
			param.Status = domain.SmsSuccess
tangxvhui authored
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		} else {
			param.Status = domain.SmsSuccess
		}
	}
	err = logSmsRepo.Save(param)
	if err != nil {
		return err
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}

func dayZeroTime(t time.Time) time.Time {
tangxvhui authored
183 184
	y, m, d := t.Local().Date()
	t2 := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
tangxvhui authored
185 186
	return t2
}