dao.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package manager
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/conf"
  5. "go-common/app/interface/main/app-intl/model/manager"
  6. xsql "go-common/library/database/sql"
  7. )
  8. const (
  9. _relateSQL = "SELECT `id`,`param`,`goto`,`title`,`resource_ids`,`tag_ids`,`archive_ids`,`rec_reason`,`position`,`plat_ver`, `stime`,`etime` FROM app_rcmd_pos WHERE `state`=1"
  10. )
  11. // Dao struct
  12. type Dao struct {
  13. db *xsql.DB
  14. get *xsql.Stmt
  15. }
  16. // New a dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. db: xsql.NewMySQL(c.MySQL.Show),
  20. }
  21. // prepare
  22. d.get = d.db.Prepared(_relateSQL)
  23. return
  24. }
  25. // Relate get all relate rec.
  26. func (d *Dao) Relate(c context.Context) (rs []*manager.Relate, err error) {
  27. rows, err := d.get.Query(c)
  28. if err != nil {
  29. return
  30. }
  31. defer rows.Close()
  32. for rows.Next() {
  33. r := &manager.Relate{}
  34. if err = rows.Scan(&r.ID, &r.Param, &r.Goto, &r.Title, &r.ResourceIDs, &r.TagIDs, &r.ArchiveIDs, &r.RecReason, &r.Position, &r.PlatVer, &r.STime, &r.ETime); err != nil {
  35. return
  36. }
  37. r.Change()
  38. rs = append(rs, r)
  39. }
  40. return
  41. }
  42. // Close close db resource.
  43. func (d *Dao) Close() {
  44. if d.db != nil {
  45. d.db.Close()
  46. }
  47. }