jt_content.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TJtContent struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. Title string `gorm:"column:title" json:"title"`
  14. FirstPics string `gorm:"column:first_pics" json:"first_pics"`
  15. Content string `gorm:"column:content" json:"content"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. PublishStatus int32 `gorm:"column:publish_status" json:"publish_status"`
  19. table string
  20. }
  21. func NewJtContent(table string) *TJtContent {
  22. return &TJtContent{table: table}
  23. }
  24. func (p *TJtContent) TableName() string {
  25. return p.table
  26. }
  27. func (p *TJtContent) SetTable(table string) {
  28. p.table = table
  29. }
  30. func (p *TJtContent) Find(db *gorm.DB, where map[string]interface{}) error {
  31. err := db.Table(p.TableName()).Where(where).First(p).Error
  32. if err != nil {
  33. fields, _ := util.MarshalToString(where)
  34. logger.Error("mysql",
  35. zap.String("sql", "select from "+p.TableName()),
  36. zap.String("fields", fields),
  37. zap.String("error", err.Error()))
  38. }
  39. return err
  40. }
  41. func (p *TJtContent) Last(db *gorm.DB) error {
  42. err := db.Table(p.TableName()).Last(p).Error
  43. if err != nil {
  44. logger.Error("mysql",
  45. zap.String("sql", "select last from "+p.TableName()),
  46. zap.String("fields", ""),
  47. zap.String("error", err.Error()))
  48. }
  49. return err
  50. }
  51. // Insert 插入一条记录
  52. func (p *TJtContent) Insert(db *gorm.DB) error {
  53. err := db.Table(p.TableName()).Create(p).Error
  54. if err != nil {
  55. fields, _ := util.MarshalToString(*p)
  56. logger.Error("mysql",
  57. zap.String("sql", "insert into "+p.TableName()),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *TJtContent) Delete(db *gorm.DB, filter map[string]interface{}) error {
  64. cond, val, err := whereBuild(filter)
  65. if err != nil {
  66. return err
  67. }
  68. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  69. if err != nil {
  70. fields, _ := util.MarshalToString(filter)
  71. logger.Error("mysql",
  72. zap.String("sql", "delete from "+p.TableName()),
  73. zap.String("where", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. func (p *TJtContent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  79. cond, val, err := whereBuild(where)
  80. if err != nil {
  81. if err != nil {
  82. fields, _ := util.MarshalToString(values)
  83. logger.Error("mysql",
  84. zap.String("sql", "update "+p.TableName()),
  85. zap.String("fields", fields),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  91. }
  92. func (p *TJtContent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  93. cond, val, err := whereBuildAndOr(where, or)
  94. if err != nil {
  95. return 0, err
  96. }
  97. ret := int64(0)
  98. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  99. if err != nil {
  100. fields, _ := util.MarshalToString(where)
  101. logger.Error("mysql",
  102. zap.String("sql", "select count "+p.TableName()),
  103. zap.String("fields", fields),
  104. zap.String("error", err.Error()))
  105. }
  106. return ret, err
  107. }
  108. func (p *TJtContent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TJtContent, err error) {
  109. cond, val, err := whereBuildAndOr(where, or)
  110. if err != nil {
  111. return list, err
  112. }
  113. if pageSize < 0 {
  114. result := db.Table(p.TableName()).Where(cond, val...).Order("updated_at desc").Find(&list)
  115. if result.Error != nil {
  116. wherefields, _ := util.MarshalToString(where)
  117. logger.Error("mysql",
  118. zap.String("sql", "select * from "+p.TableName()),
  119. zap.String("where", wherefields),
  120. zap.String("error", result.Error.Error()))
  121. }
  122. return list, result.Error
  123. }
  124. offset := (page - 1) * pageSize
  125. result := db.Table(p.TableName()).Where(cond, val...).Order("updated_at desc").Limit(pageSize).Offset(offset).Find(&list)
  126. if result.Error != nil {
  127. wherefields, _ := util.MarshalToString(where)
  128. logger.Error("mysql",
  129. zap.String("sql", "select * from "+p.TableName()),
  130. zap.String("where", wherefields),
  131. zap.String("error", result.Error.Error()))
  132. }
  133. return list, result.Error
  134. }