splash.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package splash
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-resource/conf"
  5. "go-common/app/interface/main/app-resource/model/splash"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _actAllSQL = `SELECT i.splash_id,s.animate,s.duration,s.type,s.times,s.goto,s.param,s.skip,s.starttime,s.endtime,s.platform,i.url,i.hash,i.width,i.height,s.area,s.conditions,s.build,s.operate,s.no_preview FROM
  11. splash AS s, splash_image AS i WHERE s.id=i.splash_id AND s.publish=1 AND i.type=3 AND s.platform!=0 AND s.type!=2 AND s.type!=4 AND s.state=0 ORDER BY s.starttime DESC`
  12. _actBirthSQL = `SELECT i.splash_id,s.animate,s.duration,s.type,s.times,s.goto,s.param,s.skip,s.starttime,s.endtime,s.platform,i.url,i.hash,i.width,i.height,s.area,s.conditions,s.build,s.operate FROM
  13. splash AS s, splash_image AS i WHERE s.id=i.splash_id AND s.publish=1 AND i.type=3 AND s.platform!=0 AND s.type=2 AND s.state=0 ORDER BY s.starttime DESC`
  14. _actVipSQL = `SELECT i.splash_id,s.animate,s.duration,s.type,s.times,s.goto,s.param,s.skip,s.starttime,s.endtime,s.platform,i.url,i.hash,i.width,i.height,s.area,s.conditions,s.build,s.operate FROM
  15. splash AS s, splash_image AS i WHERE s.id=i.splash_id AND s.publish=1 AND i.type=3 AND s.platform!=0 AND s.type=4 AND s.state=0 ORDER BY s.starttime DESC`
  16. )
  17. // Dao is splash dao.
  18. type Dao struct {
  19. resdb *sql.DB
  20. // splash_active
  21. actAll *sql.Stmt
  22. actBirth *sql.Stmt
  23. actVip *sql.Stmt
  24. }
  25. // New new splash dao and return.
  26. func New(c *conf.Config) *Dao {
  27. d := &Dao{
  28. resdb: sql.NewMySQL(c.MySQL.Resource),
  29. }
  30. // splah_active
  31. d.actAll = d.resdb.Prepared(_actAllSQL)
  32. d.actBirth = d.resdb.Prepared(_actBirthSQL)
  33. d.actVip = d.resdb.Prepared(_actVipSQL)
  34. return d
  35. }
  36. // GetActiveAll get all splash from table splash_active.
  37. func (d *Dao) ActiveAll(ctx context.Context) (res []*splash.Splash, err error) {
  38. rows, err := d.actAll.Query(ctx)
  39. if err != nil {
  40. log.Error("dao.Exec(), err (%v)", err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. sp := &splash.Splash{}
  46. if err = rows.Scan(&sp.ID, &sp.Animate, &sp.Duration, &sp.Type, &sp.Times, &sp.Goto, &sp.Param, &sp.Skip, &sp.Start,
  47. &sp.End, &sp.Plat, &sp.Image, &sp.Hash, &sp.Width, &sp.Height, &sp.Area, &sp.Condition, &sp.Build, &sp.Operate, &sp.NoPreview); err != nil {
  48. log.Error("rows.Scan err (%v)", err)
  49. res = nil
  50. return
  51. }
  52. sp.PlatChange()
  53. if sp.Operate == 1 {
  54. res = append([]*splash.Splash{sp}, res...)
  55. } else {
  56. res = append(res, sp)
  57. }
  58. }
  59. return
  60. }
  61. // ActiveBirth from table splash and splash_image.
  62. func (d *Dao) ActiveBirth(ctx context.Context) (res []*splash.Splash, err error) {
  63. rows, err := d.actBirth.Query(ctx)
  64. if err != nil {
  65. log.Error("dao.Exec(), err (%v)", err)
  66. return
  67. }
  68. defer rows.Close()
  69. for rows.Next() {
  70. sp := &splash.Splash{}
  71. if err = rows.Scan(&sp.ID, &sp.Animate, &sp.Duration, &sp.Type, &sp.Times, &sp.Goto, &sp.Param, &sp.Skip, &sp.Start,
  72. &sp.End, &sp.Plat, &sp.Image, &sp.Hash, &sp.Width, &sp.Height, &sp.Area, &sp.Condition, &sp.Build, &sp.Operate); err != nil {
  73. log.Error("rows.Scan err (%v)", err)
  74. res = nil
  75. return
  76. }
  77. sp.PlatChange()
  78. sp.BirthDate()
  79. res = append(res, sp)
  80. }
  81. return
  82. }
  83. // ActiveVip form table vip splash
  84. func (d *Dao) ActiveVip(ctx context.Context) (res []*splash.Splash, err error) {
  85. rows, err := d.actVip.Query(ctx)
  86. if err != nil {
  87. log.Error("dao.Exec(), err (%v)", err)
  88. return
  89. }
  90. defer rows.Close()
  91. for rows.Next() {
  92. sp := &splash.Splash{}
  93. if err = rows.Scan(&sp.ID, &sp.Animate, &sp.Duration, &sp.Type, &sp.Times, &sp.Goto, &sp.Param, &sp.Skip, &sp.Start,
  94. &sp.End, &sp.Plat, &sp.Image, &sp.Hash, &sp.Width, &sp.Height, &sp.Area, &sp.Condition, &sp.Build, &sp.Operate); err != nil {
  95. log.Error("rows.Scan err (%v)", err)
  96. res = nil
  97. return
  98. }
  99. sp.PlatChange()
  100. res = append(res, sp)
  101. }
  102. return
  103. }
  104. // Close close memcache resource.
  105. func (dao *Dao) Close() {
  106. if dao.resdb != nil {
  107. dao.resdb.Close()
  108. }
  109. }