You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package wallet
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/user"
user2 "recook/internal/v2/model/recook/user"
"recook/tools"
"time"
)
type withdrawParam struct {
UserID uint `json:"userId"`
Amount float64 `json:"amount"`
}
/*
提现
金额不低于10元
保证用户已实名认证 且绑定了银行卡信息
每个月最多提现三次
减去钱包金额
增加一条记录
增加一条提现记录
*/
func UpdateWithdrawals(c *gin.Context) {
var p withdrawParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
if p.Amount < 10.0 {
back.Fail(c, "最低提取额度十元")
return
}
var realInfo user.RealInfo
dbc.DB.First(&realInfo, "user_id = ?", p.UserID)
if realInfo.ID == 0 {
back.Suc(c, "银行卡未绑定", gin.H{
"binding": false,
})
return
}
now := time.Now()
month := time.Date(now.Year(), now.Month(), 0, 0, 0, 0, 0, time.Local)
var count uint
dbc.DB.Table((&user.WalletWithdrawalsList{}).TableName()).
Where("user_id = ? AND created_at >= ?", p.UserID, month).Count(&count)
if count == 3 {
back.Fail(c, "每月最多可提现三次")
return
}
tx := dbc.DB.Begin()
{
var wallet user.Wallet
if err := tx.First(&wallet, "user_id = ?", p.UserID).Error; err != nil {
back.Err(c, err.Error())
tx.Rollback()
return
}
if wallet.Balance.LessThan(decimal.NewFromFloat(p.Amount)) {
back.Fail(c, "钱包金额不足")
tx.Rollback()
return
}
if err := tx.Model(&wallet).Updates(map[string]interface{}{
//"balance": wallet.Balance.Add(decimal.NewFromFloat(p.Amount).Mul(decimal.NewFromInt(-1))),
"balance": gorm.Expr("balance - ?", p.Amount),
"total_withdraw": gorm.Expr("total_withdraw + ?", p.Amount),
}).Error; err != nil {
back.Err(c, err.Error())
tx.Rollback()
return
}
//one := user.WalletBalanceList{
// UserID: wallet.UserID,
// Amount: decimal.NewFromFloat(p.Amount),
// Style: "提现",
// Comment: "卡号:" + realInfo.BankNo,
//}
//if err := tx.Create(&one).Error; err != nil {
// http.Err(c, err.Error())
// tx.Rollback()
// return
//}
recookUserWalletBalanceListModel := &user2.RecookUserWalletBalanceListModel{
UserId: wallet.UserID,
IncomeType: user2.RecookUserWalletBalanceListIncomeTypeWithdraw, // 提现
Amount: decimal.NewFromFloat(p.Amount),
Title: "卡号:" + realInfo.BankNo,
Comment: "提现",
}
recookUserWalletBalanceListModel.SetDb(tx)
recookUserWalletBalanceListModel.Create(recookUserWalletBalanceListModel)
if recookUserWalletBalanceListModel.Id == 0 {
back.Err(c, "系统异常")
tx.Rollback()
return
}
withdrawalsList := user.WalletWithdrawalsList{
UserID: wallet.UserID,
Amount: decimal.NewFromFloat(p.Amount),
UserName: realInfo.IdCardName,
BankNo: realInfo.BankNo,
}
if err := tx.Create(&withdrawalsList).Error; err != nil {
tx.Rollback()
back.Err(c, err.Error())
return
}
}
tx.Commit()
back.Suc(c, "提现成功预计资金将在本月21至25日到账", gin.H{
"binding": true,
})
}