msg_package.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 TMsgPackageOrder struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. MsgCount int64 `gorm:"column:msg_count" json:"msg_count"`
  14. Name string `gorm:"column:name" json:"name"`
  15. Comment string `gorm:"column:comment" json:"comment"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. PackageId int64 `gorm:"column:package_id" json:"package_id"`
  19. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  20. Price int64 `gorm:"column:price" json:"price"`
  21. PackageCount int64 `gorm:"column:package_count" json:"package_count"`
  22. Amount int64 `gorm:"column:amount" json:"amount"`
  23. Status int32 `gorm:"column:status" json:"status"`
  24. Feedback string `gorm:"column:feedback" json:"feedback"`
  25. MsgTotalCount int64 `gorm:"column:msg_total_count" json:"msg_total_count"`
  26. MsgAvailableCount int64 `gorm:"column:msg_available_count" json:"msg_available_count"`
  27. Cid int64 `gorm:"column:cid" json:"cid"`
  28. }
  29. func (p *TMsgPackageOrder) TableName() string {
  30. return "t_msg_package_order"
  31. }
  32. func (p *TMsgPackageOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  33. err := db.Table(p.TableName()).Where(where).Find(p).Error
  34. if err != nil {
  35. fields, _ := util.MarshalToString(where)
  36. logger.Error("mysql",
  37. zap.String("sql", "select from "+p.TableName()),
  38. zap.String("fields", fields),
  39. zap.String("error", err.Error()))
  40. }
  41. return err
  42. }
  43. func (p *TMsgPackageOrder) Last(db *gorm.DB) error {
  44. err := db.Table(p.TableName()).Last(p).Error
  45. if err != nil {
  46. logger.Error("mysql",
  47. zap.String("sql", "select last from "+p.TableName()),
  48. zap.String("fields", ""),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. // Insert 插入一条记录
  54. func (p *TMsgPackageOrder) Insert(db *gorm.DB) error {
  55. err := db.Create(p).Error
  56. if err != nil {
  57. fields, _ := util.MarshalToString(*p)
  58. logger.Error("mysql",
  59. zap.String("sql", "insert into "+p.TableName()),
  60. zap.String("fields", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TMsgPackageOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  66. cond, val, err := whereBuild(filter)
  67. if err != nil {
  68. return err
  69. }
  70. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  71. }
  72. func (p *TMsgPackageOrder) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. if err != nil {
  76. fields, _ := util.MarshalToString(values)
  77. logger.Error("mysql",
  78. zap.String("sql", "update "+p.TableName()),
  79. zap.String("fields", fields),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  85. }
  86. func (p *TMsgPackageOrder) UpdateByModel(db *gorm.DB) error {
  87. err := db.Model(p).Updates(p).Error
  88. if err != nil {
  89. fields, _ := util.MarshalToString(*p)
  90. logger.Error("mysql",
  91. zap.String("sql", "update "+p.TableName()),
  92. zap.String("fields", fields),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. func (p *TMsgPackageOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  98. cond, val, err := whereBuildAndOr(where, or)
  99. if err != nil {
  100. return 0, err
  101. }
  102. ret := int64(0)
  103. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  104. if err != nil {
  105. fields, _ := util.MarshalToString(where)
  106. logger.Error("mysql",
  107. zap.String("sql", "select count "+p.TableName()),
  108. zap.String("fields", fields),
  109. zap.String("error", err.Error()))
  110. }
  111. return ret, err
  112. }
  113. func (p *TMsgPackageOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int, order string) (list []TMsgPackageOrder, err error) {
  114. cond, val, err := whereBuildAndOr(where, or)
  115. if err != nil {
  116. return list, err
  117. }
  118. if pageSize < 0 {
  119. result := db.Table(p.TableName()).Where(cond, val...).Order(order).Find(&list)
  120. if result.Error != nil {
  121. wherefields, _ := util.MarshalToString(where)
  122. logger.Error("mysql",
  123. zap.String("sql", "select * from "+p.TableName()),
  124. zap.String("where", wherefields),
  125. zap.String("error", result.Error.Error()))
  126. }
  127. return list, result.Error
  128. }
  129. offset := (page - 1) * pageSize
  130. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order(order).Find(&list)
  131. if result.Error != nil {
  132. wherefields, _ := util.MarshalToString(where)
  133. logger.Error("mysql",
  134. zap.String("sql", "select * from "+p.TableName()),
  135. zap.String("where", wherefields),
  136. zap.String("error", result.Error.Error()))
  137. }
  138. return list, result.Error
  139. }
  140. func (p *TMsgPackageOrder) ListColums(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, columns []string) (list []TMsgPackageOrder, err error) {
  141. cond, val, err := whereBuildAndOr(where, or)
  142. if err != nil {
  143. return list, err
  144. }
  145. result := db.Table(p.TableName()).Where(cond, val...).Select(columns).Find(&list)
  146. if result.Error != nil {
  147. wherefields, _ := util.MarshalToString(where)
  148. logger.Error("mysql",
  149. zap.String("sql", "select * from "+p.TableName()),
  150. zap.String("where", wherefields),
  151. zap.String("error", result.Error.Error()))
  152. }
  153. return list, result.Error
  154. }