<?php

namespace App\Jobs;

use App\Models\Group;
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;

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

    protected $user, $chat_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user, $chat_id)
    {
        $this->user = $user;
        $this->chat_id = $chat_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->chat_id) {
            $group = Group::with('order')->where('chat_id', $this->chat_id)->first();
            if (empty($group) || empty($group->order)) throw new \Exception("占位订单转虚拟订单，没有群信息或没有占位订单");
            if ($group->order->type == 'TEMP') {
                $group->order->update([
                    'user_id'=>$this->user->id, 'type'=>'SERVICE', 'type_id'=>1, 'pay_status'=>'PAID', 'name'=>$this->user->name,
                    'mobile'=>$this->user->mobile, 'desc'=>'福恋智能健康', 'is_hook'=>1
                ]);
            }elseif (empty($group->order->user_id)) {
                $group->order->update(['user_id'=>$this->user->id]);
            }
        }
        //同步订单服务信息状态
//        SyncOrderServiceStatus::dispatch($this->user)->onQueue('health');
    }
}
