statistics.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/up-rating/model"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _totalType int64 = iota
  10. _creativeType
  11. _influenceType
  12. _creditType
  13. )
  14. const (
  15. // select
  16. _ratingStatisticsSQL = "SELECT ups,section,tips,total_score,creativity_score,influence_score,credit_score,fans,avs,coin,play,tag_id,ctype,cdate FROM up_rating_statistics WHERE cdate = '%s' AND ctype = ? AND tag_id IN (%s)"
  17. _trendAscCountSQL = "SELECT count(*) FROM up_rating_trend_asc WHERE date='%s' %s"
  18. _trendDescCountSQL = "SELECT count(*) FROM up_rating_trend_desc WHERE date='%s' %s"
  19. _trendAscSQL = "SELECT mid,magnetic_score,creativity_score,influence_score,credit_score,%s_diff FROM up_rating_trend_asc WHERE date='%s' %s"
  20. _trendDescSQL = "SELECT mid,magnetic_score,creativity_score,influence_score,credit_score,%s_diff FROM up_rating_trend_desc WHERE date='%s' %s"
  21. )
  22. // GetRatingStatis get rating statistics
  23. func (d *Dao) GetRatingStatis(c context.Context, ctype int64, date, tags string) (statis []*model.RatingStatis, err error) {
  24. statis = make([]*model.RatingStatis, 0)
  25. rows, err := d.db.Query(c, fmt.Sprintf(_ratingStatisticsSQL, date, tags), ctype)
  26. if err != nil {
  27. log.Error("d.db.Query error(%v)", err)
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. s := &model.RatingStatis{}
  33. err = rows.Scan(
  34. &s.Ups, &s.Section, &s.Tips, &s.TotalScore, &s.CreativityScore, &s.InfluenceScore, &s.CreditScore, &s.Fans, &s.Avs, &s.Coin, &s.Play, &s.TagID, &s.CType, &s.CDate)
  35. if err != nil {
  36. log.Error("rows.Scan error(%v)", err)
  37. return
  38. }
  39. switch ctype {
  40. case _totalType:
  41. s.Score = s.TotalScore
  42. case _creativeType:
  43. s.Score = s.CreativityScore
  44. case _influenceType:
  45. s.Score = s.InfluenceScore
  46. case _creditType:
  47. s.Score = s.CreditScore
  48. }
  49. statis = append(statis, s)
  50. }
  51. err = rows.Err()
  52. return
  53. }
  54. // AscTrendCount asc trend count
  55. func (d *Dao) AscTrendCount(c context.Context, date string, query string) (count int, err error) {
  56. row := d.db.QueryRow(c, fmt.Sprintf(_trendAscCountSQL, date, query))
  57. if err = row.Scan(&count); err != nil {
  58. log.Error("d.db.Query error(%v)", err)
  59. }
  60. return
  61. }
  62. // DescTrendCount desc trend count
  63. func (d *Dao) DescTrendCount(c context.Context, date string, query string) (count int, err error) {
  64. row := d.db.QueryRow(c, fmt.Sprintf(_trendDescCountSQL, date, query))
  65. if err = row.Scan(&count); err != nil {
  66. log.Error("d.db.Query error(%v)", err)
  67. }
  68. return
  69. }
  70. // GetTrendAsc get asc trend
  71. func (d *Dao) GetTrendAsc(c context.Context, ctype string, date string, query string) (ts []*model.Trend, err error) {
  72. rows, err := d.db.Query(c, fmt.Sprintf(_trendAscSQL, ctype, date, query))
  73. if err != nil {
  74. return
  75. }
  76. ts = make([]*model.Trend, 0)
  77. defer rows.Close()
  78. for rows.Next() {
  79. t := &model.Trend{}
  80. err = rows.Scan(&t.MID, &t.MagneticScore, &t.CreativityScore, &t.InfluenceScore, &t.CreditScore, &t.DValue)
  81. if err != nil {
  82. log.Error("rows.Scan error(%v)", err)
  83. return
  84. }
  85. ts = append(ts, t)
  86. }
  87. return
  88. }
  89. // GetTrendDesc get desc trend
  90. func (d *Dao) GetTrendDesc(c context.Context, ctype string, date string, query string) (ts []*model.Trend, err error) {
  91. rows, err := d.db.Query(c, fmt.Sprintf(_trendDescSQL, ctype, date, query))
  92. if err != nil {
  93. return
  94. }
  95. ts = make([]*model.Trend, 0)
  96. defer rows.Close()
  97. for rows.Next() {
  98. t := &model.Trend{}
  99. err = rows.Scan(&t.MID, &t.MagneticScore, &t.CreativityScore, &t.InfluenceScore, &t.CreditScore, &t.DValue)
  100. if err != nil {
  101. log.Error("rows.Scan error(%v)", err)
  102. return
  103. }
  104. ts = append(ts, t)
  105. }
  106. return
  107. }