advertise.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package tables
  2. import (
  3. "Wine-Server/utils"
  4. "database/sql"
  5. )
  6. type AdvertiseTable struct {
  7. Id uint32 `json:"id"`
  8. Order uint8 `json:"order"`
  9. Time utils.TimeType `json:"time"`
  10. Src string `json:"src"`
  11. Type bool `json:"type"` // true: pic, false: video
  12. Duration uint16 `json:"duration"` // ms
  13. Describe string `json:"describe"`
  14. }
  15. func CreateAdvertiseTable() error {
  16. SQL := "CREATE TABLE IF NOT EXISTS `advertise`(" +
  17. "`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
  18. "`order` TINYINT UNSIGNED NOT NULL," +
  19. "`time` DATETIME DEFAULT CURRENT_TIMESTAMP," +
  20. "`type` BOOL DEFAULT TRUE," +
  21. "`src` VARCHAR(128) NOT NULL," +
  22. "`duration` SMALLINT UNSIGNED DEFAULT 3000," +
  23. "`describe` VARCHAR(64) DEFAULT '');"
  24. _, err := utils.Mysql.Exec(SQL)
  25. return err
  26. }
  27. func (row *AdvertiseTable) Insert() error {
  28. SQL := "INSERT INTO `advertise`(`order`,`type`,`src`,`duration`,`describe`) 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, row.Describe)
  34. return err
  35. }
  36. func (row *AdvertiseTable) Delete() error {
  37. pre, err := utils.Mysql.Prepare("DELETE FROM `advertise` WHERE `id`=?;")
  38. if err != nil {
  39. return err
  40. }
  41. _, err = pre.Exec(row.Id)
  42. return err
  43. }
  44. func (row *AdvertiseTable) Update(args utils.JsonType) error {
  45. keys, values := utils.UnZip(args)
  46. SQL := utils.Format("UPDATE `advertise` SET %s WHERE `id`=%d;", utils.SqlFields(keys), row.Id)
  47. pre, err := utils.Mysql.Prepare(SQL)
  48. if err != nil {
  49. return err
  50. }
  51. _, err = pre.Exec(values...)
  52. return err
  53. }
  54. func (row *AdvertiseTable) UpdateSelf() error {
  55. SQL := "UPDATE `advertise` SET `order`=?,`src`=?,`type`=?,`duration`=?,`describe`=? WHERE `id`=?;"
  56. pre, err := utils.Mysql.Prepare(SQL)
  57. if err != nil {
  58. return err
  59. }
  60. _, err = pre.Exec(row.Order, row.Src, row.Type, row.Duration, row.Describe, row.Id)
  61. return err
  62. }
  63. func (row *AdvertiseTable) Get() error {
  64. SQL := "SELECT `order`,`time`,`src`,`type`,`duration`,`describe` FROM `advertise` WHERE `id`=?;"
  65. pre, err := utils.Mysql.Prepare(SQL)
  66. if err != nil {
  67. return err
  68. }
  69. err = pre.QueryRow(row.Id).Scan(&row.Order, &row.Time, &row.Src, &row.Type, &row.Duration, &row.Describe)
  70. return err
  71. }
  72. func AdvertiseListAll() ([]AdvertiseTable, error) {
  73. var res []AdvertiseTable
  74. SQL := "SELECT `id`,`order`,`time`,`src`,`type`,`duration`,`describe` FROM `advertise` ORDER BY `order`;"
  75. query, err := utils.Mysql.Query(SQL)
  76. if err != nil {
  77. return nil, err
  78. }
  79. for query.Next() {
  80. var one AdvertiseTable
  81. err = query.Scan(&one.Id, &one.Order, &one.Time, &one.Src, &one.Type, &one.Duration, &one.Describe)
  82. if err != nil {
  83. return nil, err
  84. }
  85. res = append(res, one)
  86. }
  87. return res, nil
  88. }
  89. func AdvertisesQuery(cond string, kind, limit, page int) (int, []AdvertiseTable, error) {
  90. SQL := "none"
  91. if kind == -1 {
  92. SQL = utils.Format("SELECT COUNT(`id`) AS `total` FROM `advertise` WHERE `describe` LIKE '%%%s%%';", cond)
  93. } else {
  94. SQL = utils.Format(
  95. "SELECT COUNT(`id`) AS `total` FROM `advertise` WHERE `type`=%d AND `describe` LIKE '%%%s%%';", kind, cond,
  96. )
  97. }
  98. total := 0
  99. err := utils.Mysql.QueryRow(SQL).Scan(&total)
  100. if err != nil {
  101. return 0, nil, err
  102. }
  103. if total == 0 {
  104. return 0, []AdvertiseTable{}, err
  105. }
  106. if kind == -1 {
  107. SQL = utils.Format(
  108. "SELECT `id`,`order`,`time`,`type`,`src`,`duration`,`describe` FROM `advertise` "+
  109. "WHERE `describe` LIKE '%%%s%%' LIMIT %d OFFSET %d;", cond, limit, (page-1)*limit,
  110. )
  111. } else {
  112. SQL = utils.Format(
  113. "SELECT `id`,`order`,`time`,`type`,`src`,`duration`,`describe` FROM `advertise` "+
  114. "WHERE `type`=%d AND `describe` LIKE '%%%s%%' LIMIT %d OFFSET %d;", kind, cond, limit, (page-1)*limit,
  115. )
  116. }
  117. var rows *sql.Rows
  118. res := make([]AdvertiseTable, 0)
  119. rows, err = utils.Mysql.Query(SQL)
  120. if err != nil {
  121. return 0, nil, err
  122. }
  123. for rows.Next() {
  124. tmp := AdvertiseTable{}
  125. _ = rows.Scan(&tmp.Id, &tmp.Order, &tmp.Time, &tmp.Type, &tmp.Src, &tmp.Duration, &tmp.Describe)
  126. res = append(res, tmp)
  127. }
  128. _ = rows.Close()
  129. return total, res, nil
  130. }
  131. func AdvertiseQueryMaxId() (uint32, error) {
  132. SQL := "SELECT MAX(`id`) FROM `advertise`;"
  133. var max uint32
  134. err := utils.Mysql.QueryRow(SQL).Scan(&max)
  135. if err != nil {
  136. return 0, err
  137. }
  138. return max, nil
  139. }