announcement_read.go 5.8 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 TAnnouncementRead struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. AnnouncementId int64 `gorm:"column:announcement_id" json:"announcement_id"`
  15. Uid int64 `gorm:"column:uid" json:"uid"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. table string
  19. }
  20. func (p *TAnnouncementRead) TableName() string {
  21. return p.table
  22. }
  23. func NewAnnouncementRead(database string) *TAnnouncementRead {
  24. return &TAnnouncementRead{table: fmt.Sprintf("%s.%s", database, "t_announcement_read")}
  25. }
  26. func (p *TAnnouncementRead) SetTable(database string) {
  27. p.table = fmt.Sprintf("%s.%s", database, "t_announcement_read")
  28. }
  29. func (p *TAnnouncementRead) CreateTable(db *gorm.DB) error {
  30. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  31. "`id` int(11) NOT NULL AUTO_INCREMENT," +
  32. "`announcement_id` int(11) NOT NULL," +
  33. "`uid` bigint(20) NOT NULL," +
  34. "`created_at` datetime NOT NULL," +
  35. "`updated_at` datetime NOT NULL," +
  36. "PRIMARY KEY (`id`)," +
  37. "UNIQUE KEY `announce` (`announcement_id`,`uid`) USING BTREE," +
  38. "KEY `uid` (`uid`) USING BTREE" +
  39. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"
  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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) 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 *TAnnouncementRead) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TAnnouncementRead, 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. }