115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package jobs
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"quyun/v2/app/services"
|
|
"quyun/v2/database/models"
|
|
"quyun/v2/pkg/fields"
|
|
|
|
"github.com/pkg/errors"
|
|
. "github.com/riverqueue/river"
|
|
log "github.com/sirupsen/logrus"
|
|
_ "go.ipao.vip/atom"
|
|
"go.ipao.vip/atom/contracts"
|
|
)
|
|
|
|
var _ contracts.JobArgs = (*BalancePayNotify)(nil)
|
|
|
|
type BalancePayNotify struct {
|
|
OrderNo string `json:"order_no"`
|
|
}
|
|
|
|
func (s BalancePayNotify) InsertOpts() InsertOpts {
|
|
return InsertOpts{
|
|
Queue: QueueDefault,
|
|
Priority: PriorityDefault,
|
|
}
|
|
}
|
|
|
|
func (BalancePayNotify) Kind() string { return "balance_pay_notify" }
|
|
func (a BalancePayNotify) UniqueID() string { return a.Kind() }
|
|
|
|
var _ Worker[BalancePayNotify] = (*BalancePayNotifyWorker)(nil)
|
|
|
|
// @provider(job)
|
|
type BalancePayNotifyWorker struct {
|
|
WorkerDefaults[BalancePayNotify]
|
|
}
|
|
|
|
func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNotify]) error {
|
|
log := log.WithField("job", job.Args.Kind())
|
|
|
|
log.Infof("[Start] Working on job with strings: %+v", job.Args)
|
|
defer log.Infof("[End] Finished %s", job.Args.Kind())
|
|
|
|
order, err := services.Orders.GetByOrderNO(ctx, job.Args.OrderNo)
|
|
if err != nil {
|
|
log.Errorf("GetByOrderNo error:%v", err)
|
|
return err
|
|
}
|
|
|
|
if order.Status != fields.OrderStatusPending {
|
|
log.Infof("Order %s is paid, processing...", job.Args.OrderNo)
|
|
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
|
|
}
|
|
|
|
user, err := services.Users.FindByID(ctx, order.UserID)
|
|
if err != nil {
|
|
log.Errorf("GetByID error:%v", err)
|
|
return errors.Wrap(err, "get user error")
|
|
}
|
|
|
|
payPrice := order.Price * int64(order.Discount) / 100
|
|
|
|
order.PaymentMethod = "balance"
|
|
order.Status = fields.OrderStatusCompleted
|
|
|
|
meta := order.Meta.Data()
|
|
|
|
if user.Balance-meta.CostBalance < 0 {
|
|
log.Errorf("User %d balance is not enough, current balance: %d, cost: %d", user.ID, user.Balance, payPrice)
|
|
return JobCancel(
|
|
fmt.Errorf("User %d balance is not enough, current balance: %d, cost: %d", user.ID, user.Balance, payPrice),
|
|
)
|
|
}
|
|
|
|
log.Infof("Updated order details: %+v", order)
|
|
tx := models.Q.Begin()
|
|
if err != nil {
|
|
return errors.Wrap(err, "Transaction error")
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
// update user balance
|
|
err = services.Users.DescBalance(ctx, user.ID, payPrice)
|
|
if err != nil {
|
|
log.WithError(err).Error("SetBalance error")
|
|
return JobCancel(errors.Wrap(err, "set user balance failed"))
|
|
}
|
|
|
|
if err := services.Users.BuyPosts(context.Background(), user.ID, order.PostID, order.Price); err != nil {
|
|
log.Errorf("BuyPosts error:%v", err)
|
|
return errors.Wrap(err, "BuyPosts error")
|
|
}
|
|
|
|
if _, err := order.Update(ctx); err != nil {
|
|
log.Errorf("Update order error:%v", err)
|
|
return errors.Wrap(err, "Update order error")
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
log.Errorf("Commit error:%v", err)
|
|
return errors.Wrap(err, "Commit error")
|
|
}
|
|
|
|
log.Infof("Successfully processed order %s", order.OrderNo)
|
|
return nil
|
|
}
|
|
|
|
func (w *BalancePayNotifyWorker) NextRetry(job *Job[BalancePayNotify]) time.Time {
|
|
return time.Now().Add(30 * time.Second)
|
|
}
|