trans_records.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package mysql
  2. import (
  3. orm "duoduo/database"
  4. "duoduo/tools"
  5. "github.com/shopspring/decimal"
  6. )
  7. type TransRecords struct {
  8. Amount decimal.Decimal `gorm:"column:amount" json:"amount"`
  9. CreateTime string `gorm:"column:create_time" json:"createTime"`
  10. OpenID string `gorm:"column:open_id" json:"openId"`
  11. TransStatus int `gorm:"column:trans_status" json:"transStatus"`
  12. UpdateTime string `gorm:"column:update_time" json:"updateTime"`
  13. Pid string `gorm:"column:pid" json:"pid"`
  14. PidStatus int `gorm:"column:pid_status" json:"pid_status"`
  15. Pay string `gorm:"column:pay" json:"pay"`
  16. UserId int `gorm:"column:user_id" json:"userId"`
  17. PayName string `gorm:"column:pay_name" json:"payName"`
  18. Id int64 `gorm:"column:id" json:"id"`
  19. }
  20. // TableName sets the insert table name for this struct type
  21. func (t *TransRecords) TableName() string {
  22. return "trans_records"
  23. }
  24. func (t *TransRecords) Create() (TransRecords, error) {
  25. var doc TransRecords
  26. result := orm.Eloquent.Table(t.TableName()).Create(&t)
  27. if result.Error != nil {
  28. err := result.Error
  29. return doc, err
  30. }
  31. doc = *t
  32. return doc, nil
  33. }
  34. func (t *TransRecords) GetAmount() (TransRecords, error) {
  35. var trans TransRecords
  36. //可体现金额:5求和-已体现金额
  37. //已体现金额:提现记录求和
  38. //历史总收益
  39. err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = ? ", t.OpenID, t.TransStatus).Table(t.TableName()).First(&trans).Error
  40. if err != nil {
  41. return trans, err
  42. }
  43. return trans, nil
  44. }
  45. func (t *TransRecords) GetTransNum() int64 {
  46. var count int64
  47. tableCount := orm.Eloquent.Table(t.TableName()).Where("open_id = ? and trans_status = ?", t.OpenID, t.TransStatus)
  48. tableCount.Count(&count)
  49. return count
  50. }
  51. func (t *TransRecords) GetTransSum() (TransRecords, error) {
  52. var trans TransRecords
  53. err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = ? and pid = ? and pid_status = ?", t.OpenID, t.TransStatus, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
  54. if err != nil {
  55. return trans, err
  56. }
  57. return trans, nil
  58. }
  59. //
  60. func (t *TransRecords) GetTrans() (TransRecords, error) {
  61. var trans TransRecords
  62. err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status in (1,3) and pid = ? and pid_status = ?", t.OpenID, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
  63. if err != nil {
  64. return trans, err
  65. }
  66. return trans, nil
  67. }
  68. //已发放金额求和
  69. func (t *TransRecords) GetTransStatus() (TransRecords, error) {
  70. var trans TransRecords
  71. err := orm.Eloquent.Select("SUM(amount) as amount").Where("open_id = ? and trans_status = 3 and pid = ? and pid_status = ?", t.OpenID, t.Pid, t.PidStatus).Table(t.TableName()).First(&trans).Error
  72. if err != nil {
  73. return trans, err
  74. }
  75. return trans, nil
  76. }
  77. func (t *TransRecords) Update(id int) (update TransRecords, err error) {
  78. if err := orm.Eloquent.Table(t.TableName()).Model(&update).Where("id = ? ", id).Updates(
  79. map[string]interface{}{
  80. "trans_status": t.TransStatus,
  81. "pay": t.Pay,
  82. "pay_name": t.PayName,
  83. "user_id": t.UserId,
  84. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  85. return update, err
  86. }
  87. return update, nil
  88. }
  89. //list 接口使用
  90. func (t *TransRecords) GetTransList(pageSize int, pageIndex int) ([]TransRecords, int, error) {
  91. var doc []TransRecords
  92. table := orm.Eloquent.Table(t.TableName())
  93. if t.OpenID != "" {
  94. table = table.Where("open_id = ? ", t.OpenID)
  95. }
  96. if t.PidStatus != 0 {
  97. table = table.Where("pid_status = ? ", t.PidStatus)
  98. }
  99. var count int
  100. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  101. return nil, 0, err
  102. }
  103. table.Count(&count)
  104. return doc, count, nil
  105. }