merchant.account.go 6.1 KB

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