<?php
/**
 * Created by PhpStorm.
 * User: wuzhenke
 * Date: 2019/1/17
 * Time: 16:19
 */

namespace App\Http\Controllers\Jkq\Jobfair;

use App\Exceptions\ResponseException;
use App\Http\Controllers\Jkq\JkqBaseController;
use App\Models\Subsite;
use App\Repositories\Jobfair\JobfairCompanyRepository;
use App\Repositories\Jobfair\JobfairFloorPlanRepository;
use App\Repositories\Jobfair\JobfairFloorplanStandRepository;
use App\Repositories\Jobfair\JobfairPersonalJobsApplyRepository;
use App\Repositories\Jobfair\JobfairPutJobRepository;
use App\Repositories\Jobfair\JobfairRepository;
use App\Repositories\Jobfair\JobfairJobRepository;
use App\Repositories\ResumeRepository;
use App\Services\Jobfair\JobfairCompanyService;
use App\Services\Jobfair\JobfairJobsService;
use App\Services\Jobfair\JobfairPersonalJopApplyService;
use App\Services\Jobfair\JobfairPutJobService;
use App\Services\Jobfair\JobfairService;
use App\Services\Person\ResumeService;
use App\Services\Company\CompanyService;
use App\Services\Company\CompanyDownResumeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Services\Content\ArticleService;
use App\Repositories\ArticleRepository;
class JobfairController extends JkqBaseController
{
    protected $jobfairJobsService;
    protected $jobfairCompanyRepository;
    protected $jobfairCompanyService;
    protected $jobfairService;
    protected $jobfairRepository;
    protected $jobfairPersonalJobsApplyRepository;
    protected $jobfairPersonalJopApplyService;
    protected $resumeService;
    protected $jobfairFloorplanStandRepository;
    protected $jobfairFloorPlanRepository;
    protected $jobfairPutJobRepository;
    protected $resumeRepository;
    protected $jobfairPutJobService;
    protected $companyService;
    protected $jobfairJobRepository;
    protected $companyDownResumeService;
    protected $articleService;
    protected $articleRepository;

    /*
     * 企业中心
     * */
    /**
     * JobfairController constructor.
     * @param $jobfairJobsService
     * @param $jobfairCompanyRepository
     * @param $jobfairCompanyService
     * @param $jobfairService
     * @param $jobfairRepository
     * @param $jobfairPersonalJobsApplyRepository
     * @param $jobfairPersonalJopApplyService
     * @param $resumeService
     * @param $jobfairFloorplanStandRepository
     * @param $jobfairFloorPlanRepository
     * @param $jobfairPutJobRepository
     * @param $resumeRepository
     * @param $jobfairPutJobService
     * @param $companyService
     * @param $jobfairJobRepository
     * @param $companyDownResumeService
     * @param $articleService
     * @param $articleRepository
     */
    public function __construct(
        ResumeService $resumeService,
        JobfairPutJobService $jobfairPutJobService,
        ResumeRepository $resumeRepository,
        JobfairPutJobRepository $jobfairPutJobRepository,
        JobfairJobsService $jobfairJobsService,
        JobfairFloorPlanRepository $jobfairFloorPlanRepository,
        JobfairFloorplanStandRepository $jobfairFloorplanStandRepository,
        JobfairPersonalJopApplyService $jobfairPersonalJopApplyService,
        JobfairPersonalJobsApplyRepository $jobfairPersonalJobsApplyRepository,
        JobfairRepository $jobfairRepository,
        JobfairService $jobfairService,
        JobfairCompanyService $jobfairCompanyService,
        JobfairCompanyRepository $jobfairCompanyRepository,
        CompanyService $companyService,
        JobfairJobRepository $jobfairJobRepository,
        CompanyDownResumeService $companyDownResumeService,
        ArticleRepository $articleRepository,
        ArticleService $articleService
    )
    {
        $this->jobfairJobsService = $jobfairJobsService;
        $this->jobfairCompanyRepository = $jobfairCompanyRepository;
        $this->jobfairCompanyService = $jobfairCompanyService;
        $this->jobfairService = $jobfairService;
        $this->jobfairRepository = $jobfairRepository;
        $this->jobfairPersonalJobsApplyRepository = $jobfairPersonalJobsApplyRepository;
        $this->jobfairPersonalJopApplyService = $jobfairPersonalJopApplyService;
        $this->resumeService = $resumeService;
        $this->jobfairFloorplanStandRepository = $jobfairFloorplanStandRepository;
        $this->jobfairFloorPlanRepository = $jobfairFloorPlanRepository;
        $this->jobfairPutJobRepository = $jobfairPutJobRepository;
        $this->resumeRepository = $resumeRepository;
        $this->jobfairPutJobService = $jobfairPutJobService;
        $this->companyService = $companyService;
        $this->jobfairJobRepository = $jobfairJobRepository;
        $this->companyDownResumeService = $companyDownResumeService;
        $this->articleService = $articleService;
        $this->articleRepository = $articleRepository;
    }

