sms.go
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
131
132
133
134
135
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
167
168
169
170
171
172
173
174
package notify
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)
}
// 短信通知
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)
notices.interval = 10 * time.Minute
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) {
notices.newSms <- task
}
// RunTask 执行短信通知任务
func (notices *notifySms) runTask() {
notices.init()
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:
err := notices.checkSendSms()
if err != nil {
log.Logger.Error("检查发送短信通知任务:" + err.Error())
}
timer.Reset(notices.interval) // 重置定时
}
}
}
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
}
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
}
func (notices *notifySms) sendSms(param *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})
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()
} 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 {
y, m, d := t.UTC().Date()
t2 := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
return t2
}