mysql_recommend.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. artmdl "go-common/app/interface/openplatform/article/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. // RecommendByCategory find recommend by category
  10. func (d *Dao) RecommendByCategory(c context.Context, categoryID int64) (res []*artmdl.Recommend, err error) {
  11. ts := time.Now().Unix()
  12. rows, err := d.recommendCategoryStmt.Query(c, ts, ts, categoryID)
  13. if err != nil {
  14. PromError("db:推荐列表")
  15. log.Error("dao.recommendCategoryStmt.Query error(%+v)", err)
  16. return
  17. }
  18. defer rows.Close()
  19. for rows.Next() {
  20. var (
  21. r = &artmdl.Recommend{Rec: true}
  22. )
  23. if err = rows.Scan(&r.ArticleID, &r.RecImageURL, &r.RecFlag, &r.Position, &r.EndTime, &r.RecImageStartTime, &r.RecImageEndTime); err != nil {
  24. PromError("db:推荐列表scan")
  25. log.Error("dao.RecommendByCategory.rows.Scan error(%+v)", err)
  26. return
  27. }
  28. r.RecImageURL = artmdl.CompleteURL(r.RecImageURL)
  29. res = append(res, r)
  30. }
  31. err = rows.Err()
  32. promErrorCheck(err)
  33. return
  34. }
  35. // DelRecommend delete recommend
  36. func (d *Dao) DelRecommend(c context.Context, aid int64) (err error) {
  37. if _, err := d.delRecommendStmt.Exec(c, time.Now().Unix(), aid); err != nil {
  38. PromError("db:删除推荐")
  39. log.Error("dao.delRecommendStmt.Exec(%v) error(%+v)", aid, err)
  40. }
  41. return
  42. }
  43. // AllRecommends .
  44. func (d *Dao) AllRecommends(c context.Context, t time.Time, pn, ps int) (res []int64, err error) {
  45. ts := t.Unix()
  46. offset := (pn - 1) * ps
  47. rows, err := d.allRecommendStmt.Query(c, ts, ts, offset, ps)
  48. if err != nil {
  49. PromError("db:全部推荐列表")
  50. log.Error("dao.AllRecommends(pn: %v ps: %v)error(%+v)", pn, ps, err)
  51. return
  52. }
  53. defer rows.Close()
  54. for rows.Next() {
  55. var (
  56. aid int64
  57. )
  58. if err = rows.Scan(&aid); err != nil {
  59. PromError("db:全部推荐列表")
  60. log.Error("dao.AllRecommends.rows.Scan error(%+v)", err)
  61. return
  62. }
  63. res = append(res, aid)
  64. }
  65. err = rows.Err()
  66. promErrorCheck(err)
  67. return
  68. }
  69. // AllRecommendCount .
  70. func (d *Dao) AllRecommendCount(c context.Context, t time.Time) (res int64, err error) {
  71. ts := t.Unix()
  72. if err = d.allRecommendCountStmt.QueryRow(c, ts, ts).Scan(&res); err != nil {
  73. if err == sql.ErrNoRows {
  74. err = nil
  75. return
  76. }
  77. PromError("db:推荐列表计数")
  78. log.Error("dao.AllRecommendCount() error(%+v)", err)
  79. }
  80. return
  81. }