companyNames((string) $this->argument('path')); if ($companies === []) { throw new \RuntimeException('公司保留名单中没有可识别的公司名称。'); } $connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')); $accounts = $connection->table('fin_accounts') ->orderBy('id') ->get(['id', 'company_name', 'account_type']); $retainedIds = []; $temporaryIds = []; $preservedIds = []; $matchedKeys = []; foreach ($accounts as $account) { $key = $this->normalizeCompanyName((string) ($account->company_name ?? '')); $accountId = (int) $account->id; if ($key !== '' && isset($companies[$key])) { $matchedKeys[$key] = true; } if (in_array(trim((string) ($account->account_type ?? '')), self::PRESERVED_ACCOUNT_TYPES, true)) { $preservedIds[] = $accountId; } elseif ($key !== '' && isset($companies[$key])) { $retainedIds[] = $accountId; } else { $temporaryIds[] = $accountId; } } $unmatchedCompanies = array_values(array_diff_key($companies, $matchedKeys)); $this->table( ['名单公司', '匹配名单公司', '账户总数', '更新为保留', '更新为暂时保留', '保持原状'], [[ count($companies), count($matchedKeys), $accounts->count(), count($retainedIds), count($temporaryIds), count($preservedIds), ]], ); if ($unmatchedCompanies !== []) { $this->warn('以下名单公司没有匹配到账户:' . implode('、', $unmatchedCompanies)); } if ((bool) $this->option('dry-run')) { $this->info('核对完成,未写入数据。'); return self::SUCCESS; } $now = now(); $connection->transaction(function () use ( $connection, $retainedIds, $temporaryIds, $now, ): void { foreach (array_chunk($retainedIds, 500) as $ids) { $connection->table('fin_accounts') ->whereIn('id', $ids) ->update([ 'company_plan' => '保留', 'account_should_keep' => '是', 'updated_at' => $now, ]); } foreach (array_chunk($temporaryIds, 500) as $ids) { $connection->table('fin_accounts') ->whereIn('id', $ids) ->update([ 'company_plan' => '不保留', 'account_should_keep' => FinanceLedgerAccountShouldKeep::TEMPORARY, 'updated_at' => $now, ]); } }); $this->info(sprintf( '已更新 %d 个账户:保留 %d 个,暂时保留 %d 个;另有 %d 个支付宝或网商银行账户保持原状。', count($retainedIds) + count($temporaryIds), count($retainedIds), count($temporaryIds), count($preservedIds), )); return self::SUCCESS; } catch (Throwable $exception) { $this->error($exception->getMessage()); return self::FAILURE; } } /** * @return array */ private function companyNames(string $path): array { if (! is_file($path)) { throw new \RuntimeException('公司保留名单文件不存在:' . $path); } $worksheet = IOFactory::load($path)->getActiveSheet(); $rows = $worksheet->toArray(null, true, true, false); if ($rows === []) { return []; } $header = array_map( fn (mixed $value): string => $this->normalizeCompanyName((string) $value), $rows[0], ); $column = array_search('公司名称', $header, true); if ($column === false) { $column = 0; } else { array_shift($rows); } $companies = []; foreach ($rows as $row) { $name = trim((string) ($row[$column] ?? '')); $key = $this->normalizeCompanyName($name); if ($key !== '') { $companies[$key] = $name; } } return $companies; } private function normalizeCompanyName(string $name): string { $name = str_replace( ['(', ')', '【', '】', ' '], ['(', ')', '[', ']', ' '], trim($name), ); $name = (string) preg_replace('/\s+/u', '', $name); return mb_strtolower($name, 'UTF-8'); } }