package_order.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TPackageOrder struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. PackageId int64 `gorm:"column:package_id" json:"package_id"`
  14. Status int32 `gorm:"column:status" json:"status"`
  15. Amount int64 `gorm:"column:amount" json:"amount"`
  16. Period int64 `gorm:"column:period" json:"period"`
  17. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  19. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  20. OrderType int64 `gorm:"column:order_type" json:"order_type"`
  21. Cid int64 `gorm:"column:cid" json:"cid"`
  22. CompanyName string `gorm:"column:company_name" json:"company_name"`
  23. Feedback string `gorm:"column:feedback" json:"feedback"`
  24. PayType int32 `gorm:"column:pay_type" json:"pay_type"`
  25. PackageContent string `gorm:"column:package_content" json:"package_content"`
  26. }
  27. func (p *TPackageOrder) TableName() string {
  28. return "t_package_order"
  29. }
  30. func (p *TPackageOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  31. err := db.Table(p.TableName()).Where(where).Find(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 *TPackageOrder) 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 *TPackageOrder) 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 *TPackageOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  64. cond, val, err := whereBuild(filter)
  65. if err != nil {
  66. return err
  67. }
  68. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  69. }
  70. func (p *TPackageOrder) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  71. cond, val, err := whereBuild(where)
  72. if err != nil {
  73. if err != nil {
  74. fields, _ := util.MarshalToString(values)
  75. logger.Error("mysql",
  76. zap.String("sql", "update "+p.TableName()),
  77. zap.String("fields", fields),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  83. }
  84. func (p *TPackageOrder) UpdateByModel(db *gorm.DB) error {
  85. err := db.Model(p).Updates(p).Error
  86. if err != nil {
  87. fields, _ := util.MarshalToString(*p)
  88. logger.Error("mysql",
  89. zap.String("sql", "update "+p.TableName()),
  90. zap.String("fields", fields),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. func (p *TPackageOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  96. cond, val, err := whereBuildAndOr(where, or)
  97. if err != nil {
  98. return 0, err
  99. }
  100. ret := int64(0)
  101. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  102. if err != nil {
  103. fields, _ := util.MarshalToString(where)
  104. logger.Error("mysql",
  105. zap.String("sql", "select count "+p.TableName()),
  106. zap.String("fields", fields),
  107. zap.String("error", err.Error()))
  108. }
  109. return ret, err
  110. }
  111. func (p *TPackageOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPackageOrder, err error) {
  112. cond, val, err := whereBuildAndOr(where, or)
  113. if err != nil {
  114. return list, err
  115. }
  116. if pageSize < 0 {
  117. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  118. if result.Error != nil {
  119. wherefields, _ := util.MarshalToString(where)
  120. logger.Error("mysql",
  121. zap.String("sql", "select * from "+p.TableName()),
  122. zap.String("where", wherefields),
  123. zap.String("error", result.Error.Error()))
  124. }
  125. return list, result.Error
  126. }
  127. offset := (page - 1) * pageSize
  128. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  129. if result.Error != nil {
  130. wherefields, _ := util.MarshalToString(where)
  131. logger.Error("mysql",
  132. zap.String("sql", "select * from "+p.TableName()),
  133. zap.String("where", wherefields),
  134. zap.String("error", result.Error.Error()))
  135. }
  136. return list, result.Error
  137. }
  138. func (p *TPackageOrder) ListColums(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, columns []string) (list []TPackageOrder, err error) {
  139. cond, val, err := whereBuildAndOr(where, or)
  140. if err != nil {
  141. return list, err
  142. }
  143. result := db.Table(p.TableName()).Where(cond, val...).Select(columns).Find(&list)
  144. if result.Error != nil {
  145. wherefields, _ := util.MarshalToString(where)
  146. logger.Error("mysql",
  147. zap.String("sql", "select * from "+p.TableName()),
  148. zap.String("where", wherefields),
  149. zap.String("error", result.Error.Error()))
  150. }
  151. return list, result.Error
  152. }