- Updated ReconcileItemIndexController to include admin user validation and improved transfer order handling. - Refactored ReconcileJobStoreController to ensure date range handling is more precise with start and end of day adjustments. - Enhanced various report controllers to include currency code in responses for better financial clarity. - Introduced new methods in AdminReconcileJobService for wallet transfer job creation and improved item scanning logic. - Added wallet lookup idempotent path configuration for integration services to enhance API interactions.
135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\AdminUser;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\ReconcileJob;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\ReconcileItem;
|
|
use App\Services\AuditLogger;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\Wallet\WalletTransferReconcileDetector;
|
|
|
|
/**
|
|
* 对账任务:落库 `reconcile_jobs` / `reconcile_items`。
|
|
*/
|
|
final class AdminReconcileJobService
|
|
{
|
|
public function __construct(
|
|
private readonly WalletTransferReconcileDetector $detector,
|
|
) {}
|
|
|
|
/**
|
|
* @param list<array{side_a_ref?: ?string, side_b_ref?: ?string, difference_amount?: int, status?: string}>|null $items
|
|
*/
|
|
public function createJob(
|
|
AdminUser $admin,
|
|
Request $request,
|
|
string $reconcileType,
|
|
?Carbon $periodStart,
|
|
?Carbon $periodEnd,
|
|
?array $items,
|
|
?int $playerId = null,
|
|
): ReconcileJob {
|
|
if ($reconcileType === 'wallet_transfer' && $items === null) {
|
|
return $this->createWalletTransferScanJob($admin, $request, $periodStart, $periodEnd, $playerId);
|
|
}
|
|
|
|
return DB::transaction(function () use ($admin, $request, $reconcileType, $periodStart, $periodEnd, $items): ReconcileJob {
|
|
$jobNo = 'REC'.now()->format('YmdHis').strtoupper(Str::random(4));
|
|
|
|
$job = ReconcileJob::query()->create([
|
|
'job_no' => $jobNo,
|
|
'admin_user_id' => (int) $admin->getKey(),
|
|
'reconcile_type' => $reconcileType,
|
|
'status' => 'completed',
|
|
'period_start' => $periodStart,
|
|
'period_end' => $periodEnd,
|
|
'summary_json' => null,
|
|
'finished_at' => now(),
|
|
]);
|
|
|
|
$mismatch = 0;
|
|
foreach ($items ?? [] as $row) {
|
|
ReconcileItem::query()->create([
|
|
'reconcile_job_id' => (int) $job->getKey(),
|
|
'side_a_ref' => $row['side_a_ref'] ?? null,
|
|
'side_b_ref' => $row['side_b_ref'] ?? null,
|
|
'difference_amount' => (int) ($row['difference_amount'] ?? 0),
|
|
'status' => (string) ($row['status'] ?? 'mismatch'),
|
|
'resolved_at' => null,
|
|
]);
|
|
if (($row['status'] ?? 'mismatch') === 'mismatch') {
|
|
$mismatch++;
|
|
}
|
|
}
|
|
|
|
$job->forceFill([
|
|
'summary_json' => [
|
|
'item_count' => count($items ?? []),
|
|
'mismatch_count' => $mismatch,
|
|
],
|
|
])->save();
|
|
|
|
AuditLogger::recordForAdmin(
|
|
$admin,
|
|
$request,
|
|
'reconcile_jobs',
|
|
'create',
|
|
'reconcile_job',
|
|
(string) $job->getKey(),
|
|
null,
|
|
[
|
|
'job_no' => $jobNo,
|
|
'reconcile_type' => $reconcileType,
|
|
'item_count' => count($items ?? []),
|
|
],
|
|
);
|
|
|
|
return $job->fresh();
|
|
});
|
|
}
|
|
|
|
private function createWalletTransferScanJob(
|
|
AdminUser $admin,
|
|
Request $request,
|
|
?Carbon $periodStart,
|
|
?Carbon $periodEnd,
|
|
?int $playerId,
|
|
): ReconcileJob {
|
|
$periodEnd ??= now()->endOfDay();
|
|
$periodStart ??= $periodEnd->copy()->startOfDay();
|
|
|
|
[$items] = $this->detector->scanItems(
|
|
$periodStart,
|
|
$periodEnd,
|
|
limit: 1000,
|
|
staleMinutes: 15,
|
|
playerId: $playerId,
|
|
includeMainSiteCheck: true,
|
|
);
|
|
|
|
$job = $this->detector->persistJob($items, $periodStart, $periodEnd, (int) $admin->getKey());
|
|
|
|
AuditLogger::recordForAdmin(
|
|
$admin,
|
|
$request,
|
|
'reconcile_jobs',
|
|
'create',
|
|
'reconcile_job',
|
|
(string) $job->getKey(),
|
|
null,
|
|
[
|
|
'job_no' => $job->job_no,
|
|
'reconcile_type' => 'wallet_transfer',
|
|
'item_count' => count($items),
|
|
'player_id' => $playerId,
|
|
],
|
|
);
|
|
|
|
return $job->fresh();
|
|
}
|
|
}
|