page_pics.go 3.9 KB

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