|
|
package model |
|
|
|
|
|
import ( |
|
|
"errors" |
|
|
"fmt" |
|
|
|
|
|
"github.com/QuantumNous/new-api/common" |
|
|
"github.com/QuantumNous/new-api/logger" |
|
|
|
|
|
"github.com/shopspring/decimal" |
|
|
"gorm.io/gorm" |
|
|
) |
|
|
|
|
|
type TopUp struct { |
|
|
Id int `json:"id"` |
|
|
UserId int `json:"user_id" gorm:"index"` |
|
|
Amount int64 `json:"amount"` |
|
|
Money float64 `json:"money"` |
|
|
TradeNo string `json:"trade_no" gorm:"unique;type:varchar(255);index"` |
|
|
PaymentMethod string `json:"payment_method" gorm:"type:varchar(50)"` |
|
|
CreateTime int64 `json:"create_time"` |
|
|
CompleteTime int64 `json:"complete_time"` |
|
|
Status string `json:"status"` |
|
|
} |
|
|
|
|
|
func (topUp *TopUp) Insert() error { |
|
|
var err error |
|
|
err = DB.Create(topUp).Error |
|
|
return err |
|
|
} |
|
|
|
|
|
func (topUp *TopUp) Update() error { |
|
|
var err error |
|
|
err = DB.Save(topUp).Error |
|
|
return err |
|
|
} |
|
|
|
|
|
func GetTopUpById(id int) *TopUp { |
|
|
var topUp *TopUp |
|
|
var err error |
|
|
err = DB.Where("id = ?", id).First(&topUp).Error |
|
|
if err != nil { |
|
|
return nil |
|
|
} |
|
|
return topUp |
|
|
} |
|
|
|
|
|
func GetTopUpByTradeNo(tradeNo string) *TopUp { |
|
|
var topUp *TopUp |
|
|
var err error |
|
|
err = DB.Where("trade_no = ?", tradeNo).First(&topUp).Error |
|
|
if err != nil { |
|
|
return nil |
|
|
} |
|
|
return topUp |
|
|
} |
|
|
|
|
|
func Recharge(referenceId string, customerId string) (err error) { |
|
|
if referenceId == "" { |
|
|
return errors.New("未提供支付单号") |
|
|
} |
|
|
|
|
|
var quota float64 |
|
|
topUp := &TopUp{} |
|
|
|
|
|
refCol := "`trade_no`" |
|
|
if common.UsingPostgreSQL { |
|
|
refCol = `"trade_no"` |
|
|
} |
|
|
|
|
|
err = DB.Transaction(func(tx *gorm.DB) error { |
|
|
err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", referenceId).First(topUp).Error |
|
|
if err != nil { |
|
|
return errors.New("充值订单不存在") |
|
|
} |
|
|
|
|
|
if topUp.Status != common.TopUpStatusPending { |
|
|
return errors.New("充值订单状态错误") |
|
|
} |
|
|
|
|
|
topUp.CompleteTime = common.GetTimestamp() |
|
|
topUp.Status = common.TopUpStatusSuccess |
|
|
err = tx.Save(topUp).Error |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
quota = topUp.Money * common.QuotaPerUnit |
|
|
err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Updates(map[string]interface{}{"stripe_customer": customerId, "quota": gorm.Expr("quota + ?", quota)}).Error |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
return nil |
|
|
}) |
|
|
|
|
|
if err != nil { |
|
|
return errors.New("充值失败," + err.Error()) |
|
|
} |
|
|
|
|
|
RecordLog(topUp.UserId, LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v,支付金额:%d", logger.FormatQuota(int(quota)), topUp.Amount)) |
|
|
|
|
|
return nil |
|
|
} |
|
|
|
|
|
func GetUserTopUps(userId int, pageInfo *common.PageInfo) (topups []*TopUp, total int64, err error) { |
|
|
|
|
|
tx := DB.Begin() |
|
|
if tx.Error != nil { |
|
|
return nil, 0, tx.Error |
|
|
} |
|
|
defer func() { |
|
|
if r := recover(); r != nil { |
|
|
tx.Rollback() |
|
|
} |
|
|
}() |
|
|
|
|
|
|
|
|
err = tx.Model(&TopUp{}).Where("user_id = ?", userId).Count(&total).Error |
|
|
if err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
|
|
|
err = tx.Where("user_id = ?", userId).Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Find(&topups).Error |
|
|
if err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
|
|
|
if err = tx.Commit().Error; err != nil { |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
return topups, total, nil |
|
|
} |
|
|
|
|
|
|
|
|
func GetAllTopUps(pageInfo *common.PageInfo) (topups []*TopUp, total int64, err error) { |
|
|
tx := DB.Begin() |
|
|
if tx.Error != nil { |
|
|
return nil, 0, tx.Error |
|
|
} |
|
|
defer func() { |
|
|
if r := recover(); r != nil { |
|
|
tx.Rollback() |
|
|
} |
|
|
}() |
|
|
|
|
|
if err = tx.Model(&TopUp{}).Count(&total).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = tx.Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Find(&topups).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = tx.Commit().Error; err != nil { |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
return topups, total, nil |
|
|
} |
|
|
|
|
|
|
|
|
func SearchUserTopUps(userId int, keyword string, pageInfo *common.PageInfo) (topups []*TopUp, total int64, err error) { |
|
|
tx := DB.Begin() |
|
|
if tx.Error != nil { |
|
|
return nil, 0, tx.Error |
|
|
} |
|
|
defer func() { |
|
|
if r := recover(); r != nil { |
|
|
tx.Rollback() |
|
|
} |
|
|
}() |
|
|
|
|
|
query := tx.Model(&TopUp{}).Where("user_id = ?", userId) |
|
|
if keyword != "" { |
|
|
like := "%%" + keyword + "%%" |
|
|
query = query.Where("trade_no LIKE ?", like) |
|
|
} |
|
|
|
|
|
if err = query.Count(&total).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = query.Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Find(&topups).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = tx.Commit().Error; err != nil { |
|
|
return nil, 0, err |
|
|
} |
|
|
return topups, total, nil |
|
|
} |
|
|
|
|
|
|
|
|
func SearchAllTopUps(keyword string, pageInfo *common.PageInfo) (topups []*TopUp, total int64, err error) { |
|
|
tx := DB.Begin() |
|
|
if tx.Error != nil { |
|
|
return nil, 0, tx.Error |
|
|
} |
|
|
defer func() { |
|
|
if r := recover(); r != nil { |
|
|
tx.Rollback() |
|
|
} |
|
|
}() |
|
|
|
|
|
query := tx.Model(&TopUp{}) |
|
|
if keyword != "" { |
|
|
like := "%%" + keyword + "%%" |
|
|
query = query.Where("trade_no LIKE ?", like) |
|
|
} |
|
|
|
|
|
if err = query.Count(&total).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = query.Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Find(&topups).Error; err != nil { |
|
|
tx.Rollback() |
|
|
return nil, 0, err |
|
|
} |
|
|
|
|
|
if err = tx.Commit().Error; err != nil { |
|
|
return nil, 0, err |
|
|
} |
|
|
return topups, total, nil |
|
|
} |
|
|
|
|
|
|
|
|
func ManualCompleteTopUp(tradeNo string) error { |
|
|
if tradeNo == "" { |
|
|
return errors.New("未提供订单号") |
|
|
} |
|
|
|
|
|
refCol := "`trade_no`" |
|
|
if common.UsingPostgreSQL { |
|
|
refCol = `"trade_no"` |
|
|
} |
|
|
|
|
|
var userId int |
|
|
var quotaToAdd int |
|
|
var payMoney float64 |
|
|
|
|
|
err := DB.Transaction(func(tx *gorm.DB) error { |
|
|
topUp := &TopUp{} |
|
|
|
|
|
if err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", tradeNo).First(topUp).Error; err != nil { |
|
|
return errors.New("充值订单不存在") |
|
|
} |
|
|
|
|
|
|
|
|
if topUp.Status == common.TopUpStatusSuccess { |
|
|
return nil |
|
|
} |
|
|
|
|
|
if topUp.Status != common.TopUpStatusPending { |
|
|
return errors.New("订单状态不是待支付,无法补单") |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if topUp.PaymentMethod == "stripe" { |
|
|
dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) |
|
|
quotaToAdd = int(decimal.NewFromFloat(topUp.Money).Mul(dQuotaPerUnit).IntPart()) |
|
|
} else { |
|
|
dAmount := decimal.NewFromInt(topUp.Amount) |
|
|
dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) |
|
|
quotaToAdd = int(dAmount.Mul(dQuotaPerUnit).IntPart()) |
|
|
} |
|
|
if quotaToAdd <= 0 { |
|
|
return errors.New("无效的充值额度") |
|
|
} |
|
|
|
|
|
|
|
|
topUp.CompleteTime = common.GetTimestamp() |
|
|
topUp.Status = common.TopUpStatusSuccess |
|
|
if err := tx.Save(topUp).Error; err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
|
|
|
if err := tx.Model(&User{}).Where("id = ?", topUp.UserId).Update("quota", gorm.Expr("quota + ?", quotaToAdd)).Error; err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
userId = topUp.UserId |
|
|
payMoney = topUp.Money |
|
|
return nil |
|
|
}) |
|
|
|
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
|
|
|
RecordLog(userId, LogTypeTopup, fmt.Sprintf("管理员补单成功,充值金额: %v,支付金额:%f", logger.FormatQuota(quotaToAdd), payMoney)) |
|
|
return nil |
|
|
} |
|
|
func RechargeCreem(referenceId string, customerEmail string, customerName string) (err error) { |
|
|
if referenceId == "" { |
|
|
return errors.New("未提供支付单号") |
|
|
} |
|
|
|
|
|
var quota int64 |
|
|
topUp := &TopUp{} |
|
|
|
|
|
refCol := "`trade_no`" |
|
|
if common.UsingPostgreSQL { |
|
|
refCol = `"trade_no"` |
|
|
} |
|
|
|
|
|
err = DB.Transaction(func(tx *gorm.DB) error { |
|
|
err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", referenceId).First(topUp).Error |
|
|
if err != nil { |
|
|
return errors.New("充值订单不存在") |
|
|
} |
|
|
|
|
|
if topUp.Status != common.TopUpStatusPending { |
|
|
return errors.New("充值订单状态错误") |
|
|
} |
|
|
|
|
|
topUp.CompleteTime = common.GetTimestamp() |
|
|
topUp.Status = common.TopUpStatusSuccess |
|
|
err = tx.Save(topUp).Error |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
|
|
|
quota = topUp.Amount |
|
|
|
|
|
|
|
|
updateFields := map[string]interface{}{ |
|
|
"quota": gorm.Expr("quota + ?", quota), |
|
|
} |
|
|
|
|
|
|
|
|
if customerEmail != "" { |
|
|
|
|
|
var user User |
|
|
err = tx.Where("id = ?", topUp.UserId).First(&user).Error |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
|
|
|
if user.Email == "" { |
|
|
updateFields["email"] = customerEmail |
|
|
} |
|
|
} |
|
|
|
|
|
err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Updates(updateFields).Error |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
|
|
|
return nil |
|
|
}) |
|
|
|
|
|
if err != nil { |
|
|
return errors.New("充值失败," + err.Error()) |
|
|
} |
|
|
|
|
|
RecordLog(topUp.UserId, LogTypeTopup, fmt.Sprintf("使用Creem充值成功,充值额度: %v,支付金额:%.2f", quota, topUp.Money)) |
|
|
|
|
|
return nil |
|
|
} |
|
|
|