/
combinemails.go
76 lines (67 loc) · 1.85 KB
/
combinemails.go
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
package service
import (
"context"
"fmt"
"time"
"go-common/library/log"
)
const _combine = "%d年%d月%d日统计信息邮件"
// CombineMailsByHTTP send income mail by http.
func (s *Service) CombineMailsByHTTP(c context.Context, year int, month int, day int) (err error) {
t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
err = s.execCombineMails(c, t)
if err != nil {
log.Error("s.CombineMailsByHTTP error(%v)", err)
}
return
}
// CombineMails combine mails.
func (s *Service) CombineMails() (err error) {
c := context.TODO()
date := time.Now().Add(-24 * time.Hour)
t := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
err = s.execCombineMails(c, t)
if err != nil {
log.Error("s.CombineMails execCombineMails error(%v)", err)
}
return
}
func (s *Service) execCombineMails(c context.Context, date time.Time) (err error) {
var body string
singedUpBody, err := s.execSignedUps(c, date)
if err != nil {
log.Error("s.CombineMails s.execSignedUps error(%v)", err)
return
}
body += singedUpBody
incomeBody, err := s.execIncome(c, date.Add(-24*time.Hour))
if err != nil {
log.Error("s.CombineMails s.execIncome error(%v)", err)
return
}
body += incomeBody
uploadBody, err := s.execSendUpload(c, date.Add(-24*time.Hour))
if err != nil {
log.Error("s.CombineMails s.execSendUpload error(%v)", err)
return
}
body += uploadBody
topTenBody, err := s.execSendTopTen(c, date.Add(-24*time.Hour))
if err != nil {
log.Error("s.CombineMails s.execSendTopTen error(%v)", err)
return
}
body += topTenBody
var send []string
for _, v := range s.conf.Mail.Send {
if v.Type == 2 {
send = v.Addr
}
}
err = s.email.SendMail(date, fmt.Sprintf("<table border='1'>%s</table>", body), _combine, send...)
if err != nil {
log.Error("s.execSendUpload send upload.csv error(%v)", err)
return
}
return
}