k.zhang 1 month ago
parent
commit
5f9abdc211
3 changed files with 171 additions and 2 deletions
  1. 2 1
      models/shanghu/active.account.log.go
  2. 149 0
      models/shanghu/actvie.client.account.go
  3. 20 1
      report/cash.out.go

+ 2 - 1
models/shanghu/active.account.log.go

@@ -11,7 +11,7 @@ type ActiveClientAccountLog struct {
 	ReviewAmountPre   decimal.Decimal `gorm:"column:review_amount_pre;type:decimal(10,2)" json:"review_amount_pre"`     // 审核资金 交易前
 	AmountPre         decimal.Decimal `gorm:"column:amount_pre;type:decimal(10,2)" json:"amount_pre"`                   // 交易前
 	AmountAfter       decimal.Decimal `gorm:"column:amount_after;type:decimal(10,2)" json:"amount_after"`               // 交易后
-	TransType         int             `gorm:"column:trans_type;type:int(11)" json:"trans_type"`                         // 交易类型 1-拼团核销入账  3-提现
+	TransType         int             `gorm:"column:trans_type;type:int(11)" json:"trans_type"`                         // 交易类型 1-拼团核销入账  2-提现
 	ClientOpenID      string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`            // 客户openid
 	CreateBy          int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                        // 创建者
 	UpdateBy          int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                        // 更新者
@@ -25,6 +25,7 @@ type ActiveClientAccountLog struct {
 const (
 	ActiveClientAccountLogTransTypeGroupBuy = 1 //拼团入账
 	ActiveClientAccountLogTypeCashOut       = 2 //提现
+	ActiveClientAccountLogTypeCashOutFee    = 3 //提现手续费
 )
 
 func (m *ActiveClientAccountLog) TableName() string {

+ 149 - 0
models/shanghu/actvie.client.account.go

@@ -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
+
+}

+ 20 - 1
report/cash.out.go

@@ -123,7 +123,26 @@ func CashOutAccount() {
 				accountClient.SettleAddCashOutFee(cashOutInfo.Fee, clientInfo.ClientOpenID, cashOutInfo.ID)
 			}
 
-		} else if cashOutInfo.AppID == "wx8595c589dd736486" {
+		} else if cashOutInfo.AppID == "wx8595c589dd736486" { //c端 拼个好运
+			var clientAccount shanghu.ActiveClientAccount
+			err = clientAccount.SettleSubClient(cashOutInfo.Amount, cashOutInfo.OpenID, cashOutInfo.ID)
+			if err != nil {
+				cashOut.AccountStatus = 2 //分账失败
+				cashOut.AccountFailRes = "wx8595c589dd736486-分账失败:" + err.Error()
+				cashOut.ID = cashOutInfo.ID
+				cashOut.UpdateCashOutAccountStatus()
+				continue
+			}
+			var activeUser shanghu.ActiveUser
+			activeUser.Code = "7jb6"
+			activeUserInfo, _ := activeUser.GetUserInfoByCode()
+			if activeUserInfo.OpenID == activeUserInfo.OpenID { //超级管理员钱已经有了不需要收服务费  暂时无效
+				continue
+			} else {
+				var client shanghu.ActiveClientAccount
+				client.SettleAddCashOutFee(cashOutInfo.Fee, activeUserInfo.OpenID, cashOutInfo.ID)
+
+			}
 
 		} else {
 			continue