application_order.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 TApplicationOrder struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. Status int32 `gorm:"column:status" json:"status"`
  14. Amount int64 `gorm:"column:amount" json:"amount"`
  15. Period int64 `gorm:"column:period" json:"period"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. Cid int64 `gorm:"column:cid" json:"cid"`
  19. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  20. Feedback string `gorm:"column:feedback" json:"feedback"`
  21. PayType int32 `gorm:"column:pay_type" json:"pay_type"`
  22. ApplicationContent string `gorm:"column:application_content" json:"application_content"`
  23. ApplicationId int64 `gorm:"column:application_id" json:"application_id"`
  24. }
  25. func (p *TApplicationOrder) TableName() string {
  26. return "t_application_order"
  27. }
  28. func (p *TApplicationOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  29. err := db.Table(p.TableName()).Where(where).Find(p).Error
  30. if err != nil {
  31. fields, _ := util.MarshalToString(where)
  32. logger.Error("mysql",
  33. zap.String("sql", "select from "+p.TableName()),
  34. zap.String("fields", fields),
  35. zap.String("error", err.Error()))
  36. }
  37. return err
  38. }
  39. func (p *TApplicationOrder) Last(db *gorm.DB) error {
  40. err := db.Table(p.TableName()).Last(p).Error
  41. if err != nil {
  42. logger.Error("mysql",
  43. zap.String("sql", "select last from "+p.TableName()),
  44. zap.String("fields", ""),
  45. zap.String("error", err.Error()))
  46. }
  47. return err
  48. }
  49. // Insert 插入一条记录
  50. func (p *TApplicationOrder) Insert(db *gorm.DB) error {
  51. err := db.Create(p).Error
  52. if err != nil {
  53. fields, _ := util.MarshalToString(*p)
  54. logger.Error("mysql",
  55. zap.String("sql", "insert into "+p.TableName()),
  56. zap.String("fields", fields),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *TApplicationOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  62. cond, val, err := whereBuild(filter)
  63. if err != nil {
  64. return err
  65. }
  66. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  67. }
  68. func (p *TApplicationOrder) 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 *TApplicationOrder) UpdateByModel(db *gorm.DB) error {
  83. err := db.Model(p).Updates(p).Error
  84. if err != nil {
  85. fields, _ := util.MarshalToString(*p)
  86. logger.Error("mysql",
  87. zap.String("sql", "update "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return err
  92. }
  93. func (p *TApplicationOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  94. cond, val, err := whereBuildAndOr(where, or)
  95. if err != nil {
  96. return 0, err
  97. }
  98. ret := int64(0)
  99. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  100. if err != nil {
  101. fields, _ := util.MarshalToString(where)
  102. logger.Error("mysql",
  103. zap.String("sql", "select count "+p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return ret, err
  108. }
  109. func (p *TApplicationOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TApplicationOrder, err error) {
  110. cond, val, err := whereBuildAndOr(where, or)
  111. if err != nil {
  112. return list, err
  113. }
  114. if pageSize < 0 {
  115. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").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. }
  125. offset := (page - 1) * pageSize
  126. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  127. if result.Error != nil {
  128. wherefields, _ := util.MarshalToString(where)
  129. logger.Error("mysql",
  130. zap.String("sql", "select * from "+p.TableName()),
  131. zap.String("where", wherefields),
  132. zap.String("error", result.Error.Error()))
  133. }
  134. return list, result.Error
  135. }
  136. func (p *TApplicationOrder) ListColums(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, columns []string) (list []TApplicationOrder, err error) {
  137. cond, val, err := whereBuildAndOr(where, or)
  138. if err != nil {
  139. return list, err
  140. }
  141. result := db.Table(p.TableName()).Where(cond, val...).Select(columns).Find(&list)
  142. if result.Error != nil {
  143. wherefields, _ := util.MarshalToString(where)
  144. logger.Error("mysql",
  145. zap.String("sql", "select * from "+p.TableName()),
  146. zap.String("where", wherefields),
  147. zap.String("error", result.Error.Error()))
  148. }
  149. return list, result.Error
  150. }