feat: enhance reconciliation job processing and reporting features
- 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.
This commit is contained in:
109
app/Services/Wallet/HttpMainSiteWalletIdempotentProbeClient.php
Normal file
109
app/Services/Wallet/HttpMainSiteWalletIdempotentProbeClient.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Wallet;
|
||||
|
||||
use App\Models\Player;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Services\Integration\PartnerSiteConfigResolver;
|
||||
use App\Support\Integration\WalletApiUrlSanitizer;
|
||||
|
||||
/** 按幂等键查询主站钱包侧是否已有对应划转记录。 */
|
||||
final class HttpMainSiteWalletIdempotentProbeClient
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PartnerSiteConfigResolver $partnerSiteConfigResolver,
|
||||
) {}
|
||||
|
||||
public function probe(Player $player, string $idempotentKey): MainSiteWalletIdempotentProbeResult
|
||||
{
|
||||
$idempotentKey = trim($idempotentKey);
|
||||
if ($idempotentKey === '') {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED,
|
||||
message: 'empty_idempotent_key',
|
||||
);
|
||||
}
|
||||
|
||||
$config = $this->partnerSiteConfigResolver->resolveForPlayer($player);
|
||||
if (! $config->enabled || ! $config->hasWalletApi()) {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED,
|
||||
message: 'wallet_api_not_configured',
|
||||
);
|
||||
}
|
||||
|
||||
$base = WalletApiUrlSanitizer::normalizeAndValidate($config->walletApiUrl);
|
||||
if ($base === null) {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_UNAVAILABLE,
|
||||
message: 'wallet_api_url_invalid',
|
||||
);
|
||||
}
|
||||
|
||||
$path = $config->walletLookupIdempotentPath;
|
||||
$url = $base.'/'.ltrim($path, '/');
|
||||
$headers = ['Accept' => 'application/json'];
|
||||
if (is_string($config->walletApiKey) && $config->walletApiKey !== '') {
|
||||
$headers['Authorization'] = 'Bearer '.$config->walletApiKey;
|
||||
}
|
||||
|
||||
$query = [
|
||||
'site_code' => $player->site_code,
|
||||
'site_player_id' => $player->site_player_id,
|
||||
'idempotent_key' => $idempotentKey,
|
||||
];
|
||||
|
||||
try {
|
||||
$response = Http::withHeaders($headers)
|
||||
->timeout($config->walletTimeoutSeconds)
|
||||
->acceptJson()
|
||||
->get($url, $query);
|
||||
} catch (\Throwable $e) {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_UNAVAILABLE,
|
||||
message: $e->getMessage(),
|
||||
);
|
||||
}
|
||||
|
||||
$payload = $response->json();
|
||||
if (! $response->successful() || ! is_array($payload)) {
|
||||
$message = is_array($payload) && is_string($payload['message'] ?? null)
|
||||
? (string) $payload['message']
|
||||
: 'HTTP '.$response->status();
|
||||
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_UNAVAILABLE,
|
||||
message: $message,
|
||||
);
|
||||
}
|
||||
|
||||
$data = data_get($payload, 'data');
|
||||
if (! is_array($data)) {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_UNAVAILABLE,
|
||||
message: 'invalid_response',
|
||||
);
|
||||
}
|
||||
|
||||
$found = (bool) ($data['found'] ?? false);
|
||||
if (! $found) {
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: MainSiteWalletIdempotentProbeResult::STATUS_NOT_FOUND,
|
||||
found: false,
|
||||
success: null,
|
||||
);
|
||||
}
|
||||
|
||||
$success = (bool) ($data['success'] ?? false);
|
||||
|
||||
return new MainSiteWalletIdempotentProbeResult(
|
||||
status: $success
|
||||
? MainSiteWalletIdempotentProbeResult::STATUS_MATCHED
|
||||
: MainSiteWalletIdempotentProbeResult::STATUS_FAILED,
|
||||
found: true,
|
||||
success: $success,
|
||||
externalRefNo: is_string($data['external_ref_no'] ?? null) ? $data['external_ref_no'] : null,
|
||||
operation: is_string($data['operation'] ?? null) ? $data['operation'] : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
22
app/Services/Wallet/MainSiteWalletIdempotentProbeResult.php
Normal file
22
app/Services/Wallet/MainSiteWalletIdempotentProbeResult.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Wallet;
|
||||
|
||||
/** 主站钱包幂等键查询结果(对账勾选用)。 */
|
||||
final readonly class MainSiteWalletIdempotentProbeResult
|
||||
{
|
||||
public const STATUS_SKIPPED = 'skipped';
|
||||
public const STATUS_MATCHED = 'matched';
|
||||
public const STATUS_NOT_FOUND = 'not_found';
|
||||
public const STATUS_FAILED = 'failed_on_main';
|
||||
public const STATUS_UNAVAILABLE = 'unavailable';
|
||||
|
||||
public function __construct(
|
||||
public string $status,
|
||||
public ?bool $found = null,
|
||||
public ?bool $success = null,
|
||||
public ?string $externalRefNo = null,
|
||||
public ?string $operation = null,
|
||||
public ?string $message = null,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Wallet;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Models\TransferOrder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/** 扫描结果追加主站幂等记录核对项。 */
|
||||
final class WalletTransferMainSiteReconcileChecker
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HttpMainSiteWalletIdempotentProbeClient $probeClient,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<array{side_a_ref: string, side_b_ref: ?string, difference_amount: int, status: string}> $items
|
||||
* @param Collection<int, TransferOrder> $orders
|
||||
* @return list<array{side_a_ref: string, side_b_ref: ?string, difference_amount: int, status: string}>
|
||||
*/
|
||||
public function appendIssues(array $items, Collection $orders): array
|
||||
{
|
||||
$existingTransferNos = collect($items)
|
||||
->map(fn (array $row): string => (string) ($row['side_a_ref'] ?? ''))
|
||||
->filter()
|
||||
->flip();
|
||||
|
||||
$players = Player::query()
|
||||
->whereIn('id', $orders->pluck('player_id')->unique()->all())
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
foreach ($orders as $order) {
|
||||
if (! $this->shouldProbeMainSite($order)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$transferNo = (string) $order->transfer_no;
|
||||
if ($existingTransferNos->has($transferNo)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$player = $players->get((int) $order->player_id);
|
||||
if (! $player instanceof Player) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$probe = $this->probeClient->probe($player, (string) $order->idempotent_key);
|
||||
if ($probe->status === MainSiteWalletIdempotentProbeResult::STATUS_SKIPPED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($probe->status === MainSiteWalletIdempotentProbeResult::STATUS_NOT_FOUND) {
|
||||
$items[] = [
|
||||
'side_a_ref' => $transferNo,
|
||||
'side_b_ref' => null,
|
||||
'difference_amount' => (int) $order->amount,
|
||||
'status' => 'main_site_record_missing',
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($probe->status === MainSiteWalletIdempotentProbeResult::STATUS_FAILED) {
|
||||
$items[] = [
|
||||
'side_a_ref' => $transferNo,
|
||||
'side_b_ref' => $probe->externalRefNo,
|
||||
'difference_amount' => (int) $order->amount,
|
||||
'status' => 'main_site_failed',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function shouldProbeMainSite(TransferOrder $order): bool
|
||||
{
|
||||
if (trim((string) $order->external_ref_no) !== '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($order->status === 'success') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $order->direction === 'in'
|
||||
&& $order->status === 'pending_reconcile'
|
||||
&& $order->fail_reason === 'lottery_credit_failed';
|
||||
}
|
||||
}
|
||||
@@ -28,15 +28,40 @@ final class WalletTransferReconcileDetector
|
||||
?Carbon $periodEnd = null,
|
||||
int $limit = 500,
|
||||
int $staleMinutes = 15,
|
||||
?int $playerId = null,
|
||||
bool $includeMainSiteCheck = true,
|
||||
): ?ReconcileJob {
|
||||
$periodEnd ??= now();
|
||||
$periodStart ??= $periodEnd->copy()->subDay();
|
||||
|
||||
[$items, $orders] = $this->scanItems($periodStart, $periodEnd, $limit, $staleMinutes, $playerId, $includeMainSiteCheck);
|
||||
|
||||
if ($items === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->persistJob($items, $periodStart, $periodEnd, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* 0: list<array{side_a_ref: string, side_b_ref: ?string, difference_amount: int, status: string}>,
|
||||
* 1: \Illuminate\Support\Collection<int, TransferOrder>
|
||||
* }
|
||||
*/
|
||||
public function scanItems(
|
||||
Carbon $periodStart,
|
||||
Carbon $periodEnd,
|
||||
int $limit = 500,
|
||||
int $staleMinutes = 15,
|
||||
?int $playerId = null,
|
||||
bool $includeMainSiteCheck = true,
|
||||
): array {
|
||||
$limit = max(1, $limit);
|
||||
$staleMinutes = max(1, $staleMinutes);
|
||||
|
||||
$staleCutoff = $periodEnd->copy()->subMinutes($staleMinutes);
|
||||
|
||||
$orders = TransferOrder::query()
|
||||
$query = TransferOrder::query()
|
||||
->where(function ($q) use ($periodStart, $periodEnd, $staleCutoff): void {
|
||||
$q->whereBetween('created_at', [$periodStart, $periodEnd])
|
||||
->orWhere('status', 'pending_reconcile')
|
||||
@@ -46,11 +71,16 @@ final class WalletTransferReconcileDetector
|
||||
});
|
||||
})
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
->limit($limit);
|
||||
|
||||
if ($playerId !== null) {
|
||||
$query->where('player_id', $playerId);
|
||||
}
|
||||
|
||||
$orders = $query->get();
|
||||
|
||||
if ($orders->isEmpty()) {
|
||||
return null;
|
||||
return [[], $orders];
|
||||
}
|
||||
|
||||
$txnsByTransferNo = WalletTxn::query()
|
||||
@@ -73,16 +103,28 @@ final class WalletTransferReconcileDetector
|
||||
}
|
||||
}
|
||||
|
||||
if ($items === []) {
|
||||
return null;
|
||||
if ($includeMainSiteCheck) {
|
||||
$items = app(WalletTransferMainSiteReconcileChecker::class)->appendIssues($items, $orders);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($items, $periodStart, $periodEnd): ReconcileJob {
|
||||
return [$items, $orders];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{side_a_ref: string, side_b_ref: ?string, difference_amount: int, status: string}> $items
|
||||
*/
|
||||
public function persistJob(
|
||||
array $items,
|
||||
Carbon $periodStart,
|
||||
Carbon $periodEnd,
|
||||
?int $adminUserId,
|
||||
): ReconcileJob {
|
||||
return DB::transaction(function () use ($items, $periodStart, $periodEnd, $adminUserId): ReconcileJob {
|
||||
$jobNo = 'REC'.now()->format('YmdHis').strtoupper(str_replace('-', '', Str::uuid()->toString()));
|
||||
|
||||
$job = ReconcileJob::query()->create([
|
||||
'job_no' => $jobNo,
|
||||
'admin_user_id' => null,
|
||||
'admin_user_id' => $adminUserId,
|
||||
'reconcile_type' => self::RECONCILE_TYPE,
|
||||
'status' => 'completed',
|
||||
'period_start' => $periodStart,
|
||||
|
||||
Reference in New Issue
Block a user