upper.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/log"
  6. "go-common/library/time"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _upCntSQL = "SELECT COUNT(*) FROM archive WHERE mid=? AND (state>=0 or state=-6)"
  12. _upsCntSQL = "SELECT mid,COUNT(*) FROM archive WHERE mid IN(%s) AND (state>=0 or state=-6) GROUP BY mid"
  13. _upPasSQL = "SELECT aid,pubtime,copyright FROM archive WHERE mid=? AND state>=0 ORDER BY pubtime DESC"
  14. _upsPasSQL = "SELECT aid,mid,pubtime,copyright FROM archive WHERE mid IN (%s) AND state>=0 ORDER BY pubtime DESC"
  15. )
  16. // UppersCount get mids count
  17. func (d *Dao) UppersCount(c context.Context, mids []int64) (uc map[int64]int64, err error) {
  18. rows, err := d.resultDB.Query(c, fmt.Sprintf(_upsCntSQL, xstr.JoinInts(mids)))
  19. if err != nil {
  20. err = errors.WithStack(err)
  21. return
  22. }
  23. uc = make(map[int64]int64, len(mids))
  24. defer rows.Close()
  25. for rows.Next() {
  26. var (
  27. mid int64
  28. cnt int64
  29. )
  30. if err = rows.Scan(&mid, &cnt); err != nil {
  31. err = errors.WithStack(err)
  32. return
  33. }
  34. uc[mid] = cnt
  35. }
  36. return
  37. }
  38. // UpperCount get the count of archives by mid of Up.
  39. func (d *Dao) UpperCount(c context.Context, mid int64) (count int64, err error) {
  40. d.infoProm.Incr("UpperCount")
  41. row := d.resultDB.QueryRow(c, _upCntSQL, mid)
  42. if err = row.Scan(&count); err != nil {
  43. log.Error("row.Scan error(%v)", err)
  44. }
  45. return
  46. }
  47. // UpperPassed get upper passed archives.
  48. func (d *Dao) UpperPassed(c context.Context, mid int64) (aids []int64, ptimes []time.Time, copyrights []int8, err error) {
  49. d.infoProm.Incr("UpperPassed")
  50. rows, err := d.resultDB.Query(c, _upPasSQL, mid)
  51. if err != nil {
  52. log.Error("getUpPasStmt.Query(%d) error(%v)", mid, err)
  53. return
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. var (
  58. aid int64
  59. ptime time.Time
  60. copyright int8
  61. )
  62. if err = rows.Scan(&aid, &ptime, &copyright); err != nil {
  63. log.Error("rows.Scan error(%v)", err)
  64. return
  65. }
  66. aids = append(aids, aid)
  67. ptimes = append(ptimes, ptime)
  68. copyrights = append(copyrights, copyright)
  69. }
  70. return
  71. }
  72. // UppersPassed get uppers passed archives.
  73. func (d *Dao) UppersPassed(c context.Context, mids []int64) (aidm map[int64][]int64, ptimes map[int64][]time.Time, copyrights map[int64][]int8, err error) {
  74. d.infoProm.Incr("UppersPassed")
  75. rows, err := d.resultDB.Query(c, fmt.Sprintf(_upsPasSQL, xstr.JoinInts(mids)))
  76. if err != nil {
  77. log.Error("UpsPassed error(%v)", err)
  78. return
  79. }
  80. defer rows.Close()
  81. aidm = make(map[int64][]int64, len(mids))
  82. ptimes = make(map[int64][]time.Time, len(mids))
  83. copyrights = make(map[int64][]int8, len(mids))
  84. for rows.Next() {
  85. var (
  86. aid, mid int64
  87. ptime time.Time
  88. copyright int8
  89. )
  90. if err = rows.Scan(&aid, &mid, &ptime, &copyright); err != nil {
  91. log.Error("rows.Scan error(%v)", err)
  92. return
  93. }
  94. aidm[mid] = append(aidm[mid], aid)
  95. ptimes[mid] = append(ptimes[mid], ptime)
  96. copyrights[mid] = append(copyrights[mid], copyright)
  97. }
  98. return
  99. }