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.

282 lines
7.4 KiB

9 months ago
package service
import (
"errors"
"time"
"pure/global"
"pure/model"
"pure/model/request"
"pure/utils"
"gorm.io/gorm"
)
func GetUserWalletDetail(userID string) (err error, userInfo *model.Wallet) {
var user model.Wallet
err = global.MG_DB.Model(&model.Wallet{}).Where("user_id = ?", userID).Find(&user).Error
if err != nil {
return errors.New("获取用户失败"), nil
}
return nil, &user
}
func GetAccountList(userId string) ([]model.Account, error) {
var (
err error
result []model.Account
)
err = global.MG_DB.Model(&model.Account{}).Where("user_id = ?", userId).Order("created_at desc").Find(&result).Error
return result, err
}
func AddAccount(userId string, info *request.AddAccount) error {
var (
err error
checkAccount model.Account
)
// 校验重复绑定
if info.BankCard != "" {
err = global.MG_DB.Model(&model.Account{}).Where("platform = 'influencer' AND card_number = ?", info.BankCard).Find(&checkAccount).Error
if err != nil {
return err
}
}
if checkAccount.ID != 0 {
if checkAccount.UserID == userId {
return errors.New("the account already exists in yours account list")
} else {
//TODO 校验他人重复账户
}
}
tx := global.MG_DB.Begin()
if info.IsDefault {
err = tx.Model(&model.Account{}).Where("user_id = ? AND platform = ?", userId, "influencer").UpdateColumn("is_default", "0").Error
if err != nil {
return err
}
}
var account model.Account
account.UserID = userId
if info.BankCard != "" {
account.Type = 2
}
account.Platform = "influencer"
account.AccountName = info.AccountName
account.BankCode = info.BankCode
account.SwiftCode = info.SwiftCode
account.CardNumber = info.BankCard
account.Address = info.Address
account.Country = info.Country
account.Currency = info.Currency
account.IsDefault = info.IsDefault
err = tx.Create(&account).Error
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func UpdateAccountIsDefault(userId string, info *request.IdReq) error {
var (
err error
account model.Account
)
tx := global.MG_DB.Begin()
err = tx.Model(&model.Account{}).Where("user_id = ? AND id = ?", userId, info.ID).First(&account).Error
if err != nil {
tx.Rollback()
return errors.New("get account failed")
}
if account.IsDefault {
err = tx.Model(&model.Account{}).Where("id = ?", account.ID).UpdateColumn("is_default", "0").Error
if err != nil {
tx.Rollback()
return err
}
} else {
err = tx.Model(&model.Account{}).Where("id != ?", account.ID).UpdateColumn("is_default", "0").Error
if err != nil {
tx.Rollback()
return err
}
err = tx.Model(&model.Account{}).Where("id = ?", account.ID).UpdateColumn("is_default", "1").Error
if err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
return err
}
func DeleteAccount(userId string, info *request.DeleteAccount) error {
// TODO 校验手机验证码
err := global.MG_DB.Model(&model.Account{}).Where("user_id=? and id=?", userId, info.ID).Delete(&model.Account{}).Error
return err
}
func GetUserCommissionList(userID string, info *request.SearchCommission) (error, interface{}, int64) {
var (
err error
data []model.Bill
limit int
offset int
total int64
)
limit = info.PageSize
offset = info.PageSize * (info.Page - 1)
db := global.MG_DB.Model(&model.Bill{})
if info.Receipt != 0 {
db = db.Where("receipt=?", info.Receipt)
}
err = db.Count(&total).Error
if err != nil {
return errors.New("获取总数失败"), data, total
}
err = db.Where("user_id=? and type=1", userID).Offset(offset).Limit(limit).Find(&data).Error
if err != nil {
return errors.New("获取用户账单失败"), data, total
}
return nil, data, total
}
func GetUserWithdrawalList(userID string, info *request.SearchWithdrawal) (error, interface{}, int64) {
var (
err error
data []model.Bill
limit int
offset int
total int64
)
limit = info.PageSize
offset = info.PageSize * (info.Page - 1)
db := global.MG_DB.Model(&model.Bill{})
if info.Status != 0 {
db = db.Where("receipt=?", info.Status)
}
err = db.Count(&total).Error
if err != nil {
return errors.New("获取总数失败"), data, total
}
err = db.Where("user_id=? and type=3", userID).Offset(offset).Limit(limit).Find(&data).Error
if err != nil {
return errors.New("获取用户账单失败"), data, total
}
return nil, data, total
}
func Withdrawal(userId string, info *request.WithdrawalParams) (error, model.WithdrawalView) {
var (
err error
user model.UserSimple
result model.WithdrawalView
)
user, err = getUserSimple("influencer", userId)
if err != nil {
return errors.New("获取用户信息失败"), result
}
if user.Phone == "" {
return errors.New("please bind your phone number"), result
}
// 开始交易
for {
ok, err := global.MG_REDIS.SetNX("withdrawal-"+userId, "used", 10*time.Second).Result()
if ok && err == nil {
// 获取成功
break
}
}
defer func() {
// 释放锁
_, _ = utils.RedisDel("withdrawal-" + userId)
}()
var (
wallet model.Wallet
//check model.Withdrawal
)
err = global.MG_DB.Model(&model.Wallet{}).Where("platform = 'influencer' AND user_id = ?", userId).First(&wallet).Error
if err != nil {
return err, result
}
if wallet.State != 0 {
return errors.New("account no withdrawal allowed"), result
}
//_ = global.MG_DB.Model(&model.Withdrawal{}).Where("platform = '1' AND user_id = ? AND status = '0'", userId).Find(&check).Error
//if check.ID != 0 {
// return errors.New("you have a withdrawal in progress"), result
//}
if wallet.Balance < info.Amount {
return errors.New("金额不足,无法提现"), result
}
var account model.Account
err = global.MG_DB.Model(&model.Account{}).Where("user_id = ? AND id = ?", userId, info.AccountID).First(&account).Error
if err != nil {
return errors.New("获取账户信息失败"), result
}
//if
tx := global.MG_DB.Begin()
err = tx.Model(&model.Wallet{}).Where("id = ?", wallet.ID).Updates(map[string]interface{}{"balance": gorm.Expr("balance - ?", info.Amount), "state": "1"}).Error
if err != nil {
tx.Rollback()
return errors.New("金额扣除失败,取消提现"), result
}
var withdrawal model.Withdrawal
withdrawal.Platform = "1"
withdrawal.WalletType = 1
withdrawal.FlowNo = Generate()
withdrawal.BillNo = Generate()
withdrawal.AccountType = account.Type
withdrawal.BankCard = account.BankCard
withdrawal.CreateBy = userId
withdrawal.Amount = info.Amount
withdrawal.CheckStatus = "0"
withdrawal.Status = "0"
withdrawal.Title = "提现"
err = tx.Create(&withdrawal).Error
if err != nil {
tx.Rollback()
return errors.New("提交申请失败"), result
}
var bill model.Bill
bill.UserID = userId
bill.Type = "3"
bill.WithdID = withdrawal.BillNo
bill.TransactionId = "B" + bill.WithdID
bill.Price = info.Amount
bill.Status = 1
bill.Receipt = 2
bill.WithdrawalStatus = 0
bill.Platform = "influencer"
//bill.Account = account.PayPalName
bill.CheckStatus = "0"
bill.Title = "钱包提现"
err = tx.Create(&bill).Error
if err != nil {
tx.Rollback()
return errors.New("提现失败,请稍后再试"), result
}
tx.Commit()
// 添加通知
var notify model.Notify
notify.UserId = userId
notify.RelationType = "1"
notify.RelationId = withdrawal.FlowNo
notify.Title = "当前有一笔提现还在审核中,不可发起新的提现"
_ = global.MG_DB.Model(&model.Notify{}).Create(&notify).Error
result = model.WithdrawalView{
FlowNo: withdrawal.FlowNo,
}
return nil, result
}
//func addTransitReward(tx *gorm.DB) error {
//
//}