|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use App\Exceptions\YuanhePackInfoException;
|
|
|
use App\Models\Company;
|
|
|
use App\Models\CompanyQccAccount;
|
|
|
use App\Repositories\YuanheRepository;
|
|
|
use App\Services\QccCallService;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Throwable;
|
|
|
|
|
|
class VerifyQccEnterprisePackInfo extends Command
|
|
|
{
|
|
|
protected $signature = 'qcc:verify-pack-info
|
|
|
{company_id : 已纳入候选池的本地企业 ID}
|
|
|
{--raw : 输出元禾完整响应 JSON}';
|
|
|
|
|
|
protected $description = '调用一次元禾企业户聚合接口 packInfo,验证企业户占额状态流转';
|
|
|
|
|
|
public function handle(): int
|
|
|
{
|
|
|
$company = Company::find($this->argument('company_id'));
|
|
|
if (!$company) {
|
|
|
$this->error('企业不存在。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$account = $company->qccAccount;
|
|
|
if (!$account || $account->status !== CompanyQccAccount::STATUS_SELECTED) {
|
|
|
$status = $account ? $account->status : 'none';
|
|
|
$this->error("企业必须处于候选状态(selected),当前状态:{$status}。");
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
if (empty(trim((string) $company->credit_code))) {
|
|
|
$this->error('企业缺少统一社会信用代码,无法调用 packInfo。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$requestRef = sprintf('pack-info-cli-%d-%s', $company->id, now()->format('YmdHisv'));
|
|
|
$yuanheRepository = new YuanheRepository();
|
|
|
$this->info("开始调用 packInfo:{$company->company_name}({$company->credit_code})");
|
|
|
$this->line("请求引用:{$requestRef}");
|
|
|
|
|
|
try {
|
|
|
$response = (new QccCallService())->call(
|
|
|
$company,
|
|
|
'enterprise-pack-info-cli',
|
|
|
$requestRef,
|
|
|
fn () => $yuanheRepository->enterprisePackInfo([
|
|
|
'creditCode' => $company->credit_code,
|
|
|
'enterpriseName' => $company->company_name,
|
|
|
]),
|
|
|
fn (array $result) => array_key_exists('code', $result) && (int) $result['code'] === 0,
|
|
|
fn (array $result) => !array_key_exists('code', $result)
|
|
|
? '元禾 packInfo 响应缺少业务状态码 code'
|
|
|
: false
|
|
|
);
|
|
|
} catch (Throwable $exception) {
|
|
|
$this->error($exception->getMessage());
|
|
|
$this->line('签名 timestamp:' . ($yuanheRepository->lastRequestTimestamp ?? '(未生成)'));
|
|
|
if ($exception instanceof YuanhePackInfoException) {
|
|
|
$this->table(
|
|
|
['HTTP 状态', 'Content-Type', '响应字节数'],
|
|
|
[[
|
|
|
$exception->httpStatus ?? '(无响应)',
|
|
|
$exception->contentType ?? '(未提供)',
|
|
|
$exception->rawResponse === null ? 0 : strlen($exception->rawResponse),
|
|
|
]]
|
|
|
);
|
|
|
if ($this->option('raw') && $exception->rawResponse !== null) {
|
|
|
$this->line('原始响应:');
|
|
|
$this->line($exception->rawResponse);
|
|
|
}
|
|
|
}
|
|
|
$this->line('调用后的企业户状态:' . $account->fresh()->status);
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$freshAccount = $account->fresh();
|
|
|
$this->line('签名 timestamp:' . ($yuanheRepository->lastRequestTimestamp ?? '(未生成)'));
|
|
|
$this->table(
|
|
|
['元禾 code', '元禾 msg', '调用后状态', '首次成功引用'],
|
|
|
[[
|
|
|
$response['code'] ?? '(缺失)',
|
|
|
$response['msg'] ?? '',
|
|
|
$freshAccount->status,
|
|
|
$freshAccount->first_success_ref ?? '',
|
|
|
]]
|
|
|
);
|
|
|
|
|
|
if ($this->option('raw')) {
|
|
|
$this->line(json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
|
|
|
}
|
|
|
|
|
|
if (($response['code'] ?? null) !== 0 && ($response['code'] ?? null) !== '0') {
|
|
|
$this->warn('元禾未返回明确成功(code !== 0);企业保持候选状态。');
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$this->info('元禾返回明确成功,企业已标记为已占额。');
|
|
|
return self::SUCCESS;
|
|
|
}
|
|
|
}
|