jt_content.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.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. err := db.Where(filter).Delete(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(filter)
  67. logger.Error("mysql",
  68. zap.String("sql", "delete from "+p.TableName()),
  69. zap.String("where", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *TJtContent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  75. cond, val, err := whereBuild(where)
  76. if err != nil {
  77. if err != nil {
  78. fields, _ := util.MarshalToString(values)
  79. logger.Error("mysql",
  80. zap.String("sql", "update "+p.TableName()),
  81. zap.String("fields", fields),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  87. }
  88. func (p *TJtContent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  89. cond, val, err := whereBuildAndOr(where, or)
  90. if err != nil {
  91. return 0, err
  92. }
  93. ret := int64(0)
  94. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(where)
  97. logger.Error("mysql",
  98. zap.String("sql", "select count "+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return ret, err
  103. }
  104. func (p *TJtContent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TJtContent, err error) {
  105. cond, val, err := whereBuildAndOr(where, or)
  106. if err != nil {
  107. return list, err
  108. }
  109. if pageSize < 0 {
  110. result := db.Table(p.TableName()).Where(cond, val...).Order("updated_at desc").Find(&list)
  111. if result.Error != nil {
  112. wherefields, _ := util.MarshalToString(where)
  113. logger.Error("mysql",
  114. zap.String("sql", "select * from "+p.TableName()),
  115. zap.String("where", wherefields),
  116. zap.String("error", result.Error.Error()))
  117. }
  118. return list, result.Error
  119. }
  120. offset := (page - 1) * pageSize
  121. result := db.Table(p.TableName()).Where(cond, val...).Order("updated_at desc").Limit(pageSize).Offset(offset).Find(&list)
  122. if result.Error != nil {
  123. wherefields, _ := util.MarshalToString(where)
  124. logger.Error("mysql",
  125. zap.String("sql", "select * from "+p.TableName()),
  126. zap.String("where", wherefields),
  127. zap.String("error", result.Error.Error()))
  128. }
  129. return list, result.Error
  130. }