<?php

namespace app\subscribe;

use app\common\model\User as UserModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\Comjobs as ComjobsModel;
use app\common\model\ComjobsLog as ComjobsLogModel;
use app\common\model\ComjobsReport as ComjobsReportModel;
use app\common\model\ComjobsStar as ComjobsStarModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\WorkerForm as WorkerFormModel;
use app\common\model\WorkerLog as WorkerLogModel;
use app\common\model\ResumeInvite as ResumeInviteModel;
use app\common\model\Agent as AgentModel;
use app\common\model\UserAuths as UserAuthsModel;
use app\common\model\Demand as DemandModel;
use app\common\model\DemandLog as DemandLogModel;
use app\common\model\Supply as SupplyModel;
use think\Event;

class User
{
    /**
     * 删除人员后事件
     */
    public function onUserDel($ids)
    {
        //要删除的经纪人id
        $del_broker_ids = [];

        //公司
        $worker_ids = WorkerModel::where('userid', 'in', $ids)->column('id');
        if (!empty($worker_ids)) {
            //岗位
            $com_ids = ComjobsModel::where('workerid', 'in', $worker_ids)->column('id');
            if (!empty($com_ids)) {
                ComjobsStarModel::where('comjobsid', 'in', $com_ids)->delete();
            }
            ComjobsModel::where('workerid', 'in', $worker_ids)->delete();
            ComjobsLogModel::where('workerid', 'in', $worker_ids)->delete();
            ComjobsReportModel::where('workerid', 'in', $worker_ids)->delete();
            ResumeInviteModel::where('workerid', 'in', $worker_ids)->delete();

            //门店
            $worker_agent_ids = AgentModel::where('workerid', 'in', $worker_ids)->column('id');
            if (!empty($worker_agent_ids)) {
                $worker_agent_broker_ids = BrokerModel::where('agentid', 'in', $worker_agent_ids)->column('id');
                if (!empty($worker_agent_broker_ids)) {
                    $del_broker_ids = array_merge($del_broker_ids, $worker_agent_broker_ids);
                }
                AgentModel::where('id', 'in', $worker_agent_ids)->delete();
                ComjobsReportModel::where('agentid', 'in', $worker_agent_ids)->delete();
            }

            //公司
            WorkerFormModel::where('workerid', 'in', $worker_ids)->delete();
            WorkerLogModel::where('workerid', 'in', $worker_ids)->delete();
            WorkerModel::where('userid', 'in', $worker_ids)->update(['brokerid' => 0]);
        }

        //门店
        WorkerModel::where('userid', 'in', $ids)->update(['userid' => 0]);

        //经纪人
        $broker_ids = BrokerModel::where('userid', 'in', $ids)->column('id');
        if (!empty($broker_ids)) {
            $del_broker_ids = array_merge($del_broker_ids, $broker_ids);
        }

        //删除登录权限
        UserAuthsModel::where('userid', 'in', $ids)->delete();

        //订单
        DemandModel::where('workerid', 'in', $ids)->delete();
        DemandLogModel::where('workerid', 'in', $ids)->delete();
        DemandLogModel::where('gworkerid', 'in', $ids)->delete();

        //供人
        SupplyModel::where('workerid', 'in', $ids)->delete();

        //删除经纪人
        if (!empty($del_broker_ids)) {
            BrokerModel::where('brokerid', 'in', $del_broker_ids)->delete();
            UserModel::where('brokerid', 'in', $del_broker_ids)->update(['brokerid' => 0]);
        }
    }

    /**
     * 自定义订阅列表
     */
    public function subscribe(Event $event)
    {
        $event->listen('userDel', [$this, 'onUserDel']);
    }
}
