match.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. match "go-common/app/interface/main/activity/model/like"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _matchSidSQL = "SELECT id,sid,max_stake,stake,name,url,cover,ctime,mtime FROM act_matchs WHERE status=0 AND sid = ? order by id"
  13. _matchSQL = "SELECT id,sid,max_stake,stake,name,url,cover,ctime,mtime FROM act_matchs WHERE status=0 AND id = ?"
  14. _objIDSQL = "SELECT id,match_id,sid,home_name,home_logo,home_score,away_name,away_logo,away_score,result,ctime,mtime,stime,etime,game_stime FROM act_matchs_object WHERE status=0 AND id= ?"
  15. _objIDsSQL = "SELECT id,match_id,sid,home_name,home_logo,home_score,away_name,away_logo,away_score,result,ctime,mtime,stime,etime,game_stime FROM act_matchs_object WHERE status=0 AND id IN (%s)"
  16. _objSIDSQL = "SELECT o.id,o.match_id,o.sid,o.home_name,o.home_logo,o.home_score,o.away_name,o.away_logo,o.away_score,o.result,o.ctime,o.mtime,o.stime,o.etime,o.game_stime,m.name as match_name FROM act_matchs_object as o INNER JOIN act_matchs as m ON o.match_id=m.id WHERE o.status=0 AND o.result=0 AND o.sid= ? ORDER BY o.game_stime ASC"
  17. _userAddSQL = "INSERT INTO act_match_user_log (mid,match_id,m_o_id,sid,result,stake) VALUES (?,?,?,?,?,?)"
  18. _userResultSQL = "SELECT id,mid,match_id,m_o_id,sid,result,stake,ctime,mtime FROM act_match_user_log WHERE sid = ? AND mid = ? ORDER BY id DESC"
  19. )
  20. // Match get Match
  21. func (d *Dao) Match(c context.Context, id int64) (res *match.Match, err error) {
  22. res = &match.Match{}
  23. row := d.db.QueryRow(c, _matchSQL, id)
  24. if err = row.Scan(&res.ID, &res.Sid, &res.MaxStake, &res.Stake, &res.Name, &res.Url, &res.Cover, &res.Ctime, &res.Mtime); err != nil {
  25. if err == sql.ErrNoRows {
  26. err = nil
  27. } else {
  28. log.Error("Match:row.Scan error(%v)", err)
  29. }
  30. }
  31. return
  32. }
  33. // ActMatch get Activity Match
  34. func (d *Dao) ActMatch(c context.Context, sid int64) (res []*match.Match, err error) {
  35. var (
  36. rows *xsql.Rows
  37. matchs []*match.Match
  38. )
  39. if rows, err = d.db.Query(c, _matchSidSQL, sid); err != nil {
  40. log.Error("Match: db.Exec(%d) error(%v)", sid, err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. r := new(match.Match)
  46. if err = rows.Scan(&r.ID, &r.Sid, &r.MaxStake, &r.Stake, &r.Name, &r.Url, &r.Cover, &r.Ctime, &r.Mtime); err != nil {
  47. log.Error("Match:row.Scan() error(%v)", err)
  48. return
  49. }
  50. matchs = append(matchs, r)
  51. }
  52. res = matchs
  53. return
  54. }
  55. // Object get object
  56. func (d *Dao) Object(c context.Context, id int64) (res *match.Object, err error) {
  57. res = &match.Object{}
  58. row := d.db.QueryRow(c, _objIDSQL, id)
  59. if err = row.Scan(&res.ID, &res.MatchId, &res.Sid, &res.HomeName, &res.HomeLogo, &res.HomeScore, &res.AwayName, &res.AwayLogo, &res.AwayScore, &res.Result, &res.Ctime, &res.Mtime, &res.Stime, &res.Etime, &res.GameStime); err != nil {
  60. if err == sql.ErrNoRows {
  61. err = nil
  62. } else {
  63. log.Error("Match:row.Scan error(%v)", err)
  64. }
  65. }
  66. return
  67. }
  68. // RawMatchSubjects .
  69. func (d *Dao) RawMatchSubjects(c context.Context, ids []int64) (res map[int64]*match.Object, err error) {
  70. var rows *xsql.Rows
  71. if rows, err = d.db.Query(c, fmt.Sprintf(_objIDsSQL, xstr.JoinInts(ids))); err != nil {
  72. log.Error("RawMatchSubjects: d.db.Query(%v) error(%v)", ids, err)
  73. return
  74. }
  75. defer rows.Close()
  76. res = make(map[int64]*match.Object, len(ids))
  77. for rows.Next() {
  78. r := new(match.Object)
  79. if err = rows.Scan(&r.ID, &r.MatchId, &r.Sid, &r.HomeName, &r.HomeLogo, &r.HomeScore, &r.AwayName, &r.AwayLogo, &r.AwayScore, &r.Result, &r.Ctime, &r.Mtime, &r.Stime, &r.Etime, &r.GameStime); err != nil {
  80. log.Error("RawMatchSubjects:row.Scan() error(%v)", err)
  81. return
  82. }
  83. res[r.ID] = r
  84. }
  85. return
  86. }
  87. // ObjectsUnStart get unstart objects.
  88. func (d *Dao) ObjectsUnStart(c context.Context, sid int64) (res []*match.Object, err error) {
  89. var (
  90. rows *xsql.Rows
  91. )
  92. if rows, err = d.db.Query(c, _objSIDSQL, sid); err != nil {
  93. log.Error("ObjectsUnStart: db.Exec(%d) error(%v)", sid, err)
  94. return
  95. }
  96. defer rows.Close()
  97. for rows.Next() {
  98. r := new(match.Object)
  99. if err = rows.Scan(&r.ID, &r.MatchId, &r.Sid, &r.HomeName, &r.HomeLogo, &r.HomeScore, &r.AwayName, &r.AwayLogo, &r.AwayScore, &r.Result, &r.Ctime, &r.Mtime, &r.Stime, &r.Etime, &r.GameStime, &r.MatchName); err != nil {
  100. log.Error("ObjectsUnStart:row.Scan() error(%v)", err)
  101. return
  102. }
  103. res = append(res, r)
  104. }
  105. return
  106. }
  107. // AddGuess add match user log
  108. func (d *Dao) AddGuess(c context.Context, mid, matID, objID, sid, result, stake int64) (lastID int64, err error) {
  109. var res sql.Result
  110. if res, err = d.db.Exec(c, _userAddSQL, mid, matID, objID, sid, result, stake); err != nil {
  111. log.Error("AddGuess: db.Exec(%d,%d,%d,%d,%d,%d) error(%v)", mid, matID, objID, sid, result, stake, err)
  112. return
  113. }
  114. return res.LastInsertId()
  115. }
  116. // ListGuess get match user log
  117. func (d *Dao) ListGuess(c context.Context, sid, mid int64) (res []*match.UserLog, err error) {
  118. var rows *xsql.Rows
  119. if rows, err = d.db.Query(c, _userResultSQL, sid, mid); err != nil {
  120. log.Error("ListGuess: db.Exec(%d,%d) error(%v)", sid, mid, err)
  121. return
  122. }
  123. defer rows.Close()
  124. for rows.Next() {
  125. r := new(match.UserLog)
  126. if err = rows.Scan(&r.ID, &r.Mid, &r.MatchId, &r.MOId, &r.Sid, &r.Result, &r.Stake, &r.Ctime, &r.Mtime); err != nil {
  127. log.Error("ListGuess:row.Scan() error(%v)", err)
  128. return
  129. }
  130. res = append(res, r)
  131. }
  132. return
  133. }