neighbor_article.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 TNeighborArticle 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. Pics string `gorm:"column:pics" json:"pics"`
  17. Uid int64 `gorm:"column:uid" json:"uid"`
  18. ClassId int64 `gorm:"column:class_id" json:"class_id"`
  19. Likes int64 `gorm:"column:likes" json:"likes"`
  20. NickName string `gorm:"column:nick_name" json:"nick_name"`
  21. CommentCount int64 `gorm:"column:comment_count" json:"comment_count"`
  22. UserIcon string `gorm:"column:user_icon" json:"user_icon"`
  23. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  24. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  25. table string
  26. }
  27. func (p *TNeighborArticle) TableName() string {
  28. return p.table
  29. }
  30. func NewNeighborArticle(database string) *TNeighborArticle {
  31. return &TNeighborArticle{table: fmt.Sprintf("%s.%s", database, "t_neighbor_article")}
  32. }
  33. func (p *TNeighborArticle) SetTable(database string) {
  34. p.table = fmt.Sprintf("%s.%s", database, "t_neighbor_article")
  35. }
  36. func (p *TNeighborArticle) CreateTable(db *gorm.DB) error {
  37. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  38. "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
  39. "`title` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
  40. "`content` text COLLATE utf8mb4_bin NOT NULL," +
  41. "`pics` text COLLATE utf8mb4_bin," +
  42. "`uid` bigint(11) NOT NULL," +
  43. "`class_id` bigint(11) NOT NULL," +
  44. "`likes` int(10) NOT NULL," +
  45. "`comment_count` int(10) NOT NULL," +
  46. "`created_at` datetime NOT NULL," +
  47. "`updated_at` datetime NOT NULL," +
  48. "`user_icon` varchar(255) COLLATE utf8mb4_bin NOT NULL," +
  49. " `nick_name` varchar(255) COLLATE utf8mb4_bin NOT NULL," +
  50. "PRIMARY KEY (`id`)," +
  51. "KEY `class_id` (`class_id`) USING BTREE," +
  52. "KEY `uid` (`uid`) USING BTREE," +
  53. "KEY `title` (`title`) USING BTREE" +
  54. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  55. err := db.Exec(sql).Error
  56. if err != nil {
  57. logger.Error("mysql",
  58. zap.String("sql", "create table "+p.TableName()),
  59. zap.String("fields", ""),
  60. zap.String("error", err.Error()))
  61. }
  62. return err
  63. }
  64. func (p *TNeighborArticle) Find(db *gorm.DB, where map[string]interface{}) error {
  65. err := db.Table(p.TableName()).Where(where).First(p).Error
  66. if err != nil {
  67. fields, _ := util.MarshalToString(where)
  68. logger.Error("mysql",
  69. zap.String("sql", "select from "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. func (p *TNeighborArticle) Last(db *gorm.DB) error {
  76. err := db.Table(p.TableName()).Last(p).Error
  77. if err != nil {
  78. logger.Error("mysql",
  79. zap.String("sql", "select last from "+p.TableName()),
  80. zap.String("fields", ""),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. // Insert 插入一条记录
  86. func (p *TNeighborArticle) Insert(db *gorm.DB) error {
  87. err := db.Table(p.TableName()).Create(p).Error
  88. if err != nil {
  89. fields, _ := util.MarshalToString(*p)
  90. logger.Error("mysql",
  91. zap.String("sql", "insert into "+p.TableName()),
  92. zap.String("fields", fields),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. // Insert 插入多条记录
  98. func (p *TNeighborArticle) InsertMulti(db *gorm.DB, values interface{}) error {
  99. err := db.Table(p.TableName()).Create(values).Error
  100. if err != nil {
  101. fields, _ := util.MarshalToString(*p)
  102. logger.Error("mysql",
  103. zap.String("sql", "insert into "+p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return err
  108. }
  109. func (p *TNeighborArticle) Delete(db *gorm.DB, filter map[string]interface{}) error {
  110. cond, val, err := whereBuild(filter)
  111. if err != nil {
  112. return err
  113. }
  114. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  115. }
  116. func (p *TNeighborArticle) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  117. cond, val, err := whereBuild(where)
  118. if err != nil {
  119. if err != nil {
  120. fields, _ := util.MarshalToString(values)
  121. logger.Error("mysql",
  122. zap.String("sql", "update "+p.TableName()),
  123. zap.String("fields", fields),
  124. zap.String("error", err.Error()))
  125. }
  126. return err
  127. }
  128. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  129. }
  130. func (p *TNeighborArticle) UpdateByModel(db *gorm.DB) error {
  131. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  132. if err != nil {
  133. fields, _ := util.MarshalToString(*p)
  134. logger.Error("mysql",
  135. zap.String("sql", "update "+p.TableName()),
  136. zap.String("fields", fields),
  137. zap.String("error", err.Error()))
  138. }
  139. return err
  140. }
  141. func (p *TNeighborArticle) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  142. cond, val, err := whereBuildAndOr(where, or)
  143. if err != nil {
  144. return 0, err
  145. }
  146. ret := int64(0)
  147. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  148. if err != nil {
  149. fields, _ := util.MarshalToString(where)
  150. logger.Error("mysql",
  151. zap.String("sql", "select count "+p.TableName()),
  152. zap.String("fields", fields),
  153. zap.String("error", err.Error()))
  154. }
  155. return ret, err
  156. }
  157. func (p *TNeighborArticle) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TNeighborArticle, err error) {
  158. cond, val, err := whereBuildAndOr(where, or)
  159. if err != nil {
  160. return list, err
  161. }
  162. if pageSize < 0 {
  163. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  164. if result.Error != nil {
  165. wherefields, _ := util.MarshalToString(where)
  166. logger.Error("mysql",
  167. zap.String("sql", "select * from "+p.TableName()),
  168. zap.String("where", wherefields),
  169. zap.String("error", result.Error.Error()))
  170. }
  171. return list, result.Error
  172. }
  173. offset := (page - 1) * pageSize
  174. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  175. if result.Error != nil {
  176. wherefields, _ := util.MarshalToString(where)
  177. logger.Error("mysql",
  178. zap.String("sql", "select * from "+p.TableName()),
  179. zap.String("where", wherefields),
  180. zap.String("error", result.Error.Error()))
  181. }
  182. return list, result.Error
  183. }