topten.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package service
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/app/job/main/growup/model"
  13. "go-common/library/log"
  14. )
  15. const (
  16. _topTen = "%d年%d月%d日UP主,稿件收入top 10 统计"
  17. )
  18. func (s *Service) execSendTopTen(c context.Context, date time.Time) (body string, err error) {
  19. var ups []*model.MIDInfo
  20. ups, err = s.getTopTenUps(c, date)
  21. if err != nil {
  22. log.Error("s.execSendTopTen s.getTopTenUps error(%v)", err)
  23. return
  24. }
  25. avs, err := s.getTopTenAVs(c, date)
  26. if err != nil {
  27. log.Error("s.execSendTopTen s.getTopTenAVs error(%v)", err)
  28. return
  29. }
  30. data := [][]string{
  31. {fmt.Sprintf(_topTen, date.Year(), date.Month(), date.Day())},
  32. {"日期", "名次", "UID", "昵称", "新增收入(元)", "累计收入(元)"},
  33. }
  34. for i, up := range ups {
  35. a := []string{
  36. fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day()), strconv.Itoa(i + 1), strconv.FormatInt(up.MID, 10), up.NickName, strconv.FormatFloat(float64(up.Income)/100, 'f', 2, 32), strconv.FormatFloat(float64(up.TotalIncome)/100, 'f', 2, 32),
  37. }
  38. data = append(data, a)
  39. }
  40. t := []string{"日期", "名次", "avid", "UID", "昵称", "新增收入(元)", "累计收入(元)"}
  41. data = append(data, t)
  42. for i, av := range avs {
  43. a := []string{
  44. fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day()), strconv.Itoa(i + 1), strconv.FormatInt(av.AVID, 10), strconv.FormatInt(av.MID, 10), av.NickName, strconv.FormatFloat(float64(av.Income)/100, 'f', 2, 32), strconv.FormatFloat(float64(av.TotalIncome)/100, 'f', 2, 32),
  45. }
  46. data = append(data, a)
  47. }
  48. f, err := os.Create("top10.csv")
  49. if err != nil {
  50. log.Error("s.execSendTopTen create top10.csv error(%v)", err)
  51. return
  52. }
  53. defer f.Close()
  54. w := csv.NewWriter(f)
  55. w.WriteAll(data)
  56. w.Flush()
  57. rfile, err := os.Open("top10.csv")
  58. if err != nil {
  59. log.Error("s.execSendTopTen open top10.csv error(%v)", err)
  60. return
  61. }
  62. defer rfile.Close()
  63. r := csv.NewReader(rfile)
  64. for {
  65. var strs []string
  66. strs, err = r.Read()
  67. if err == io.EOF {
  68. break
  69. }
  70. body += fmt.Sprintf("<tr><td>%s</td></tr>", strings.Join(strs, "</td><td>"))
  71. }
  72. err = os.Remove("top10.csv")
  73. if err != nil {
  74. log.Error("s.execSendTopTen remove top10.csv error(%v)", err)
  75. }
  76. return
  77. }
  78. func (s *Service) getTopTenUps(c context.Context, date time.Time) (infos []*model.MIDInfo, err error) {
  79. ups, err := s.getUpIncome(c, date)
  80. if err != nil {
  81. log.Error("s.getTopTenUps s.getUpIncome date(%v) error(%v)", date, err)
  82. return
  83. }
  84. infos = make([]*model.MIDInfo, 0)
  85. sort.SliceStable(ups, func(i, j int) bool {
  86. return ups[i].Income > ups[j].Income
  87. })
  88. cnt := 10
  89. if len(ups) < 10 {
  90. cnt = len(ups)
  91. }
  92. for i := 0; i < cnt; i++ {
  93. a := &model.MIDInfo{MID: ups[i].MID, Income: ups[i].Income, TotalIncome: ups[i].TotalIncome}
  94. a.NickName, err = s.dao.GetNickname(c, a.MID)
  95. if err != nil {
  96. log.Error("s.getTopTenUps dao.GetNickname mid(%v) error(%v)", a.MID, err)
  97. return
  98. }
  99. infos = append(infos, a)
  100. }
  101. return
  102. }
  103. func (s *Service) getTopTenAVs(c context.Context, date time.Time) (infos []*model.AVIDInfo, err error) {
  104. avs, err := s.getAvIncome(c, date)
  105. if err != nil {
  106. log.Error("s.getTopTenAVs s.getAvIncome error(%v)", err)
  107. return
  108. }
  109. sort.SliceStable(avs, func(i, j int) bool {
  110. return avs[i].Income > avs[j].Income
  111. })
  112. infos = make([]*model.AVIDInfo, 0)
  113. cnt := 10
  114. if len(avs) < 10 {
  115. cnt = len(avs)
  116. }
  117. for i := 0; i < cnt; i++ {
  118. a := &model.AVIDInfo{AVID: avs[i].AVID, MID: avs[i].MID, Income: avs[i].Income, TotalIncome: avs[i].TotalIncome}
  119. a.NickName, err = s.dao.GetNickname(c, a.MID)
  120. if err != nil {
  121. log.Error("s.getTopTenAVs dao.GetNickname mid(%v) error(%v)", a.MID, err)
  122. return
  123. }
  124. infos = append(infos, a)
  125. }
  126. return
  127. }