$storeNames * @return array{ * current_companies:array, * taxpayer_types:array, * company_plan_shops:array> * } */ public function map(array $storeNames, int $companyId): array { $names = []; foreach ($storeNames as $storeName) { $name = trim((string) $storeName); if ($name !== '') { $names[$name] = $name; } } if ($names === []) { return ['current_companies' => [], 'taxpayer_types' => [], 'company_plan_shops' => []]; } $taxpayerTypes = []; foreach ($this->companyPlans->allSubjectReferences($companyId) as $plan) { $key = $this->normalize($plan['company_name'] ?? null); $taxpayerType = trim((string) ($plan['taxpayer_type'] ?? '')); if ($key !== '' && $taxpayerType !== '') { $taxpayerTypes[$key] = $taxpayerType; } } $companyPlanShops = []; foreach ($this->companyPlanShops->allShopReferences($companyId) as $shop) { $key = $this->normalize($shop['shop_name'] ?? null); if ($key !== '') { $companyPlanShops[$key] = $shop; } } return [ 'current_companies' => $this->periods->currentCompanies(array_map( static fn (string $name): array => ['erp_store_name' => $name], array_values($names), )), 'taxpayer_types' => $taxpayerTypes, 'company_plan_shops' => $companyPlanShops, ]; } /** * @param array{ * current_companies:array, * taxpayer_types:array, * company_plan_shops:array> * } $map */ public function apply(object $row, array $map): object { // 业绩明细使用 store_name,小额打款使用 shop_name;两者共用同一套当前主体口径。 $storeName = trim((string) ($row->store_name ?? $row->shop_name ?? '')); $companyName = trim((string) ($map['current_companies'][$storeName] ?? '')); if ($companyName === '') { $companyName = $this->companyPlanCompany($storeName, $map['company_plan_shops'] ?? []); } $row->company_name = $companyName !== '' ? $companyName : null; $row->taxpayer_type = $companyName !== '' ? ($map['taxpayer_types'][$this->normalize($companyName)] ?? null) : null; return $row; } /** * @param array $row * @param array $map * @return array */ public function applyArray(array $row, array $map): array { return (array) $this->apply((object) $row, $map); } /** @param array> $shops */ private function companyPlanCompany(string $storeName, array $shops): string { $shop = $shops[$this->normalize($storeName)] ?? null; if ($shop === null) { return ''; } $successor = trim((string) ($shop['successor'] ?? '')); if ($successor !== '') { return $successor; } return trim((string) ($shop['company_name'] ?? '')); } private function normalize(mixed $value): string { $text = str_replace(['(', ')'], ['(', ')'], trim((string) $value)); $text = preg_replace('/[\s\x{00A0}\x{3000}]+/u', '', $text) ?? $text; return mb_strtolower($text); } }