This commit is contained in:
2025-05-27 18:35:01 +08:00
parent ca66cc91d4
commit 79e1ba2616
115 changed files with 22039 additions and 1045 deletions

View File

@@ -3,20 +3,21 @@ package queue
import (
"context"
"time"
"tyc-server/app/main/api/internal/config"
"tyc-server/app/main/api/internal/svc"
"github.com/hibiken/asynq"
"github.com/zeromicro/go-zero/core/logx"
)
const TASKTIME = "0 3 * * *"
type CleanQueryDataHandler struct {
config *config.Config
svcCtx *svc.ServiceContext
}
func NewCleanQueryDataHandler(svcCtx *svc.ServiceContext) *CleanQueryDataHandler {
func NewCleanQueryDataHandler(config *config.Config, svcCtx *svc.ServiceContext) *CleanQueryDataHandler {
return &CleanQueryDataHandler{
config: config,
svcCtx: svcCtx,
}
}
@@ -25,14 +26,10 @@ func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task)
now := time.Now().Format("2006-01-02 15:04:05")
logx.Infof("%s - 开始执行查询数据清理任务", now)
// 计算7天前的时间
sevenDaysAgo := time.Now().AddDate(0, 0, -7)
// 计算15天前的时间
sevenDaysAgo := time.Now().AddDate(0, 0, -l.config.CleanTask.Days)
// 创建查询条件排除product_id为4的记录
conditions := l.svcCtx.QueryModel.SelectBuilder().Where("product_id != ?", 4)
// 调用QueryModel删除7天前的数据排除product_id为4的记录
result, err := l.svcCtx.QueryModel.DeleteBefore(ctx, sevenDaysAgo, conditions)
result, err := l.svcCtx.QueryModel.DeleteBefore(ctx, sevenDaysAgo)
if err != nil {
logx.Errorf("%s - 清理查询数据失败: %v", time.Now().Format("2006-01-02 15:04:05"), err)
return err

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"strings"
"tyc-server/app/main/api/internal/service"
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"tyc-server/app/main/model"
@@ -159,33 +160,29 @@ func (l *PaySuccessNotifyUserHandler) handleError(ctx context.Context, err error
return asynq.SkipRetry
}
payRefundRequest := &service.RefundRequest{
OrderId: order.Id,
RefundAmount: order.Amount,
Reason: "查询失败退款",
}
// 退款
if order.PaymentPlatform == "wechat" {
refundErr := l.svcCtx.WechatPayService.WeChatRefund(ctx, order.OrderNo, order.Amount, order.Amount)
refundResp, refundErr := l.svcCtx.PayService.Refund(ctx, payRefundRequest)
if refundErr != nil {
logx.Error(refundErr)
return asynq.SkipRetry
}
if refundResp.Status == "success" {
// 直接成功
} else {
return asynq.SkipRetry
}
} else {
refund, refundErr := l.svcCtx.AlipayService.AliRefund(ctx, order.OrderNo, order.Amount)
_, refundErr := l.svcCtx.PayService.Refund(ctx, payRefundRequest)
if refundErr != nil {
logx.Error(refundErr)
return asynq.SkipRetry
}
if refund.IsSuccess() {
logx.Errorf("支付宝退款成功, orderID: %d", order.Id)
// 更新订单状态为退款
order.Status = "refunded"
updateOrderErr := l.svcCtx.OrderModel.UpdateWithVersion(ctx, nil, order)
if updateOrderErr != nil {
logx.Errorf("更新订单状态失败订单ID: %d, 错误: %v", order.Id, updateOrderErr)
return fmt.Errorf("更新订单状态失败: %v", updateOrderErr)
}
return asynq.SkipRetry
} else {
logx.Errorf("支付宝退款失败:%v", refundErr)
return asynq.SkipRetry
}
// 直接成功
}

View File

@@ -3,19 +3,23 @@ package queue
import (
"context"
"fmt"
"tyc-server/app/main/api/internal/config"
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"github.com/hibiken/asynq"
"github.com/zeromicro/go-zero/core/logx"
)
type CronJob struct {
config *config.Config
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCronJob(ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
func NewCronJob(config *config.Config, ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
return &CronJob{
config: config,
ctx: ctx,
svcCtx: svcCtx,
}
@@ -24,17 +28,21 @@ func NewCronJob(ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
func (l *CronJob) Register() *asynq.ServeMux {
redisClientOpt := asynq.RedisClientOpt{Addr: l.svcCtx.Config.CacheRedis[0].Host, Password: l.svcCtx.Config.CacheRedis[0].Pass}
scheduler := asynq.NewScheduler(redisClientOpt, nil)
task := asynq.NewTask(types.MsgCleanQueryData, nil, nil)
_, err := scheduler.Register(TASKTIME, task)
if err != nil {
panic(fmt.Sprintf("定时任务注册失败:%v", err))
// 根据配置决定是否启动清理任务
if l.config.CleanTask.Enabled {
task := asynq.NewTask(types.MsgCleanQueryData, nil, nil)
_, err := scheduler.Register(l.config.CleanTask.Time, task)
if err != nil {
panic(fmt.Sprintf("定时任务注册失败:%v", err))
}
scheduler.Start()
logx.Infof("清理数据定时任务已启动")
}
scheduler.Start()
fmt.Println("定时任务启动!!!")
mux := asynq.NewServeMux()
mux.Handle(types.MsgPaySuccessQuery, NewPaySuccessNotifyUserHandler(l.svcCtx))
mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.svcCtx))
mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.config, l.svcCtx))
mux.Handle(types.MsgCarMaintenanceQuery, NewCarMaintenanceQueryHandler(l.svcCtx))
return mux