client.active.pay.trans.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "github.com/shopspring/decimal"
  5. "time"
  6. )
  7. type ClientActivePayTrans struct {
  8. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  9. RequestID string `gorm:"column:request_id;type:varchar(255)" json:"request_id"` // 请求id,幂等性
  10. OutTradeNo string `gorm:"column:out_trade_no;type:varchar(255)" json:"out_trade_no"` // 交易id
  11. ActiveConfigID int64 `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"` // 活动id
  12. ClientOpenID string `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"` // 客户端openid
  13. Status int `gorm:"column:status;type:int(11)" json:"status"` // 1-未支付 2-支付成功 3-取消支付 4-退款
  14. ThirdTradeNo string `gorm:"column:third_trade_no;type:varchar(255)" json:"third_trade_no"` // 微信交易id
  15. AccountStatus int `gorm:"column:account_status;type:int(11)" json:"account_status"` // 分账状态 99-分账成功 2-分账失败 1-未分账 3-分账中
  16. PayTime time.Time `gorm:"column:pay_time;type:datetime(3);default:null" json:"pay_time"` // 支付时间
  17. InvitationCode string `gorm:"column:invitation_code;type:varchar(25)" json:"invitation_code"` // 邀请码
  18. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 交易金额
  19. AccountErrLog string `gorm:"column:account_err_log;type:varchar(255)" json:"account_err_log"` // 分账err日志
  20. GroupBuyID int64 `gorm:"column:group_buy_id;type:bigint(20)" json:"group_buy_id"` // 团购id
  21. SeckillID int64 `gorm:"column:seckill_id;type:bigint(20)" json:"seckill_id"` // 秒杀id
  22. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  23. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  24. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  25. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  26. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  27. MerchantAmount decimal.Decimal `gorm:"column:merchant_amount;type:decimal(10,2)" json:"merchant_amount"` // 商家金额
  28. ClientAmount decimal.Decimal `gorm:"column:client_amount;type:decimal(10,2)" json:"client_amount"` // 客户分销金额
  29. MerchantOpenID string `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"` // 商家openid
  30. }
  31. const (
  32. ClientActivePayTransAccountStatusDef = 0
  33. ClientActivePayTransAccountStatusUnSettle = 1 //未分账
  34. ClientActivePayTransAccountStatusSettleFail = 2 //分账失败
  35. ClientActivePayTransAccountStatusSettlePending = 3 //分账中
  36. ClientActivePayTransAccountStatusSettleSuccess = 99 //分账成功
  37. )
  38. const (
  39. ClientActivePayTransStatusDef = 0
  40. ClientActivePayTransStatusUnPay = 1 // 未支付
  41. ClientActivePayTransStatusPaySuccess = 2 //支付成功
  42. ClientActivePayTransStatusPayCancel = 3 //取消支付
  43. ClientActivePayTransStatusRefund = 4 // 退款
  44. )
  45. const (
  46. YuanShiMa = "yuanshima" //
  47. )
  48. func (m *ClientActivePayTrans) TableName() string {
  49. return "client_active_pay_trans"
  50. }
  51. func (m *ClientActivePayTrans) GetRequestNum() int {
  52. var count int
  53. tableCount := orm.ShMysql.Table(m.TableName()).Where("request_id = ? ", m.RequestID)
  54. tableCount.Count(&count)
  55. return count
  56. }
  57. func (m *ClientActivePayTrans) Create() (ClientActivePayTrans, error) {
  58. var doc ClientActivePayTrans
  59. var err error
  60. doc = *m
  61. err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
  62. if err != nil {
  63. return doc, err
  64. }
  65. return doc, nil
  66. }
  67. func (m *ClientActivePayTrans) GetPayTransByTradeNo() (ClientActivePayTrans, error) {
  68. var doc ClientActivePayTrans
  69. table := orm.ShMysql.Table(m.TableName())
  70. table = table.Where("out_trade_no = ? ", m.OutTradeNo)
  71. if err := table.Select("*").First(&doc).Error; err != nil {
  72. return doc, err
  73. }
  74. return doc, nil
  75. }
  76. func (m *ClientActivePayTrans) UpdatePayTransByTradeNo() error {
  77. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("out_trade_no = ? ", m.OutTradeNo).Updates(
  78. map[string]interface{}{
  79. "third_trade_no": m.ThirdTradeNo,
  80. "pay_time": m.PayTime,
  81. "status": m.Status,
  82. "account_status": m.AccountStatus,
  83. "updated_at": time.Now()}).Error; err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. func (m *ClientActivePayTrans) GetPendingCancelByMerchant() (ClientActivePayTrans, error) {
  89. var doc ClientActivePayTrans
  90. table := orm.ShMysql.Table(m.TableName())
  91. table = table.Where("status = 2 and account_status = 1 and merchant_open_id = ?", m.MerchantOpenID)
  92. if err := table.Select("sum(merchant_amount) as merchant_amount").First(&doc).Error; err != nil {
  93. return doc, err
  94. }
  95. return doc, nil
  96. }
  97. func (m *ClientActivePayTrans) GetPendingCancelByClient() (ClientActivePayTrans, error) {
  98. var doc ClientActivePayTrans
  99. table := orm.ShMysql.Table(m.TableName())
  100. table = table.Where("status = 2 and account_status = 1 and client_open_id = ?", m.ClientOpenID)
  101. if err := table.Select("sum(client_amount) as client_amount").First(&doc).Error; err != nil {
  102. return doc, err
  103. }
  104. return doc, nil
  105. }
  106. // 交易记录
  107. func (m *ClientActivePayTrans) GetActivePayTransList(pageSize int, pageIndex int, openId string) ([]ClientActivePayTrans, int, error) {
  108. var doc []ClientActivePayTrans
  109. table := orm.ShMysql.Table(m.TableName())
  110. //if userType == 1 { // 商家
  111. // table = table.Where("merchant_open_id = ?", openId)
  112. //
  113. //} else if userType == 2 { // c端用户
  114. // table = table.Where("client_open_id = ?", openId)
  115. //} else {
  116. // return doc, 0, errors.New("用户类型错误")
  117. //}
  118. //
  119. //if groupBuyType == 1 { // 待核销 核销次数大于0
  120. // table = table.Where("status = 2 and account_status in (1,2)")
  121. //} else if groupBuyType == 2 { //已核销
  122. // table = table.Where("status = 2 and account_status = 99")
  123. //} else {
  124. // return doc, 0, errors.New("核销类型错误")
  125. //}
  126. table = table.Where("client_open_id = ? and status = 2 and account_status in (1,2)", openId)
  127. var count int
  128. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  129. return nil, 0, err
  130. }
  131. table.Count(&count)
  132. return doc, count, nil
  133. }
  134. func (m *ClientActivePayTrans) GetPayTransByThirdTradeNo() (ClientActivePayTrans, error) {
  135. var doc ClientActivePayTrans
  136. table := orm.ShMysql.Table(m.TableName())
  137. table = table.Where("third_trade_no = ? ", m.ThirdTradeNo)
  138. if err := table.Select("*").First(&doc).Error; err != nil {
  139. return doc, err
  140. }
  141. return doc, nil
  142. }
  143. // 分账
  144. func (m *ClientActivePayTrans) TXSettle(payInfo ClientActivePayTrans) error {
  145. var err error
  146. tx := orm.ShMysql.Begin()
  147. defer func() {
  148. if err != nil {
  149. tx.Rollback()
  150. } else {
  151. tx.Commit()
  152. }
  153. }()
  154. //判断是否有这个商家账号,c端用户账号,没有用户需要创建
  155. //商家加款 c端用户加款
  156. return nil
  157. }
  158. func (m *ClientActivePayTrans) UpdateAccountStatusByID() error {
  159. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  160. map[string]interface{}{
  161. "account_status": m.Status,
  162. "updated_at": time.Now()}).Error; err != nil {
  163. return err
  164. }
  165. return nil
  166. }
  167. func (m *ClientActivePayTrans) UpdateById(data map[string]interface{}) error {
  168. err := orm.ShMysql.Table(m.TableName()).Where("id = ?", m.ID).Updates(data).Error
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }