rating.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/job/main/up-rating/model"
  9. )
  10. // DelRatings del ratings
  11. func (s *Service) DelRatings(c context.Context, date time.Time) (err error) {
  12. for {
  13. var rows int64
  14. rows, err = s.dao.DelRatings(c, date, _limit)
  15. if err != nil {
  16. return
  17. }
  18. if rows == 0 {
  19. break
  20. }
  21. }
  22. return s.DelPastRecord(c, date)
  23. }
  24. // RatingFast close chan when part finished
  25. func (s *Service) RatingFast(c context.Context, date time.Time, start, end int, ch chan []*model.Rating) (err error) {
  26. defer close(ch)
  27. return s.Ratings(c, date, start, end, ch)
  28. }
  29. // Ratings chan <- ratings, close chan outside
  30. func (s *Service) Ratings(c context.Context, date time.Time, start, end int, ch chan []*model.Rating) (err error) {
  31. for {
  32. var rs []*model.Rating
  33. rs, start, err = s.dao.GetRatingsFast(c, date, start, end, _limit)
  34. if err != nil {
  35. return
  36. }
  37. if len(rs) == 0 {
  38. break
  39. }
  40. ch <- rs
  41. }
  42. return
  43. }
  44. // RatingInfos rating infos
  45. func (s *Service) RatingInfos(c context.Context, date time.Time, ch chan []*model.Rating) (err error) {
  46. defer close(ch)
  47. var id int
  48. for {
  49. var rs []*model.Rating
  50. rs, id, err = s.dao.GetRatings(c, date, id, _limit)
  51. if err != nil {
  52. return
  53. }
  54. ch <- rs
  55. if len(rs) == 0 {
  56. break
  57. }
  58. }
  59. return
  60. }
  61. // BatchInsertRatingStat batch insert rating stat
  62. func (s *Service) BatchInsertRatingStat(c context.Context, wch chan []*model.Rating, date time.Time) (err error) {
  63. var (
  64. buff = make([]*model.Rating, _limit)
  65. buffEnd = 0
  66. )
  67. dateStr := date.Format(_layout)
  68. for rs := range wch {
  69. for _, r := range rs {
  70. buff[buffEnd] = r
  71. buffEnd++
  72. if buffEnd >= _limit {
  73. values := ratingStatValues(buff[:buffEnd], dateStr)
  74. buffEnd = 0
  75. _, err = s.dao.InsertRatingStat(c, date.Month(), values)
  76. if err != nil {
  77. return
  78. }
  79. }
  80. }
  81. if buffEnd > 0 {
  82. values := ratingStatValues(buff[:buffEnd], dateStr)
  83. buffEnd = 0
  84. _, err = s.dao.InsertRatingStat(c, date.Month(), values)
  85. }
  86. }
  87. return
  88. }
  89. func ratingStatValues(rs []*model.Rating, date string) (values string) {
  90. var buf bytes.Buffer
  91. for _, r := range rs {
  92. buf.WriteString("(")
  93. buf.WriteString(strconv.FormatInt(r.MID, 10))
  94. buf.WriteByte(',')
  95. buf.WriteString(strconv.FormatInt(r.TagID, 10))
  96. buf.WriteByte(',')
  97. buf.WriteString(fmt.Sprintf("'%s'", date))
  98. buf.WriteByte(',')
  99. buf.WriteString(strconv.FormatInt(r.CreativityScore, 10))
  100. buf.WriteByte(',')
  101. buf.WriteString(strconv.FormatInt(r.InfluenceScore, 10))
  102. buf.WriteByte(',')
  103. buf.WriteString(strconv.FormatInt(r.CreditScore, 10))
  104. buf.WriteByte(',')
  105. buf.WriteString(strconv.FormatInt(r.MetaCreativityScore, 10))
  106. buf.WriteByte(',')
  107. buf.WriteString(strconv.FormatInt(r.MetaInfluenceScore, 10))
  108. buf.WriteByte(',')
  109. buf.WriteString(strconv.FormatInt(r.MagneticScore, 10))
  110. buf.WriteString(")")
  111. buf.WriteByte(',')
  112. }
  113. if buf.Len() > 0 {
  114. buf.Truncate(buf.Len() - 1)
  115. }
  116. values = buf.String()
  117. buf.Reset()
  118. return
  119. }