dao.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package sidebar
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/app-resource/conf"
  7. "go-common/app/interface/main/app-resource/model/sidebar"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _initSidebarKey = "sidebar_%d_%d"
  13. _selSideSQL = `SELECT s.id,s.plat,s.module,s.name,s.logo,s.logo_white,s.param,s.rank,l.build,l.conditions,s.tip FROM
  14. sidebar AS s,sidebar_limit AS l WHERE s.state=1 AND s.id=l.s_id AND s.online_time<? ORDER BY s.rank DESC,l.id ASC`
  15. )
  16. type Dao struct {
  17. db *xsql.DB
  18. get *xsql.Stmt
  19. }
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. db: xsql.NewMySQL(c.MySQL.Show),
  23. }
  24. d.get = d.db.Prepared(_selSideSQL)
  25. return
  26. }
  27. // SideBar
  28. func (d *Dao) SideBar(ctx context.Context, now time.Time) (ss map[string][]*sidebar.SideBar, limits map[int64][]*sidebar.Limit, err error) {
  29. ss = map[string][]*sidebar.SideBar{}
  30. limits = map[int64][]*sidebar.Limit{}
  31. rows, err := d.get.Query(ctx, now)
  32. if err != nil {
  33. log.Error("mysqlDB.Query error(%v)", err)
  34. return
  35. }
  36. defer rows.Close()
  37. for rows.Next() {
  38. s := &sidebar.SideBar{}
  39. if err = rows.Scan(&s.ID, &s.Plat, &s.Module, &s.Name, &s.Logo, &s.LogoWhite, &s.Param, &s.Rank, &s.Build, &s.Conditions, &s.Tip); err != nil {
  40. log.Error("row.Scan error(%v)", err)
  41. return
  42. }
  43. key := fmt.Sprintf(_initSidebarKey, s.Plat, s.Module)
  44. if _, ok := limits[s.ID]; !ok {
  45. ss[key] = append(ss[key], s)
  46. }
  47. limit := &sidebar.Limit{
  48. ID: s.ID,
  49. Build: s.Build,
  50. Condition: s.Conditions,
  51. }
  52. limits[s.ID] = append(limits[s.ID], limit)
  53. }
  54. return
  55. }
  56. func (d *Dao) Close() {
  57. if d.db != nil {
  58. d.db.Close()
  59. }
  60. }