<?php

namespace Kylin\Payment\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Kylin\Payment\Payment;
use Illuminate\Support\Facades\Log;

/**
 * 查询订单队列任务
 */
class QueryJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 构造函数
     */
    public function __construct(
        protected readonly string $type,
        protected readonly string $orderNo,
        protected readonly string $name = 'default'
    ) {}

    /**
     * 执行队列任务
     */
    public function handle(): void
    {
        try {
            // 执行查询订单
            $result = Payment::query($this->type, $this->orderNo, $this->name);

            // 记录成功日志
            Log::info('查询订单队列任务执行成功', [
                'type' => $this->type,
                'order_no' => $this->orderNo,
                'result' => $result,
            ]);
        } catch (\Exception $e) {
            // 记录失败日志
            Log::error('查询订单队列任务执行失败', [
                'type' => $this->type,
                'order_no' => $this->orderNo,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
            ]);

            // 可以根据需要进行重试或其他处理
            $this->fail($e);
        }
    }
}
