积分与余额账务系统硬切重构
This commit is contained in:
@@ -53,8 +53,10 @@ function aksrt_notification_type_label(string $type): string {
|
||||
'comment_reply' => __('评论回复', 'aksrt'),
|
||||
'comment_approved' => __('评论审核', 'aksrt'),
|
||||
'download_ready' => __('下载就绪', 'aksrt'),
|
||||
'credits_added' => __('积分增加', 'aksrt'),
|
||||
'credits_deducted' => __('积分扣减', 'aksrt'),
|
||||
'asset_points_increase' => __('积分增加', 'aksrt'),
|
||||
'asset_points_decrease' => __('积分扣减', 'aksrt'),
|
||||
'asset_balance_increase' => __('余额增加', 'aksrt'),
|
||||
'asset_balance_decrease' => __('余额扣减', 'aksrt'),
|
||||
'pending_comments' => __('待审核评论', 'aksrt'),
|
||||
'notice' => __('通知', 'aksrt'),
|
||||
];
|
||||
@@ -549,25 +551,38 @@ add_action('transition_comment_status', function ($new_status, $old_status, $com
|
||||
update_comment_meta($comment->comment_ID, 'aksrt_reply_notified', 1);
|
||||
}, 20, 3);
|
||||
|
||||
add_action('aksrt_credits_added', function ($user_id, $delta, $reason, $new_balance) {
|
||||
if ($user_id <= 0 || $delta <= 0) return;
|
||||
aksrt_add_user_notification((int) $user_id, [
|
||||
'type' => 'credits_added',
|
||||
'title' => __('积分增加', 'aksrt'),
|
||||
'desc' => sprintf(__('积分变动:+%1$d,当前余额 %2$d。', 'aksrt'), (int) $delta, (int) $new_balance),
|
||||
'url' => aksrt_get_user_center_url('credits'),
|
||||
]);
|
||||
}, 10, 4);
|
||||
|
||||
add_action('aksrt_credits_deducted', function ($user_id, $cost, $reason, $new_balance) {
|
||||
if ($user_id <= 0 || $cost <= 0) return;
|
||||
aksrt_add_user_notification((int) $user_id, [
|
||||
'type' => 'credits_deducted',
|
||||
'title' => __('积分扣减', 'aksrt'),
|
||||
'desc' => sprintf(__('积分变动:-%1$d,当前余额 %2$d。', 'aksrt'), (int) $cost, (int) $new_balance),
|
||||
'url' => aksrt_get_user_center_url('credits'),
|
||||
]);
|
||||
}, 10, 4);
|
||||
add_action('aksrt_asset_transaction_committed', function ($result, $command): void {
|
||||
if (!$result instanceof \AKSRT\Ledger\LedgerResult || !$command instanceof \AKSRT\Ledger\LedgerCommand) {
|
||||
return;
|
||||
}
|
||||
$changes = [];
|
||||
foreach ($result->entries as $entry) {
|
||||
$user_id = (int) ($entry['user_id'] ?? 0);
|
||||
$asset = (string) ($entry['asset'] ?? '');
|
||||
if ($user_id <= 0 || !in_array($asset, ['points', 'balance'], true)) continue;
|
||||
$key = $user_id . ':' . $asset;
|
||||
$changes[$key] ??= ['user_id' => $user_id, 'asset' => $asset, 'delta' => 0, 'balance' => 0];
|
||||
$changes[$key]['delta'] += (int) ($entry['delta_minor'] ?? 0);
|
||||
$changes[$key]['balance'] = (int) ($entry['balance_after_minor'] ?? 0);
|
||||
}
|
||||
foreach ($changes as $change) {
|
||||
if ((int) $change['delta'] === 0) continue;
|
||||
$is_points = $change['asset'] === 'points';
|
||||
$positive = $change['delta'] > 0;
|
||||
$amount = $is_points
|
||||
? (string) abs((int) $change['delta'])
|
||||
: \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, abs((int) $change['delta']))->decimal();
|
||||
$balance = $is_points
|
||||
? (string) $change['balance']
|
||||
: \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, (int) $change['balance'])->decimal();
|
||||
aksrt_add_user_notification((int) $change['user_id'], [
|
||||
'type' => 'asset_' . $change['asset'] . '_' . ($positive ? 'increase' : 'decrease'),
|
||||
'title' => $is_points ? __('积分变动', 'aksrt') : __('余额变动', 'aksrt'),
|
||||
'desc' => sprintf(__('%1$s:%2$s%3$s,当前余额 %4$s。', 'aksrt'), \AKSRT\Ledger\LedgerPresentation::reason_label($command->reason), $positive ? '+' : '-', $amount, $balance),
|
||||
'url' => aksrt_get_user_center_url('credits'),
|
||||
]);
|
||||
}
|
||||
}, 10, 2);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user