search_con.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package ugc
  2. import (
  3. "context"
  4. model "go-common/app/job/main/tv/model/pgc"
  5. "go-common/library/database/sql"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _UgcFromwhere = "FROM ugc_archive WHERE result=1 AND valid=1 AND deleted=0 "
  11. _UgcCont = "SELECT aid,title,cover,`content`,pubtime,typeid " + _UgcFromwhere + "AND aid > ? ORDER BY aid ASC LIMIT ?"
  12. _UgcContCount = " SELECT count(*) " + _UgcFromwhere
  13. )
  14. // UgcCont is used for getting valid ugc archive data
  15. func (d *Dao) UgcCont(ctx context.Context, aid int, limit int) (res []*model.SearUgcCon, maxID int, err error) {
  16. var (
  17. rows *sql.Rows
  18. )
  19. if rows, err = d.DB.Query(ctx, _UgcCont, aid, limit); err != nil {
  20. log.Error("d.UgcCont.Query: %s error(%v)", _UgcCont, err)
  21. return
  22. }
  23. defer rows.Close()
  24. for rows.Next() {
  25. var r = &model.SearUgcCon{}
  26. if err = rows.Scan(&r.AID, &r.Title, &r.Cover, &r.Content, &r.Pubtime, &r.Typeid); err != nil {
  27. log.Error("UgcCont row.Scan() error(%v)", err)
  28. return
  29. }
  30. res = append(res, r)
  31. }
  32. if err = rows.Err(); err != nil {
  33. log.Error("d.UgcCont.Query error(%v)", err)
  34. return
  35. }
  36. if len(res) == 0 {
  37. err = ecode.NothingFound
  38. return
  39. }
  40. maxID = res[len(res)-1].AID
  41. return
  42. }
  43. // UgcCnt is used for getting valid data count
  44. func (d *Dao) UgcCnt(ctx context.Context) (upCnt int, err error) {
  45. row := d.DB.QueryRow(ctx, _UgcContCount)
  46. if err = row.Scan(&upCnt); err != nil {
  47. log.Error("d.SeaContCount.Query: %s error(%v)", _UgcContCount, err)
  48. }
  49. return
  50. }