merchant.account.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "errors"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. )
  8. type MerchantAccount struct {
  9. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  10. MerchantOpenID string `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"` //openid
  11. ReviewAmount decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"` // 审核金额
  12. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 可提现金额
  13. Version int `gorm:"column:version;type:int(11)" json:"version"` // 创建者
  14. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  15. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  16. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  17. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  18. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  19. }
  20. func (m *MerchantAccount) TableName() string {
  21. return "merchant_account"
  22. }
  23. func (m *MerchantAccount) GetMerchantAccount() (MerchantAccount, error) {
  24. var doc MerchantAccount
  25. table := orm.ShMysql.Table(m.TableName())
  26. table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
  27. if err := table.Select("*").First(&doc).Error; err != nil {
  28. return doc, err
  29. }
  30. return doc, nil
  31. }
  32. func (m *MerchantAccount) Create() (MerchantAccount, error) {
  33. var doc MerchantAccount
  34. var err error
  35. doc = *m
  36. err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
  37. if err != nil {
  38. return doc, err
  39. }
  40. return doc, nil
  41. }
  42. // 拼团核销与分账
  43. func (m *MerchantAccount) ActiveCancelSettle(log []ActiveCancelLog, merchantAccount MerchantAccount, activeClientAccount ActiveClientAccount, payTrans ClientActivePayTrans) error {
  44. var err error
  45. var merchantAccountLog MerchantAccountLog
  46. var clientAccountLog ActiveClientAccountLog
  47. tx := orm.ShMysql.Begin()
  48. defer func() {
  49. if err != nil {
  50. tx.Rollback()
  51. } else {
  52. tx.Commit()
  53. }
  54. }()
  55. //核销日志
  56. for i := 0; i < len(log); i++ {
  57. err = tx.Table(log[i].TableName()).Create(&log[i]).Error
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. //分账
  63. //商家加款
  64. merchantAmountAdd := merchantAccount.Amount.Add(payTrans.MerchantAmount)
  65. result := tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantAccount.MerchantOpenID, merchantAccount.Version).Updates(
  66. map[string]interface{}{
  67. "amount": merchantAmountAdd,
  68. "version": merchantAccount.Version + 1,
  69. "updated_at": time.Now()})
  70. if result.Error != nil {
  71. err = result.Error
  72. return err
  73. }
  74. if result.RowsAffected <= 0 {
  75. err = errors.New("rows is zero")
  76. return err
  77. }
  78. merchantAccountLog.MerchantOpenID = merchantAccount.MerchantOpenID
  79. merchantAccountLog.UpdatedAt = time.Now()
  80. merchantAccountLog.AmountPre = merchantAccount.Amount
  81. merchantAccountLog.AmountAfter = merchantAmountAdd
  82. merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
  83. merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
  84. merchantAccountLog.Amount = payTrans.MerchantAmount
  85. merchantAccountLog.PayTransId = payTrans.ID
  86. merchantAccountLog.TransType = MerchantAccountLogTransTypeGroupBuy
  87. err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  88. if err != nil {
  89. return err
  90. }
  91. //err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  92. //if err != nil {
  93. // return err
  94. //}
  95. // 客户加款
  96. if activeClientAccount.ID > 0 {
  97. clientAmountAdd := activeClientAccount.Amount.Add(payTrans.ClientAmount)
  98. clientResult := tx.Table("active_client_account").Model(&activeClientAccount).Where("client_open_id = ? and version = ?", merchantAccount.MerchantOpenID, merchantAccount.Version).Updates(
  99. map[string]interface{}{
  100. "amount": clientAmountAdd,
  101. "version": activeClientAccount.Version + 1,
  102. "updated_at": time.Now()})
  103. if clientResult.Error != nil {
  104. err = clientResult.Error
  105. return err
  106. }
  107. if clientResult.RowsAffected <= 0 {
  108. err = errors.New("rows is zero")
  109. return err
  110. }
  111. clientAccountLog.ClientOpenID = activeClientAccount.ClientOpenID
  112. clientAccountLog.UpdatedAt = time.Now()
  113. clientAccountLog.AmountPre = activeClientAccount.Amount
  114. clientAccountLog.AmountAfter = clientAmountAdd
  115. clientAccountLog.ReviewAmountAfter = activeClientAccount.ReviewAmount
  116. clientAccountLog.ReviewAmountPre = activeClientAccount.ReviewAmount
  117. clientAccountLog.Amount = payTrans.ClientAmount
  118. clientAccountLog.PayTransID = payTrans.ID
  119. clientAccountLog.TransType = ActiveClientAccountLogTransTypeGroupBuy
  120. err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. //更新状态
  126. var activePay ClientActivePayTrans
  127. resultPay := tx.Table("client_active_pay_trans").Model(&activePay).Where("id = ? ", payTrans.ID).Updates(
  128. map[string]interface{}{
  129. "account_status": ClientActivePayTransAccountStatusSettleSuccess,
  130. "updated_at": time.Now()})
  131. if resultPay.Error != nil {
  132. err = resultPay.Error
  133. return err
  134. }
  135. if resultPay.RowsAffected <= 0 {
  136. err = errors.New("pay rows is zero")
  137. return err
  138. }
  139. return nil
  140. }