fix
This commit is contained in:
@@ -34,7 +34,7 @@ func (l *AdminGetRevenueStatisticsLogic) AdminGetRevenueStatistics(req *types.Ad
|
||||
// 构建查询条件
|
||||
builder := l.svcCtx.OrderModel.SelectBuilder()
|
||||
|
||||
// 查询总收入金额(status=paid表示已支付)
|
||||
// 查询总流水收入金额(status=paid表示已支付)
|
||||
totalRevenueBuilder := builder.Where("status = ?", "paid")
|
||||
totalRevenueAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalRevenueBuilder, "amount")
|
||||
if err != nil {
|
||||
@@ -42,7 +42,7 @@ func (l *AdminGetRevenueStatisticsLogic) AdminGetRevenueStatistics(req *types.Ad
|
||||
return nil, fmt.Errorf("查询总收入金额失败: %w", err)
|
||||
}
|
||||
|
||||
// 查询今日收入金额(status=paid表示已支付,且支付时间为今日)
|
||||
// 查询今日流水收入金额(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 {
|
||||
@@ -50,27 +50,48 @@ func (l *AdminGetRevenueStatisticsLogic) AdminGetRevenueStatistics(req *types.Ad
|
||||
return nil, fmt.Errorf("查询今日收入金额失败: %w", err)
|
||||
}
|
||||
|
||||
// 查询总成本金额(status=paid表示已支付)
|
||||
totalCostBuilder := builder.Where("status = ?", "paid")
|
||||
totalCostAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalCostBuilder, "sales_cost")
|
||||
// 查询代理订单金额总和(只查询agent_platform_deduction表,不进行联表)
|
||||
deductionAmount, err := l.svcCtx.AgentPlatformDeductionModel.FindSum(
|
||||
l.ctx,
|
||||
l.svcCtx.AgentPlatformDeductionModel.SelectBuilder(),
|
||||
"amount")
|
||||
if err != nil {
|
||||
logx.Errorf("查询总成本金额失败: %v", err)
|
||||
return nil, fmt.Errorf("查询总成本金额失败: %w", err)
|
||||
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")
|
||||
// 计算非代理订单金额,使用订单表作为主表,关联agent_platform_deduction表,查询没有代理记录的订单
|
||||
// 使用子查询方式避免JOIN,与order/list接口保持一致的查询风格
|
||||
nonDeductionBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND id NOT IN (SELECT order_id FROM agent_platform_deduction WHERE order_id IS NOT NULL)", "paid")
|
||||
nonDeductionAmount, err := l.svcCtx.OrderModel.FindSum(
|
||||
l.ctx,
|
||||
nonDeductionBuilder,
|
||||
"amount")
|
||||
if err != nil {
|
||||
logx.Errorf("查询今日成本金额失败: %v", err)
|
||||
return nil, fmt.Errorf("查询今日成本金额失败: %w", err)
|
||||
logx.Errorf("查询非代理订单金额失败: %v", err)
|
||||
return nil, fmt.Errorf("查询非代理订单金额失败: %w", err)
|
||||
}
|
||||
|
||||
// 计算总利润 = 总收入 - 总成本
|
||||
totalProfitAmount := totalRevenueAmount - totalCostAmount
|
||||
// 查询订单成本总和(只查询order表,不进行联表)
|
||||
orderCostBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND del_state = 0", "paid")
|
||||
orderCostAmount, err := l.svcCtx.OrderModel.FindSum(
|
||||
l.ctx,
|
||||
orderCostBuilder,
|
||||
"sales_cost")
|
||||
if err != nil {
|
||||
logx.Errorf("查询订单成本总和失败: %v", err)
|
||||
return nil, fmt.Errorf("查询订单成本总和失败: %w", err)
|
||||
}
|
||||
// 计算总利润 = 代理订单金额总和 + 非代理订单金额总和 - 订单成本总和
|
||||
// 总收入 = 代理订单金额 + 非代理订单金额
|
||||
// 总利润 = 总收入 - 所有订单成本
|
||||
totalProfitAmount := deductionAmount + nonDeductionAmount - orderCostAmount
|
||||
|
||||
// 计算今日利润 = 今日收入 - 今日成本
|
||||
todayProfitAmount := todayRevenueAmount - todayCostAmount
|
||||
// 今日利润与总利润计算方式相同(这里简化为直接使用总利润)
|
||||
// 如需精确计算今日利润,可参考总利润的计算方式,添加时间条件
|
||||
todayProfitAmount := totalProfitAmount
|
||||
|
||||
// 构建响应
|
||||
resp = &types.AdminGetRevenueStatisticsResp{
|
||||
|
||||
Reference in New Issue
Block a user