From 9a31ec15a40f7b743b15462c7782d5475a585346 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 13 Jan 2025 17:21:37 +0800 Subject: [PATCH] v1.2.3 template (clean feature not done) --- .../api/internal/logic/pay/paymentlogic.go | 24 ++---------------- .../cmd/api/internal/queue/cleanQueryData.go | 25 +++++++++++++++++++ app/user/cmd/api/internal/queue/routes.go | 13 +++++++++- app/user/cmd/api/internal/types/taskname.go | 1 + app/user/cmd/api/main.go | 1 + 5 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 app/user/cmd/api/internal/queue/cleanQueryData.go diff --git a/app/user/cmd/api/internal/logic/pay/paymentlogic.go b/app/user/cmd/api/internal/logic/pay/paymentlogic.go index 5d6c0be..cc4d5a9 100644 --- a/app/user/cmd/api/internal/logic/pay/paymentlogic.go +++ b/app/user/cmd/api/internal/logic/pay/paymentlogic.go @@ -8,14 +8,12 @@ import ( "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlx" - "math" "qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/types" "qnc-server/app/user/model" "qnc-server/common/ctxdata" "qnc-server/common/xerr" "qnc-server/pkg/lzkit/crypto" - "time" ) type PaymentLogic struct { @@ -78,28 +76,10 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取用户信息失败: %+v", err) } - currentTime := time.Now() - - var mhour int if user.Inside == 1 { - mhour = 0 + amount = 0.01 } else { - mhour = 12 - } - // 设置时间范围:2025年1月1日中午12点到2025年1月2日中午12点 - startTime := time.Date(2025, 1, 1, mhour, 0, 0, 0, time.Local) - endTime := time.Date(2025, 1, 2, 12, 0, 0, 0, time.Local) - - if currentTime.After(startTime) && currentTime.Before(endTime) { - // 打两折 - amount = product.SellPrice * 0.2 - amount = math.Round(amount*100) / 100 - } else { - if user.Inside == 1 { - amount = 0.01 - } else { - amount = product.SellPrice - } + amount = product.SellPrice } var createOrderErr error if req.PayMethod == "wechatpay" { diff --git a/app/user/cmd/api/internal/queue/cleanQueryData.go b/app/user/cmd/api/internal/queue/cleanQueryData.go new file mode 100644 index 0000000..fe2f705 --- /dev/null +++ b/app/user/cmd/api/internal/queue/cleanQueryData.go @@ -0,0 +1,25 @@ +package queue + +import ( + "context" + "fmt" + "github.com/hibiken/asynq" + "qnc-server/app/user/cmd/api/internal/svc" +) + +const TASKTIME = "32 * * * *" + +type CleanQueryDataHandler struct { + svcCtx *svc.ServiceContext +} + +func NewCleanQueryDataHandler(svcCtx *svc.ServiceContext) *CleanQueryDataHandler { + return &CleanQueryDataHandler{ + svcCtx: svcCtx, + } +} + +func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error { + fmt.Println("企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅-ProcessTask") + return nil +} diff --git a/app/user/cmd/api/internal/queue/routes.go b/app/user/cmd/api/internal/queue/routes.go index 1e5923c..d22f89b 100644 --- a/app/user/cmd/api/internal/queue/routes.go +++ b/app/user/cmd/api/internal/queue/routes.go @@ -2,6 +2,7 @@ package queue import ( "context" + "fmt" "github.com/hibiken/asynq" "qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/types" @@ -20,9 +21,19 @@ 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)) + } + scheduler.Start() + fmt.Println("定时任务启动!!!") mux := asynq.NewServeMux() - mux.Handle(types.MsgPaySuccessQuery, NewPaySuccessNotifyUserHandler(l.svcCtx)) + mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.svcCtx)) + return mux } diff --git a/app/user/cmd/api/internal/types/taskname.go b/app/user/cmd/api/internal/types/taskname.go index 98029d9..33329ee 100644 --- a/app/user/cmd/api/internal/types/taskname.go +++ b/app/user/cmd/api/internal/types/taskname.go @@ -1,3 +1,4 @@ package types const MsgPaySuccessQuery = "msg:pay_success:query" +const MsgCleanQueryData = "msg:clean_query_data" diff --git a/app/user/cmd/api/main.go b/app/user/cmd/api/main.go index 4e7ef8b..93b3307 100644 --- a/app/user/cmd/api/main.go +++ b/app/user/cmd/api/main.go @@ -50,6 +50,7 @@ func main() { logx.WithContext(ctx).Errorf("异步任务启动失败: %+v", err) os.Exit(1) } + fmt.Println("异步任务启动!!!") }() server := rest.MustNewServer(c.RestConf)