mysql.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package esports
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. mdlesp "go-common/app/job/main/web-goblin/model/esports"
  8. arcmdl "go-common/app/service/main/archive/api"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _contestsSQL = "SELECT c.id,c.stime,c.live_room,c.home_id,c.away_id,c.success_team,c.special,c.special_name,c.special_tips,s.title,s.sub_title FROM `es_contests` as c INNER JOIN `es_seasons` as s ON c.sid=s.id WHERE c.status = 0 AND c.stime >= ? and c.stime < ? "
  16. _teamSQL = "SELECT id,title,sub_title FROM `es_teams` WHERE is_deleted = 0 AND (id = ? or id = ?)"
  17. _arcSQL = "SELECT id,aid,score,is_deleted FROM `es_archives` WHERE is_deleted = 0 AND id > ? ORDER BY id ASC LIMIT ? "
  18. _arcEditSQL = "UPDATE es_archives SET score = CASE %s END WHERE aid IN (%s)"
  19. )
  20. // Contests contests by time.
  21. func (d *Dao) Contests(c context.Context, stime, etime int64) (res []*mdlesp.Contest, err error) {
  22. var (
  23. rows *xsql.Rows
  24. )
  25. if rows, err = d.db.Query(c, _contestsSQL, stime, etime); err != nil {
  26. log.Error("Contests:d.db.Query(%d) error(%v)", stime, err)
  27. return
  28. }
  29. defer rows.Close()
  30. for rows.Next() {
  31. r := new(mdlesp.Contest)
  32. if err = rows.Scan(&r.ID, &r.Stime, &r.LiveRoom, &r.HomeID, &r.AwayID, &r.SuccessTeam, &r.Special, &r.SpecialName, &r.SpecialTips, &r.SeasonTitle, &r.SeasonSubTitle); err != nil {
  33. log.Error("Contests:row.Scan() error(%v)", err)
  34. return
  35. }
  36. res = append(res, r)
  37. }
  38. if err = rows.Err(); err != nil {
  39. log.Error("rows.Err() error(%v)", err)
  40. }
  41. return
  42. }
  43. // Teams teams by id.
  44. func (d *Dao) Teams(c context.Context, homeID, awayID int64) (res []*mdlesp.Team, err error) {
  45. var (
  46. rows *xsql.Rows
  47. )
  48. if rows, err = d.db.Query(c, _teamSQL, homeID, awayID); err != nil {
  49. log.Error("Teams:d.db.Query homeID(%d) awayID(%d) error(%v)", homeID, awayID, err)
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. r := new(mdlesp.Team)
  55. if err = rows.Scan(&r.ID, &r.Title, &r.SubTitle); err != nil {
  56. log.Error("Teams:row.Scan() error(%v)", err)
  57. return
  58. }
  59. res = append(res, r)
  60. }
  61. if err = rows.Err(); err != nil {
  62. log.Error("rows.Err() error(%v)", err)
  63. }
  64. return
  65. }
  66. // Arcs archives by ids.
  67. func (d *Dao) Arcs(c context.Context, id int64, limit int) (res []*mdlesp.Arc, err error) {
  68. var (
  69. rows *xsql.Rows
  70. )
  71. if rows, err = d.db.Query(c, _arcSQL, id, limit); err != nil {
  72. log.Error("Arcs:d.db.Query id(%d) limit(%d) error(%v)", id, limit, err)
  73. return
  74. }
  75. defer rows.Close()
  76. for rows.Next() {
  77. r := new(mdlesp.Arc)
  78. if err = rows.Scan(&r.ID, &r.Aid, &r.Score, &r.IsDeleted); err != nil {
  79. log.Error("Arcs:row.Scan() error(%v)", err)
  80. return
  81. }
  82. res = append(res, r)
  83. }
  84. if err = rows.Err(); err != nil {
  85. log.Error("rows.Err() error(%v)", err)
  86. }
  87. return
  88. }
  89. // UpArcScore update archive score.
  90. func (d *Dao) UpArcScore(c context.Context, partArcs []*mdlesp.Arc, arcs map[int64]*arcmdl.Arc) (err error) {
  91. var (
  92. caseStr string
  93. aids []int64
  94. score int64
  95. )
  96. for _, v := range partArcs {
  97. if arc, ok := arcs[v.Aid]; ok {
  98. score = d.score(arc)
  99. } else {
  100. continue
  101. }
  102. caseStr = fmt.Sprintf("%s WHEN aid = %d THEN %d", caseStr, v.Aid, score)
  103. aids = append(aids, v.Aid)
  104. }
  105. if len(aids) == 0 {
  106. return
  107. }
  108. if _, err = d.db.Exec(c, fmt.Sprintf(_arcEditSQL, caseStr, xstr.JoinInts(aids))); err != nil {
  109. err = errors.Wrapf(err, "UpArcScore d.db.Exec")
  110. }
  111. return
  112. }
  113. func (d *Dao) score(arc *arcmdl.Arc) (res int64) {
  114. tmpRs := float64(arc.Stat.Coin)*d.c.Rule.CoinPercent +
  115. float64(arc.Stat.Fav)*d.c.Rule.FavPercent + float64(arc.Stat.Danmaku)*d.c.Rule.DmPercent +
  116. float64(arc.Stat.Reply)*d.c.Rule.ReplyPercent + float64(arc.Stat.View)*d.c.Rule.ViewPercent +
  117. float64(arc.Stat.Like)*d.c.Rule.LikePercent + float64(arc.Stat.Share)*d.c.Rule.SharePercent
  118. now := time.Now()
  119. hours := now.Sub(arc.PubDate.Time()).Hours()
  120. if hours/24 <= d.c.Rule.NewDay {
  121. tmpRs = tmpRs * 1.5
  122. }
  123. decimal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", tmpRs), 64)
  124. res = int64(decimal * 100)
  125. return
  126. }