feat: 店铺流程审批人显示卡单数量 #972

Merged
renshen merged 1 commits from fix/renshen-process-approver-counts into main 2026-11-06 23:19:46 +08:00
3 changed files with 50 additions and 17 deletions

View File

@ -116,8 +116,9 @@ class ShopInfoFlowMirrorRepository
return ['data' => $rows, 'total' => $total];
}
/** @param array<string,mixed> $filters
* @return list<string>
/**
* @param array<string,mixed> $filters
* @return list<array{value:string,count:int}>
*/
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();
}

View File

@ -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) => `<li><button type="button" data-directory-suggestion-value="${item.replaceAll('&','&amp;').replaceAll('\"','&quot;').replaceAll('<','&lt;').replaceAll('>','&gt;')}">${item.replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;')}</button></li>`).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('&','&amp;').replaceAll('\"','&quot;').replaceAll('<','&lt;').replaceAll('>','&gt;');
list.innerHTML = values.map(({ value, count }) => {
const safe = escapeSuggestion(value);
const label = count ? `${safe}(${count})` : safe;
return `<li><button type="button" data-directory-suggestion-value="${safe}">${label}</button></li>`;
}).join('');
list.hidden = values.length === 0;
list.querySelectorAll('button').forEach((button) => button.addEventListener('click', () => {
const input = field.querySelector('[data-directory-query-input]');

View File

@ -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' => ['店铺变更'],
]));
}