feat(dashboard): 从专业财富观角度重构升级后台收益与分成统计仪表盘

这个提交包含在:
chen
2026-09-17 08:22:23 +08:00
父节点 64a9563779
当前提交 b6287bf2e0
共有 40 个文件被更改,包括 5145 次插入 和 1015 次删除

查看文件

@@ -35,24 +35,53 @@ class VoucherService
error_stop('兑换券可用额度不足');
}
$agent->setInc('voucher_used', $amount);
PartnerRewardService::write([
'order_id' => $orderId,
'buyer_id' => $userId,
'user_id' => $userId,
'role' => 'taster',
'reward_type' => 'voucher_consume',
'amount' => -$amount,
'base_amount' => $amount,
'ext' => ['formula' => "订单抵扣兑换券 {$amount}", 'order_id' => $orderId],
'status' => 1,
]);
return true;
}
public function refundConsume($order)
public function refundConsume($order, $refundFee = null)
{
$ext = $order['ext'] ?? [];
if (is_string($ext)) {
$ext = json_decode($ext, true) ?: [];
}
$fee = round((float) ($ext['voucher_fee'] ?? 0), 2);
if ($fee <= 0 || empty($ext['voucher_consumed'])) {
$totalVoucher = round((float) ($ext['voucher_fee'] ?? 0), 2);
if ($totalVoucher <= 0 || empty($ext['voucher_consumed'])) {
return;
}
$fee = $refundFee !== null ? min($totalVoucher, round((float) $refundFee, 2)) : $totalVoucher;
if ($fee <= 0) {
return;
}
$userId = intval($order['user_id'] ?? 0);
$orderId = intval($order['id'] ?? 0);
$agent = AgentModel::where('user_id', $userId)->lock(true)->find();
if ($agent) {
$agent->voucher_used = max(0, round((float) $agent->voucher_used - $fee, 2));
$agent->save();
PartnerRewardService::write([
'order_id' => $orderId,
'buyer_id' => $userId,
'user_id' => $userId,
'role' => 'taster',
'reward_type' => 'voucher_consume_refund',
'amount' => $fee,
'base_amount' => $fee,
'ext' => ['formula' => "订单退款退回兑换券 {$fee}", 'order_id' => $orderId],
'status' => 1,
]);
}
}
@@ -212,17 +241,57 @@ class VoucherService
$plan->status = 0;
$plan->save();
$agent = AgentModel::where('user_id', $plan->user_id)->find();
$agent = AgentModel::where('user_id', $plan->user_id)->lock(true)->find();
if ($agent) {
$unusedReleased = max(0, (float) $plan->released_amount);
$newReleased = max(0, (float) $agent->voucher_released - $unusedReleased);
$newTotal = max(0, (float) $agent->voucher_total - (float) $plan->total_amount);
if ($newReleased < (float) $agent->voucher_used) {
$newReleased = (float) $agent->voucher_used;
$releasedAmount = round((float) $plan->released_amount, 2);
$totalAmount = round((float) $plan->total_amount, 2);
$currentReleased = round((float) $agent->voucher_released, 2);
$currentUsed = round((float) $agent->voucher_used, 2);
$newTotal = max(0, round((float) $agent->voucher_total - $totalAmount, 2));
$newReleased = max(0, round($currentReleased - $releasedAmount, 2));
$overdraft = 0;
if ($newReleased < $currentUsed) {
$overdraft = round($currentUsed - $newReleased, 2);
$newReleased = $currentUsed;
}
$agent->voucher_released = $newReleased;
$agent->voucher_total = $newTotal;
$agent->save();
if ($overdraft > 0) {
\think\Log::record("[VoucherService] 订单 {$orderId} 退款关闭计划,用户 {$agent->user_id} 已透支兑换券额度: {$overdraft}", 'warning');
}
}
}
public function reduceByRefund($orderId, $refundItemFee)
{
$plan = VoucherPlan::where('order_id', $orderId)->find();
if (!$plan || $plan->status == 0) {
return;
}
$times = (float) PartnerConfig::get('partner_voucher_times', 1.5);
$reduceTotal = round((float) $refundItemFee * $times, 2);
if ($reduceTotal <= 0) {
return;
}
$reduceTotal = min($reduceTotal, (float) $plan->total_amount);
$plan->total_amount = max(0, round((float) $plan->total_amount - $reduceTotal, 2));
$leftDays = max(1, $plan->total_days - $plan->released_days);
$plan->daily_amount = round(($plan->total_amount - $plan->released_amount) / $leftDays, 2);
if ($plan->total_amount <= $plan->released_amount) {
$plan->status = 2;
}
$plan->save();
$agent = AgentModel::where('user_id', $plan->user_id)->lock(true)->find();
if ($agent) {
$agent->voucher_total = max(0, round((float) $agent->voucher_total - $reduceTotal, 2));
$agent->save();
}
}