web_data.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package like
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/job/main/activity/model/like"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _webDataCntSQL = "SELECT COUNT(1) AS cnt FROM act_web_data WHERE state = 1 AND vid = ?"
  10. _webDataListSQL = "SELECT id,vid,data FROM act_web_data WHERE state= 1 AND vid = ? ORDER BY id LIMIT ?,?"
  11. )
  12. // WebDataCnt get web data count.
  13. func (d *Dao) WebDataCnt(c context.Context, vid int64) (count int, err error) {
  14. row := d.db.QueryRow(c, _webDataCntSQL, vid)
  15. if err = row.Scan(&count); err != nil {
  16. if err == sql.ErrNoRows {
  17. err = nil
  18. } else {
  19. err = errors.Wrapf(err, "WebDataCnt:QueryRow(%d)", vid)
  20. }
  21. }
  22. return
  23. }
  24. // WebDataList get web data list by vid.
  25. func (d *Dao) WebDataList(c context.Context, vid int64, offset, limit int) (list []*like.WebData, err error) {
  26. rows, err := d.db.Query(c, _webDataListSQL, vid, offset, limit)
  27. if err != nil {
  28. err = errors.Wrapf(err, "WebDataList:d.db.Query(%d,%d,%d)", vid, offset, limit)
  29. return
  30. }
  31. defer rows.Close()
  32. for rows.Next() {
  33. n := new(like.WebData)
  34. if err = rows.Scan(&n.ID, &n.Vid, &n.Data); err != nil {
  35. err = errors.Wrapf(err, "WebDataList:row.Scan row (%d,%d,%d)", vid, offset, limit)
  36. return
  37. }
  38. list = append(list, n)
  39. }
  40. if err = rows.Err(); err != nil {
  41. err = errors.Wrapf(err, "LikeList:rowsErr(%d,%d,%d)", vid, offset, limit)
  42. }
  43. return
  44. }