CaiWuXiTong/app/Console/Commands/ImportSystemizationTodos.php

58 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

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\Business\SystemizationTodoWorkbookImporter;
use Illuminate\Console\Command;
use Throwable;
class ImportSystemizationTodos extends Command
{
protected $signature = 'systemization-todos:import
{file : 待办 Excel 文件路径}
{--company=1 : 写入的 company_id}
{--user= : 操作人用户 ID}
{--sync : 新增并更新现有来源事项,保留截图}
{--dry-run : 只预览同步结果,不写入数据库}';
protected $description = '导入或同步财务系统化待办清单';
public function handle(SystemizationTodoWorkbookImporter $importer): int
{
try {
$file = (string) $this->argument('file');
$companyId = max((int) $this->option('company'), 1);
$userId = $this->option('user') !== null ? (int) $this->option('user') : null;
if ($this->option('sync')) {
$result = $importer->sync($file, $companyId, $userId, (bool) $this->option('dry-run'));
} else {
$result = $importer->import($file, $companyId, $userId);
}
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
if ($this->option('sync')) {
$this->info(sprintf(
'%s工作表 %s:新增 %d 条,更新 %d 条,保留工作簿外事项 %d 条。',
$this->option('dry-run') ? '同步预检:' : '同步完成:',
$result['sheet'],
$result['created'],
$result['updated'],
$result['retained'],
));
} else {
$this->info(sprintf(
'工作表 %s:新增 %d 条,跳过已存在 %d 条。',
$result['sheet'],
$result['created'],
$result['skipped'],
));
}
return self::SUCCESS;
}
}