<?php

namespace App\Jobs\OpenWork\Contact;

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 App\Servers\DB\DbService;
use App\Models\OpenWork\job\Records;
use App\Facades\Servers\Logs\Log;
use App\Models\OpenWork\Group\Chat as chatModel;
use Illuminate\Support\Facades\Cache;


/**
 * 群成员队列
 * @author 唐远望
 * @date 2025-06-04
 * @version 1.0
 */

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

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


    public function getCorpId()
    {
        return $this->member_data['authCorpId'] ?? null;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $corpId = $this->member_data['authCorpId'];
        (new DbService())->getConnectionNameByCorpId($corpId);

        // 创建任务记录
        $this->Records = Records::create([
            'job_id' => $this->member_data['authCorpId'] . '_GroupMemberJobs',
            'name' => static::class,
            'payload' => json_encode($this->member_data),
            'status' => 'processing',
            'started_at' => now()
        ]);
        $Chat_model = new chatModel();
        $corpid = $this->member_data['authCorpId'];
        $chat_id = $this->member_data['chat_id'];
        $status = $this->member_data['status'];
        //获取redis缓存
        $chat_list_redis = Cache::get('openwork_chat_list_' . $corpid);
        try {
            if (!empty($chat_list_redis)) {
                $corpid_chat_id = $corpid . '_' . $chat_id;
                if (in_array($corpid_chat_id, $chat_list_redis)) {
                    $Chat_model->edit($corpid, $chat_id, $status);
                } else {
                    $Chat_model->add($corpid, $chat_id, $status);
                }
            } else {
                $chat_data_log  = $Chat_model->where([['corpid', '=', $corpid], ['chat_id', '=', $chat_id]])->first();
                if (!$chat_data_log) {
                    $Chat_model->add($corpid, $chat_id, $status);
                } else {
                    $Chat_model->edit($corpid, $chat_id, $status);
                }
            }
            //删除任务记录
            $this->Records->delete();
        } catch (\Exception $e) {
            // 失败处理...
            if ($this->Records) {
                $this->Records->delete();
            }
            Log::info('job_error', '群成员队列失败-异常原因', ['data'=>$this->member_data,'error' => $e->getMessage()]);
        }
    }

    public function failed(\Throwable $exception)
    {
        Log::info('job_error', 'GroupMemberJobs彻底失败', ['data'=>$this->member_data,'error' => $exception]);
        // 失败处理...
        if ($this->Records) {
            $this->Records->delete();
        }
    }
}
