CaiWuXiTong/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php
shanshi 803764d559 feat: link company plan to public account keep status
Public accounts follow company plan; personal cards remain retained.
2026-12-01 17:13:03 +08:00

185 lines
6.6 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use App\Utilities\FinanceLedgerAccountType;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Throwable;
class AssignFinanceLedgerCompanyPlans extends Command
{
private const PRESERVED_ACCOUNT_TYPES = ['支付宝', '网商银行'];
protected $signature = 'finance-ledger:assign-company-plans
{path : 公司保留名单 XLSX/XLS 文件}
{--dry-run : 只核对公司和账户数量,不写入数据}';
protected $description = '按公司保留名单设置账户公司规划与是否保留';
public function handle(): int
{
try {
$companies = $this->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 = [];
$personalCardIds = [];
$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 (FinanceLedgerAccountType::isAlwaysRetained($account->account_type ?? null)) {
$personalCardIds[] = $accountId;
} elseif (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($personalCardIds),
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,
$personalCardIds,
$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,
]);
}
foreach (array_chunk($personalCardIds, 500) as $ids) {
$connection->table('fin_accounts')
->whereIn('id', $ids)
->update([
'account_should_keep' => '是',
'updated_at' => $now,
]);
}
});
$this->info(sprintf(
'已更新 %d 个账户:保留 %d 个,暂时保留 %d 个,个人卡固定保留 %d 个;另有 %d 个支付宝或网商银行账户保持原状。',
count($retainedIds) + count($temporaryIds) + count($personalCardIds),
count($retainedIds),
count($temporaryIds),
count($personalCardIds),
count($preservedIds),
));
return self::SUCCESS;
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
}
/**
* @return array<string,string>
*/
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');
}
}