172 lines
6.9 KiB
PHP
172 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Business\WithdrawalBalanceAlertSynchronizer;
|
||
use App\Models\Business\StoreReceivingAccount;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Console\Command;
|
||
use Modules\EcommerceFinance\Services\FinanceBillShopBalanceFallback;
|
||
use Modules\EcommerceFinance\Services\JushuitanShopBalanceImporter;
|
||
use Throwable;
|
||
|
||
class ImportJushuitanShopBalances extends Command
|
||
{
|
||
protected $signature = 'business:import-jushuitan-shop-balances
|
||
{path : 聚水潭店铺余额接口 JSON 或 XLSX/XLS 导出文件}
|
||
{--balance-date= : 导入日期,格式 YYYY-MM-DD;多日期导出必须指定}
|
||
{--date-range= : 连续日期范围 YYYY-MM-DD:YYYY-MM-DD;一次导入多天余额}
|
||
{--scope=all : 来源范围,同一日期同一范围会被原子替换}
|
||
{--dry-run : 只校验和统计,不写入共享业务库}';
|
||
|
||
protected $description = '导入聚水潭店铺余额,并用财务账单补录缺失店铺';
|
||
|
||
public function handle(
|
||
JushuitanShopBalanceImporter $importer,
|
||
WithdrawalBalanceAlertSynchronizer $alertSynchronizer,
|
||
FinanceBillShopBalanceFallback $financeFallback,
|
||
): int
|
||
{
|
||
$dateRange = trim((string) ($this->option('date-range') ?? ''));
|
||
$balanceDate = $this->option('balance-date');
|
||
$allDates = $dateRange !== '';
|
||
if ($allDates && $balanceDate !== null) {
|
||
$this->error('--date-range 与 --balance-date 不能同时使用。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
try {
|
||
if ($allDates) {
|
||
$result = $importer->importMany(
|
||
(string) $this->argument('path'),
|
||
$this->rangeDates($dateRange),
|
||
(string) $this->option('scope'),
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
} else {
|
||
$result = $importer->import(
|
||
(string) $this->argument('path'),
|
||
$balanceDate === null ? null : (string) $balanceDate,
|
||
(string) $this->option('scope'),
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
}
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->error($exception->getMessage());
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($allDates) {
|
||
foreach ($result['dates'] as $daily) {
|
||
$this->info(sprintf(
|
||
'%s:%s 共 %d 条,替换原有 %d 条。',
|
||
$daily['dry_run'] ? '校验完成' : '导入完成',
|
||
$daily['balance_date'],
|
||
$daily['total'],
|
||
$daily['replaced'],
|
||
));
|
||
}
|
||
} else {
|
||
$this->info(sprintf(
|
||
'%s:%s 共 %d 条,替换原有 %d 条。',
|
||
$result['dry_run'] ? '校验完成' : '导入完成',
|
||
$result['balance_date'],
|
||
$result['total'],
|
||
$result['replaced'],
|
||
));
|
||
}
|
||
|
||
$financeFallbackFailed = false;
|
||
if ($allDates) {
|
||
$this->warn('多日期历史导入未执行财务账单回填,聚水潭原始快照已按日期写入。');
|
||
} else {
|
||
try {
|
||
$fallback = $financeFallback->fillMissing(
|
||
$result['balance_date'],
|
||
(string) $this->option('scope'),
|
||
(bool) $result['dry_run'],
|
||
);
|
||
if (! $fallback['supported']) {
|
||
$this->warn('账单余额回填:当前来源的店铺收款账户账单没有累计账户余额字段,跳过回填。');
|
||
} elseif ($fallback['missing'] === 0) {
|
||
$this->info('账单余额回填:聚水潭余额没有缺失店铺。');
|
||
} else {
|
||
$this->info(sprintf(
|
||
'账单余额回填:缺失 %d 家,匹配 %d 家,写入 %d 条,仍有 %d 家待补。',
|
||
$fallback['missing'],
|
||
$fallback['resolved'],
|
||
$fallback['filled'],
|
||
$fallback['unresolved'],
|
||
));
|
||
}
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->error('财务系统店铺收款账户账单余额回填失败,聚水潭余额已保留,财务回填未覆盖。');
|
||
$financeFallbackFailed = true;
|
||
}
|
||
}
|
||
|
||
if (! $result['dry_run']) {
|
||
$companyIds = StoreReceivingAccount::withoutGlobalScopes()
|
||
->whereNull('deleted_at')
|
||
->distinct()
|
||
->pluck('company_id')
|
||
->filter()
|
||
->map(static fn (mixed $id): int => (int) $id)
|
||
->unique()
|
||
->values();
|
||
|
||
foreach ($companyIds as $companyId) {
|
||
try {
|
||
$alerts = $alertSynchronizer->syncLatest($companyId);
|
||
if ($alerts['created'] > 0 || $alerts['updated'] > 0) {
|
||
$this->info(sprintf(
|
||
'余额预警待办:公司 %d 新增 %d 条、更新 %d 条。',
|
||
$companyId,
|
||
$alerts['created'],
|
||
$alerts['updated'],
|
||
));
|
||
}
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->warn("余额预警待办生成失败(公司 {$companyId}),余额快照已保留。");
|
||
}
|
||
}
|
||
}
|
||
|
||
return $financeFallbackFailed ? self::FAILURE : self::SUCCESS;
|
||
}
|
||
|
||
/** @return list<string> */
|
||
private function rangeDates(string $range): array
|
||
{
|
||
$parts = explode(':', $range);
|
||
if (count($parts) !== 2) {
|
||
throw new \InvalidArgumentException('日期范围格式应为 YYYY-MM-DD:YYYY-MM-DD。');
|
||
}
|
||
|
||
$start = CarbonImmutable::createFromFormat('!Y-m-d', trim($parts[0]), 'Asia/Shanghai');
|
||
$end = CarbonImmutable::createFromFormat('!Y-m-d', trim($parts[1]), 'Asia/Shanghai');
|
||
if (! $start instanceof CarbonImmutable
|
||
|| $start->format('Y-m-d') !== trim($parts[0])
|
||
|| ! $end instanceof CarbonImmutable
|
||
|| $end->format('Y-m-d') !== trim($parts[1])
|
||
|| $end->lt($start)) {
|
||
throw new \InvalidArgumentException('日期范围格式应为 YYYY-MM-DD:YYYY-MM-DD,且结束日期不能早于开始日期。');
|
||
}
|
||
if ($start->diffInDays($end) > 366) {
|
||
throw new \InvalidArgumentException('单次多日期导入不能超过 366 天。');
|
||
}
|
||
|
||
$dates = [];
|
||
for ($date = $start; $date->lte($end); $date = $date->addDay()) {
|
||
$dates[] = $date->format('Y-m-d');
|
||
}
|
||
|
||
return $dates;
|
||
}
|
||
}
|