fix analysis

This commit is contained in:
2025-12-27 13:51:26 +08:00
parent ed50e9b778
commit cc1680cf01
17 changed files with 485 additions and 57 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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,

View File

@@ -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
}

View File

@@ -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"),

View File

@@ -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],

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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")

View File

@@ -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"` // 创建时间