advertise.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package tables
  2. import (
  3. "Wine-Server/utils"
  4. )
  5. type AdvertiseTable struct {
  6. Id uint32 `json:"id"`
  7. Order uint8 `json:"order"`
  8. Time utils.TimeType `json:"time"`
  9. Src string `json:"src"`
  10. Type bool `json:"type"` // true: pic, false: video
  11. Duration uint16 `json:"duration"` // ms
  12. }
  13. func CreateAdvertiseTable() error {
  14. sql := "CREATE TABLE IF NOT EXISTS `advertise`(" +
  15. "`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
  16. "`order` TINYINT UNSIGNED NOT NULL," +
  17. "`time` DATETIME DEFAULT CURRENT_TIMESTAMP," +
  18. "`type` BOOL DEFAULT TRUE," +
  19. "`src` VARCHAR(128) NOT NULL," +
  20. "`duration` SMALLINT UNSIGNED DEFAULT 3000);"
  21. _, err := utils.Mysql.Exec(sql)
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. func (row *AdvertiseTable) Insert() error {
  28. sql := "INSERT INTO `advertise`(`order`,`type`,`src`,`duration`) VALUES(?,?,?,?);"
  29. pre, err := utils.Mysql.Prepare(sql)
  30. if err != nil {
  31. return err
  32. }
  33. _, err = pre.Exec(row.Order, row.Type, row.Src, row.Duration)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func (row *AdvertiseTable) Delete() error {
  40. pre, err := utils.Mysql.Prepare("DELETE FROM `advertise` WHERE `id`=?;")
  41. if err != nil {
  42. return err
  43. }
  44. _, err = pre.Exec(row.Id)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func (row *AdvertiseTable) Update(args utils.JsonType) error {
  51. keys, values := utils.UnZip(args)
  52. sql := utils.Format("UPDATE `advertise` SET %s WHERE `id`=%d;", utils.SqlFields(keys), row.Id)
  53. pre, err := utils.Mysql.Prepare(sql)
  54. if err != nil {
  55. return err
  56. }
  57. _, err = pre.Exec(values...)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (row *AdvertiseTable) UpdateSelf() error {
  64. sql := "UPDATE `advertise` SET `order`=?,`src`=?,`type`=?,`duration`=? WHERE `id`=?;"
  65. pre, err := utils.Mysql.Prepare(sql)
  66. if err != nil {
  67. return err
  68. }
  69. _, err = pre.Exec(row.Order, row.Src, row.Type, row.Duration, row.Id)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func (row *AdvertiseTable) Get() error {
  76. sql := "SELECT `order`,`time`,`src`,`type`,`duration` FROM `advertise` WHERE `id`=?;"
  77. pre, err := utils.Mysql.Prepare(sql)
  78. if err != nil {
  79. return err
  80. }
  81. err = pre.QueryRow(row.Id).Scan(&row.Order, &row.Time, &row.Src, &row.Type, &row.Duration)
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func AdvListAll() ([]AdvertiseTable, error) {
  88. var res []AdvertiseTable
  89. query, err := utils.Mysql.Query("SELECT `id`,`order`,`time`,`src`,`type`,`duration` FROM `advertise` ORDER BY `order`;")
  90. if err != nil {
  91. return nil, err
  92. }
  93. for query.Next() {
  94. var one AdvertiseTable
  95. err = query.Scan(&one.Id, &one.Order, &one.Time, &one.Src, &one.Type, &one.Duration)
  96. if err != nil {
  97. return nil, err
  98. }
  99. res = append(res, one)
  100. }
  101. return res, nil
  102. }