neighbor_comment.go 6.5 KB

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