|
@@ -2,6 +2,7 @@ package shanghu
|
|
|
|
|
|
import (
|
|
|
orm "duoduo/database"
|
|
|
+ "errors"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"time"
|
|
|
)
|
|
@@ -49,3 +50,151 @@ func (m *ActiveClientAccount) Create() (ActiveClientAccount, error) {
|
|
|
|
|
|
return doc, nil
|
|
|
}
|
|
|
+
|
|
|
+// c端拼个好运提现
|
|
|
+func (m *ActiveClientAccount) SettleSubClient(clientAmount decimal.Decimal, clientOpenId string, cashOutId int64) error {
|
|
|
+ // 使用事务 添加
|
|
|
+ var err error
|
|
|
+ var clientAccount ActiveClientAccount
|
|
|
+ var cashOut CashOut
|
|
|
+ var amountPreClient decimal.Decimal
|
|
|
+ tx := orm.ShMysql.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ } else {
|
|
|
+ tx.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ err = tx.Table("active_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ amountPreClient = clientAccount.Amount
|
|
|
+
|
|
|
+ if clientAccount.Amount.Cmp(clientAmount) < 0 {
|
|
|
+ err = errors.New("账户金额不够")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ clientAmountSub := clientAccount.Amount.Sub(clientAmount)
|
|
|
+ result := tx.Table("active_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
|
|
|
+ map[string]interface{}{
|
|
|
+ "amount": clientAmountSub,
|
|
|
+ "version": clientAccount.Version + 1,
|
|
|
+ "updated_at": time.Now()})
|
|
|
+ if result.Error != nil {
|
|
|
+ err = result.Error
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if result.RowsAffected <= 0 {
|
|
|
+ err = errors.New("rows is zero")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ var clientAccountLog ActiveClientAccountLog
|
|
|
+
|
|
|
+ clientAccountLog.ClientOpenID = clientOpenId
|
|
|
+ clientAccountLog.UpdatedAt = time.Now()
|
|
|
+ clientAccountLog.AmountPre = amountPreClient
|
|
|
+ clientAccountLog.AmountAfter = clientAmountSub
|
|
|
+ clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
|
|
|
+ clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
|
|
|
+ clientAccountLog.Amount = clientAmount
|
|
|
+ clientAccountLog.PayTransID = cashOutId
|
|
|
+ clientAccountLog.TransType = ActiveClientAccountLogTypeCashOut
|
|
|
+
|
|
|
+ err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ result = tx.Table("cash_out").Model(&cashOut).Where("id = ? ", cashOutId).Updates(
|
|
|
+ map[string]interface{}{
|
|
|
+ "status": 99, //体现成功
|
|
|
+ "account_status": 99, //分账成功
|
|
|
+ "updated_at": time.Now()})
|
|
|
+ if result.Error != nil {
|
|
|
+ err = result.Error
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if result.RowsAffected <= 0 {
|
|
|
+ err = errors.New("rows is zero")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (m *ActiveClientAccount) SettleAddCashOutFee(amount decimal.Decimal, clientOpenId string, cashOutId int64) error {
|
|
|
+ // 使用事务 添加
|
|
|
+ var err error
|
|
|
+ var clientAccount ActiveClientAccount
|
|
|
+ var amountPreClient decimal.Decimal
|
|
|
+ tx := orm.ShMysql.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ } else {
|
|
|
+ tx.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ err = tx.Table("active_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
|
|
|
+ if err != nil && err.Error() != "record not found" {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if clientAccount.ID == 0 {
|
|
|
+ clientAccount.ClientOpenID = clientOpenId
|
|
|
+ clientAccount.Version = 1
|
|
|
+ clientAccount.UpdatedAt = time.Now()
|
|
|
+ clientAccount.CreatedAt = time.Now()
|
|
|
+ err = tx.Table("active_client_account").Create(&clientAccount).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ amountPreClient = clientAccount.Amount
|
|
|
+
|
|
|
+ clientAmountAdd := clientAccount.Amount.Add(amount)
|
|
|
+ result := tx.Table("active_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
|
|
|
+ map[string]interface{}{
|
|
|
+ "amount": clientAmountAdd,
|
|
|
+ "version": clientAccount.Version + 1,
|
|
|
+ "updated_at": time.Now()})
|
|
|
+ if result.Error != nil {
|
|
|
+ err = result.Error
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if result.RowsAffected <= 0 {
|
|
|
+ err = errors.New("rows is zero")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ var clientAccountLog ActiveClientAccountLog
|
|
|
+
|
|
|
+ clientAccountLog.ClientOpenID = clientOpenId
|
|
|
+ clientAccountLog.UpdatedAt = time.Now()
|
|
|
+ clientAccountLog.AmountPre = amountPreClient
|
|
|
+ clientAccountLog.AmountAfter = clientAmountAdd
|
|
|
+ clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
|
|
|
+ clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
|
|
|
+ clientAccountLog.Amount = amount
|
|
|
+ clientAccountLog.PayTransID = cashOutId
|
|
|
+ clientAccountLog.TransType = ActiveClientAccountLogTypeCashOutFee //服务费
|
|
|
+
|
|
|
+ err = tx.Table("active_client_account_log").Create(&clientAccountLog).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+
|
|
|
+}
|