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