notice.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package notice
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-resource/conf"
  6. "go-common/app/interface/main/app-resource/model/notice"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _getSQL = `SELECT id,plat,title,content,build,conditions,area,url,type,ef_time,ex_time FROM notice WHERE state=1 AND ef_time<? AND ex_time>? ORDER BY mtime DESC`
  12. )
  13. // Dao is notice dao.
  14. type Dao struct {
  15. db *sql.DB
  16. get *sql.Stmt
  17. }
  18. // New new a notice dao.
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. db: sql.NewMySQL(c.MySQL.Show),
  22. }
  23. d.get = d.db.Prepared(_getSQL)
  24. return
  25. }
  26. // GetAll get all notice data.
  27. func (d *Dao) All(ctx context.Context, now time.Time) (res []*notice.Notice, err error) {
  28. rows, err := d.get.Query(ctx, now, now)
  29. if err != nil {
  30. log.Error("query error (%v)", err)
  31. return
  32. }
  33. defer rows.Close()
  34. res = []*notice.Notice{}
  35. for rows.Next() {
  36. b := &notice.Notice{}
  37. if err = rows.Scan(&b.ID, &b.Plat, &b.Title, &b.Content, &b.Build, &b.Condition, &b.Area, &b.URI, &b.Type, &b.Start, &b.End); err != nil {
  38. log.Error("rows.Scan err (%v)", err)
  39. return nil, err
  40. }
  41. res = append(res, b)
  42. }
  43. return
  44. }
  45. // Close close memcache resource.
  46. func (dao *Dao) Close() {
  47. if dao.db != nil {
  48. dao.db.Close()
  49. }
  50. }