cash.out.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "errors"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. )
  8. type CashOut struct {
  9. ID int64 `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // Id
  10. OpenID string `gorm:"column:open_id;type:varchar(255)" json:"open_id"` // Openid
  11. AppID string `gorm:"column:app_id;type:varchar(255)" json:"app_id"` // Appid
  12. Status int `gorm:"column:status;type:int(11)" json:"status"` // 0-待提现 1-提现中 2-提现失败 3-钱已到账待账户扣减 99-提现成功
  13. FailRes string `gorm:"column:fail_res;type:varchar(255)" json:"fail_res"` // 失败原因
  14. PartnerTradeNo string `gorm:"column:partner_trade_no;type:varchar(50)" json:"partner_trade_no"` // 商户卡提现id
  15. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 金额
  16. CornTime time.Time `gorm:"column:corn_time;type:datetime;default:null" json:"corn_time"` // 定时任务下次查询状态时间
  17. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  18. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  19. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  20. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  21. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  22. Fee decimal.Decimal `gorm:"column:fee;type:decimal(10,2)" json:"fee"` // 手续费
  23. WxPartnerTradeNo string `gorm:"column:wx_partner_trade_no;type:varchar(50)" json:"wx_partner_trade_no"` // wx商户卡提现id
  24. AccountStatus int32 `gorm:"column:account_status;default:0;comment:'分账状态 99-分账成功 2-分账失败 0-未分账 3-分账中'" json:"account_status"`
  25. AccountFailRes string `gorm:"column:account_fail_res;type:varchar(255);comment:分账失败原因" json:"account_fail_res"`
  26. }
  27. func (m *CashOut) TableName() string {
  28. return "cash_out"
  29. }
  30. func (m *CashOut) GetCashOutByStatusNum(status []int) int {
  31. var count int
  32. table := orm.ShMysql.Table(m.TableName())
  33. table = table.Where("open_id = ? and status in (?) and app_id = ? ", m.OpenID, status, m.AppID)
  34. table.Count(&count)
  35. return count
  36. }
  37. func (u *CashOut) Create() (CashOut, error) {
  38. var doc CashOut
  39. var err error
  40. doc = *u
  41. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  42. if err != nil {
  43. return doc, err
  44. }
  45. return doc, nil
  46. }
  47. func (m *CashOut) UpdateMerchantStatus() error {
  48. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  49. map[string]interface{}{
  50. "status": m.Status,
  51. "fail_res": m.FailRes,
  52. "updated_at": time.Now()}).Error; err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func (m *CashOut) UpdateCashOutWxBachNo() error {
  58. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("partner_trade_no = ? ", m.PartnerTradeNo).Updates(
  59. map[string]interface{}{
  60. "wx_partner_trade_no": m.WxPartnerTradeNo,
  61. "updated_at": time.Now()}).Error; err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. func (m *CashOut) UpdateCashOut() error {
  67. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("partner_trade_no = ? ", m.PartnerTradeNo).Updates(
  68. map[string]interface{}{
  69. "wx_partner_trade_no": m.WxPartnerTradeNo,
  70. "updated_at": time.Now()}).Error; err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func (m *CashOut) UpdateCashOutStatus() error {
  76. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("partner_trade_no = ? ", m.PartnerTradeNo).Updates(
  77. map[string]interface{}{
  78. "status": m.Status,
  79. "updated_at": time.Now()}).Error; err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func (m *CashOut) UpdateCashOutStatusClose() error {
  85. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("partner_trade_no = ? ", m.PartnerTradeNo).Updates(
  86. map[string]interface{}{
  87. "status": m.Status,
  88. "fail_res": m.FailRes,
  89. "updated_at": time.Now()}).Error; err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. func (m *CashOut) GetCashOutByStatus() (CashOut, error) {
  95. var doc CashOut
  96. table := orm.ShMysql.Table(m.TableName())
  97. table = table.Where("status = ? ", m.Status)
  98. if err := table.Select("*").First(&doc).Error; err != nil {
  99. return doc, err
  100. }
  101. return doc, nil
  102. }
  103. func (m *CashOut) GetCashOutByAccount() (CashOut, error) {
  104. var doc CashOut
  105. table := orm.ShMysql.Table(m.TableName())
  106. table = table.Where("status = ? and account_status = ?", m.Status, m.AccountStatus)
  107. if err := table.Select("*").First(&doc).Error; err != nil {
  108. return doc, err
  109. }
  110. return doc, nil
  111. }
  112. func (m *CashOut) UpdateCashOutAccountStatus() error {
  113. reply := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  114. map[string]interface{}{
  115. "account_status": m.AccountStatus,
  116. "account_fail_res": m.AccountFailRes,
  117. "updated_at": time.Now()})
  118. if reply.Error != nil {
  119. return reply.Error
  120. }
  121. if reply.RowsAffected <= 0 {
  122. return errors.New("更新记录0条")
  123. }
  124. return nil
  125. }
  126. // list 接口使用
  127. func (m *CashOut) GetCashOutList(pageSize int, pageIndex int) ([]CashOut, int, error) {
  128. var doc []CashOut
  129. table := orm.ShMysql.Table(m.TableName())
  130. table = table.Where("open_id = ? and app_id = ? ", m.OpenID, m.AppID)
  131. var count int
  132. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  133. return nil, 0, err
  134. }
  135. table.Count(&count)
  136. return doc, count, nil
  137. }