    /**近期招聘会
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairIndex()
    {
        $user = auth('web-company')->user();
        $res = $this->jobfairService->jobfairIndex($user);
        return view('jkq.jobfair.company.index', $res);
    }

    /**定展记录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairEnact()
    {
        $user = auth('web-company')->user();
        $res = $this->jobfairCompanyService->jobfairEnact($user);
        return view('jkq.jobfair.company.jobfair_enact', $res);
    }

    /**招聘会职位库管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairJobsList()
    {
        $res = $this->jobfairJobsService->jobfairJobsList();
        return view('jkq.jobfair.company.jobfair_joblist', $res);
    }

    /**招聘会职位添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairJobsAdd()
    {
        $res = $this->jobfairJobsService->jobfairJobsAdd();
        $res['title'] = '发布招聘会职位';
        return view('jkq.jobfair.company.jobfair_job_add', $res);
    }

    /**招聘会职位修改
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairJobsEdit($id)
    {
        $user = auth('web-company')->user();
        $res = $this->jobfairJobsService->jobfairJobsEdit($id, $user);
        if (!$res) {
            return $this->showMessage('参数错误', route('jobfair.quarterslist'), true, '上一页');
        }
        $res['title'] = '编辑招聘会职位';
        return view('jkq.jobfair.company.jobfair_job_add', $res);
    }

    /**招聘会职位删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function jobfairJobsDel(Request $request)
    {
        if ($request->method() == 'POST') {
            return  $this->jobfairJobsService->jobfairJobsDel($request);
        }
        $tip = "被删除后将无法恢复,您确定要删除选中的职位吗?";
        return response()->json(['status'=>1,'data'=>['html'=>view('app.company.ajax.ajax_warning', ['tip'=>$tip])->render()]]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function jobfairJobsDis(Request $request)
    {
        if ($request->method() == 'POST') {
            return  $this->jobfairJobsService->jobfairJobsDis($request);
        }
        if($request->display ==1 ){
            $tip = "您需要关闭职位吗?";
        }else{
            $tip = "您需要开启职位吗?";
        }
        return response()->json(['status'=>1,'data'=>['html'=>view('jkq.company.ajax.ajax_warning', ['tip'=>$tip])->render()]]);
    }

    /**招聘会职位修改和添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function jobfairJobsSave(Request $request)
    {
        $user = auth('web-company')->user();
        $data = $request->except(['_method', '_token']);
        return $this->jobfairJobsService->jobsSave($data, $user);
    }

    /**职位添加到招聘会中
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function jobfairJobsToJobfair(Request $request)
    {
        $user = auth('web-company')->user();
        if ($request->method() == 'POST') {
            $jids = $request->y_id;
            if (!$jids || !$request->exid_list) {
                return $this->showMessage('请选择职位或加入的招聘会', route('jkq.jobfair.quarterslist'), true, '上一页');
            }
            $this->jobfairJobsService->isOwn($jids,$user);
            $exidArr = explode(',', $request->exid_list);
            $this->jobfairCompanyService->isOwnArr($exidArr,$user);
            $res = $this->jobfairJobsService->jobfairJobsToJobfair($request);
            if ($res['state']) {
                return $this->showMessage('添加成功!'.$res['error'], route('jkq.jobfair.quarterslist'), false, '上一页');
            }
            return $this->showMessage($res['msg'], route('jkq.jobfair.quarterslist'), true, '上一页');
        }
        //公司参加的招聘会
        $jobfairInfo = $this->jobfairCompanyRepository->getJobfair($user->id);
        if ($jobfairInfo->isEmpty()) {
            return response()->json(['status'=>0, 'msg'=>"您还没有参加招聘会或申请未通过审核,请点击右侧链接参加招聘会<a href='" .route('jkq.jobfair.lists'). "' style='color: #FF0000'>【点击参加】</a>"]);
        }
        return response()->json(['status'=>1,'data'=>['html'=>view('jkq.jobfair.company.ajax.ajax_jobfair_mod', ['jobfairInfo'=>$jobfairInfo])->render()]]);
    }

    /**参展职位
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairjob(Request $request)
    {
        $user = auth('web-company')->user();
        $jobfairid = $request->jobfairid?$request->jobfairid:'';
        $audit = $request->audit?$request->audit:'';
        $map = [];
        $map1['company_id'] = $user->id;
        if ($audit) {
            $map1['audit'] = $audit;
        }
        if ($jobfairid) {
            $map['jobfair_id'] = $jobfairid;
        }
        $param_array = array('jobfairid','audit');
        $params= array();
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array)) {
                    $params[$k] = $v;
                }
            }
        }
        $res = $this->jobfairPutJobService->jobfairPutJobsList($map,$map1);
        $jobfairArr = $this->jobfairCompanyRepository->findWhere(['company_id'=>$user->id], ['jobfair_id']);

        $jobfairList = [];
        if ($jobfairArr) {
            $list = $jobfairArr->toArray();
            $jobfairList = $this->jobfairRepository->findWhereIn('id', $list, ['id','title'])->toArray();
        }
        $res['jobfairid'] = $jobfairid;
        $res['audit'] = $audit;
        $res['params'] = $params;
        $res['jobfairList'] = array_column($jobfairList, 'title', 'id');
        $res['audtiArr'] = [1=>'审核通过',2=>'待审核',3=>'审核未通过'];
        return view('jkq.jobfair.company.jobfair_job', $res);
    }

    /*
     * 旧方法,废弃
     */
//    public function jobfairjobModify($id)
//    {
//        $user = auth('web-company')->user();
//        $res = $this->jobfairPutJobService->jobfairjobModify($id, $user);
//        return view('app.jobfair.company.job_jobfair_edit', $res);
//    }

