score.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/up-rating/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _scoreTotalSQL = "SELECT count(*) FROM up_rating_%02d WHERE cdate=? %s"
  12. _scoreListSQL = "SELECT mid, tag_id, cdate, creativity_score, influence_score, credit_score, magnetic_score FROM up_rating_%02d WHERE cdate=? %s"
  13. _levelListSQL = "SELECT mid, total_fans, total_avs FROM up_level_info_%02d WHERE cdate=? AND mid IN (%s)"
  14. _upScoresSQL = "SELECT cdate, creativity_score, influence_score, credit_score, magnetic_score FROM up_rating_%02d WHERE mid = ?"
  15. _upScoreSQL = "SELECT cdate, creativity_score, influence_score, credit_score, magnetic_score FROM up_rating_%02d WHERE mid = ? AND cdate=?"
  16. _taskStatusSQL = "SELECT status FROM task_status WHERE date=?"
  17. )
  18. // Total counts scores
  19. func (d *Dao) Total(c context.Context, mon int, date, where string) (total int64, err error) {
  20. row := d.db.QueryRow(c, fmt.Sprintf(_scoreTotalSQL, mon, where), date)
  21. if err = row.Scan(&total); err != nil {
  22. log.Error("d.Total row.Scan error(%v)", err)
  23. }
  24. return
  25. }
  26. // ScoreList return info
  27. func (d *Dao) ScoreList(c context.Context, mon int, date, where string) (list []*model.RatingInfo, err error) {
  28. list = make([]*model.RatingInfo, 0)
  29. rows, err := d.db.Query(c, fmt.Sprintf(_scoreListSQL, mon, where), date)
  30. if err != nil {
  31. log.Error("d.ScoreList d.db.Query error(%v)", err)
  32. return
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. v := &model.RatingInfo{}
  37. err = rows.Scan(&v.Mid, &v.TagID, &v.ScoreDate, &v.CreativityScore, &v.InfluenceScore, &v.CreditScore, &v.MagneticScore)
  38. if err != nil {
  39. log.Error("d.ScoreList rows.Scan error(%v)", err)
  40. return
  41. }
  42. list = append(list, v)
  43. }
  44. err = rows.Err()
  45. return
  46. }
  47. // LevelList returns info
  48. func (d *Dao) LevelList(c context.Context, mon int, date string, mids []int64) (list []*model.RatingInfo, err error) {
  49. list = make([]*model.RatingInfo, 0, len(mids))
  50. rows, err := d.db.Query(c, fmt.Sprintf(_levelListSQL, mon, xstr.JoinInts(mids)), date)
  51. if err != nil {
  52. log.Error("d.LevelList d.db.Query error(%v)", err)
  53. return
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. v := &model.RatingInfo{}
  58. if err = rows.Scan(&v.Mid, &v.TotalFans, &v.TotalAvs); err != nil {
  59. log.Error("d.LevelList rows.Scan error(%v)", err)
  60. return
  61. }
  62. list = append(list, v)
  63. }
  64. err = rows.Err()
  65. return
  66. }
  67. // UpScores ...
  68. func (d *Dao) UpScores(c context.Context, mon int, mid int64) (list []*model.RatingInfo, err error) {
  69. list = make([]*model.RatingInfo, 0)
  70. rows, err := d.db.Query(c, fmt.Sprintf(_upScoresSQL, mon), mid)
  71. if err != nil {
  72. log.Error("d.db.Query error(%v)", err)
  73. return
  74. }
  75. defer rows.Close()
  76. for rows.Next() {
  77. v := &model.RatingInfo{}
  78. err = rows.Scan(&v.ScoreDate, &v.CreativityScore, &v.InfluenceScore, &v.CreditScore, &v.MagneticScore)
  79. if err != nil {
  80. log.Error("rows.Scan error(%v)", err)
  81. return
  82. }
  83. list = append(list, v)
  84. }
  85. err = rows.Err()
  86. return
  87. }
  88. // UpScore ...
  89. func (d *Dao) UpScore(c context.Context, mon int, mid int64, date string) (res *model.RatingInfo, err error) {
  90. res = new(model.RatingInfo)
  91. row := d.db.QueryRow(c, fmt.Sprintf(_upScoreSQL, mon), mid, date)
  92. err = row.Scan(&res.ScoreDate, &res.CreativityScore, &res.InfluenceScore, &res.CreditScore, &res.MagneticScore)
  93. if err != nil {
  94. if err == sql.ErrNoRows {
  95. return nil, nil
  96. }
  97. log.Error("row.Scan error(%v)", err)
  98. }
  99. return
  100. }
  101. // TaskStatus returns status of task on date
  102. func (d *Dao) TaskStatus(c context.Context, date string) (status int, err error) {
  103. row := d.db.QueryRow(c, _taskStatusSQL, date)
  104. if err = row.Scan(&status); err != nil {
  105. log.Error("d.TaskStatus row.Scan error(%v)", err)
  106. }
  107. return
  108. }