combinemails.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/log"
  7. )
  8. const _combine = "%d年%d月%d日统计信息邮件"
  9. // CombineMailsByHTTP send income mail by http.
  10. func (s *Service) CombineMailsByHTTP(c context.Context, year int, month int, day int) (err error) {
  11. t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
  12. err = s.execCombineMails(c, t)
  13. if err != nil {
  14. log.Error("s.CombineMailsByHTTP error(%v)", err)
  15. }
  16. return
  17. }
  18. // CombineMails combine mails.
  19. func (s *Service) CombineMails() (err error) {
  20. c := context.TODO()
  21. date := time.Now().Add(-24 * time.Hour)
  22. t := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
  23. err = s.execCombineMails(c, t)
  24. if err != nil {
  25. log.Error("s.CombineMails execCombineMails error(%v)", err)
  26. }
  27. return
  28. }
  29. func (s *Service) execCombineMails(c context.Context, date time.Time) (err error) {
  30. var body string
  31. singedUpBody, err := s.execSignedUps(c, date)
  32. if err != nil {
  33. log.Error("s.CombineMails s.execSignedUps error(%v)", err)
  34. return
  35. }
  36. body += singedUpBody
  37. incomeBody, err := s.execIncome(c, date.Add(-24*time.Hour))
  38. if err != nil {
  39. log.Error("s.CombineMails s.execIncome error(%v)", err)
  40. return
  41. }
  42. body += incomeBody
  43. uploadBody, err := s.execSendUpload(c, date.Add(-24*time.Hour))
  44. if err != nil {
  45. log.Error("s.CombineMails s.execSendUpload error(%v)", err)
  46. return
  47. }
  48. body += uploadBody
  49. topTenBody, err := s.execSendTopTen(c, date.Add(-24*time.Hour))
  50. if err != nil {
  51. log.Error("s.CombineMails s.execSendTopTen error(%v)", err)
  52. return
  53. }
  54. body += topTenBody
  55. var send []string
  56. for _, v := range s.conf.Mail.Send {
  57. if v.Type == 2 {
  58. send = v.Addr
  59. }
  60. }
  61. err = s.email.SendMail(date, fmt.Sprintf("<table border='1'>%s</table>", body), _combine, send...)
  62. if err != nil {
  63. log.Error("s.execSendUpload send upload.csv error(%v)", err)
  64. return
  65. }
  66. return
  67. }