<?php
namespace App\Listeners;


use App\Events\ExternalAndFollowOperateEvent;
use App\Facades\Servers\Logs\Log;
use App\Models\OpenWork\External\ExternalFollowOperateRecord as Model;
use App\Models\OpenWork\External\Follow;
use App\Servers\DB\DbService;
use Illuminate\Support\Facades\DB;

class ExternalAndFollowOperateListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function handle(ExternalAndFollowOperateEvent $event)
    {
        $data['corpid']                 = $event->corpId;
        $data['follow_userid']          = $event->followUserId;
        $data['external_userid']        = $event->externalUserId;
        $data['event_type']             = $event->eventType;
        $data['state']                  = $event->state;
        $data['insert_time']            = time();
        $data['update_time']            = time();

        (new DbService())->getConnectionNameByCorpId($event->corpId);
        if ($data['event_type']  == 2 || $data['event_type']  == 3){
            $Follow                     = new Follow();
            $data['state']              = $Follow->query()->where([['corpid', '=', $event->corpId],['follow_userid', '=', $event->followUserId],['external_userid', '=', $event->externalUserId]])
                ->value('state');

        }
        Log::info('open_notify_add_event', '-----------------------企业客户事件-----------------------', $data);
        $Model = new Model();
        $Model->query()->insert($data); // 写入数据


//        if (!$Model->query()->where('corpid', '=', $event->corpId)->where('follow_userid', '=', $event->followUserId)
//            ->where('external_userid', '=', $event->externalUserId)->where('event_type', '=', $event->eventType)->exists()) {
//            $Model->query()->insert($data); // 写入数据
//        }
    }

}