82 lines
4.0 KiB
PHP
82 lines
4.0 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace app\command;
|
||
|
||
use app\common\service\esign\EsignClient;
|
||
use app\common\service\esign\PrescriptionEsignService;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\console\input\Option;
|
||
use think\facade\Config;
|
||
use think\facade\Db;
|
||
|
||
final class EsignSetup extends Command
|
||
{
|
||
protected function configure(): void
|
||
{
|
||
$this->setName('esign:setup')->setDescription('检查 e签宝配置、初始化内网 SDK 或恢复已归档签署结果')
|
||
->addOption('initialize', null, Option::VALUE_NONE, '初始化已部署的 SDK;不创建医生、认证或处方签署')
|
||
->addOption('recover', null, Option::VALUE_REQUIRED, '重新验签已归档的处方 ID;不会再次签署')
|
||
->addOption('backfill-signature-times', null, Option::VALUE_NONE, '从当前签署环境及应用的已验签原件补录签署时间');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output): int
|
||
{
|
||
$config = (array) Config::get('esign', []);
|
||
$output->writeln('environment=' . ($config['environment'] ?? 'sandbox'));
|
||
$output->writeln('enabled=' . (PrescriptionEsignService::enabled() ? 'true' : 'false'));
|
||
$output->writeln('credentials=' . (!empty($config['app_id']) && !empty($config['app_secret']) ? 'configured' : 'missing'));
|
||
try {
|
||
if ($input->getOption('recover') !== null) {
|
||
$id = filter_var($input->getOption('recover'), FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||
if (!$id) {
|
||
throw new \DomainException('recover 必须为正整数处方ID');
|
||
}
|
||
PrescriptionEsignService::recover($id);
|
||
$output->writeln('原件验签及状态恢复完成;未发送新的签署请求');
|
||
} elseif ($input->getOption('backfill-signature-times')) {
|
||
(new EsignClient())->assertConfigured();
|
||
return $this->backfillSignatureTimes($config, $output);
|
||
} elseif ($input->getOption('initialize')) {
|
||
// Deployment may initialize before enabling the new clinical workflow.
|
||
$client = new EsignClient(array_merge($config, ['enabled' => true]));
|
||
$client->assertConfigured();
|
||
$client->initialize();
|
||
$output->writeln('SDK 初始化成功;尚未执行医生核身或处方签署');
|
||
} else {
|
||
$output->writeln('配置检查完成;部署 SDK 后使用 --initialize,完成数据库迁移后再启用签署');
|
||
}
|
||
return 0;
|
||
} catch (\Throwable $e) {
|
||
$output->writeln('<error>' . ($e instanceof \DomainException ? $e->getMessage() : 'SDK 操作失败,请检查内网服务、应用环境和 e签宝 IP 白名单') . '</error>');
|
||
return 1;
|
||
}
|
||
}
|
||
|
||
private function backfillSignatureTimes(array $config, Output $output): int
|
||
{
|
||
$filled = 0;
|
||
$failed = 0;
|
||
Db::name('prescription_esign')
|
||
->where('environment', $config['environment'])->where('app_id', $config['app_id'])
|
||
->where('signed_at', '>', 0)->where('signature_time', 0)->field('prescription_id')
|
||
->chunk(100, static function ($rows) use ($output, &$filled, &$failed): void {
|
||
foreach ($rows as $row) {
|
||
$id = (int) $row['prescription_id'];
|
||
try {
|
||
PrescriptionEsignService::recover($id);
|
||
$filled++;
|
||
} catch (\Throwable $error) {
|
||
$failed++;
|
||
$message = $error instanceof \DomainException ? $error->getMessage() : '暂时无法验签,请检查 SDK 服务';
|
||
$output->writeln("处方 {$id} 的签署时间待核实({$message})");
|
||
}
|
||
}
|
||
}, 'prescription_id');
|
||
$output->writeln("签署时间补录完成 {$filled} 张,待核实 {$failed} 张");
|
||
return $failed === 0 ? 0 : 1;
|
||
}
|
||
}
|