<?php

namespace App\Repositories;

use App\Models\CompanyInterView;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;

/**
 * Class MemberRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class CompanyInterviewRepository extends BaseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return CompanyInterView::class;
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 面试邀请数。
     * @param $resume_id
     * @param $resume_uid
     * @return mixed
     */
    public function getInterview($resume_uid, $where)
    {
        return $this->model->with(['resumes','jobs'=>function ($query) use ($where) {
            $query->whereIn('audit', $where)->where('display', 1)->where('valid', 1);
        }])->whereHas('resumes')->whereHas('jobs')->where(['resume_uid'=>$resume_uid,'personal_look'=>1])->count();
    }

    public function getInterviewDef($resume_id, $where)
    {
        return $this->model->with(['resumes','jobs'=>function ($query) use ($where) {
            $query->whereIn('audit', $where)->where('display', 1)->where('valid', 1);
        }])->whereHas('resumes')->whereHas('jobs')->where(['resume_id'=>$resume_id,'personal_look'=>1])->count();
    }


    public function getInterviewByUid($data, $where)
    {
        return $this->model->with(['resumes','jobs'=>function ($query) use ($where) {
            $query->whereIn('audit', $where)->where('display', 1)->where('valid', 1);
        }])->where($data)->whereHas('resumes')->whereHas('jobs')->orderByRaw('personal_look asc,interview_time desc')->paginate(10);
    }

    /**
     * 3天内的面试邀请数。
     */
    public function getInterviewCount($uid, $where)
    {
        return $this->model->with(['resumes','jobs'=>function ($query) use ($where) {
            $query->whereIn('audit', $where)->where('display', 1)->where('valid', 1);
        }])->whereHas('resumes')->whereHas('jobs')->where(['resume_uid'=>$uid])->where('created_at', '>=', date('Y-m-d H:i:s', strtotime("-3 day")))->count();
    }

    public function setInterview($id, $data)
    {
        return $this->model->whereIn('id', $id)->update($data);
    }

    public function delInterview($id,$company_id)
    {
        return $this->model->whereIn('id', $id)->where("company_id",$company_id)->delete();
    }

    public function getInterviewById($id)
    {
        return $this->model->find($id);
    }
    /**
     * 核查简历是否被邀请面试
     * @param $resumeId
     * @param $uid
     * @param $jobsId
     * @return bool
     */
    public function checkInterview($resumeId, $uid,$jobsId,$personal_jobs_id=0)
    {
        $where['resume_id'] = $resumeId;
        $where['company_id'] = $uid;
        $where['jobs_id'] = $jobsId;
        if (!empty($personal_jobs_id)){
            $where['personal_jobs_id']=$personal_jobs_id;
        }
        if (!$this->model->where($where)->first()) {
            return false;
        }
        return true;
    }


    /**
     * 企业中心======面试邀请记录
     * @param $page
     * @param $where
     * @param $valid 停招职位
     * @return mixed
     */
    public function interviewJobs($page, $where, $valid)
    {
        return $this->model->whereHas('jobs', function ($query) use ($valid) {
            if (!empty($valid)) {
                $query->whereIn('valid', $valid);
            }
        })->with('resumes')->where($where)->orderBy('interview_time', 'desc')->paginate($page, ['*']);
    }

    /**面试邀请详情
     * @param $where
     * @return mixed
     */
    public function interviewDetail($where)
    {
        return $this->model->where($where)->first();
    }

    public function InterviewUpdate(array $attributes, $id,$company_id)
    {
        return $this->model->where(['id'=>$id,'company_id'=>$company_id])->update($attributes);
    }

    /**邀请面试数
     * @param $where
     * @return mixed
     */
    public function interviewCount($where)
    {
        $valid = ['1'];
        return $this->model->whereHas('jobs', function ($query) use ($valid) {
                $query->whereIn('valid', $valid);
        })->with('jobs')->with('resumes')->where($where)->count();
    }
}