<?php

namespace App\Jobs\Cron;

use App\Services\Jobfair\JobfairService;
use App\Services\Jobfairout\JobfairoutService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Jobfair\Jobfair as JobF;
use App\Models\Jobfairout\Jobfairout;
use App\Models\Jobfair\JobfairCompany;
use App\Models\MembersPoint;
use Illuminate\Support\Facades\Log;
use App\Repositories\MemberHandselRepository;
class Jobfair implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const CREATE_LIST = 'create';

    const UPDATE_LIST = 'update';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(JobfairService $jobfairService,JobfairoutService $jobfairoutService,MemberHandselRepository $memberHandselRepository)
    {
        $id = $jobfairService->getJobfairOpen();
        if ($id) {
            $res = JobF::whereIn('id', $id)->update(['predetermined_status'=>2]);
            if (!$res) {
                Log::error('jobfairs failed to update.', ['date' =>$id]);
            }
        }
        foreach ($id as $value)
        {
            $is_commonweal = JobF::where('id',$value)->value('is_commonweal');
            if($is_commonweal != 3){
                $nosign_point = JobF::where('id',$value)->value('nosign_point');
                if($nosign_point){
                    $list = JobfairCompany::where(['type'=>0,'jobfair_id'=>$value])->get();
                    foreach ($list as $val)
                    {
                        $points = MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->value('points');
                        if($points > $nosign_point){
                            MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->decrement('points',$nosign_point);
                            $handsel['uid'] = $val->company_id;
                            $handsel['utype'] = 1;
                            $handsel['htype'] = 'jobfair_late';
                            $handsel['htype_cn'] = '招聘会未签到';
                            $handsel['operate'] = 2;
                            $handsel['points'] = $nosign_point;
                            $memberHandselRepository->addNew($handsel);
                        }else{
                            MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->update(['points'=>0]);
                        }
                    }
                }
            }
        }

        $id = $jobfairoutService->getJobfairOpen();
        if ($id) {
            $res = Jobfairout::whereIn('id', $id)->update(['predetermined_status'=>2]);
            if (!$res) {
                Log::error('jobfairout failed to update.', ['date' =>$id]);
            }
        }
        foreach ($id as $value)
        {
            $is_commonweal = Jobfairout::where('id',$value)->value('is_commonweal');
            if($is_commonweal != 3){
                $nosign_point = Jobfairout::where('id',$value)->value('nosign_point');
                if($nosign_point){
                    $list = JobfairCompany::where(['type'=>0,'jobfair_id'=>$value])->get();
                    foreach ($list as $val)
                    {
                        $points = MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->value('points');
                        if($points > $nosign_point){
                            MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->decrement('points',$nosign_point);
                            $handsel['uid'] = $val->company_id;
                            $handsel['utype'] = 1;
                            $handsel['htype'] = 'jobfair_late';
                            $handsel['htype_cn'] = '赴外招聘会未签到';
                            $handsel['operate'] = 2;
                            $handsel['points'] = $nosign_point;
                            $memberHandselRepository->addNew($handsel);
                        }else{
                            MembersPoint::where(['utype'=>1,'uid'=>$val->company_id])->update(['points'=>0]);
                        }
                    }
                }
            }
        }


    }
}