dao.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package region
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-view/conf"
  5. "go-common/app/interface/main/app-view/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. type Dao struct {
  12. db *xsql.DB
  13. get *xsql.Stmt
  14. }
  15. func New(c *conf.Config) (d *Dao) {
  16. d = &Dao{
  17. db: xsql.NewMySQL(c.MySQL.Show),
  18. }
  19. // prepare
  20. d.get = d.db.Prepared(_relateSQL)
  21. return
  22. }
  23. // Relate get all relate rec.
  24. func (d *Dao) Relate(c context.Context) (rs []*manager.Relate, err error) {
  25. rows, err := d.get.Query(c)
  26. if err != nil {
  27. return
  28. }
  29. defer rows.Close()
  30. for rows.Next() {
  31. r := &manager.Relate{}
  32. 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 {
  33. return
  34. }
  35. r.Change()
  36. rs = append(rs, r)
  37. }
  38. return
  39. }
  40. // Close close db resource.
  41. func (d *Dao) Close() {
  42. if d.db != nil {
  43. d.db.Close()
  44. }
  45. }