rating.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/up-rating/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _layout = "2006-01-02"
  12. // get up_rating start & end
  13. _ratingStartSQL = "SELECT id FROM up_rating_%02d WHERE cdate='%s' ORDER BY id LIMIT 1"
  14. _ratingEndSQL = "SELECT id FROM up_rating_%02d WHERE cdate='%s' ORDER BY id DESC LIMIT 1"
  15. _ratingCountSQL = "SELECT COUNT(*) FROM up_rating_%02d WHERE cdate='%s'"
  16. _ratingSQL = "SELECT id,mid,tag_id,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score,cdate FROM up_rating_%02d WHERE cdate= '%s' AND id > ? ORDER BY id LIMIT ?"
  17. _ratingByIDSQL = "SELECT id,mid,tag_id,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score,magnetic_score,cdate FROM up_rating_%02d WHERE id > ? AND id <= ? LIMIT ?"
  18. _ratingScoreSQL = "INSERT INTO up_rating_%02d(mid,tag_id,cdate,creativity_score,influence_score,credit_score,meta_creativity_score,meta_influence_score, magnetic_score) VALUES %s ON DUPLICATE KEY UPDATE tag_id=VALUES(tag_id), cdate=VALUES(cdate), creativity_score=VALUES(creativity_score),influence_score=VALUES(influence_score),credit_score=VALUES(credit_score),meta_creativity_score=VALUES(meta_creativity_score),meta_influence_score=VALUES(meta_influence_score),magnetic_score=VALUES(magnetic_score)"
  19. _delRatingScoreSQL = "DELETE FROM up_rating_%02d WHERE cdate='%s' LIMIT ?"
  20. )
  21. // DelRatings del ratings
  22. func (d *Dao) DelRatings(c context.Context, date time.Time, limit int) (rows int64, err error) {
  23. res, err := d.db.Exec(c, fmt.Sprintf(_delRatingScoreSQL, date.Month(), date.Format(_layout)), limit)
  24. if err != nil {
  25. return
  26. }
  27. return res.RowsAffected()
  28. }
  29. // RatingStart get start id by date
  30. func (d *Dao) RatingStart(c context.Context, date time.Time) (start int, err error) {
  31. row := d.db.QueryRow(c, fmt.Sprintf(_ratingStartSQL, date.Month(), date.Format(_layout)))
  32. if err = row.Scan(&start); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. }
  36. }
  37. return
  38. }
  39. // RatingEnd get end id by date
  40. func (d *Dao) RatingEnd(c context.Context, date time.Time) (end int, err error) {
  41. row := d.db.QueryRow(c, fmt.Sprintf(_ratingEndSQL, date.Month(), date.Format(_layout)))
  42. if err = row.Scan(&end); err != nil {
  43. if err == sql.ErrNoRows {
  44. err = nil
  45. }
  46. }
  47. return
  48. }
  49. // RatingCount get end id by date
  50. func (d *Dao) RatingCount(c context.Context, date time.Time) (count int, err error) {
  51. row := d.db.QueryRow(c, fmt.Sprintf(_ratingCountSQL, date.Month(), date.Format(_layout)))
  52. if err = row.Scan(&count); err != nil {
  53. if err == sql.ErrNoRows {
  54. err = nil
  55. }
  56. }
  57. return
  58. }
  59. // GetRatings get ratings by date
  60. func (d *Dao) GetRatings(c context.Context, date time.Time, offset, limit int) (rs []*model.Rating, last int, err error) {
  61. rows, err := d.db.Query(c, fmt.Sprintf(_ratingSQL, date.Month(), date.Format(_layout)), offset, limit)
  62. if err != nil {
  63. log.Error("d.db.Query Rating Info error(%v)", err)
  64. return
  65. }
  66. defer rows.Close()
  67. for rows.Next() {
  68. r := &model.Rating{}
  69. err = rows.Scan(&last, &r.MID, &r.TagID, &r.CreativityScore, &r.InfluenceScore, &r.CreditScore, &r.MetaCreativityScore, &r.MetaInfluenceScore, &r.Date)
  70. if err != nil {
  71. log.Error("rows scan error(%v)", err)
  72. return
  73. }
  74. rs = append(rs, r)
  75. }
  76. return
  77. }
  78. // GetRatingsFast get rating fast
  79. func (d *Dao) GetRatingsFast(c context.Context, date time.Time, start, end, limit int) (rs []*model.Rating, id int, err error) {
  80. rows, err := d.db.Query(c, fmt.Sprintf(_ratingByIDSQL, date.Month()), start, end, limit)
  81. if err != nil {
  82. log.Error("d.db.Query Rating Info error(%v)", err)
  83. return
  84. }
  85. defer rows.Close()
  86. for rows.Next() {
  87. r := &model.Rating{}
  88. err = rows.Scan(&id, &r.MID, &r.TagID, &r.CreativityScore, &r.InfluenceScore, &r.CreditScore, &r.MetaCreativityScore, &r.MetaInfluenceScore, &r.MagneticScore, &r.Date)
  89. if err != nil {
  90. log.Error("rows scan error(%v)", err)
  91. return
  92. }
  93. rs = append(rs, r)
  94. }
  95. return
  96. }
  97. // InsertRatingStat batch insert rating score stat
  98. func (d *Dao) InsertRatingStat(c context.Context, month time.Month, values string) (rows int64, err error) {
  99. res, err := d.db.Exec(c, fmt.Sprintf(_ratingScoreSQL, month, values))
  100. if err != nil {
  101. log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_ratingScoreSQL, month, values), err)
  102. return
  103. }
  104. return res.RowsAffected()
  105. }