base.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/up-rating/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. // get base info from up_level_info
  12. _baseInfoSQL = "SELECT id,mid,tag_id,inc_play,inc_coin,avs,maafans,mahfans,open_avs,lock_avs,cdate FROM up_level_info_%02d WHERE id > ? AND id <= ? LIMIT ?"
  13. _baseTotalSQL = "SELECT id,mid,total_fans,total_avs,total_coin,total_play FROM up_level_info_%02d WHERE id > ? AND cdate = '%s' LIMIT ?"
  14. // get up_level_info start & end
  15. _baseInfoStartSQL = "SELECT id FROM up_level_info_%02d WHERE cdate=? ORDER BY id LIMIT 1"
  16. _baseInfoEndSQL = "SELECT id FROM up_level_info_%02d WHERE cdate=? ORDER BY id DESC LIMIT 1"
  17. )
  18. // GetBaseInfo get rating info
  19. func (d *Dao) GetBaseInfo(c context.Context, month time.Month, start, end, limit int) (bs []*model.BaseInfo, id int, err error) {
  20. rows, err := d.db.Query(c, fmt.Sprintf(_baseInfoSQL, month), start, end, limit)
  21. if err != nil {
  22. log.Error("d.db.Query Rating Base Info error(%v)", err)
  23. return
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. b := &model.BaseInfo{}
  28. err = rows.Scan(&id, &b.MID, &b.TagID, &b.PlayIncr, &b.CoinIncr, &b.Avs, &b.MAAFans, &b.MAHFans, &b.OpenAvs, &b.LockedAvs, &b.Date)
  29. if err != nil {
  30. log.Error("rows scan error(%v)", err)
  31. return
  32. }
  33. bs = append(bs, b)
  34. }
  35. return
  36. }
  37. // GetBaseTotal get total
  38. func (d *Dao) GetBaseTotal(c context.Context, date time.Time, id, limit int64) (bs []*model.BaseInfo, err error) {
  39. bs = make([]*model.BaseInfo, 0)
  40. rows, err := d.db.Query(c, fmt.Sprintf(_baseTotalSQL, date.Month(), date.Format("2006-01-02")), id, limit)
  41. if err != nil {
  42. log.Error("d.db.Query error(%v)", err)
  43. return
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. b := &model.BaseInfo{}
  48. err = rows.Scan(&b.ID, &b.MID, &b.TotalFans, &b.TotalAvs, &b.TotalCoin, &b.TotalPlay)
  49. if err != nil {
  50. log.Error("rows scan error(%v)", err)
  51. return
  52. }
  53. bs = append(bs, b)
  54. }
  55. return
  56. }
  57. // BaseInfoStart get start id by date
  58. func (d *Dao) BaseInfoStart(c context.Context, date time.Time) (start int, err error) {
  59. row := d.db.QueryRow(c, fmt.Sprintf(_baseInfoStartSQL, date.Month()), date)
  60. if err = row.Scan(&start); err != nil {
  61. if err == sql.ErrNoRows {
  62. err = nil
  63. }
  64. }
  65. return
  66. }
  67. // BaseInfoEnd get end id by date
  68. func (d *Dao) BaseInfoEnd(c context.Context, date time.Time) (end int, err error) {
  69. row := d.db.QueryRow(c, fmt.Sprintf(_baseInfoEndSQL, date.Month()), date)
  70. if err = row.Scan(&end); err != nil {
  71. if err == sql.ErrNoRows {
  72. err = nil
  73. }
  74. }
  75. return
  76. }