action.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/admin/main/activity/model"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _likeActSumSQL = "SELECT SUM(`action`) AS `like`,lid FROM like_action WHERE lid IN(%s) GROUP BY lid"
  12. _likeActListSQL = "SELECT id,mid FROM like_action WHERE lid = ? AND id > ? ORDER BY id LIMIT ?"
  13. )
  14. // BatchLikeActSum .
  15. func (d *Dao) BatchLikeActSum(c context.Context, lids []int64) (res map[int64]int64, err error) {
  16. rows, err := d.db.Query(c, fmt.Sprintf(_likeActSumSQL, xstr.JoinInts(lids)))
  17. if err != nil {
  18. if err == sql.ErrNoRows {
  19. err = nil
  20. } else {
  21. err = errors.Wrap(err, "d.db.Query()")
  22. }
  23. return
  24. }
  25. defer rows.Close()
  26. res = make(map[int64]int64)
  27. for rows.Next() {
  28. like := sql.NullInt64{}
  29. lid := sql.NullInt64{}
  30. if err = rows.Scan(&like, &lid); err != nil {
  31. err = errors.Wrap(err, "rows.Scan()")
  32. return
  33. }
  34. res[lid.Int64] = like.Int64
  35. }
  36. if err = rows.Err(); err != nil {
  37. err = errors.Wrap(err, "rows.Err()")
  38. }
  39. return
  40. }
  41. // LikeActList .
  42. func (d *Dao) LikeActList(c context.Context, lid, minID, limit int64) (res []*model.LikeAction, err error) {
  43. rows, err := d.db.Query(c, _likeActListSQL, lid, minID, limit)
  44. if err != nil {
  45. if err == sql.ErrNoRows {
  46. err = nil
  47. } else {
  48. err = errors.Wrap(err, "d.db.Query()")
  49. }
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. action := new(model.LikeAction)
  55. if err = rows.Scan(&action.ID, &action.Mid); err != nil {
  56. err = errors.Wrap(err, "rows.Scan()")
  57. return
  58. }
  59. res = append(res, action)
  60. }
  61. if err = rows.Err(); err != nil {
  62. err = errors.Wrap(err, "rows.Err()")
  63. }
  64. return
  65. }