upload.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "go-common/app/job/main/growup/model"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _upload = "%d年%d月%d日投稿转换统计"
  16. )
  17. func (s *Service) execSendUpload(c context.Context, date time.Time) (body string, err error) {
  18. // total signed up count
  19. ts, err := s.dao.GetDateSignedUps(c, time.Date(0, 0, 0, 0, 0, 0, 0, time.Local), date.Add(24*time.Hour))
  20. if err != nil {
  21. log.Error("s.execSendUpload GetDateSignedUps error(%v)", date, err)
  22. return
  23. }
  24. // total_income > 0 up count
  25. iu, err := s.dao.GetUpTotalIncomeCnt(c)
  26. if err != nil {
  27. log.Error("s.execSendUpload dao.GetUpTotalIncomeCnt date(%v) error(%v)", date, err)
  28. return
  29. }
  30. // total_income > 0 archive count
  31. ia, err := s.avSatisCount(c)
  32. if err != nil {
  33. log.Error("s.execSendUpload s.avSatisCount date(%v) error(%v)", date, err)
  34. return
  35. }
  36. // upcnt, avcnt
  37. upCnt, avCnt, err := s.recvSignedUpInfo(c, date)
  38. if err != nil {
  39. log.Error("s.execSendUpload s.recvSignedUpInfo date(%v) error(%v)", date, err)
  40. return
  41. }
  42. data := [][]string{
  43. {fmt.Sprintf(_upload, date.Year(), date.Month(), date.Day())},
  44. {"日期", "累计签约人数", "投稿UP数", "获得收入UP数", "UP收入率", "投稿总数", "获得收入稿件数", "稿件收入率"},
  45. {fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day()), strconv.Itoa(ts), strconv.Itoa(upCnt), strconv.Itoa(iu), strconv.FormatFloat(float64(iu)/float64(upCnt)*100, 'f', 2, 32), strconv.Itoa(avCnt), strconv.Itoa(ia), strconv.FormatFloat(float64(ia)/float64(avCnt)*100, 'f', 2, 32)},
  46. }
  47. f, err := os.Create("upload.csv")
  48. if err != nil {
  49. log.Error("s.execSendUpload create upload.csv error(%v)", err)
  50. return
  51. }
  52. defer f.Close()
  53. w := csv.NewWriter(f)
  54. w.WriteAll(data)
  55. w.Flush()
  56. rfile, err := os.Open("upload.csv")
  57. if err != nil {
  58. log.Error("s.execSendUpload open upload.csv error(%v)", err)
  59. return
  60. }
  61. defer rfile.Close()
  62. r := csv.NewReader(rfile)
  63. for {
  64. var strs []string
  65. strs, err = r.Read()
  66. if err == io.EOF {
  67. break
  68. }
  69. body += fmt.Sprintf("<tr><td>%s</td></tr>", strings.Join(strs, "</td><td>"))
  70. }
  71. err = os.Remove("upload.csv")
  72. if err != nil {
  73. log.Error("s.execSendUpload remove upload.csv error(%v)", err)
  74. }
  75. return
  76. }
  77. func (s *Service) avSatisCount(c context.Context) (avCnt int, err error) {
  78. return s.dao.GetAvStatisCount(c)
  79. }
  80. // recvSignedUpInfo recv signed up upload archive cnt; archive cnt > 0, up cnt.
  81. func (s *Service) recvSignedUpInfo(c context.Context, date time.Time) (upCnt, avCnt int, err error) {
  82. query := "{\"select\": [{\"name\": \"up_cnt\",\"as\": \"up_cnt\"},{\"name\": \"archive_cnt\",\"as\": \"archive_cnt\"},{\"name\": \"log_date\",\"as\": \"log_date\"}],\"where\": {\"log_date\": {\"in\": [\"%s\"]}}}"
  83. t := strconv.Itoa(date.Year())
  84. if int(date.Month()) < 10 {
  85. t += "0"
  86. }
  87. t += strconv.Itoa(int(date.Month()))
  88. if date.Day() < 10 {
  89. t += "0"
  90. }
  91. t += strconv.Itoa(date.Day())
  92. var res []*model.ArchiveInfo
  93. res, err = s.dp.Send(c, fmt.Sprintf(query, t))
  94. if err != nil {
  95. log.Error("s.recvSignedUpInfo error(%v)", err)
  96. return
  97. }
  98. for _, i := range res {
  99. if i.UploadDate == t {
  100. upCnt = i.UpCnt
  101. avCnt = i.ArchiveCnt
  102. }
  103. }
  104. return
  105. }