transfer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/up-rating/model"
  8. )
  9. var (
  10. _avCategory = map[int]string{
  11. 0: "默认",
  12. 1: "动画",
  13. 3: "音乐",
  14. 129: "舞蹈",
  15. 4: "游戏",
  16. 36: "科技",
  17. 160: "生活",
  18. 119: "鬼畜",
  19. 155: "时尚",
  20. 23: "电影",
  21. 11: "电视剧",
  22. 13: "番剧",
  23. 167: "国创",
  24. 165: "广告",
  25. 5: "娱乐",
  26. 177: "纪录片",
  27. 181: "影视",
  28. }
  29. _scoreField = map[model.ScoreType]string{
  30. model.Magnetic: "magnetic_score",
  31. model.Creativity: "creativity_score",
  32. model.Influence: "influence_score",
  33. model.Credit: "credit_score",
  34. }
  35. )
  36. // FormatCSV format to csv data
  37. func formatCSV(records [][]string) (data []byte, err error) {
  38. buf := new(bytes.Buffer)
  39. // add utf bom
  40. if len(records) > 0 {
  41. buf.WriteString("\xEF\xBB\xBF")
  42. }
  43. w := csv.NewWriter(buf)
  44. err = w.WriteAll(records)
  45. if err != nil {
  46. return
  47. }
  48. data = buf.Bytes()
  49. return
  50. }
  51. func tagDesc(tagID int) string {
  52. if v, ok := _avCategory[tagID]; ok {
  53. return v
  54. }
  55. return _avCategory[0]
  56. }
  57. func formatScores(ratings []*model.RatingInfo) (data [][]string) {
  58. if len(ratings) <= 0 {
  59. return
  60. }
  61. data = make([][]string, len(ratings)+1)
  62. data[0] = []string{"月份", "UID", "昵称", "分区", "总分", "创作力", "影响力", "信用分", "投稿量", "粉丝量"}
  63. for i, v := range ratings {
  64. data[i+1] = []string{
  65. v.Date,
  66. strconv.FormatInt(v.Mid, 10),
  67. v.NickName,
  68. tagDesc(v.TagID),
  69. strconv.FormatInt(v.MagneticScore, 10),
  70. strconv.FormatInt(v.CreativityScore, 10),
  71. strconv.FormatInt(v.InfluenceScore, 10),
  72. strconv.FormatInt(v.CreditScore, 10),
  73. strconv.FormatInt(v.TotalAvs, 10),
  74. strconv.FormatInt(v.TotalFans, 10),
  75. }
  76. }
  77. return
  78. }
  79. func scoreField(st model.ScoreType) string {
  80. if v, ok := _scoreField[st]; ok {
  81. return v
  82. }
  83. return _scoreField[model.Magnetic]
  84. }
  85. func cDateStr(cdate time.Time) string {
  86. return cdate.Format("2006-01-02")
  87. }
  88. func prevComputation(t time.Time) time.Time {
  89. return time.Date(t.Year(), t.Month()-1, 1, 0, 0, 0, 0, time.Local)
  90. }
  91. func formatStatis(list []*model.RatingStatis, ctype int64) (data [][]string) {
  92. if len(list) <= 0 {
  93. return
  94. }
  95. data = make([][]string, len(list)+1)
  96. data[0] = []string{"分数段", "本月", "占比", "对比", "占比", "平均分"}
  97. switch ctype {
  98. case 0:
  99. data[0] = append(data[0], []string{"创造力", "影响力", "信用分"}...)
  100. case 1:
  101. data[0] = append(data[0], []string{"平均稿件数", "平均播放量", "平均互动量"}...)
  102. case 2:
  103. data[0] = append(data[0], []string{"平均粉丝量"}...)
  104. }
  105. for i, v := range list {
  106. data[i+1] = []string{
  107. v.Tips,
  108. strconv.FormatInt(v.Ups, 10),
  109. v.Proportion,
  110. strconv.FormatInt(v.Compare, 10),
  111. v.ComparePropor,
  112. strconv.FormatInt(v.Score, 10),
  113. }
  114. switch ctype {
  115. case 0:
  116. data[i+1] = append(data[i+1], []string{strconv.FormatInt(v.CreativityScore, 10), strconv.FormatInt(v.InfluenceScore, 10), strconv.FormatInt(v.CreditScore, 10)}...)
  117. case 1:
  118. data[i+1] = append(data[i+1], []string{strconv.FormatInt(v.Avs, 10), strconv.FormatInt(v.Play, 10), strconv.FormatInt(v.Coin, 10)}...)
  119. case 2:
  120. data[i+1] = append(data[i+1], []string{strconv.FormatInt(v.Fans, 10)}...)
  121. }
  122. }
  123. return
  124. }