announcement.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TAnnouncement struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Title string `gorm:"column:title" json:"title"`
  15. Content string `gorm:"column:content" json:"content"`
  16. ReadCount int64 `gorm:"column:read_count" json:"read_count"`
  17. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  18. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  19. table string
  20. }
  21. func (p *TAnnouncement) TableName() string {
  22. return p.table
  23. }
  24. func NewAnnouncement(database string) *TAnnouncement {
  25. return &TAnnouncement{table: fmt.Sprintf("%s.%s", database, "t_announcement")}
  26. }
  27. func (p *TAnnouncement) SetTable(database string) {
  28. p.table = fmt.Sprintf("%s.%s", database, "t_announcement")
  29. }
  30. func (p *TAnnouncement) CreateTable(db *gorm.DB) error {
  31. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  32. "`id` int(11) NOT NULL AUTO_INCREMENT," +
  33. "`title` varchar(225) COLLATE utf8mb4_bin NOT NULL," +
  34. "`content` text COLLATE utf8mb4_bin NOT NULL," +
  35. "`created_at` datetime NOT NULL," +
  36. "`updated_at` datetime NOT NULL," +
  37. "`read_count` int(11) NOT NULL," +
  38. "PRIMARY KEY (`id`)" +
  39. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='公告';"
  40. err := db.Exec(sql).Error
  41. if err != nil {
  42. logger.Error("mysql",
  43. zap.String("sql", "create table "+p.TableName()),
  44. zap.String("fields", ""),
  45. zap.String("error", err.Error()))
  46. }
  47. return err
  48. }
  49. func (p *TAnnouncement) Find(db *gorm.DB, where map[string]interface{}) error {
  50. err := db.Table(p.TableName()).Where(where).Find(p).Error
  51. if err != nil {
  52. fields, _ := util.MarshalToString(where)
  53. logger.Error("mysql",
  54. zap.String("sql", "select from "+p.TableName()),
  55. zap.String("fields", fields),
  56. zap.String("error", err.Error()))
  57. }
  58. return err
  59. }
  60. func (p *TAnnouncement) Last(db *gorm.DB) error {
  61. err := db.Table(p.TableName()).Last(p).Error
  62. if err != nil {
  63. logger.Error("mysql",
  64. zap.String("sql", "select last from "+p.TableName()),
  65. zap.String("fields", ""),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. // Insert 插入一条记录
  71. func (p *TAnnouncement) Insert(db *gorm.DB) error {
  72. err := db.Table(p.TableName()).Create(p).Error
  73. if err != nil {
  74. fields, _ := util.MarshalToString(*p)
  75. logger.Error("mysql",
  76. zap.String("sql", "insert into "+p.TableName()),
  77. zap.String("fields", fields),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. // Insert 插入多条记录
  83. func (p *TAnnouncement) InsertMulti(db *gorm.DB, values interface{}) error {
  84. err := db.Table(p.TableName()).Create(values).Error
  85. if err != nil {
  86. fields, _ := util.MarshalToString(*p)
  87. logger.Error("mysql",
  88. zap.String("sql", "insert into "+p.TableName()),
  89. zap.String("fields", fields),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. func (p *TAnnouncement) Delete(db *gorm.DB, filter map[string]interface{}) error {
  95. cond, val, err := whereBuild(filter)
  96. if err != nil {
  97. return err
  98. }
  99. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  100. }
  101. func (p *TAnnouncement) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. fields, _ := util.MarshalToString(values)
  105. logger.Error("mysql",
  106. zap.String("sql", "update "+p.TableName()),
  107. zap.String("fields", fields),
  108. zap.String("error", err.Error()))
  109. return err
  110. }
  111. err = db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  112. if err != nil {
  113. fields, _ := util.MarshalToString(values)
  114. logger.Error("mysql",
  115. zap.String("sql", "update "+p.TableName()),
  116. zap.String("fields", fields),
  117. zap.String("error", err.Error()))
  118. }
  119. return err
  120. }
  121. func (p *TAnnouncement) UpdateByModel(db *gorm.DB) error {
  122. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  123. if err != nil {
  124. fields, _ := util.MarshalToString(*p)
  125. logger.Error("mysql",
  126. zap.String("sql", "update "+p.TableName()),
  127. zap.String("fields", fields),
  128. zap.String("error", err.Error()))
  129. }
  130. return err
  131. }
  132. func (p *TAnnouncement) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  133. cond, val, err := whereBuildAndOr(where, or)
  134. if err != nil {
  135. return 0, err
  136. }
  137. ret := int64(0)
  138. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  139. if err != nil {
  140. fields, _ := util.MarshalToString(where)
  141. logger.Error("mysql",
  142. zap.String("sql", "select count "+p.TableName()),
  143. zap.String("fields", fields),
  144. zap.String("error", err.Error()))
  145. }
  146. return ret, err
  147. }
  148. func (p *TAnnouncement) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TAnnouncement, err error) {
  149. cond, val, err := whereBuildAndOr(where, or)
  150. if err != nil {
  151. return list, err
  152. }
  153. if pageSize < 0 {
  154. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  155. if result.Error != nil {
  156. wherefields, _ := util.MarshalToString(where)
  157. logger.Error("mysql",
  158. zap.String("sql", "select * from "+p.TableName()),
  159. zap.String("where", wherefields),
  160. zap.String("error", result.Error.Error()))
  161. }
  162. return list, result.Error
  163. }
  164. offset := (page - 1) * pageSize
  165. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  166. if result.Error != nil {
  167. wherefields, _ := util.MarshalToString(where)
  168. logger.Error("mysql",
  169. zap.String("sql", "select * from "+p.TableName()),
  170. zap.String("where", wherefields),
  171. zap.String("error", result.Error.Error()))
  172. }
  173. return list, result.Error
  174. }