merchant.account.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if payTrans.InvitationCode != YuanShiMa {
  55. err = orm.ShMysql.Table("active_client_account").Select("*").Where("client_open_id = ?", activeClientAccount.ClientOpenID).First(&activeClientAccount).Error
  56. if err != nil {
  57. return err
  58. }
  59. }
  60. fmt.Println("merchantAccount =", merchantAccount)
  61. fmt.Println("activeClientAccount =", activeClientAccount)
  62. tx := orm.ShMysql.Begin()
  63. defer func() {
  64. if err != nil {
  65. tx.Rollback()
  66. } else {
  67. tx.Commit()
  68. }
  69. }()
  70. //核销日志
  71. for i := 0; i < len(log); i++ {
  72. err = tx.Table(log[i].TableName()).Create(&log[i]).Error
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. //分账
  78. //商家加款
  79. merchantAmountAdd := merchantAccount.Amount.Add(payTrans.MerchantAmount)
  80. result := tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantAccount.MerchantOpenID, merchantAccount.Version).Updates(
  81. map[string]interface{}{
  82. "amount": merchantAmountAdd,
  83. "version": merchantAccount.Version + 1,
  84. "updated_at": time.Now()})
  85. if result.Error != nil {
  86. err = result.Error
  87. return err
  88. }
  89. if result.RowsAffected <= 0 {
  90. err = errors.New("rows is zero")
  91. return err
  92. }
  93. merchantAccountLog.MerchantOpenID = merchantAccount.MerchantOpenID
  94. merchantAccountLog.UpdatedAt = time.Now()
  95. merchantAccountLog.AmountPre = merchantAccount.Amount
  96. merchantAccountLog.AmountAfter = merchantAmountAdd
  97. merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
  98. merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
  99. merchantAccountLog.Amount = payTrans.MerchantAmount
  100. merchantAccountLog.PayTransId = payTrans.ID
  101. merchantAccountLog.TransType = MerchantAccountLogTransTypeGroupBuy
  102. err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  103. if err != nil {
  104. return err
  105. }
  106. //err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  107. //if err != nil {
  108. // return err
  109. //}
  110. // 客户加款
  111. if payTrans.InvitationCode != YuanShiMa && payTrans.InvitationCode != "" {
  112. clientAmountAdd := activeClientAccount.Amount.Add(payTrans.ClientAmount)
  113. clientResult := tx.Table("active_client_account").Model(&activeClientAccount).Where("client_open_id = ? and version = ?", activeClientAccount.ClientOpenID, activeClientAccount.Version).Updates(
  114. map[string]interface{}{
  115. "amount": clientAmountAdd,
  116. "version": activeClientAccount.Version + 1,
  117. "updated_at": time.Now()})
  118. if clientResult.Error != nil {
  119. err = clientResult.Error
  120. return err
  121. }
  122. if clientResult.RowsAffected <= 0 {
  123. err = errors.New("rows is zero")
  124. return err
  125. }
  126. clientAccountLog.ClientOpenID = activeClientAccount.ClientOpenID
  127. clientAccountLog.UpdatedAt = time.Now()
  128. clientAccountLog.AmountPre = activeClientAccount.Amount
  129. clientAccountLog.AmountAfter = clientAmountAdd
  130. clientAccountLog.ReviewAmountAfter = activeClientAccount.ReviewAmount
  131. clientAccountLog.ReviewAmountPre = activeClientAccount.ReviewAmount
  132. clientAccountLog.Amount = payTrans.ClientAmount
  133. clientAccountLog.PayTransID = payTrans.ID
  134. clientAccountLog.TransType = ActiveClientAccountLogTransTypeGroupBuy
  135. err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
  136. if err != nil {
  137. return err
  138. }
  139. }
  140. //更新状态
  141. var activePay ClientActivePayTrans
  142. resultPay := tx.Table("client_active_pay_trans").Model(&activePay).Where("id = ? ", payTrans.ID).Updates(
  143. map[string]interface{}{
  144. "account_status": ClientActivePayTransAccountStatusSettleSuccess,
  145. "updated_at": time.Now()})
  146. if resultPay.Error != nil {
  147. err = resultPay.Error
  148. return err
  149. }
  150. if resultPay.RowsAffected <= 0 {
  151. err = errors.New("pay rows is zero")
  152. return err
  153. }
  154. return nil
  155. }