<?php

namespace App\Jobs\Callback;

use App\Service\Callback\JuliangAccountReportChargeService;
use App\Service\Util\Support\Trace\TraceContext;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class ReportCharge implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /***
     * @var array
     * <pre>
     * [
     *  'uid' => '1',
     *  'orderId' => 1
     *  'ranseId' => 1,
     * ]
     * </pre>
     */
    private $info;
    /**
     * Create a new job instance.
     */
    public function __construct($info)
    {
        $this->info = $info;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $traceContext = TraceContext::newFromParent($this->info['traceInfo']);
        myLog('reportCharge')->info('开始处理订单回传', [
            'orderInfo' => $this->info,
            'traceInfo' => $traceContext->getTraceInfo()
        ]);

        $order = DB::table('orders')->where('id', $this->info['orderId'])->first();
        if($order && $order->promotion_id) {
            $promotion = DB::table('promotions')->where([
                'id'=> $order->promotion_id,
                'is_enabled' => 1
            ])->first();
            switch ($promotion->callback_type ?? 0) {
                case 0:
                    myLog('reportCharge')->error('end:订单染色配置有问题', [
                        'traceInfo' => $traceContext->getTraceInfo()
                    ]);
                    break;
                case 1:
                    //巨量账户级别回传
                    $reportService = new JuliangAccountReportChargeService($this->info['uid'], $order, $this->info['traceInfo']);
                    $reportService->report();
                    break;
            }
        }

    }
}