diff --git a/modules/EcommerceFinance/Repositories/ShopInfoFlowMirrorRepository.php b/modules/EcommerceFinance/Repositories/ShopInfoFlowMirrorRepository.php index 9229726d..c76fdb1f 100644 --- a/modules/EcommerceFinance/Repositories/ShopInfoFlowMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/ShopInfoFlowMirrorRepository.php @@ -116,8 +116,9 @@ class ShopInfoFlowMirrorRepository return ['data' => $rows, 'total' => $total]; } - /** @param array $filters - * @return list + /** + * @param array $filters + * @return list */ public function currentApproverOptions(array $filters = []): array { @@ -133,16 +134,26 @@ class ShopInfoFlowMirrorRepository $query = $connection->table($form . ' as f') ->leftJoin($flow . ' as o', 'o.flowId', '=', 'f.flowId') ->whereNotNull('o.approver') - ->where('o.approver', '!=', ''); + ->where('o.approver', '!=', '') + ->selectRaw('o.approver, COUNT(*) AS total') + ->groupBy('o.approver'); $this->applyFilters($query, $filters); - return $query - ->pluck('o.approver') - ->flatMap(static fn (mixed $value): array => preg_split('/[,,]/u', (string) $value, -1, PREG_SPLIT_NO_EMPTY) ?: []) - ->map(static fn (mixed $value): string => trim((string) $value)) - ->filter(static fn (string $value): bool => $value !== '') - ->unique() - ->sort(static fn (string $left, string $right): int => strnatcasecmp($left, $right)) + $counts = []; + foreach ($query->get() as $row) { + $names = preg_split('/[,,]/u', (string) $row->approver, -1, PREG_SPLIT_NO_EMPTY) ?: []; + foreach (array_unique(array_map('trim', $names)) as $name) { + if ($name !== '') { + $counts[$name] = ($counts[$name] ?? 0) + (int) $row->total; + } + } + } + uksort($counts, static function (string $left, string $right) use ($counts): int { + return ($counts[$right] <=> $counts[$left]) ?: strnatcasecmp($left, $right); + }); + + return collect($counts) + ->map(static fn (int $count, string $value): array => ['value' => $value, 'count' => $count]) ->values() ->all(); } diff --git a/resources/views/business/pages/partials/directory-table.blade.php b/resources/views/business/pages/partials/directory-table.blade.php index 0c98a7de..9c608b3f 100644 --- a/resources/views/business/pages/partials/directory-table.blade.php +++ b/resources/views/business/pages/partials/directory-table.blade.php @@ -3733,11 +3733,29 @@ ? rows().flatMap((row) => ['legal_representative', 'shareholders', 'supervisor', 'billing_officer'] .map((column) => (row.querySelector(`[data-column-key="${column}"]`)?.textContent || '').trim())) : rows().map((row) => fieldValues(row, key)); - const matchedValues = [...new Set([...serverOptions, ...candidates].filter( - (item) => item && item !== '—' && (!needle || String(item).toLocaleLowerCase().includes(needle)), - ))]; - const values = serverOptions.length > 0 ? matchedValues : matchedValues.slice(0, 8); - list.innerHTML = values.map((item) => `
  • `).join(''); + const serverEntries = Array.isArray(serverOptions) ? serverOptions : []; + const suggestionCounts = new Map(); + serverEntries.forEach((entry) => { + const item = entry && typeof entry === 'object' ? entry.value : entry; + const count = entry && typeof entry === 'object' ? Number(entry.count) : null; + const text = item === null || item === undefined ? '' : String(item).trim(); + if (text !== '') suggestionCounts.set(text, Number.isFinite(count) && count > 0 ? Math.trunc(count) : null); + }); + candidates.forEach((item) => { + const text = item === null || item === undefined ? '' : String(item).trim(); + if (text !== '' && text !== '—' && !suggestionCounts.has(text)) suggestionCounts.set(text, null); + }); + const matchedValues = [...suggestionCounts.entries()] + .filter(([item]) => !needle || item.toLocaleLowerCase().includes(needle)) + .map(([value, count]) => ({ value, count })); + const values = serverEntries.length > 0 ? matchedValues : matchedValues.slice(0, 8); + const escapeSuggestion = (item) => String(item).replaceAll('&','&').replaceAll('\"','"').replaceAll('<','<').replaceAll('>','>'); + list.innerHTML = values.map(({ value, count }) => { + const safe = escapeSuggestion(value); + const label = count ? `${safe}(${count})` : safe; + + return `
  • `; + }).join(''); list.hidden = values.length === 0; list.querySelectorAll('button').forEach((button) => button.addEventListener('click', () => { const input = field.querySelector('[data-directory-query-input]'); diff --git a/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php index 123c4dd4..b50f5232 100644 --- a/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php @@ -173,8 +173,12 @@ class ShopInfoFlowMirrorRepositoryTest extends TestCase 'current_approver' => '玉兔', ]); - $this->assertEqualsCanonicalizing(['查查', '玉兔', '常仪'], $options); - $this->assertSame(['审核人A'], $this->repository->currentApproverOptions([ + $this->assertEquals([ + '查查' => 2, + '玉兔' => 1, + '常仪' => 1, + ], array_column($options, 'count', 'value')); + $this->assertSame([['value' => '审核人A', 'count' => 1]], $this->repository->currentApproverOptions([ 'flow_type' => ['店铺变更'], ])); }