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.

123 lines
3.1 KiB

package jyy
import (
"errors"
"fmt"
"recook/internal/libs/bean"
"recook/internal/model/user"
"recook/internal/v2/model/jyy"
3 years ago
"time"
"git.oa00.com/go/mysql"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type ArgsDeposit struct {
bean.Page
Name string `json:"name"`
State int `json:"state"`
IsCompany *bool `json:"is_company"`
Start string `json:"start"`
End string `json:"end"`
}
func (o logic) DepositList(args ArgsDeposit) (data []jyy.UserWalletApply, total int64) {
query := mysql.Db.Table((&jyy.UserWalletApply{}).TableName())
{
if args.Name != "" {
sub := mysql.Db.Model(&user.Information{}).Select("id").
Where("nickname like ?", fmt.Sprintf("%%%s%%", args.Name))
query = query.Where("user_id in ?", sub)
}
if args.Start != "" {
query = query.Where("created_at > ?", args.Start)
}
if args.End != "" {
query = query.Where("created_at < ?", args.End)
}
if args.State != 0 {
query = query.Where("state = ?", args.State)
}
if args.IsCompany != nil {
if *args.IsCompany {
3 years ago
query = query.Where("company_id <> 0")
} else {
3 years ago
query = query.Where("company_id = 0")
}
}
}
query.Count(&total)
query.Preload(clause.Associations).Limit(args.GetLimit()).Offset(args.GetStart()).Order("id desc").Find(&data)
return
}
type ArgsDepositReject struct {
ID int `json:"id" validate:"required"`
Reason string `json:"reason" validate:"required"`
3 years ago
UserID uint `json:"-"`
}
func (o logic) DepositReject(args ArgsDepositReject) error {
3 years ago
now := time.Now()
return mysql.Db.Table((&jyy.UserWalletApply{}).TableName()).Where("id = ?", args.ID).Updates(jyy.UserWalletApply{
3 years ago
State: 99,
Reason: args.Reason,
ApplyUserID: int(args.UserID),
ApplyTime: &now,
}).Error
}
type ArgsDepositSure struct {
3 years ago
ID int `json:"id" validate:"required"`
UserID uint `json:"-"`
}
func (o logic) DepositSure(args ArgsDepositSure) error {
return mysql.Db.Transaction(func(tx *gorm.DB) error {
var a jyy.UserWalletApply
if e := tx.First(&a, "id = ?", args.ID).Error; e != nil {
return e
}
3 years ago
now := time.Now()
if e := tx.Table((&jyy.UserWalletApply{}).TableName()).Where("id = ?", args.ID).Updates(jyy.UserWalletApply{
3 years ago
State: 2,
ApplyUserID: int(args.UserID),
ApplyTime: &now,
}).Error; e != nil {
return e
}
var m jyy.UserWallet
if e := tx.First(&m, "user_id = ?", a.UserID).Error; e != nil {
if e == gorm.ErrRecordNotFound {
m = jyy.UserWallet{
Deposit: decimal.Zero,
UserID: a.UserID,
}
if e := tx.Create(&m).Error; e != nil {
return e
}
} else {
return e
}
}
if row := tx.Model(&m).Where("version = ?", m.Version).Updates(map[string]interface{}{
"version": gorm.Expr("version + 1"),
"deposit": gorm.Expr("deposit + ?", a.Amount),
}).RowsAffected; row != 1 {
return errors.New("更新失败")
}
if e := tx.Create(&jyy.UserWalletRecord{
WalletID: m.ID,
UserID: m.UserID,
Amount: a.Amount,
Current: m.Deposit,
Kind: 3,
}).Error; e != nil {
return e
}
return nil
})
}