neighbor_like.go 5.8 KB

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