|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use App\Exceptions\YuanhePackInfoException;
|
|
|
use App\Models\Company;
|
|
|
use App\Models\CompanyQccAccount;
|
|
|
use App\Repositories\YuanheRepository;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
use Throwable;
|
|
|
|
|
|
class VerifyQccEnterprisePackInfo extends Command
|
|
|
{
|
|
|
protected $signature = 'qcc:verify-pack-info
|
|
|
{company_id : 候选、已占额或待人工确认企业的本地企业 ID}
|
|
|
{--timeout=60 : 请求超时时间(秒,正整数)}
|
|
|
{--raw : 兼容旧用法,默认即输出完整响应}';
|
|
|
|
|
|
protected $description = '诊断元禾 packInfo 接口,按 code=200 判断成功,仅输出响应,不修改企业状态';
|
|
|
|
|
|
public function handle(YuanheRepository $yuanheRepository): int
|
|
|
{
|
|
|
$timeout = filter_var($this->option('timeout'), FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
|
|
if ($timeout === false) {
|
|
|
$this->error('timeout 必须为正整数(单位:秒)。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$company = Company::find($this->argument('company_id'));
|
|
|
if (!$company) {
|
|
|
$this->error('企业不存在。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$account = $company->qccAccount;
|
|
|
if (!$account || !in_array($account->status, [CompanyQccAccount::STATUS_SELECTED, CompanyQccAccount::STATUS_OCCUPIED, CompanyQccAccount::STATUS_UNKNOWN], true)) {
|
|
|
$status = $account ? $account->status : 'none';
|
|
|
$this->error("企业必须处于候选(selected)、已占额(occupied)或待人工确认(unknown)状态,当前状态:{$status}。");
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
if (empty(trim((string) $company->credit_code))) {
|
|
|
$this->error('企业缺少统一社会信用代码,无法调用 packInfo。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
$response = $yuanheRepository->enterprisePackInfo([
|
|
|
'creditCode' => $company->credit_code,
|
|
|
'enterpriseName' => $company->company_name,
|
|
|
], $timeout);
|
|
|
} catch (Throwable $exception) {
|
|
|
// 非 JSON 响应保留原文;没有响应时仅输出错误,不打印签名或请求头。
|
|
|
$result = $exception instanceof YuanhePackInfoException && $exception->rawResponse !== null
|
|
|
? $exception->rawResponse
|
|
|
: $exception->getMessage();
|
|
|
$this->output->writeln($result, OutputInterface::OUTPUT_RAW);
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$this->output->writeln(
|
|
|
json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT),
|
|
|
OutputInterface::OUTPUT_RAW
|
|
|
);
|
|
|
|
|
|
return in_array($response['code'] ?? null, [200, '200'], true) ? self::SUCCESS : self::FAILURE;
|
|
|
}
|
|
|
}
|