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.
124 lines
3.4 KiB
124 lines
3.4 KiB
package jyy
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"recook/internal/libs/bean"
|
|
"recook/internal/v2/model/company"
|
|
"recook/internal/v2/model/recook/user"
|
|
"time"
|
|
|
|
"git.oa00.com/go/mysql"
|
|
"github.com/golangkit/formatime"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type ArgsWithdrawal struct {
|
|
bean.Page
|
|
Name string `json:"name"`
|
|
Tax int `json:"tax"`
|
|
Start *time.Time `json:"start"`
|
|
End *time.Time `json:"end"`
|
|
ManageUserID int `json:"manage_user_id"`
|
|
State int `json:"state"`
|
|
}
|
|
|
|
func (o logic) WithdrawalList(args ArgsWithdrawal) (data []company.Apply, total int64) {
|
|
query := mysql.Db.Table((&company.Apply{}).TableName())
|
|
{
|
|
if args.Name != "" {
|
|
sub := mysql.Db.Model(&user.RecookUserInfoModel{}).Select("id").
|
|
Where("nickname like ?", fmt.Sprintf("%%%s%%", args.Name))
|
|
query = query.Where("user_id in ?", sub)
|
|
}
|
|
if args.Tax != 0 {
|
|
query = query.Where("tax = ?", args.Tax)
|
|
}
|
|
if args.Start != nil {
|
|
query = query.Where("created_at > ?", args.Start)
|
|
}
|
|
if args.End != nil {
|
|
query = query.Where("created_at < ?", args.End)
|
|
}
|
|
if args.State != 0 {
|
|
query = query.Where("state = ?", args.State)
|
|
}
|
|
}
|
|
query.Count(&total)
|
|
query.Preload(clause.Associations).Limit(args.GetLimit()).Offset(args.GetStart()).Order("id desc").Find(&data)
|
|
return
|
|
}
|
|
|
|
type ArgsWithdrawalApply struct {
|
|
ID int `json:"id" validate:"required"`
|
|
Apply int `json:"-"`
|
|
ManageUserID int `json:"-"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
func (o logic) WithdrawalApply(args ArgsWithdrawalApply) error {
|
|
now := time.Now()
|
|
return mysql.Db.Table((&company.Apply{}).TableName()).
|
|
Where("state=1").Where("id = ?", args.ID).Updates(company.Apply{
|
|
State: args.Apply,
|
|
ApplyUserID: args.ManageUserID,
|
|
ApplyTime: &now,
|
|
Content: args.Content,
|
|
}).Error
|
|
}
|
|
|
|
type ArgsWithdrawalPass struct {
|
|
ID int `json:"id" validate:"required"`
|
|
Proof string `json:"proof"`
|
|
ManageUserID int `json:"-"`
|
|
}
|
|
|
|
func (o logic) WithdrawPass(args ArgsWithdrawalPass) error {
|
|
now := time.Now()
|
|
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
var record company.Apply
|
|
if e := tx.First(&record, "id = ?", args.ID).Error; e != nil {
|
|
return e
|
|
}
|
|
|
|
if e := tx.Table((&company.Apply{}).TableName()).
|
|
Where("state=2").Where("id = ?", args.ID).Updates(company.Apply{
|
|
State: 3,
|
|
ProcessUserID: args.ManageUserID,
|
|
ProcessTime: &now,
|
|
Proof: args.Proof,
|
|
}).Error; e != nil {
|
|
return e
|
|
}
|
|
var w user.RecookUserWalletModel
|
|
if e := tx.First(&w, "user_id = ?", record.UserID).Error; e != nil {
|
|
return e
|
|
}
|
|
if row := tx.Model(&w).Where("version = ?", w.Version).Updates(map[string]interface{}{
|
|
"balance": gorm.Expr("balance - ?", record.Amount),
|
|
"version": gorm.Expr("version + 1"),
|
|
}).RowsAffected; row != 1 {
|
|
return errors.New("更新失败")
|
|
}
|
|
if e := tx.Create(&user.RecookUserWalletBalanceListModel{
|
|
UserId: uint(record.UserID),
|
|
IncomeType: user.RecookUserWalletBalanceListIncomeTypeWithdraw,
|
|
Amount: record.Amount.Neg(),
|
|
Title: fmt.Sprintf("提现%f", record.Amount.BigFloat()),
|
|
Comment: "提现",
|
|
OrderId: 0,
|
|
OrderGoodsId: 0,
|
|
OrderTime: formatime.Second{},
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
ProfitID: 0,
|
|
}).Error; e != nil {
|
|
return e
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|