actvie.client.account.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "errors"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. )
  8. type ActiveClientAccount struct {
  9. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  10. ClientOpenID string `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"` //
  11. ReviewAmount decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"` // 待核销金额
  12. Version int `gorm:"column:version;type:int(11)" json:"version"` // Version
  13. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 可提现金额
  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 *ActiveClientAccount) TableName() string {
  21. return "active_client_account"
  22. }
  23. func (m *ActiveClientAccount) GetActiveAccount() (ActiveClientAccount, error) {
  24. var doc ActiveClientAccount
  25. table := orm.ShMysql.Table(m.TableName())
  26. table = table.Where("client_open_id = ? ", m.ClientOpenID)
  27. if err := table.Select("*").First(&doc).Error; err != nil {
  28. return doc, err
  29. }
  30. return doc, nil
  31. }
  32. func (m *ActiveClientAccount) Create() (ActiveClientAccount, error) {
  33. var doc ActiveClientAccount
  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. // c端拼个好运提现
  43. func (m *ActiveClientAccount) SettleSubClient(clientAmount decimal.Decimal, clientOpenId string, cashOutId int64) error {
  44. // 使用事务 添加
  45. var err error
  46. var clientAccount ActiveClientAccount
  47. var cashOut CashOut
  48. var amountPreClient decimal.Decimal
  49. tx := orm.ShMysql.Begin()
  50. defer func() {
  51. if err != nil {
  52. tx.Rollback()
  53. } else {
  54. tx.Commit()
  55. }
  56. }()
  57. err = tx.Table("active_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
  58. if err != nil {
  59. return err
  60. }
  61. amountPreClient = clientAccount.Amount
  62. if clientAccount.Amount.Cmp(clientAmount) < 0 {
  63. err = errors.New("账户金额不够")
  64. return err
  65. }
  66. clientAmountSub := clientAccount.Amount.Sub(clientAmount)
  67. result := tx.Table("active_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
  68. map[string]interface{}{
  69. "amount": clientAmountSub,
  70. "version": clientAccount.Version + 1,
  71. "updated_at": time.Now()})
  72. if result.Error != nil {
  73. err = result.Error
  74. return err
  75. }
  76. if result.RowsAffected <= 0 {
  77. err = errors.New("rows is zero")
  78. return err
  79. }
  80. var clientAccountLog ActiveClientAccountLog
  81. clientAccountLog.ClientOpenID = clientOpenId
  82. clientAccountLog.UpdatedAt = time.Now()
  83. clientAccountLog.AmountPre = amountPreClient
  84. clientAccountLog.AmountAfter = clientAmountSub
  85. clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
  86. clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
  87. clientAccountLog.Amount = clientAmount
  88. clientAccountLog.PayTransID = cashOutId
  89. clientAccountLog.TransType = ActiveClientAccountLogTypeCashOut
  90. err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
  91. if err != nil {
  92. return err
  93. }
  94. result = tx.Table("cash_out").Model(&cashOut).Where("id = ? ", cashOutId).Updates(
  95. map[string]interface{}{
  96. "status": 99, //体现成功
  97. "account_status": 99, //分账成功
  98. "updated_at": time.Now()})
  99. if result.Error != nil {
  100. err = result.Error
  101. return err
  102. }
  103. if result.RowsAffected <= 0 {
  104. err = errors.New("rows is zero")
  105. return err
  106. }
  107. return nil
  108. }
  109. func (m *ActiveClientAccount) SettleAddCashOutFee(amount decimal.Decimal, clientOpenId string, cashOutId int64) error {
  110. // 使用事务 添加
  111. var err error
  112. var clientAccount ActiveClientAccount
  113. var amountPreClient decimal.Decimal
  114. tx := orm.ShMysql.Begin()
  115. defer func() {
  116. if err != nil {
  117. tx.Rollback()
  118. } else {
  119. tx.Commit()
  120. }
  121. }()
  122. err = tx.Table("active_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
  123. if err != nil && err.Error() != "record not found" {
  124. return err
  125. }
  126. if clientAccount.ID == 0 {
  127. clientAccount.ClientOpenID = clientOpenId
  128. clientAccount.Version = 1
  129. clientAccount.UpdatedAt = time.Now()
  130. clientAccount.CreatedAt = time.Now()
  131. err = tx.Table("active_client_account").Create(&clientAccount).Error
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. amountPreClient = clientAccount.Amount
  137. clientAmountAdd := clientAccount.Amount.Add(amount)
  138. result := tx.Table("active_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
  139. map[string]interface{}{
  140. "amount": clientAmountAdd,
  141. "version": clientAccount.Version + 1,
  142. "updated_at": time.Now()})
  143. if result.Error != nil {
  144. err = result.Error
  145. return err
  146. }
  147. if result.RowsAffected <= 0 {
  148. err = errors.New("rows is zero")
  149. return err
  150. }
  151. var clientAccountLog ActiveClientAccountLog
  152. clientAccountLog.ClientOpenID = clientOpenId
  153. clientAccountLog.UpdatedAt = time.Now()
  154. clientAccountLog.AmountPre = amountPreClient
  155. clientAccountLog.AmountAfter = clientAmountAdd
  156. clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
  157. clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
  158. clientAccountLog.Amount = amount
  159. clientAccountLog.PayTransID = cashOutId
  160. clientAccountLog.TransType = ActiveClientAccountLogTypeCashOutFee //服务费
  161. err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
  162. if err != nil {
  163. return err
  164. }
  165. return nil
  166. }