diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 788ac2ea..bf96088e 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -44,6 +44,7 @@ use App\Business\CourierSurchargeTypeRepository; use App\Models\StoreReceivables\StoreZhongcaiReconciliation; use App\Utilities\BusinessOperationPermissions; use App\Utilities\SidebarPermissions; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Pagination\LengthAwarePaginator; @@ -8035,30 +8036,19 @@ class Pages extends Controller public function refundPendingMonthDetails(Request $request) { - $month = trim((string) $request->query('month', '')); - $allMonths = $month === '' || $month === 'all'; - if (! $allMonths && preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { - return response()->json(['message' => '退款月份格式不正确'], 422); + $window = $this->refundPendingMonthWindow($request); + if ($window instanceof JsonResponse) { + return $window; } + ['month' => $monthKey, 'start' => $start, 'end' => $end] = $window; - $today = now()->startOfDay(); - if ($allMonths) { - $monthKey = 'all'; - $start = Carbon::createFromFormat('!Y-m-d', '2026-10-01')->startOfDay(); - $end = $today; - } else { - $monthKey = $month; - $start = Carbon::createFromFormat('!Y-m', $month)->startOfMonth(); - if ($start->format('Y-m') < '2026-10' || $start->greaterThan($today)) { - return response()->json(['message' => '退款月份超出可查询范围'], 422); - } - - $end = $start->copy()->endOfMonth()->startOfDay(); - if ($end->greaterThan($today)) { - $end = $today; - } - } $platform = mb_substr(trim((string) $request->query('platform', '')), 0, 120); + $applicationDate = trim((string) $request->query('application_date', '')); + if ($applicationDate !== '' + && $applicationDate !== 'unknown' + && preg_match('/^\d{4}-\d{2}-\d{2}$/', $applicationDate) !== 1) { + return response()->json(['message' => '申请时间格式不正确'], 422); + } $page = max((int) $request->query('page', 1), 1); $limit = min(max((int) $request->query('limit', 25), 1), 50); $filters = [ @@ -8070,6 +8060,9 @@ class Pages extends Controller 'sku_id' => '', 'platform' => $platform, ]; + if ($applicationDate !== '') { + $filters['application_date'] = $applicationDate; + } try { if (! $this->nonJingxiRefunds->available() @@ -8079,8 +8072,9 @@ class Pages extends Controller } $platformKey = sha1($platform); + $applicationDateKey = sha1($applicationDate); $details = \Illuminate\Support\Facades\Cache::remember( - "refund-pending.details.v3:{$monthKey}:{$end->toDateString()}:{$platformKey}:{$page}:{$limit}", + "refund-pending.details.v4:{$monthKey}:{$end->toDateString()}:{$platformKey}:{$applicationDateKey}:{$page}:{$limit}", now()->addMinutes(5), function () use ($filters, $page, $limit): array { $result = $this->nonJingxiRefunds->pagePendingConfirmation($filters, $page, $limit); @@ -8095,6 +8089,7 @@ class Pages extends Controller 'shop_name' => (string) ($row->shop_name ?? ''), 'platform' => (string) ($row->platform ?? ''), 'refund_date' => (string) ($row->refundDate ?? ''), + 'application_time' => (string) ($row->createTime ?? ''), 'status' => (string) ($row->status ?? ''), 'online_status' => (string) ($row->shop_status ?? ''), 'after_sales_amount' => $row->AfSalesMoney ?? null, @@ -8116,6 +8111,7 @@ class Pages extends Controller return response()->json([ 'month' => $monthKey, 'platform' => $platform, + 'application_date' => $applicationDate, 'page' => $page, 'limit' => $limit, 'has_more' => $details['has_more'], @@ -8123,6 +8119,85 @@ class Pages extends Controller ], 200, ['Cache-Control' => 'private, no-store']); } + public function refundPendingApplicationSummary(Request $request) + { + $window = $this->refundPendingMonthWindow($request); + if ($window instanceof JsonResponse) { + return $window; + } + ['month' => $monthKey, 'start' => $start, 'end' => $end] = $window; + + $platform = mb_substr(trim((string) $request->query('platform', '')), 0, 120); + $filters = [ + 'from' => $start->toDateString(), + 'to' => $end->toDateString(), + 'order_no' => '', + 'waybill_no' => '', + 'shop_name' => '', + 'sku_id' => '', + 'platform' => $platform, + ]; + + try { + if (! $this->nonJingxiRefunds->available() + || ! $this->nonJingxiRefunds->ready() + || ! $this->nonJingxiRefunds->queryReady()) { + return response()->json(['message' => '退款待确认镜像尚未就绪'], 503); + } + + $platformKey = sha1($platform); + $summary = \Illuminate\Support\Facades\Cache::remember( + "refund-pending.application-summary.v1:{$monthKey}:{$end->toDateString()}:{$platformKey}", + now()->addMinutes(5), + fn (): array => $this->nonJingxiRefunds->pendingConfirmationApplicationDailySummary($filters), + ); + } catch (Throwable $exception) { + report($exception); + + return response()->json(['message' => '退款待确认申请时间统计暂时无法读取'], 503); + } + + return response()->json([ + 'month' => $monthKey, + 'platform' => $platform, + 'total' => (int) ($summary['total'] ?? 0), + 'rows' => $summary['rows'] ?? [], + ], 200, ['Cache-Control' => 'private, no-store']); + } + + /** + * @return array{month:string,start:Carbon,end:Carbon}|JsonResponse + */ + private function refundPendingMonthWindow(Request $request): array|JsonResponse + { + $month = trim((string) $request->query('month', '')); + $allMonths = $month === '' || $month === 'all'; + if (! $allMonths && preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + return response()->json(['message' => '退款月份格式不正确'], 422); + } + + $today = now()->startOfDay(); + if ($allMonths) { + return [ + 'month' => 'all', + 'start' => Carbon::createFromFormat('!Y-m-d', '2026-10-01')->startOfDay(), + 'end' => $today, + ]; + } + + $start = Carbon::createFromFormat('!Y-m', $month)->startOfMonth(); + if ($start->format('Y-m') < '2026-10' || $start->greaterThan($today)) { + return response()->json(['message' => '退款月份超出可查询范围'], 422); + } + + $end = $start->copy()->endOfMonth()->startOfDay(); + if ($end->greaterThan($today)) { + $end = $today; + } + + return ['month' => $month, 'start' => $start, 'end' => $end]; + } + public function exportAfterSalesReturnRefunds(Request $request) { return $this->exportRefundRows( diff --git a/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php b/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php index fa2fbf9f..7a005ec8 100644 --- a/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php @@ -56,6 +56,14 @@ class NonJingxiRefundMirrorRepository private ?string $supplementDateIndex = null; + private ?bool $availableCache = null; + + private ?bool $readyCache = null; + + private ?bool $queryReadyCache = null; + + private ?bool $pendingStatusReadyCache = null; + public function __construct( private readonly BusinessMirrorDatasetRegistry $datasets, private readonly GenericBusinessMirrorRepository $mirror, @@ -65,62 +73,102 @@ class NonJingxiRefundMirrorRepository public function available(): bool { - $schema = Schema::connection($this->connection->getName()); + if ($this->availableCache !== null) { + return $this->availableCache; + } - return $schema->hasTable('business_mirror_table_states') - && collect([self::INFO_DATASET, self::ITEM_DATASET, self::SUPPLEMENT_DATASET, self::SUPPLEMENT_ITEM_DATASET, self::SHOP_DATASET]) - ->every(fn (string $key): bool => $schema->hasTable($this->dataset($key)->targetTable())); + return $this->availableCache = cache()->remember( + 'refund-pending.available.v1', + now()->addMinute(), + function (): bool { + $schema = Schema::connection($this->connection->getName()); + + return $schema->hasTable('business_mirror_table_states') + && collect([self::INFO_DATASET, self::ITEM_DATASET, self::SUPPLEMENT_DATASET, self::SUPPLEMENT_ITEM_DATASET, self::SHOP_DATASET]) + ->every(fn (string $key): bool => $schema->hasTable($this->dataset($key)->targetTable())); + }, + ); } public function ready(): bool { - return collect([self::INFO_DATASET, self::ITEM_DATASET, self::SUPPLEMENT_DATASET, self::SUPPLEMENT_ITEM_DATASET, self::SHOP_DATASET]) - ->every(fn (string $key): bool => $this->mirror->ready($this->dataset($key))); + if ($this->readyCache !== null) { + return $this->readyCache; + } + + return $this->readyCache = cache()->remember( + 'refund-pending.mirror-ready.v1', + now()->addMinute(), + fn (): bool => collect([self::INFO_DATASET, self::ITEM_DATASET, self::SUPPLEMENT_DATASET, self::SUPPLEMENT_ITEM_DATASET, self::SHOP_DATASET]) + ->every(fn (string $key): bool => $this->mirror->ready($this->dataset($key))), + ); } public function pendingConfirmationStatusReady(): bool { + if ($this->pendingStatusReadyCache !== null) { + return $this->pendingStatusReadyCache; + } + try { - return Schema::connection($this->connection->getName())->hasTable('ops_jst_refund_status') - && $this->mirror->ready($this->dataset(self::STATUS_DATASET)); + return $this->pendingStatusReadyCache = cache()->remember( + 'refund-pending.status-ready.v1', + now()->addMinute(), + fn (): bool => Schema::connection($this->connection->getName())->hasTable('ops_jst_refund_status') + && $this->mirror->ready($this->dataset(self::STATUS_DATASET)), + ); } catch (\Throwable) { - return false; + return $this->pendingStatusReadyCache = false; } } public function queryReady(): bool { - $schema = Schema::connection($this->connection->getName()); - - try { - foreach (self::REQUIRED_QUERY_INDEXES as $datasetKey => $requiredIndexes) { - $table = $this->dataset($datasetKey)->targetTable(); - if (! $schema->hasTable($table)) { - return false; - } - - $tableIndexes = collect($schema->getIndexes($table)); - if ($datasetKey === self::SUPPLEMENT_DATASET) { - $this->supplementDateIndex = $tableIndexes->first( - static fn (array $index): bool => array_map('strtolower', $index['columns']) === ['as_date', 'as_id'] - )['name'] ?? null; - } - $indexes = $tableIndexes - ->map(static fn (array $index): array => array_map( - 'strtolower', - (array) ($index['columns'] ?? []), - )); - foreach ($requiredIndexes as $required) { - if (! $indexes->contains(array_map('strtolower', $required))) { - return false; - } - } - } - } catch (\Throwable) { - return false; + if ($this->queryReadyCache !== null) { + return $this->queryReadyCache; } - return true; + $state = cache()->remember( + 'refund-pending.query-ready.v2', + now()->addMinute(), + function (): array { + $schema = Schema::connection($this->connection->getName()); + $supplementDateIndex = null; + + try { + foreach (self::REQUIRED_QUERY_INDEXES as $datasetKey => $requiredIndexes) { + $table = $this->dataset($datasetKey)->targetTable(); + if (! $schema->hasTable($table)) { + return ['ready' => false, 'supplement_date_index' => null]; + } + + $tableIndexes = collect($schema->getIndexes($table)); + if ($datasetKey === self::SUPPLEMENT_DATASET) { + $supplementDateIndex = $tableIndexes->first( + static fn (array $index): bool => array_map('strtolower', $index['columns']) === ['as_date', 'as_id'] + )['name'] ?? null; + } + $indexes = $tableIndexes + ->map(static fn (array $index): array => array_map( + 'strtolower', + (array) ($index['columns'] ?? []), + )); + foreach ($requiredIndexes as $required) { + if (! $indexes->contains(array_map('strtolower', $required))) { + return ['ready' => false, 'supplement_date_index' => $supplementDateIndex]; + } + } + } + } catch (\Throwable) { + return ['ready' => false, 'supplement_date_index' => null]; + } + + return ['ready' => true, 'supplement_date_index' => $supplementDateIndex]; + }, + ); + $this->supplementDateIndex = $state['supplement_date_index'] ?? null; + + return $this->queryReadyCache = (bool) ($state['ready'] ?? false); } /** @return array|null> */ @@ -344,6 +392,57 @@ class NonJingxiRefundMirrorRepository return array_values($months); } + /** + * 按售后申请时间汇总当前月份、平台下的待确认记录。 + * + * @param array{from:string,to:string,order_no:string,waybill_no:string,shop_name:string,sku_id:string,platform?:string} $filters + * @return array{rows:list,total:int} + */ + public function pendingConfirmationApplicationDailySummary(array $filters): array + { + $this->assertPendingStatusReady(); + if (! $this->ready() || ! $this->queryReady()) { + throw new RuntimeException('business_mirror_dataset_not_ready'); + } + + $pendingFilters = $this->pendingConfirmationFilters($filters); + $counts = []; + foreach ([false, true] as $supplement) { + $dateExpression = $this->applicationDateExpression($supplement); + $rows = $this->sourceQuery($pendingFilters, $supplement, false) + ->selectRaw("{$dateExpression} AS application_date, COUNT(*) AS total") + ->groupByRaw($dateExpression) + ->get(); + foreach ($rows as $row) { + $date = trim((string) ($row->application_date ?? '')); + $date = $date !== '' ? $date : 'unknown'; + $counts[$date] = (int) ($counts[$date] ?? 0) + (int) ($row->total ?? 0); + } + } + + uksort($counts, static function (string $left, string $right): int { + if ($left === 'unknown') { + return $right === 'unknown' ? 0 : 1; + } + if ($right === 'unknown') { + return -1; + } + + return strcmp($right, $left); + }); + + return [ + 'rows' => collect($counts) + ->map(static fn (int $count, string $date): array => [ + 'application_date' => $date, + 'count' => $count, + ]) + ->values() + ->all(), + 'total' => array_sum($counts), + ]; + } + /** * 按平台和退款月份汇总待确认记录,并同时返回月份总计。 * @@ -616,6 +715,7 @@ class NonJingxiRefundMirrorRepository } $this->applyPlatformFilter($query, $filters, $supplement); $this->applyFilters($query, $filters, $statusAlias, $statusAuthoritative); + $this->applyApplicationDateFilter($query, $filters, $supplement); if ($filters['sku_id'] !== '') { if ($withItems) { $this->applySkuFilter($query, $filters['sku_id']); @@ -630,6 +730,37 @@ class NonJingxiRefundMirrorRepository return $query; } + /** + * @param array{application_date?:string} $filters + */ + private function applyApplicationDateFilter(Builder $query, array $filters, bool $supplement): void + { + $value = trim((string) ($filters['application_date'] ?? '')); + if ($value === '') { + return; + } + + $column = $supplement ? 'r.local_created' : 'r.createTime'; + if ($value === 'unknown') { + $query->whereNull($column); + + return; + } + + $start = Carbon::parse($value)->toDateString(); + $query->where($column, '>=', $start . ' 00:00:00') + ->where($column, '<', Carbon::parse($start)->addDay()->toDateString() . ' 00:00:00'); + } + + private function applicationDateExpression(bool $supplement): string + { + $column = $supplement ? 'r.local_created' : 'r.createTime'; + + return $this->connection->getDriverName() === 'sqlite' + ? "strftime('%Y-%m-%d', {$column})" + : "DATE({$column})"; + } + private const SORT_COLUMNS = ['refundDate', '_source_rank', '_head_id', '_source_id', '_item_id']; private function orderedKeys(array $filters, ?object $cursor, int $limit): Builder diff --git a/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php b/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php index c538a7c9..edb54f8a 100644 --- a/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php +++ b/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php @@ -282,6 +282,101 @@ color: var(--vip-content-muted, #595959); font-size: 0.75rem; } + .refund-application-summary { + border: 1px solid var(--vip-border, #d8d2c7); + border-radius: 8px; + background: var(--vip-surface, #ffffff); + } + .refund-application-summary__head { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 0.75rem; + padding: 0.7rem 0.85rem; + border-bottom: 1px solid var(--vip-border, #d8d2c7); + background: var(--vip-content-accent-soft, #f7f0de); + } + .refund-application-summary__head strong { + color: var(--vip-content-ink, #171717); + font-size: 0.78rem; + font-weight: 600; + } + .refund-application-summary__head span { + color: var(--vip-content-muted, #595959); + font-size: 0.72rem; + } + .refund-application-summary__scroll { + overflow: auto; + max-height: 18rem; + } + .refund-application-summary__table { + width: 100%; + border-collapse: collapse; + font-size: 0.75rem; + } + .refund-application-summary__table th, + .refund-application-summary__table td { + padding: 0.48rem 0.8rem; + border-bottom: 1px solid var(--vip-border, #d8d2c7); + text-align: left; + white-space: nowrap; + } + .refund-application-summary__table th { + position: sticky; + top: 0; + z-index: 1; + color: var(--vip-content-muted, #595959); + background: var(--vip-surface, #ffffff); + font-size: 0.72rem; + font-weight: 500; + } + .refund-application-summary__table td:last-child, + .refund-application-summary__table th:last-child { text-align: right; } + .refund-application-summary__table tr.is-selected td { + background: var(--vip-content-accent-soft, #f7f0de); + } + .refund-application-date-count { + min-width: 3rem; + min-height: 1.7rem; + padding: 0 0.45rem; + border: 1px solid transparent; + border-radius: 4px; + background: transparent; + color: var(--vip-content-accent-dark, #856632); + font: inherit; + font-variant-numeric: tabular-nums; + text-decoration: underline dotted; + text-underline-offset: 3px; + cursor: pointer; + } + .refund-application-date-count:hover, + .refund-application-date-count[aria-pressed="true"] { + border-color: var(--vip-content-accent, #b9954f); + background: var(--vip-content-accent-soft, #f7f0de); + text-decoration-style: solid; + } + .refund-application-date-count:focus-visible { + outline: 2px solid var(--vip-content-accent-dark, #856632); + outline-offset: 1px; + } + .refund-summary-detail__records { + margin-top: 0.9rem; + padding-top: 0.85rem; + border-top: 1px solid var(--vip-border, #d8d2c7); + } + .refund-summary-detail__records[hidden] { display: none; } + .refund-summary-detail__records-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; + margin-bottom: 0.6rem; + } + .refund-summary-detail__records-title { + color: var(--vip-content-ink, #171717); + font-size: 0.78rem; + font-weight: 600; + } .refund-summary-detail__scroll { overflow: auto; max-height: min(34rem, 62vh); @@ -406,11 +501,12 @@ class="refund-month-summary" data-refund-month-summary data-details-url="{{ route('business.refunds.refund-pending.month-details') }}" + data-application-summary-url="{{ route('business.refunds.refund-pending.application-summary') }}" >
按退款月份统计 - 点击汇总数字查看对应待确认明细 + 点击汇总数字查看申请时间分布,再点日期查看待确认明细
@if ($refundShowExport)