migrate controllers
Some checks failed
build quyun / Build (push) Failing after 1m30s

This commit is contained in:
2025-12-19 23:33:02 +08:00
parent 557a641f41
commit 49072ddd79
37 changed files with 1944 additions and 69 deletions

View File

@@ -5,6 +5,10 @@ import (
"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"
@@ -41,7 +45,7 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
log.Infof("[Start] Working on job with strings: %+v", job.Args)
defer log.Infof("[End] Finished %s", job.Args.Kind())
order, err := model.OrdersModel().GetByOrderNo(context.Background(), job.Args.OrderNo)
order, err := services.Orders.GetByOrderNO(ctx, job.Args.OrderNo)
if err != nil {
log.Errorf("GetByOrderNo error:%v", err)
return err
@@ -52,7 +56,7 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
}
user, err := model.UsersModel().GetByID(context.Background(), order.UserID)
user, err := services.Users.FindByID(ctx, order.UserID)
if err != nil {
log.Errorf("GetByID error:%v", err)
return errors.Wrap(err, "get user error")
@@ -63,7 +67,7 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
order.PaymentMethod = "balance"
order.Status = fields.OrderStatusCompleted
meta := order.Meta.Data
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)
@@ -73,28 +77,29 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
}
log.Infof("Updated order details: %+v", order)
tx, err := model.Transaction(ctx)
tx := models.Q.Begin()
if err != nil {
return errors.Wrap(err, "Transaction error")
}
defer tx.Rollback()
// update user balance
err = user.SetBalance(ctx, user.Balance-payPrice)
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 := user.BuyPosts(context.Background(), order.PostID, order.Price); err != nil {
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(context.Background()); err != nil {
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")