From cc1680cf01feb0f5adf7587646b375d9c879250b Mon Sep 17 00:00:00 2001 From: 18278715334 <18278715334@163.com> Date: Sat, 27 Dec 2025 13:51:26 +0800 Subject: [PATCH] fix analysis --- app/main/api/desc/admin/admin_agent.api | 14 ++++ app/main/api/desc/admin/admin_feature.api | 34 ++++---- app/main/api/desc/admin/order.api | 34 ++++++++ .../admingetwithdrawalstatisticshandler.go | 29 +++++++ .../admingetrefundstatisticshandler.go | 29 +++++++ .../admingetrevenuestatisticshandler.go | 30 +++++++ app/main/api/internal/handler/routes.go | 17 ++++ .../admingetwithdrawalstatisticslogic.go | 60 +++++++++++++ .../admin_order/admingetorderdetaillogic.go | 1 + .../admin_order/admingetorderlistlogic.go | 1 + .../admingetrefundstatisticslogic.go | 60 +++++++++++++ .../admingetrevenuestatisticslogic.go | 84 +++++++++++++++++++ .../api/internal/queue/paySuccessNotify.go | 42 +++++++++- .../api/internal/service/apirequestService.go | 10 +-- app/main/api/internal/types/types.go | 38 +++++++-- app/main/model/orderModel_gen.go | 53 ++++++------ deploy/script/gen_models.ps1 | 6 +- 17 files changed, 485 insertions(+), 57 deletions(-) create mode 100644 app/main/api/internal/handler/admin_agent/admingetwithdrawalstatisticshandler.go create mode 100644 app/main/api/internal/handler/admin_order/admingetrefundstatisticshandler.go create mode 100644 app/main/api/internal/handler/admin_order/admingetrevenuestatisticshandler.go create mode 100644 app/main/api/internal/logic/admin_agent/admingetwithdrawalstatisticslogic.go create mode 100644 app/main/api/internal/logic/admin_order/admingetrefundstatisticslogic.go create mode 100644 app/main/api/internal/logic/admin_order/admingetrevenuestatisticslogic.go diff --git a/app/main/api/desc/admin/admin_agent.api b/app/main/api/desc/admin/admin_agent.api index 5e426a6..19e1bc1 100644 --- a/app/main/api/desc/admin/admin_agent.api +++ b/app/main/api/desc/admin/admin_agent.api @@ -65,6 +65,10 @@ service main { // 银行卡提现审核(确认/拒绝) @handler AdminReviewBankCardWithdrawal post /agent-withdrawal/bank-card/review (AdminReviewBankCardWithdrawalReq) returns (AdminReviewBankCardWithdrawalResp) + + // 获取提现统计数据 + @handler AdminGetWithdrawalStatistics + get /agent-withdrawal/statistics (AdminGetWithdrawalStatisticsReq) returns (AdminGetWithdrawalStatisticsResp) } type ( @@ -405,4 +409,14 @@ type ( AdminReviewBankCardWithdrawalResp { Success bool `json:"success"` // 是否成功 } + + // 获取提现统计数据请求 + AdminGetWithdrawalStatisticsReq { + } + + // 获取提现统计数据响应 + AdminGetWithdrawalStatisticsResp { + TotalWithdrawalAmount float64 `json:"total_withdrawal_amount"` // 总提现金额 + TodayWithdrawalAmount float64 `json:"today_withdrawal_amount"` // 今日提现金额 + } ) \ No newline at end of file diff --git a/app/main/api/desc/admin/admin_feature.api b/app/main/api/desc/admin/admin_feature.api index 4f4c1db..57a0e9a 100644 --- a/app/main/api/desc/admin/admin_feature.api +++ b/app/main/api/desc/admin/admin_feature.api @@ -45,8 +45,9 @@ service main { type ( // 创建功能请求 AdminCreateFeatureReq { - ApiId string `json:"api_id"` // API标识 - Name string `json:"name"` // 描述 + ApiId string `json:"api_id"` // API标识 + Name string `json:"name"` // 描述 + CostPrice float64 `json:"cost_price"` // 成本价 } // 创建功能响应 AdminCreateFeatureResp { @@ -54,9 +55,10 @@ type ( } // 更新功能请求 AdminUpdateFeatureReq { - Id int64 `path:"id"` // 功能ID - ApiId *string `json:"api_id,optional"` // API标识 - Name *string `json:"name,optional"` // 描述 + Id int64 `path:"id"` // 功能ID + ApiId *string `json:"api_id,optional"` // API标识 + Name *string `json:"name,optional"` // 描述 + CostPrice *float64 `json:"cost_price,optional"` // 成本价 } // 更新功能响应 AdminUpdateFeatureResp { @@ -79,11 +81,12 @@ type ( } // 功能列表项 FeatureListItem { - Id int64 `json:"id"` // 功能ID - ApiId string `json:"api_id"` // API标识 - Name string `json:"name"` // 描述 - CreateTime string `json:"create_time"` // 创建时间 - UpdateTime string `json:"update_time"` // 更新时间 + Id int64 `json:"id"` // 功能ID + ApiId string `json:"api_id"` // API标识 + Name string `json:"name"` // 描述 + CostPrice float64 `json:"cost_price"` // 成本价 + CreateTime string `json:"create_time"` // 创建时间 + UpdateTime string `json:"update_time"` // 更新时间 } // 获取功能列表响应 AdminGetFeatureListResp { @@ -96,11 +99,12 @@ type ( } // 获取功能详情响应 AdminGetFeatureDetailResp { - Id int64 `json:"id"` // 功能ID - ApiId string `json:"api_id"` // API标识 - Name string `json:"name"` // 描述 - CreateTime string `json:"create_time"` // 创建时间 - UpdateTime string `json:"update_time"` // 更新时间 + Id int64 `json:"id"` // 功能ID + ApiId string `json:"api_id"` // API标识 + Name string `json:"name"` // 描述 + CostPrice float64 `json:"cost_price"` // 成本价 + CreateTime string `json:"create_time"` // 创建时间 + UpdateTime string `json:"update_time"` // 更新时间 } // 配置功能示例数据请求 AdminConfigFeatureExampleReq { diff --git a/app/main/api/desc/admin/order.api b/app/main/api/desc/admin/order.api index 84e606c..8fec3b3 100644 --- a/app/main/api/desc/admin/order.api +++ b/app/main/api/desc/admin/order.api @@ -39,6 +39,14 @@ service main { @doc "重新执行代理处理" @handler AdminRetryAgentProcess post /retry-agent-process/:id (AdminRetryAgentProcessReq) returns (AdminRetryAgentProcessResp) + + @doc "获取退款统计数据" + @handler AdminGetRefundStatistics + get /refund-statistics (AdminGetRefundStatisticsReq) returns (AdminGetRefundStatisticsResp) + + @doc "获取收入和利润统计数据" + @handler AdminGetRevenueStatistics + get /revenue-statistics (AdminGetRevenueStatisticsReq) returns (AdminGetRevenueStatisticsResp) } type ( @@ -60,6 +68,7 @@ type ( PayTimeEnd string `form:"pay_time_end,optional"` // 支付时间结束 RefundTimeStart string `form:"refund_time_start,optional"` // 退款时间开始 RefundTimeEnd string `form:"refund_time_end,optional"` // 退款时间结束 + SalesCost float64 `form:"sales_cost,optional"` // 成本价 } // 列表响应 AdminGetOrderListResp { @@ -75,6 +84,7 @@ type ( PaymentPlatform string `json:"payment_platform"` // 支付方式 PaymentScene string `json:"payment_scene"` // 支付平台 Amount float64 `json:"amount"` // 金额 + SalesCost float64 `json:"sales_cost"` // 成本价 Status string `json:"status"` // 支付状态:pending-待支付,paid-已支付,refunded-已退款,closed-已关闭,failed-支付失败 QueryState string `json:"query_state"` // 查询状态:pending-待查询,success-查询成功,failed-查询失败 processing-查询中 CreateTime string `json:"create_time"` // 创建时间 @@ -83,6 +93,7 @@ type ( IsPromotion int64 `json:"is_promotion"` // 是否推广订单:0-否,1-是 IsAgentOrder bool `json:"is_agent_order"` // 是否是代理订单 AgentProcessStatus string `json:"agent_process_status"` // 代理事务处理状态:not_agent-非代理订单,success-处理成功,failed-处理失败,pending-待处理 + } // 详情请求 AdminGetOrderDetailReq { @@ -97,6 +108,7 @@ type ( PaymentPlatform string `json:"payment_platform"` // 支付方式 PaymentScene string `json:"payment_scene"` // 支付平台 Amount float64 `json:"amount"` // 金额 + SalesCost float64 `json:"sales_cost"` // 成本价 Status string `json:"status"` // 支付状态:pending-待支付,paid-已支付,refunded-已退款,closed-已关闭,failed-支付失败 QueryState string `json:"query_state"` // 查询状态:pending-待查询,success-查询成功,failed-查询失败 processing-查询中 CreateTime string `json:"create_time"` // 创建时间 @@ -170,4 +182,26 @@ type ( Message string `json:"message"` // 执行结果消息 ProcessedAt string `json:"processed_at"` // 处理时间 } + + // 获取退款统计数据请求 + AdminGetRefundStatisticsReq { + } + + // 获取退款统计数据响应 + AdminGetRefundStatisticsResp { + TotalRefundAmount float64 `json:"total_refund_amount"` // 总退款金额 + TodayRefundAmount float64 `json:"today_refund_amount"` // 今日退款金额 + } + + // 获取收入和利润统计数据请求 + AdminGetRevenueStatisticsReq { + } + + // 获取收入和利润统计数据响应 + AdminGetRevenueStatisticsResp { + TotalRevenueAmount float64 `json:"total_revenue_amount"` // 总收入金额 + TodayRevenueAmount float64 `json:"today_revenue_amount"` // 今日收入金额 + TotalProfitAmount float64 `json:"total_profit_amount"` // 总利润金额 + TodayProfitAmount float64 `json:"today_profit_amount"` // 今日利润金额 + } ) \ No newline at end of file diff --git a/app/main/api/internal/handler/admin_agent/admingetwithdrawalstatisticshandler.go b/app/main/api/internal/handler/admin_agent/admingetwithdrawalstatisticshandler.go new file mode 100644 index 0000000..e16552e --- /dev/null +++ b/app/main/api/internal/handler/admin_agent/admingetwithdrawalstatisticshandler.go @@ -0,0 +1,29 @@ +package admin_agent + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "tydata-server/app/main/api/internal/logic/admin_agent" + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + "tydata-server/common/result" + "tydata-server/pkg/lzkit/validator" +) + +func AdminGetWithdrawalStatisticsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdminGetWithdrawalStatisticsReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + if err := validator.Validate(req); err != nil { + result.ParamValidateErrorResult(r, w, err) + return + } + l := admin_agent.NewAdminGetWithdrawalStatisticsLogic(r.Context(), svcCtx) + resp, err := l.AdminGetWithdrawalStatistics(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/app/main/api/internal/handler/admin_order/admingetrefundstatisticshandler.go b/app/main/api/internal/handler/admin_order/admingetrefundstatisticshandler.go new file mode 100644 index 0000000..0027ee6 --- /dev/null +++ b/app/main/api/internal/handler/admin_order/admingetrefundstatisticshandler.go @@ -0,0 +1,29 @@ +package admin_order + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "tydata-server/app/main/api/internal/logic/admin_order" + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + "tydata-server/common/result" + "tydata-server/pkg/lzkit/validator" +) + +func AdminGetRefundStatisticsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdminGetRefundStatisticsReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + if err := validator.Validate(req); err != nil { + result.ParamValidateErrorResult(r, w, err) + return + } + l := admin_order.NewAdminGetRefundStatisticsLogic(r.Context(), svcCtx) + resp, err := l.AdminGetRefundStatistics(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/app/main/api/internal/handler/admin_order/admingetrevenuestatisticshandler.go b/app/main/api/internal/handler/admin_order/admingetrevenuestatisticshandler.go new file mode 100644 index 0000000..775a7ce --- /dev/null +++ b/app/main/api/internal/handler/admin_order/admingetrevenuestatisticshandler.go @@ -0,0 +1,30 @@ +package admin_order + +import ( + "net/http" + + "tydata-server/app/main/api/internal/logic/admin_order" + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + "tydata-server/common/result" + "tydata-server/pkg/lzkit/validator" + + "github.com/zeromicro/go-zero/rest/httpx" +) + +func AdminGetRevenueStatisticsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AdminGetRevenueStatisticsReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + if err := validator.Validate(req); err != nil { + result.ParamValidateErrorResult(r, w, err) + return + } + l := admin_order.NewAdminGetRevenueStatisticsLogic(r.Context(), svcCtx) + resp, err := l.AdminGetRevenueStatistics(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/app/main/api/internal/handler/routes.go b/app/main/api/internal/handler/routes.go index 78a4708..c21241e 100644 --- a/app/main/api/internal/handler/routes.go +++ b/app/main/api/internal/handler/routes.go @@ -97,6 +97,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/agent-withdrawal/list", Handler: admin_agent.AdminGetAgentWithdrawalListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/agent-withdrawal/statistics", + Handler: admin_agent.AdminGetWithdrawalStatisticsHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/list", @@ -309,6 +314,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/list", Handler: admin_order.AdminGetOrderListHandler(serverCtx), }, + { + // 获取退款统计数据 + Method: http.MethodGet, + Path: "/refund-statistics", + Handler: admin_order.AdminGetRefundStatisticsHandler(serverCtx), + }, { // 订单退款 Method: http.MethodPost, @@ -321,6 +332,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/retry-agent-process/:id", Handler: admin_order.AdminRetryAgentProcessHandler(serverCtx), }, + { + // 获取收入和利润统计数据 + Method: http.MethodGet, + Path: "/revenue-statistics", + Handler: admin_order.AdminGetRevenueStatisticsHandler(serverCtx), + }, { // 更新订单 Method: http.MethodPut, diff --git a/app/main/api/internal/logic/admin_agent/admingetwithdrawalstatisticslogic.go b/app/main/api/internal/logic/admin_agent/admingetwithdrawalstatisticslogic.go new file mode 100644 index 0000000..3e7e69d --- /dev/null +++ b/app/main/api/internal/logic/admin_agent/admingetwithdrawalstatisticslogic.go @@ -0,0 +1,60 @@ +package admin_agent + +import ( + "context" + "fmt" + "time" + + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AdminGetWithdrawalStatisticsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAdminGetWithdrawalStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetWithdrawalStatisticsLogic { + return &AdminGetWithdrawalStatisticsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *AdminGetWithdrawalStatisticsLogic) AdminGetWithdrawalStatistics(req *types.AdminGetWithdrawalStatisticsReq) (resp *types.AdminGetWithdrawalStatisticsResp, err error) { + // 获取今日的开始和结束时间 + today := time.Now() + startOfDay := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()) + endOfDay := startOfDay.Add(24 * time.Hour) + + // 构建查询条件 + builder := l.svcCtx.AgentWithdrawalModel.SelectBuilder() + + // 查询总提现金额(status=2表示成功) + totalBuilder := builder.Where("status = ?", 2) + totalWithdrawalAmount, err := l.svcCtx.AgentWithdrawalModel.FindSum(l.ctx, totalBuilder, "amount") + if err != nil { + logx.Errorf("查询总提现金额失败: %v", err) + return nil, fmt.Errorf("查询总提现金额失败: %w", err) + } + + // 查询今日提现金额(status=2表示成功) + todayBuilder := builder.Where("status = ? AND create_time >= ? AND create_time < ?", 2, startOfDay, endOfDay) + todayWithdrawalAmount, err := l.svcCtx.AgentWithdrawalModel.FindSum(l.ctx, todayBuilder, "amount") + if err != nil { + logx.Errorf("查询今日提现金额失败: %v", err) + return nil, fmt.Errorf("查询今日提现金额失败: %w", err) + } + + // 构建响应 + resp = &types.AdminGetWithdrawalStatisticsResp{ + TotalWithdrawalAmount: totalWithdrawalAmount, + TodayWithdrawalAmount: todayWithdrawalAmount, + } + + return resp, nil +} diff --git a/app/main/api/internal/logic/admin_order/admingetorderdetaillogic.go b/app/main/api/internal/logic/admin_order/admingetorderdetaillogic.go index 6969204..16f1f31 100644 --- a/app/main/api/internal/logic/admin_order/admingetorderdetaillogic.go +++ b/app/main/api/internal/logic/admin_order/admingetorderdetaillogic.go @@ -115,6 +115,7 @@ func (l *AdminGetOrderDetailLogic) AdminGetOrderDetail(req *types.AdminGetOrderD PaymentPlatform: order.PaymentPlatform, PaymentScene: order.PaymentScene, Amount: order.Amount, + SalesCost: order.SalesCost, Status: order.Status, CreateTime: order.CreateTime.Format("2006-01-02 15:04:05"), UpdateTime: order.UpdateTime.Format("2006-01-02 15:04:05"), diff --git a/app/main/api/internal/logic/admin_order/admingetorderlistlogic.go b/app/main/api/internal/logic/admin_order/admingetorderlistlogic.go index d166002..3c831c3 100644 --- a/app/main/api/internal/logic/admin_order/admingetorderlistlogic.go +++ b/app/main/api/internal/logic/admin_order/admingetorderlistlogic.go @@ -264,6 +264,7 @@ func (l *AdminGetOrderListLogic) AdminGetOrderList(req *types.AdminGetOrderListR PaymentPlatform: order.PaymentPlatform, PaymentScene: order.PaymentScene, Amount: order.Amount, + SalesCost: order.SalesCost, Status: order.Status, CreateTime: order.CreateTime.Format("2006-01-02 15:04:05"), QueryState: queryStateMap[order.Id], diff --git a/app/main/api/internal/logic/admin_order/admingetrefundstatisticslogic.go b/app/main/api/internal/logic/admin_order/admingetrefundstatisticslogic.go new file mode 100644 index 0000000..31ba0a1 --- /dev/null +++ b/app/main/api/internal/logic/admin_order/admingetrefundstatisticslogic.go @@ -0,0 +1,60 @@ +package admin_order + +import ( + "context" + "fmt" + "time" + + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AdminGetRefundStatisticsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAdminGetRefundStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetRefundStatisticsLogic { + return &AdminGetRefundStatisticsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *AdminGetRefundStatisticsLogic) AdminGetRefundStatistics(req *types.AdminGetRefundStatisticsReq) (resp *types.AdminGetRefundStatisticsResp, err error) { + // 获取今日的开始和结束时间 + today := time.Now() + startOfDay := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()) + endOfDay := startOfDay.Add(24 * time.Hour) + + // 构建查询条件 + builder := l.svcCtx.OrderModel.SelectBuilder() + + // 查询总退款金额(status=refunded表示已退款) + totalBuilder := builder.Where("status = ?", "refunded") + totalRefundAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalBuilder, "amount") + if err != nil { + logx.Errorf("查询总退款金额失败: %v", err) + return nil, fmt.Errorf("查询总退款金额失败: %w", err) + } + + // 查询今日退款金额(status=refunded表示已退款,且退款时间为今日) + todayBuilder := builder.Where("status = ? AND refund_time >= ? AND refund_time < ?", "refunded", startOfDay, endOfDay) + todayRefundAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, todayBuilder, "amount") + if err != nil { + logx.Errorf("查询今日退款金额失败: %v", err) + return nil, fmt.Errorf("查询今日退款金额失败: %w", err) + } + + // 构建响应 + resp = &types.AdminGetRefundStatisticsResp{ + TotalRefundAmount: totalRefundAmount, + TodayRefundAmount: todayRefundAmount, + } + + return resp, nil +} diff --git a/app/main/api/internal/logic/admin_order/admingetrevenuestatisticslogic.go b/app/main/api/internal/logic/admin_order/admingetrevenuestatisticslogic.go new file mode 100644 index 0000000..f328365 --- /dev/null +++ b/app/main/api/internal/logic/admin_order/admingetrevenuestatisticslogic.go @@ -0,0 +1,84 @@ +package admin_order + +import ( + "context" + "fmt" + "time" + + "tydata-server/app/main/api/internal/svc" + "tydata-server/app/main/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AdminGetRevenueStatisticsLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAdminGetRevenueStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetRevenueStatisticsLogic { + return &AdminGetRevenueStatisticsLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *AdminGetRevenueStatisticsLogic) AdminGetRevenueStatistics(req *types.AdminGetRevenueStatisticsReq) (resp *types.AdminGetRevenueStatisticsResp, err error) { + // 获取今日的开始和结束时间 + today := time.Now() + startOfDay := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()) + endOfDay := startOfDay.Add(24 * time.Hour) + + // 构建查询条件 + builder := l.svcCtx.OrderModel.SelectBuilder() + + // 查询总收入金额(status=paid表示已支付) + totalRevenueBuilder := builder.Where("status = ?", "paid") + totalRevenueAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalRevenueBuilder, "amount") + if err != nil { + logx.Errorf("查询总收入金额失败: %v", err) + return nil, fmt.Errorf("查询总收入金额失败: %w", err) + } + + // 查询今日收入金额(status=paid表示已支付,且支付时间为今日) + todayRevenueBuilder := builder.Where("status = ? AND pay_time >= ? AND pay_time < ?", "paid", startOfDay, endOfDay) + todayRevenueAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, todayRevenueBuilder, "amount") + if err != nil { + logx.Errorf("查询今日收入金额失败: %v", err) + return nil, fmt.Errorf("查询今日收入金额失败: %w", err) + } + + // 查询总成本(status=paid表示已支付) + totalCostBuilder := builder.Where("status = ?", "paid") + totalCostAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalCostBuilder, "sales_cost") + if err != nil { + logx.Errorf("查询总成本金额失败: %v", err) + return nil, fmt.Errorf("查询总成本金额失败: %w", err) + } + + // 查询今日成本(status=paid表示已支付,且支付时间为今日) + todayCostBuilder := builder.Where("status = ? AND pay_time >= ? AND pay_time < ?", "paid", startOfDay, endOfDay) + todayCostAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, todayCostBuilder, "sales_cost") + if err != nil { + logx.Errorf("查询今日成本金额失败: %v", err) + return nil, fmt.Errorf("查询今日成本金额失败: %w", err) + } + + // 计算总利润 = 总收入 - 总成本 + totalProfitAmount := totalRevenueAmount - totalCostAmount + + // 计算今日利润 = 今日收入 - 今日成本 + todayProfitAmount := todayRevenueAmount - todayCostAmount + + // 构建响应 + resp = &types.AdminGetRevenueStatisticsResp{ + TotalRevenueAmount: totalRevenueAmount, + TodayRevenueAmount: todayRevenueAmount, + TotalProfitAmount: totalProfitAmount, + TodayProfitAmount: todayProfitAmount, + } + + return resp, nil +} diff --git a/app/main/api/internal/queue/paySuccessNotify.go b/app/main/api/internal/queue/paySuccessNotify.go index 262abdb..c125fd3 100644 --- a/app/main/api/internal/queue/paySuccessNotify.go +++ b/app/main/api/internal/queue/paySuccessNotify.go @@ -142,10 +142,50 @@ func (l *PaySuccessNotifyUserHandler) ProcessTask(ctx context.Context, t *asynq. } // 调用API请求服务 - combinedResponse, err := l.svcCtx.ApiRequestService.ProcessRequests(decryptData, product.Id) + responseData, err := l.svcCtx.ApiRequestService.ProcessRequests(decryptData, product.Id) if err != nil { return l.handleError(ctx, err, order, query) } + // responseData 对这个进行筛选筛选Success == true 的,此时调用成功的模块数据将产品关联的模块产品成本价(cost_price )累加到order.SalesCost中 + // 同时在响应数据中添加成本价信息 + for i, data := range responseData { + if data.Success { + // 通过API ID查找对应的feature,获取其成本价 + feature, err := l.svcCtx.FeatureModel.FindOneByApiId(ctx, data.ApiID) + if err != nil { + logx.Errorf("查找API ID %s 对应的feature失败: %v", data.ApiID, err) + continue + } + order.SalesCost += feature.CostPrice + + // 将成本价信息添加到响应数据中 + // 首先解析原始数据 + var originalData map[string]interface{} + if err := json.Unmarshal(data.Data, &originalData); err != nil { + logx.Errorf("解析API %s 的响应数据失败: %v", data.ApiID, err) + continue + } + + // 添加成本价字段 + originalData["cost_price"] = feature.CostPrice + + // 重新序列化数据 + updatedData, err := json.Marshal(originalData) + if err != nil { + logx.Errorf("序列化API %s 的更新后数据失败: %v", data.ApiID, err) + continue + } + + // 更新responseData中的Data字段 + responseData[i].Data = json.RawMessage(updatedData) + } + } + // 对返回的类型进行二进制转换 + combinedResponse, marshalErr := json.Marshal(responseData) + if marshalErr != nil { + err = fmt.Errorf("响应数据转 JSON 失败: %v", marshalErr) + return l.handleError(ctx, err, order, query) + } // 加密返回响应 encryptData, aesEncryptErr := crypto.AesEncrypt(combinedResponse, key) if aesEncryptErr != nil { diff --git a/app/main/api/internal/service/apirequestService.go b/app/main/api/internal/service/apirequestService.go index 1518d66..fa26ec8 100644 --- a/app/main/api/internal/service/apirequestService.go +++ b/app/main/api/internal/service/apirequestService.go @@ -58,7 +58,7 @@ type APIResponseData struct { } // ProcessRequests 处理请求 -func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]byte, error) { +func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]APIResponseData, error) { var ctx, cancel = context.WithCancel(context.Background()) defer cancel() build := a.productFeatureModel.SelectBuilder().Where(squirrel.Eq{ @@ -166,12 +166,7 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b return nil, fmt.Errorf("请求失败次数超过 %d 次: %v", errorLimit, allErrors) } - combinedResponse, err := json.Marshal(responseData) - if err != nil { - return nil, fmt.Errorf("响应数据转 JSON 失败: %v", err) - } - - return combinedResponse, nil + return responseData, nil } // ------------------------------------请求处理器-------------------------- @@ -1418,6 +1413,7 @@ func (a *ApiRequestService) ProcessIVYZ7F3ARequest(params []byte) ([]byte, error // 直接返回解密后的数据,而不是再次进行JSON编码 return convertTianyuanResponse(resp) } + // ProcessIVYZ3P9MRequest 学历实时查询 func (a *ApiRequestService) ProcessIVYZ3P9MRequest(params []byte) ([]byte, error) { idCard := gjson.GetBytes(params, "id_card") diff --git a/app/main/api/internal/types/types.go b/app/main/api/internal/types/types.go index 483d5fe..9ca7472 100644 --- a/app/main/api/internal/types/types.go +++ b/app/main/api/internal/types/types.go @@ -386,11 +386,10 @@ type AdminGetFeatureExampleResp struct { } type AdminGetFeatureListReq struct { - Page int64 `form:"page"` // 页码 - PageSize int64 `form:"pageSize"` // 每页数量 - ApiId *string `form:"api_id,optional"` // API标识 - Name *string `form:"name,optional"` // 描述 - CostPrice *float64 `form:"cost_price,optional"` // 成本价 + Page int64 `form:"page"` // 页码 + PageSize int64 `form:"pageSize"` // 每页数量 + ApiId *string `form:"api_id,optional"` // API标识 + Name *string `form:"name,optional"` // 描述 } type AdminGetFeatureListResp struct { @@ -443,6 +442,7 @@ type AdminGetOrderDetailResp struct { PaymentPlatform string `json:"payment_platform"` // 支付方式 PaymentScene string `json:"payment_scene"` // 支付平台 Amount float64 `json:"amount"` // 金额 + SalesCost float64 `json:"sales_cost"` // 成本价 Status string `json:"status"` // 支付状态:pending-待支付,paid-已支付,refunded-已退款,closed-已关闭,failed-支付失败 QueryState string `json:"query_state"` // 查询状态:pending-待查询,success-查询成功,failed-查询失败 processing-查询中 CreateTime string `json:"create_time"` // 创建时间 @@ -471,6 +471,7 @@ type AdminGetOrderListReq struct { PayTimeEnd string `form:"pay_time_end,optional"` // 支付时间结束 RefundTimeStart string `form:"refund_time_start,optional"` // 退款时间开始 RefundTimeEnd string `form:"refund_time_end,optional"` // 退款时间结束 + SalesCost float64 `form:"sales_cost,optional"` // 成本价 } type AdminGetOrderListResp struct { @@ -602,6 +603,24 @@ type AdminGetQueryDetailByOrderIdResp struct { QueryState string `json:"query_state"` // 查询状态 } +type AdminGetRefundStatisticsReq struct { +} + +type AdminGetRefundStatisticsResp struct { + TotalRefundAmount float64 `json:"total_refund_amount"` // 总退款金额 + TodayRefundAmount float64 `json:"today_refund_amount"` // 今日退款金额 +} + +type AdminGetRevenueStatisticsReq struct { +} + +type AdminGetRevenueStatisticsResp struct { + TotalRevenueAmount float64 `json:"total_revenue_amount"` // 总收入金额 + TodayRevenueAmount float64 `json:"today_revenue_amount"` // 今日收入金额 + TotalProfitAmount float64 `json:"total_profit_amount"` // 总利润金额 + TodayProfitAmount float64 `json:"today_profit_amount"` // 今日利润金额 +} + type AdminGetRoleApiListReq struct { RoleId int64 `path:"role_id"` } @@ -637,6 +656,14 @@ type AdminGetUserListResp struct { Items []AdminUserListItem `json:"items"` // 列表 } +type AdminGetWithdrawalStatisticsReq struct { +} + +type AdminGetWithdrawalStatisticsResp struct { + TotalWithdrawalAmount float64 `json:"total_withdrawal_amount"` // 总提现金额 + TodayWithdrawalAmount float64 `json:"today_withdrawal_amount"` // 今日提现金额 +} + type AdminLoginReq struct { Username string `json:"username" validate:"required"` Password string `json:"password" validate:"required"` @@ -1643,6 +1670,7 @@ type OrderListItem struct { PaymentPlatform string `json:"payment_platform"` // 支付方式 PaymentScene string `json:"payment_scene"` // 支付平台 Amount float64 `json:"amount"` // 金额 + SalesCost float64 `json:"sales_cost"` // 成本价 Status string `json:"status"` // 支付状态:pending-待支付,paid-已支付,refunded-已退款,closed-已关闭,failed-支付失败 QueryState string `json:"query_state"` // 查询状态:pending-待查询,success-查询成功,failed-查询失败 processing-查询中 CreateTime string `json:"create_time"` // 创建时间 diff --git a/app/main/model/orderModel_gen.go b/app/main/model/orderModel_gen.go index 32c1a5b..7605743 100644 --- a/app/main/model/orderModel_gen.go +++ b/app/main/model/orderModel_gen.go @@ -27,8 +27,8 @@ var ( orderRowsExpectAutoSet = strings.Join(stringx.Remove(orderFieldNames, "`id`", "`create_time`", "`update_time`"), ",") orderRowsWithPlaceHolder = strings.Join(stringx.Remove(orderFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?" - cacheHmOrderIdPrefix = "cache:tydata:order:id:" - cacheHmOrderOrderNoPrefix = "cache:tydata:order:orderNo:" + cacheTydataOrderIdPrefix = "cache:tydata:order:id:" + cacheTydataOrderOrderNoPrefix = "cache:tydata:order:orderNo:" ) type ( @@ -74,6 +74,7 @@ type ( RefundTime sql.NullTime `db:"refund_time"` // 退款时间 CloseTime sql.NullTime `db:"close_time"` // 订单关闭时间 DeleteTime sql.NullTime `db:"delete_time"` // 删除时间 + SalesCost float64 `db:"sales_cost"` // 销售成本 } ) @@ -86,21 +87,21 @@ func newOrderModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultOrderModel { func (m *defaultOrderModel) Insert(ctx context.Context, session sqlx.Session, data *Order) (sql.Result, error) { data.DelState = globalkey.DelStateNo - hmOrderIdKey := fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, data.Id) - hmOrderOrderNoKey := fmt.Sprintf("%s%v", cacheHmOrderOrderNoPrefix, data.OrderNo) + tydataOrderIdKey := fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, data.Id) + tydataOrderOrderNoKey := fmt.Sprintf("%s%v", cacheTydataOrderOrderNoPrefix, data.OrderNo) return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, orderRowsExpectAutoSet) + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, orderRowsExpectAutoSet) if session != nil { - return session.ExecCtx(ctx, query, data.OrderNo, data.UserId, data.ProductId, data.PaymentPlatform, data.PaymentScene, data.PlatformOrderId, data.Amount, data.Status, data.DelState, data.Version, data.PayTime, data.RefundTime, data.CloseTime, data.DeleteTime) + return session.ExecCtx(ctx, query, data.OrderNo, data.UserId, data.ProductId, data.PaymentPlatform, data.PaymentScene, data.PlatformOrderId, data.Amount, data.Status, data.DelState, data.Version, data.PayTime, data.RefundTime, data.CloseTime, data.DeleteTime, data.SalesCost) } - return conn.ExecCtx(ctx, query, data.OrderNo, data.UserId, data.ProductId, data.PaymentPlatform, data.PaymentScene, data.PlatformOrderId, data.Amount, data.Status, data.DelState, data.Version, data.PayTime, data.RefundTime, data.CloseTime, data.DeleteTime) - }, hmOrderIdKey, hmOrderOrderNoKey) + return conn.ExecCtx(ctx, query, data.OrderNo, data.UserId, data.ProductId, data.PaymentPlatform, data.PaymentScene, data.PlatformOrderId, data.Amount, data.Status, data.DelState, data.Version, data.PayTime, data.RefundTime, data.CloseTime, data.DeleteTime, data.SalesCost) + }, tydataOrderIdKey, tydataOrderOrderNoKey) } func (m *defaultOrderModel) FindOne(ctx context.Context, id int64) (*Order, error) { - hmOrderIdKey := fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, id) + tydataOrderIdKey := fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, id) var resp Order - err := m.QueryRowCtx(ctx, &resp, hmOrderIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { + err := m.QueryRowCtx(ctx, &resp, tydataOrderIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", orderRows, m.table) return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo) }) @@ -115,9 +116,9 @@ func (m *defaultOrderModel) FindOne(ctx context.Context, id int64) (*Order, erro } func (m *defaultOrderModel) FindOneByOrderNo(ctx context.Context, orderNo string) (*Order, error) { - hmOrderOrderNoKey := fmt.Sprintf("%s%v", cacheHmOrderOrderNoPrefix, orderNo) + tydataOrderOrderNoKey := fmt.Sprintf("%s%v", cacheTydataOrderOrderNoPrefix, orderNo) var resp Order - err := m.QueryRowIndexCtx(ctx, &resp, hmOrderOrderNoKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { + err := m.QueryRowIndexCtx(ctx, &resp, tydataOrderOrderNoKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { query := fmt.Sprintf("select %s from %s where `order_no` = ? and del_state = ? limit 1", orderRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, orderNo, globalkey.DelStateNo); err != nil { return nil, err @@ -139,15 +140,15 @@ func (m *defaultOrderModel) Update(ctx context.Context, session sqlx.Session, ne if err != nil { return nil, err } - hmOrderIdKey := fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, data.Id) - hmOrderOrderNoKey := fmt.Sprintf("%s%v", cacheHmOrderOrderNoPrefix, data.OrderNo) + tydataOrderIdKey := fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, data.Id) + tydataOrderOrderNoKey := fmt.Sprintf("%s%v", cacheTydataOrderOrderNoPrefix, data.OrderNo) return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, orderRowsWithPlaceHolder) if session != nil { - return session.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.Id) + return session.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.SalesCost, newData.Id) } - return conn.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.Id) - }, hmOrderIdKey, hmOrderOrderNoKey) + return conn.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.SalesCost, newData.Id) + }, tydataOrderIdKey, tydataOrderOrderNoKey) } func (m *defaultOrderModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *Order) error { @@ -162,15 +163,15 @@ func (m *defaultOrderModel) UpdateWithVersion(ctx context.Context, session sqlx. if err != nil { return err } - hmOrderIdKey := fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, data.Id) - hmOrderOrderNoKey := fmt.Sprintf("%s%v", cacheHmOrderOrderNoPrefix, data.OrderNo) + tydataOrderIdKey := fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, data.Id) + tydataOrderOrderNoKey := fmt.Sprintf("%s%v", cacheTydataOrderOrderNoPrefix, data.OrderNo) sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, orderRowsWithPlaceHolder) if session != nil { - return session.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.Id, oldVersion) + return session.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.SalesCost, newData.Id, oldVersion) } - return conn.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.Id, oldVersion) - }, hmOrderIdKey, hmOrderOrderNoKey) + return conn.ExecCtx(ctx, query, newData.OrderNo, newData.UserId, newData.ProductId, newData.PaymentPlatform, newData.PaymentScene, newData.PlatformOrderId, newData.Amount, newData.Status, newData.DelState, newData.Version, newData.PayTime, newData.RefundTime, newData.CloseTime, newData.DeleteTime, newData.SalesCost, newData.Id, oldVersion) + }, tydataOrderIdKey, tydataOrderOrderNoKey) if err != nil { return err } @@ -393,19 +394,19 @@ func (m *defaultOrderModel) Delete(ctx context.Context, session sqlx.Session, id return err } - hmOrderIdKey := fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, id) - hmOrderOrderNoKey := fmt.Sprintf("%s%v", cacheHmOrderOrderNoPrefix, data.OrderNo) + tydataOrderIdKey := fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, id) + tydataOrderOrderNoKey := fmt.Sprintf("%s%v", cacheTydataOrderOrderNoPrefix, data.OrderNo) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) if session != nil { return session.ExecCtx(ctx, query, id) } return conn.ExecCtx(ctx, query, id) - }, hmOrderIdKey, hmOrderOrderNoKey) + }, tydataOrderIdKey, tydataOrderOrderNoKey) return err } func (m *defaultOrderModel) formatPrimary(primary interface{}) string { - return fmt.Sprintf("%s%v", cacheHmOrderIdPrefix, primary) + return fmt.Sprintf("%s%v", cacheTydataOrderIdPrefix, primary) } func (m *defaultOrderModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", orderRows, m.table) diff --git a/deploy/script/gen_models.ps1 b/deploy/script/gen_models.ps1 index 767cca1..2d75ec6 100644 --- a/deploy/script/gen_models.ps1 +++ b/deploy/script/gen_models.ps1 @@ -23,12 +23,12 @@ $tables = @( # "agent_rewards", # "agent_wallet", # "agent_real_name" - "agent_withdrawal" + # "agent_withdrawal" # "agent_withdrawal_tax" # "agent_withdrawal_tax_exemption" - "feature" + # "feature" # "global_notifications" - # "order", + "order" # "order_refund" # "product", # "product_feature"