mysql.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/interface/main/creative/model/weeklyhonor"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. )
  9. const (
  10. _honorLogsSQL = `SELECT id,mid,hid,count,ctime,mtime FROM weeklyhonor WHERE mid=?`
  11. _honorLatestLogsSQL = `SELECT id,mid,hid,mtime FROM weeklyhonor WHERE mid in (%s) and mtime >= ?`
  12. _honorClicksSQL = `SELECT mid,count FROM weeklyhonor_click WHERE mid in (%s) and mtime >= ?`
  13. _upsertCountSQL = `INSERT INTO weeklyhonor (mid,hid,count) VALUES (?,?,?) ON DUPLICATE KEY UPDATE count=count+1`
  14. )
  15. // pingMySQL check mysql connection.
  16. func (d *Dao) pingMySQL(c context.Context) error {
  17. return d.db.Ping(c)
  18. }
  19. // HonorLogs .
  20. func (d *Dao) HonorLogs(c context.Context, mid int64) (hls map[int]*model.HonorLog, err error) {
  21. rows, err := d.db.Query(c, _honorLogsSQL, mid)
  22. if err != nil {
  23. log.Error("d.db.Query(%s,%d) error(%v)", _honorLogsSQL, mid, err)
  24. return
  25. }
  26. defer rows.Close()
  27. hls = make(map[int]*model.HonorLog)
  28. for rows.Next() {
  29. h := new(model.HonorLog)
  30. if err = rows.Scan(&h.ID, &h.MID, &h.HID, &h.Count, &h.CTime, &h.MTime); err != nil {
  31. log.Error("rows.Scan error(%v)", err)
  32. return
  33. }
  34. hls[h.HID] = h
  35. }
  36. err = rows.Err()
  37. return
  38. }
  39. // LatestHonorLogs list latest honor logs by mids
  40. func (d *Dao) LatestHonorLogs(c context.Context, mids []int64) (hls []*model.HonorLog, err error) {
  41. latestSun := model.LatestSunday()
  42. midsStr := xstr.JoinInts(mids)
  43. if midsStr == "" {
  44. return
  45. }
  46. sql := fmt.Sprintf(_honorLatestLogsSQL, midsStr)
  47. rows, err := d.db.Query(c, sql, latestSun)
  48. if err != nil {
  49. log.Error("d.db.Query(%s,%q,%v) error(%v)", _honorLatestLogsSQL, mids, latestSun, err)
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. h := new(model.HonorLog)
  55. if err = rows.Scan(&h.ID, &h.MID, &h.HID, &h.MTime); err != nil {
  56. log.Error("rows.Scan error(%v)", err)
  57. return
  58. }
  59. hls = append(hls, h)
  60. }
  61. err = rows.Err()
  62. return
  63. }
  64. // UpsertCount .
  65. func (d *Dao) UpsertCount(c context.Context, mid int64, hid int) (affected int64, err error) {
  66. res, err := d.db.Exec(c, _upsertCountSQL, mid, hid, 1)
  67. if err != nil {
  68. log.Error("d.db.Exec(%s,%d,%d,%d) error(%v)", _upsertCountSQL, mid, hid, 1, err)
  69. return
  70. }
  71. return res.RowsAffected()
  72. }
  73. // ClickCounts honor click count map
  74. func (d *Dao) ClickCounts(c context.Context, mids []int64) (res map[int64]int32, err error) {
  75. res = make(map[int64]int32)
  76. twoWeekAgo := model.LatestSunday().AddDate(0, 0, -14)
  77. midsStr := xstr.JoinInts(mids)
  78. if midsStr == "" {
  79. return
  80. }
  81. sql := fmt.Sprintf(_honorClicksSQL, midsStr)
  82. rows, err := d.db.Query(c, sql, twoWeekAgo)
  83. if err != nil {
  84. log.Error("d.db.Query(%s,%v) error(%v)", sql, twoWeekAgo, err)
  85. return
  86. }
  87. defer rows.Close()
  88. for rows.Next() {
  89. var mid, count int64
  90. if err = rows.Scan(&mid, &count); err != nil {
  91. log.Error("rows.Scan error(%v)", err)
  92. return
  93. }
  94. if count > 0 {
  95. res[mid] = int32(count)
  96. }
  97. }
  98. return
  99. }