    public function jobfairPutJobsSave(Request $request)
    {
        $data = $request->except(['_token', 'method']);
        $user = auth('web-company')->user();
        return $this->jobfairPutJobService->jobfairPutJobsSave($data, $user);
    }

    public function jobfairjobdel(Request $request)
    {
        $id = $request->y_id?$request->y_id:0;
        $ids = is_array($id)?$id:explode(',', $id);
        if ($request->method() == 'POST') {
            if (!$ids) {
                return response()->json(['status'=>0,'html'=>'参数错误']);
            }
            return $this->jobfairPutJobService->jobfairPutJobsDel($ids);
        }
        $tip = "被删除后将无法恢复,您确定要删除选中的职位吗?";
        return response()->json(['status'=>1,'data'=>['html'=>view('jkq.company.ajax.ajax_warning', ['tip'=>$tip])->render()]]);
    }

    /**收到的招聘会简历
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function downResume(Request $request)
    {
        $user = auth('web-company')->user();
        $jobs_id = $request->input('jobs_id','');
        $jobfair_id = $request->input('jobfair_id','');
        $where['type'] = 2;
        $where1 = [];
        $result['jobfairList'] = $this->jobfairCompanyService->jobfairList($user);
        $result['jobs_name'] = '全部职位';
        if($jobs_id)
        {
            $where1['job_id'] = $jobs_id;
            $result['jobs_name'] = $this->jobfairJobRepository->find($jobs_id,['jobs_name'])->jobs_name;
        }
        $result['jobfair_name'] = '全部招聘会';
        if($jobfair_id)
        {
            $where['jobfair_id'] = $jobfair_id;
            $result['jobfair_name'] = $this->jobfairRepository->find($jobfair_id,['title'])->title;
        }
        $result['apply'] = $this->jobfairPersonalJobsApplyRepository->getResumeJobs($user->id,$where,$where1);
        $whereJob = [
            'company_id' => $user->id,
            'audit' => 1,
            'type' => 1,
        ];
        $result['jobs'] = $this->jobfairJobRepository->getJobs($whereJob);
        $params= array();
        $param_array = array('jobs_id','jobfair_id');
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array)) {
                    $params[$k] = $v;
                }
            }
        }
        $result['params'] = $params;
        return  view('jkq.jobfair.company.down_resume', $result);
    }

    public function downResumeDel(Request $request)
    {
        if ($request->method() == 'POST') {
            $user = $this->getUser();
            return $this->jobfairPersonalJopApplyService->downResumeDel($request,$user);
        }
        $tip = "被删除后将无法恢复,您确定要删除选中的简历吗?";
        return response()->json(['status'=>1,'data'=>['html'=>view('app.company.ajax.ajax_warning', ['tip'=>$tip])->render()]]);
    }



    /*
     * 前台页面
     * */
    /**招聘会列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function index(Request $request)
    {
        $jobfairList = $this->jobfairService->index($request);
        if ($request->ajax()) {
            if ($jobfairList['allJobfair']->isNotEmpty()) {
                return response()->json(['status'=>1,'data'=>view('jkq.jobfair.ajax.ajax_jobfair_list', $jobfairList)->render()]);
            }
            return response()->json(['status'=>0]);
        }
        return view('jkq.jobfair.index', $jobfairList);
    }

    /*招聘会资讯
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function news()
    {
        $news = $this->articleRepository->getArticle(['keywords'=>'jobfair_news'],5);
        $other = $this->articleRepository->getArticle(['keywords'=>'jobfair_review'],5);
        return view('jkq.jobfair.jobfair_newslist',['type'=>'new','news'=>$news,'other'=>$other]);
    }

    /*招聘会回顾
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function review()
    {
        $news = $this->articleRepository->getArticle(['keywords'=>'jobfair_review'],5);
        $other = $this->articleRepository->getArticle(['keywords'=>'jobfair_news'],5);
        return view('jkq.jobfair.jobfair_newslist',['type'=>'review','news'=>$news,'other'=>$other]);
    }

    /*招聘会资讯、回顾详情
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newShow($id)
    {
        if (!$id) {
            return $this->showMessage('参数错误!', route('jobfair'), true, '上一页');
        }
        $this->articleRepository->incrementData(array('id'=>$id), 1, 'click');
        $new = $this->articleService->getArticleInfo($id,1);
        $news = $this->articleRepository->getArticle(['keywords'=>'jobfair_news'],5);
        $reviews = $this->articleRepository->getArticle(['keywords'=>'jobfair_review'],5);
        return view('jkq.jobfair.jobfair_newshow',['new'=>$new,'news'=>$news,'reviews'=>$reviews]);
    }

    /**招聘会详情(废弃)
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
//    public function jobfairShow($id = 0)
//    {
//        if (!$id) {
//            return $this->showMessage('参数错误!', route('jobfair'), true, '上一页');
//        }
//        $res = $this->jobfairService->jobfairShow($id);
//
//        if (!$res['status']) {
//            return $this->showMessage($res['msg'], route('jobfair'), true, '上一页');
//        } else {
//            $res['jobfair']->sitename = Subsite::where('id',$res['jobfair']->subsite_id)->value('sitename');
//            if (array_has($res, 'jobfair')) {
//                $this->putSeoData('jobfair', $res['jobfair']);
//            }
//            return view('app.jobfair.jobfair_show', $res);
//        }
//    }

    /**招聘会企业
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function jobfairShowCom(Request $request, $id = 0)
    {
        if (!$id) {
            return $this->showMessage('参数错误!', route('jobfair'), true, '上一页');
        }
        $res = $this->jobfairService->jobfairShowCom($request, $id);
        if ($request->ajax()) {
            if ($res['jobfairCompany']->isNotEmpty()) {
                return response()->json(['status'=>1,'data'=>view('jkq.jobfair.ajax.ajax_jobfair_com', $res)->render()]);
            }
            return response()->json(['status'=>0]);
        }

        if (!$res['status']) {
            return $this->showMessage($res['msg'], route('jkq.jobfair'), true, '上一页');
        } else {
            $res['jobfair']->sitename = Subsite::where('id',$res['jobfair']->subsite_id)->value('sitename');
            $res['reserve'] = $request->reserve ? $request->reserve : '';
            $this->putSeoData('jobfair', $res['jobfair']);
            return view('jkq.jobfair.jobfair_com', $res);
        }
    }

    /**招聘会职位详情
     * @param $jobfair_id
     * @param $job_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function jobfairShowJob($id)
    {
        $job = $this->jobfairService->jobfairShowJob($id);
        if (!$job['status']) {
            return $this->showMessage($job['msg'], route('jkq.jobfair'), true, '上一页');
        } else {
            $this->putSeoData('jobfair_job', $job['jobInfo']);
            return view('jkq.jobfair.jobfair_job', $job);
        }
    }


    public function jobfairAppointmentJobs(Request $request)
    {
        $user = $this->getLoginUser();
        $jid = $request->jobsfair_job_id;
        $jobfair_id = $request->jobfairid;
        $position_id = $request->position_id;
        $company_id = $request->company_id;
        $resume_id = $request->resume_id?$request->resume_id:0;
        $jobfair = $this->jobfairRepository->find($jobfair_id);
//        if ($jobfair->isEmpty || time() > $jobfair->holddate_end) {
//            return response()->json(['status'=>0, 'html'=>"招聘会已停止"]);
//        }
//        if ($jobfair->isEmpty || time() > $jobfair->holddate_start) {
//            return response()->json(['status'=>0, 'html'=>"招聘会已开始,请去现场投递"]);
//        }
        if ($resume_id) {
            $this->resumeService->isOwn($resume_id,$user);
            $this->jobfairPutJobService->isOwn($jid,$company_id);
            $this->jobfairCompanyService->isPositionOwn($position_id,$jobfair_id,$company_id,$jid);
            $data['resume_id'] = $resume_id;
            $data['personal_uid'] = $user->id;
            $data['jobs_id'] = $jid;
            $data['company_id'] = $company_id;
            $data['jobfair_id'] = $jobfair_id;
            $data['position_id'] = $position_id;
            if (!$jid || !$company_id || !$jobfair_id || !$position_id) {
                return response()->json(['status'=>0,'type'=>3,'html'=>'参数错误!']);
            }
            //简历完整度
            $res_complete = $this->resumeRepository->find($resume_id, ['complete_percent']);
            if ($res_complete->complete_percent < config('aix.personal_set.per_set.per_set.apply_job_min_percent')) {
                return response()->json(['status'=>0,'html'=>'请完善简历后再预约职位','resume_id'=>$resume_id]);
            }
            if ($this->jobfairPersonalJobsApplyRepository->findWhere(['personal_uid'=>$user->id, 'jobs_id'=>$jid, 'company_id'=>$company_id, 'jobfair_id'=>$jobfair_id, 'position_id'=>$position_id])->isNotEmpty()) {
                return response()->json(['status'=>0, 'html'=>"您已预约该职位,请勿重复预约!"]);
            }
            DB::beginTransaction();//检查数据库事务
            try {
                $this->jobfairPersonalJopApplyService->applyJob($data);
                $this->companyDownResumeService->addDownResumes($company_id,$resume_id);
                DB::commit();
                return response()->json(['status'=>1, 'html'=>"职位预约成功!"]);
            } catch (\Exception $e) {
                DB::rollback();
                return response()->json(['status'=>0,'type'=>0 , 'html'=>"职位预约失败!"]);
            }
        } else {
            if (!$jid || !$company_id || !$jobfair_id || !$position_id) {
                return response()->json(['status'=>0,'type'=>3,'html'=>'参数错误!']);
            }
            $result = $this->jobfairPersonalJopApplyService->ifJobApply($user);
            if ($result['status'] == 2) {  //返回结果,让用户选择简历投递
                $page_data = array('status'=>array_get($result, 'status'),'resumes'=>array_get($result, 'resumes'),'def_resume'=>array_get($result, 'def_resume'),'jobs_id'=>$jid);
                $page_data['company_id'] = $company_id;
                $page_data['jobfair_id'] = $jobfair_id;
                $page_data['position_id'] = $position_id;
                $page_data['jobs_id'] = $jid;
                $html = view('jkq.jobfair.ajax.ajax_apply', $page_data)->render();
                return response()->json(['status'=>2,'html'=>$html]);
            } elseif ($result['status'] == 3) { //直接投递
                $this->jobfairPutJobService->isOwn($jid,$company_id);
                $this->jobfairCompanyService->isPositionOwn($position_id,$jobfair_id,$company_id,$jid);
                $resume = $result['resumes'][0];
                $data['resume_id'] = $resume->id;
                $data['personal_uid'] = $user->id;
                $data['jobs_id'] = $jid;
                $data['company_id'] = $company_id;
                $data['jobfair_id'] = $jobfair_id;
                $data['position_id'] = $position_id;
                $res_complete = $this->resumeRepository->find($resume->id, ['complete_percent']);
                if ($res_complete->complete_percent < config('aix.personal_set.per_set.per_set.apply_job_min_percent')) {
                    return response()->json(['status'=>0,'html'=>'请完善简历后再预约职位','resume_id'=>$resume->id]);
                }
                if ($this->jobfairPersonalJobsApplyRepository->findWhere(['resume_id'=>$resume->id, 'jobs_id'=>$jid, 'company_id'=>$company_id, 'jobfair_id'=>$jobfair_id, 'position_id'=>$position_id,'type'=>1])->isNotEmpty()) {
                    return response()->json(['status'=>0, 'html'=>"您已预约该职位,请勿重复预约!"]);
                }
                DB::beginTransaction();//检查数据库事务
                try {
                    $this->jobfairPersonalJopApplyService->applyJob($data);
                    $this->companyDownResumeService->addDownResumes($company_id,$resume->id);
                    DB::commit();
                    return response()->json(['status'=>1, 'html'=>"职位预约成功!"]);
                } catch (\Exception $e) {
                    DB::rollback();
                    return response()->json(['status'=>0,'type'=>0 , 'html'=>"职位预约失败!"]);
                }
            }

        }
    }

    /**异步查询职位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function jobfairAjaxJobs(Request $request)
    {
        $keyinput = $request->keyinput?$request->keyinput:'';
        $jobfair_id = $request->jobfair_id?$request->jobfair_id:0;
        if (!$jobfair_id) {
            return response()->json(['status'=>0,'msg'=>"招聘会ID不正确"]);
        }
        if($keyinput) {
            $where = [
                'jobfair_id'=>$jobfair_id,
                'audit'=>1
            ];
            $res = $this->jobfairCompanyRepository->findJob($where);
            foreach ($res as $key => $val) {
                $jobfairPutJob = $this->jobfairPutJobRepository->searchJob($jobfair_id, $keyinput, $val->company_id, $val->id);
                if ($jobfairPutJob->isNotEmpty()) {
                    $val->jobfairPutJob = $jobfairPutJob;
                } else {
                    unset($res[$key]);
                }
            }
        } else {
            $where = ['jobfair_companys.jobfair_id'=>$jobfair_id,'jobfair_companys.audit'=>1];
            $offset = isset($request->page)?$request->page:0;
            $limit = 20;
            $res = $this->jobfairCompanyRepository->findCompany($where, $offset, $limit);
            foreach ($res as $key => $val) {
                $val->jobfairPutJob = $this->jobfairPutJobRepository->getJobfairJob($val->id, $jobfair_id);
            }
        }
        if ($res->isEmpty()) {
            return response()->json(['status'=>0,'msg'=>"未查找到与【{$keyinput}】相似的职位或企业"]);
        }
        return response()->json(['status'=>1,'data'=>view('jkq.jobfair.ajax.ajax_jobfair_com_jobs', ['jobfairCompany'=>$res])->render()]);
    }

    /**在线预定(废弃)
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
//    public function jobfairAppointment($id)
//    {
//        if (!$id) {
//            return $this->showMessage('参数错误!', route('jobfair'), true, '上一页');
//        }
//        $res = $this->jobfairService->jobfairAppointment($id);
//        if (!$res['status']) {
//            return $this->showMessage($res['msg'], route('jobfair'), true, '上一页');
//        } else {
//            $res['jobfair']->sitename = Subsite::where('id',$res['jobfair']->subsite_id)->value('sitename');
//            $this->putSeoData('jobfair', $res['jobfair']);
//            return view('app.jobfair.jobfair_appointment', $res);
//        }
//    }


    public function personJobfair()
    {
        $resume = $this->resumeService->myResumeAjax(auth('web-member')->user());
        $jobApply = $this->jobfairPersonalJopApplyService->personJobfair();
        return view('jkq.jobfair.person.job_apply', ['resume' => $resume, 'interview' => $jobApply['interview']]);
    }

    public function personDelect()
    {
        if (request()->method()=='GET') {
            $html = view('jkq.person.ajax.resume_delete', ['tpis'=>'删除后将无法恢复,您确定要删除选择的预约职位吗?'])->render();
            return $this->sendSuccessResponse($html);
        } else {
            $user = auth('web-member')->user();
            $this->jobfairPersonalJopApplyService->isOwn(request()->id,$user);
            $res = $this->jobfairPersonalJopApplyService->personDelect(request()->id, auth('web-member')->user());
            if ($res) {
                return $this->sendSuccessResponse('删除成功!');
            } else {
                return $this->sendErrorResponse('删除失败!');
            }
        }
    }


    public function jobfairAppointmentSave(Request $request)
    {
        $user = $this->getLoginUser();
        if (!$user) {
            $jobfair_id = $request->jobfair_id?$request->jobfair_id:0;
            return response()->json(['status'=>0, 'type'=>1, 'html'=>view('jkq.common.ajax.com_login', ['redirect_url'=>route('jkq.jobfair.show.com', ['id'=>$jobfair_id])])->render(),'msg'=>'您还没有登录,请先登录再预约招聘会']);
        }
        if ($user->utype == 2) {
            return response()->json(['status'=>0,'type'=>0,'msg'=>'个人会员不允许预定招聘会!']);
        }
        //企业是否通过认证
        if ($user->audit!=1) {
            return response()->json(['status'=>0,'type'=>0,'msg'=>'企业或营业执照未通过审核!']);
        }
        $jobfair_id = $request->jobfair_id?$request->jobfair_id:0;
        $position_id = $request->position_id?$request->position_id:0;
        $floorplan_id = $request->floorplan_id?$request->floorplan_id:0;
        if (!$jobfair_id || !$jobfairInfo = $this->jobfairRepository->findOne(['id'=>$jobfair_id])) {
            return response()->json(['status'=>0,'msg'=>'查无此招聘会!']);
        }

        if (!$floorplan_id || !$floorplanInfo = $this->jobfairFloorPlanRepository->getOne($floorplan_id)) {
            return response()->json(['status'=>0,'msg'=>'查无此招聘会展位图!']);
        }
        if (!$position_id || !$positionInfo =$this->jobfairFloorplanStandRepository->getOne(['id'=>$position_id])) {
            return response()->json(['status'=>0,'msg'=>'查无此招聘会展位号!']);
        }
        if ($positionInfo->type == 2) {
            return response()->json(['status'=>0,'msg'=>'预留展位不可预定!']);
        }
        if ($jobfairInfo->more_stand == 0 && $this->jobfairCompanyRepository->findCom(['jobfair_id'=>$jobfair_id, 'company_id'=>$user->id,['audit','<>', 3]])) {
            return response()->json(['status'=>0,'msg'=>'本场招聘会不允许预定多个展位!']);
        }
        if($this->jobfairCompanyRepository->findCom(['jobfair_id'=>$jobfair_id, 'position_id'=>$position_id,['audit','<>', 3]])){
            return response()->json(['status'=>0,'msg'=>'该展位已被抢占,请重新刷新页面再预定']);
        }
        if ($request->method()=='POST') {
            return $this->jobfairService->jobfairAppointmentSave($jobfairInfo, $positionInfo, $user);
        }
        if ($jobfairInfo->is_commonweal == 3) {
            $msg = "本次招聘会为公益招聘会.<br><span style='color: red;'>请勿迟到,早退,违约不到场.</span><br>招聘会开始30分钟之后到场签到视为迟到,1小时之后还未到场视为违约,招聘会结束之前30分钟退出系统视为早退.<br>迟到早退三次,违约不到场一次,之后一个月内不能再次预定招聘会.<br>";
        } else {
            $msg ="<br><span style='color: red;'>请勿迟到,早退,违约不到场.</span><br>招聘会开始30分钟之后到场签到视为迟到,1小时之后还未到场视为违约,招聘会结束之前30分钟退出系统视为早退.<br>迟到早退三次,违约不到场一次,之后一个月内不能再次预定招聘会.<br />预定本场招聘会将消耗企业套餐招聘会<span style='color:#FC4C4E;'>".$jobfairInfo->jobsfair_num."次</span>或者企业<span style='color:#FC4C4E;'>积分".$jobfairInfo->predetermined_point."</span><br />";
        }
        return response()->json(['status'=>1,'msg'=>$msg]);
    }

    public function companyShow(Request $request)
    {
        //获取企业信息
        $jobfair_id = $request->input('jobfair_id');
        $company_id = $request->input('company_id');
        $is_job = $request->input('is_job','');
        $company_info = $this->companyService->getCompanyInfo(['id'=>$company_id]);
        $this->putSeoData('company', $company_info);
        //在招职位
        $put_jobs_where = array(
            array('company_id','=',$company_id),
            array('jobfair_id','=',$jobfair_id),
        );
        $jobs_where = array(
            array('audit','=',1),
            array('display','=',1)
        );
        $jobs = $this->jobfairPutJobRepository->getWhere($jobs_where,$put_jobs_where);
        $jobfair = $this->jobfairRepository->findOne(['id'=>$jobfair_id]);
        //看过该公司的人还看过(获取相同行业的企业)
        $company_where = array(
            'trade' => $company_info->trade,
            'except_id' =>$company_info->id
        );
        $other_companys = $this->companyService->getOtherCompanies($company_where, 5);
        $user = $this->getUser();
        if ($user && $user->utype==2) {
            //获取当前时间段内申请过的职位
            $space_time = (integer)config('aix.personal_set.per_set.per_set.apply_job_space');
            if ($space_time>0) {
                $stime = date('Y-m-d H:i:s', strtotime(date("Y-m-d", strtotime("-".$space_time." day"))));
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id),array('created_at','>=',$stime));
            } else {
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id));
            }
            $applys = $this->jobfairPersonalJopApplyService->getApplyJobs($apply_where);
        } else {
            $applys = array();
        }

        $return_data = array(
            'info'=>$company_info,
            'jobs'=>$jobs,
            'jobfair' => $jobfair,
            'applys' => $applys,
            'other_companys'=>$other_companys,
            'company_img' => $company_info->img,
        );
        if($is_job){
            return view('jkq.jobfair.company.company_jobs', $return_data);
        }else{
            return view('jkq.jobfair.company.company_show', $return_data);
        }
    }

    /**发布职位
     * return \Illuminate\Http\JsonResponse
     */
    public function ajaxComJobs(Request $request)
    {
        if ($request->method() == 'POST') {
            $user = $this->getLoginUser();
            $jobs_arr= $request->input('check_jobs_id', '');
            $jobfair_id = $request->input('jobfair_id', '');
            $exid = $request->input('exid', '');
            $position_id = $request->input('position_id', '');
            $jobs = $this->jobfairJobRepository->getPluck(['company_id'=>$user->id,'type'=>1,'audit'=>1],'id')->toArray();
            if(array_diff($jobs_arr,$jobs)) {
                throw new ResponseException('参数错误!');
            }
            $this->jobfairCompanyService->isOwn($exid,$user);
            //获取列表
            $where = ['exid'=>$exid];
            $ids = $this->jobfairPutJobRepository->getJobIds($where);
            DB::beginTransaction();
            try {
                if($ids){
                    if($jobs_arr){
                        $add_array = array_diff($jobs_arr,$ids);
                        if($add_array)
                        {
                            $this->jobfairPutJobService->addData($add_array,$exid,$jobfair_id,$position_id);
                        }
                        $del_array = array_diff($ids,$jobs_arr);
                        $this->jobfairPutJobRepository->destroyArr($del_array);
                    }else{
                        $where = [
                            'exid'=>$exid
                        ];
                        $this->jobfairPutJobRepository->delList($where,$ids);
                    }
                }else{
                    if($jobs_arr)
                    {
                        $this->jobfairPutJobService->addData($jobs_arr,$exid,$jobfair_id);
                    }else{
                        throw new \Exception('请选择职位添加');
                    }
                }
                DB::commit();
                $return_data = array('status'=>1, 'msg'=>'编辑成功');
                return response()->json($return_data);
            } catch (\Exception $e) {
                DB::rollback();
                $return_data = array('status'=>0, 'msg'=>$e->getMessage());
                return response()->json($return_data);
            }
        } else {
            $user = auth('web-company')->user();
            $jobfair_id = $request->input('jobfair_id', '');
            $exid = $request->input('exid', '');
            $position_id = $request->input('position_id', '');
            $jobs = $this->jobfairJobRepository->getJobs(['company_id'=>$user->id,'type'=>1,'audit'=>1]);
            if($jobs->isEmpty())
            {
                $return_data = array( 'status'=>2, 'msg' => '没有招聘会职位,请先添加职位');
                return response()->json($return_data);
            }else{
                $join_jobs = $this->jobfairPutJobRepository->getExidJobfairJob(['exid'=>$exid]);
                $html = view('jkq.jobfair.ajax.add_jobs', ['jobs'=>$jobs,'jobfair_id'=>$jobfair_id,'exid'=>$exid,'position_id'=>$position_id,'join_jobs'=>$join_jobs])->render();
                $return_data = array( 'status'=>1, 'data' => $html);
                return response()->json($return_data);
            }
        }
    }

    /**页面加载展位信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajaxGetData(Request $request)
    {
        $data = $request->all();
        return $this->jobfairService->ajaxGetData($data);
    }

    public function getUser()
    {
        $user = array();
        if (auth('web-member')->check()) {
            $user = auth('web-member')->user();
        } elseif (auth('web-company')->check()) {
            $user = auth('web-company')->user();
        }
        return $user;
    }
}