notice.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _inNoticeSQL = "INSERT INTO notice(title,type,platform,link,status) VALUES (?,?,?,?,?)"
  10. _noticesSQL = "SELECT id,title,type,platform,link,status FROM notice WHERE id > ? %s LIMIT ?"
  11. _noticeCountSQL = "SELECT count(*) FROM notice WHERE id > 0 %s"
  12. _updateNoticeSQL = "UPDATE notice SET %s WHERE id=?"
  13. )
  14. // InsertNotice insert notice
  15. func (d *Dao) InsertNotice(c context.Context, notice *model.Notice) (rows int64, err error) {
  16. res, err := d.rddb.Exec(c, _inNoticeSQL, notice.Title, notice.Type, notice.Platform, notice.Link, notice.Status)
  17. if err != nil {
  18. log.Error("d.db.Exec insert notice error(%v)", err)
  19. return
  20. }
  21. return res.RowsAffected()
  22. }
  23. // NoticeCount get notice count
  24. func (d *Dao) NoticeCount(c context.Context, query string) (count int, err error) {
  25. row := d.rddb.QueryRow(c, fmt.Sprintf(_noticeCountSQL, query))
  26. if err = row.Scan(&count); err != nil {
  27. log.Error("d.db.notice count error(%v)", err)
  28. }
  29. return
  30. }
  31. // Notices get notices
  32. func (d *Dao) Notices(c context.Context, query string, offset int, limit int) (notices []*model.Notice, err error) {
  33. rows, err := d.rddb.Query(c, fmt.Sprintf(_noticesSQL, query), offset, limit)
  34. if err != nil {
  35. log.Error("d.db.Query notice error(%v)", err)
  36. return
  37. }
  38. defer rows.Close()
  39. for rows.Next() {
  40. n := &model.Notice{}
  41. err = rows.Scan(&n.ID, &n.Title, &n.Type, &n.Platform, &n.Link, &n.Status)
  42. if err != nil {
  43. log.Error("rows scan error(%v)", err)
  44. return
  45. }
  46. notices = append(notices, n)
  47. }
  48. return
  49. }
  50. // UpdateNotice update notice
  51. func (d *Dao) UpdateNotice(c context.Context, kv string, id int64) (rows int64, err error) {
  52. res, err := d.rddb.Exec(c, fmt.Sprintf(_updateNoticeSQL, kv), id)
  53. if err != nil {
  54. return
  55. }
  56. return res.RowsAffected()
  57. }