<?php

namespace App\Jobs;

use App\Services\WeChat\WechatFactory;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class WeChatFansJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

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


    private $publicId;

    private $openid_list = [];

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $user_info_list = WechatFactory::Fans($this->publicId)->batchGet($this->openid_list);
        foreach ($user_info_list as $user_info) {
            try {
                if ($user_info->subscribe) {
                    $data = [
                        'nickname' => trim($user_info->nickname) ?: "",
                        'sex' => $user_info->sex,
                        'language' => $user_info->language ?: "",
                        'city' => $user_info->city ?: "",
                        'province' => $user_info->province ?: "",
                        'country' => $user_info->country ?: "",
                        'headimgurl' => $user_info->headimgurl ?: "",
                        'unionid' => isset($user_info->unionid) ? $user_info->unionid : "",
                        'subscribe_time' => $user_info->subscribe_time,
                        'remark' => $user_info->remark ?: "",
                        'groupid' => $user_info->groupid,
                        'tagid_list' => json_encode($user_info->tagid_list),
                        'subscribe_scene' => $user_info->subscribe_scene ?: "",
                        'qr_scene' => $user_info->qr_scene ?: $user_info->qr_scene_str
                    ];

                    \DB::table("kddx_user_openid")
                        ->where('openid', $user_info->openid)
                        ->update($data);

                    \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
                        ->where('openid', $user_info->openid)
                        ->update($data);
                } else {
                    \DB::table('kddx_user_openid')->where('openid', $user_info->openid)
                        ->update([
                            'subscribe' => 0
                        ]);
                    \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
                        ->where('openid', $user_info->openid)
                        ->update([
                            'subscribe' => 0
                        ]);
                }
            } catch (\Exception $e) {
                dump($user_info);
                dump($e);
            }
        }
    }
}
