past.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/up-rating/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _pastRatingRecordSQL = "SELECT times FROM past_rating_record WHERE cdate = '%s' AND is_deleted = 0"
  12. _pastStatSQL = "SELECT id,mid,creativity_score,influence_score,credit_score FROM past_score_statistics WHERE id > ? ORDER BY id LIMIT ?"
  13. // insert
  14. _inPastRecordSQL = "INSERT INTO past_rating_record(times, cdate) VALUES(?,'%s') ON DUPLICATE KEY UPDATE times=VALUES(times)"
  15. _pastScoreStatSQL = "INSERT INTO past_score_statistics(mid,creativity_score,influence_score,credit_score) VALUES %s ON DUPLICATE KEY UPDATE creativity_score=creativity_score+VALUES(creativity_score),influence_score=influence_score+VALUES(influence_score),credit_score=VALUES(credit_score)"
  16. // delete
  17. _delPastStatSQL = "DELETE FROM past_score_statistics ORDER BY ID LIMIT ?"
  18. _delPastRecordSQL = "DELETE FROM past_rating_record WHERE cdate='%s'"
  19. )
  20. // DelPastRecord del past record
  21. func (d *Dao) DelPastRecord(c context.Context, date time.Time) (rows int64, err error) {
  22. res, err := d.db.Exec(c, fmt.Sprintf(_delPastRecordSQL, date.Format(_layout)))
  23. if err != nil {
  24. return
  25. }
  26. return res.RowsAffected()
  27. }
  28. // GetPastRecord batch insert past score stat
  29. func (d *Dao) GetPastRecord(c context.Context, cdate string) (times int, err error) {
  30. err = d.db.QueryRow(c, fmt.Sprintf(_pastRatingRecordSQL, cdate)).Scan(&times)
  31. if err == sql.ErrNoRows {
  32. err = nil
  33. times = -1
  34. }
  35. return
  36. }
  37. // InsertPastRecord insert past record date and times
  38. func (d *Dao) InsertPastRecord(c context.Context, times int, cdate string) (rows int64, err error) {
  39. res, err := d.db.Exec(c, fmt.Sprintf(_inPastRecordSQL, cdate), times)
  40. if err != nil {
  41. return
  42. }
  43. return res.RowsAffected()
  44. }
  45. // InsertPastScoreStat batch insert past score stat
  46. func (d *Dao) InsertPastScoreStat(c context.Context, values string) (rows int64, err error) {
  47. res, err := d.db.Exec(c, fmt.Sprintf(_pastScoreStatSQL, values))
  48. if err != nil {
  49. log.Error("d.db.Exec(%s) error(%v)", fmt.Sprintf(_pastScoreStatSQL, values), err)
  50. return
  51. }
  52. return res.RowsAffected()
  53. }
  54. // GetPasts get past statistics
  55. func (d *Dao) GetPasts(c context.Context, offset, limit int64) (past []*model.Past, last int64, err error) {
  56. past = make([]*model.Past, 0, limit)
  57. rows, err := d.db.Query(c, _pastStatSQL, offset, limit)
  58. if err != nil {
  59. log.Error("d.db.Query GetPasts error(%v)", err)
  60. return
  61. }
  62. defer rows.Close()
  63. for rows.Next() {
  64. p := &model.Past{}
  65. err = rows.Scan(&last, &p.MID, &p.MetaCreativityScore, &p.MetaInfluenceScore, &p.CreditScore)
  66. if err != nil {
  67. log.Error("rows.Scan GetPasts error(%v)", err)
  68. return
  69. }
  70. past = append(past, p)
  71. }
  72. return
  73. }
  74. // DelPastStat del past stat
  75. func (d *Dao) DelPastStat(c context.Context, limit int64) (rows int64, err error) {
  76. res, err := d.db.Exec(c, _delPastStatSQL, limit)
  77. if err != nil {
  78. return
  79. }
  80. return res.RowsAffected()
  81. }