banner.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/log"
  7. "go-common/library/time"
  8. )
  9. const (
  10. _insertBannerSQL = "INSERT INTO banner(image,link,start_at,end_at) VALUES(?,?,?,?)"
  11. _bannersSQL = "SELECT id,image,link,start_at,end_at FROM banner WHERE id > ? ORDER BY id LIMIT ?"
  12. _totalBannerCountSQL = "SELECT count(*) FROM banner"
  13. // start_at > end_at(insert) or end_at < start_at(insert)
  14. _bannerSQL = "SELECT id FROM banner WHERE end_at > ? AND NOT ((start_at > ?) OR (end_at < ?))"
  15. _editBannerSQL = "SELECT id FROM banner WHERE id != ? AND end_at > ? AND NOT ((start_at > ?) OR (end_at < ?))"
  16. _updateBannerSQL = "UPDATE banner SET image=?,link=?,start_at=?,end_at=? WHERE id=?"
  17. _updateEndAtSQL = "UPDATE banner SET end_at=? WHERE id=?"
  18. )
  19. // TotalBannerCount get total banner count
  20. func (d *Dao) TotalBannerCount(c context.Context) (count int64, err error) {
  21. row := d.rddb.QueryRow(c, _totalBannerCountSQL)
  22. if err = row.Scan(&count); err != nil {
  23. log.Error("d.rddb.TotalBannerCount error(%v)", err)
  24. }
  25. return
  26. }
  27. // DupEditBanner find duplicate banner id where end_at >= start_at(edit)
  28. func (d *Dao) DupEditBanner(c context.Context, startAt, endAt, now, id int64) (dup int64, err error) {
  29. row := d.rddb.QueryRow(c, _editBannerSQL, id, time.Time(now), time.Time(endAt), time.Time(startAt))
  30. if err = row.Scan(&dup); err != nil {
  31. if err == sql.ErrNoRows {
  32. err = nil
  33. } else {
  34. log.Error("d.rddb.DupEditBanner error(%v)", err)
  35. }
  36. }
  37. return
  38. }
  39. // DupBanner find duplicate banner id that end_at > start_at(insert)
  40. func (d *Dao) DupBanner(c context.Context, startAt, endAt, now int64) (dup int64, err error) {
  41. row := d.rddb.QueryRow(c, _bannerSQL, time.Time(now), time.Time(endAt), time.Time(startAt))
  42. if err = row.Scan(&dup); err != nil {
  43. if err == sql.ErrNoRows {
  44. err = nil
  45. } else {
  46. log.Error("d.rddb.DupBanner error(%v)", err)
  47. }
  48. }
  49. return
  50. }
  51. // InsertBanner insert banner
  52. func (d *Dao) InsertBanner(c context.Context, image, link string, startAt, endAt int64) (rows int64, err error) {
  53. res, err := d.rddb.Exec(c, _insertBannerSQL, image, link, time.Time(startAt), time.Time(endAt))
  54. if err != nil {
  55. log.Error("d.db.Exec insert banner error(%v)", err)
  56. return
  57. }
  58. return res.RowsAffected()
  59. }
  60. // Banners get banners
  61. func (d *Dao) Banners(c context.Context, offset, limit int64) (bs []*model.Banner, err error) {
  62. rows, err := d.rddb.Query(c, _bannersSQL, offset, limit)
  63. if err != nil {
  64. log.Error("d.db.query banners error(%v)", err)
  65. return
  66. }
  67. defer rows.Close()
  68. for rows.Next() {
  69. b := &model.Banner{}
  70. err = rows.Scan(&b.ID, &b.Image, &b.Link, &b.StartAt, &b.EndAt)
  71. if err != nil {
  72. log.Error("rows scan error(%v)", err)
  73. return
  74. }
  75. bs = append(bs, b)
  76. }
  77. return
  78. }
  79. // UpdateBanner update banner
  80. func (d *Dao) UpdateBanner(c context.Context, image, link string, startAt, endAt, id int64) (rows int64, err error) {
  81. res, err := d.rddb.Exec(c, _updateBannerSQL, image, link, time.Time(startAt), time.Time(endAt), id)
  82. if err != nil {
  83. log.Error("d.db.Exec update banner error(%v)", err)
  84. return
  85. }
  86. return res.RowsAffected()
  87. }
  88. // UpdateBannerEndAt update banner end at
  89. func (d *Dao) UpdateBannerEndAt(c context.Context, endAt, id int64) (rows int64, err error) {
  90. res, err := d.rddb.Exec(c, _updateEndAtSQL, time.Time(endAt), id)
  91. if err != nil {
  92. log.Error("d.db.Exec update banner end_at error(%v)", err)
  93. return
  94. }
  95. return res.RowsAffected()
  96. }