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

namespace App\Repositories\Jobfair;


use App\Models\Jobfair\JobfairPutJob;
use function foo\func;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;

class JobfairPutJobRepository extends BaseRepository
{

    public function model()
    {
        return JobfairPutJob::class;
    }

    public function createData($data)
    {
        return $this->model->create($data);
    }

    public function findDone($where)
    {
        return $this->model->where($where)->first();
    }

    public function searchJob($jobfair_id, $keyword, $company_id, $id)
    {
        return $this->model->whereHas('jobs',function ($query) use ($company_id,$keyword){
            $query->where('audit',1)->where('company_id',$company_id)->where(function ($query) use ($keyword){
                $query->where('jobs_name', 'like', '%'.$keyword.'%')->orWhere('company_name', 'like', '%'.$keyword.'%');
            });
        })->where(['jobfair_id'=> $jobfair_id])->where('exid', $id)->get();
    }

    public function getOne($where)
    {
        return $this->model->with(['jobfairs','jobs'])->where($where)->first();
    }

    public function getJobsCount($jobfair_id)
    {
        return $this->model->whereHas('jobs',function ($query){
            $query->where('audit',1)->where('display',1);
        })->whereHas('jobfair_company',function($query){
           $query->where('audit',1);
        })->where(['jobfair_id'=>$jobfair_id])->count();
    }
    public function getNeedPerson($jobfair_id)
    {
        return $this->model->joinJobfairJob()
            ->where(['jobfair_jobs.audit'=>1,'jobfair_put_jobs.jobfair_id' => $jobfair_id])
            ->whereHas('jobfair_company',function($query){
                $query->where('audit',1);
            })
            ->selectRaw('sum(jobfair_jobs.amount) as sum')
            ->first();
    }

    public function getJobfairJob($exid)
    {
        return $this->model->with('jobs')->whereHas('jobs',function ($query){
            $query->where(['audit'=>1,'display'=>1]);
        })->where('exid', $exid)->get();
    }

    public function getExidJobfairJob($exid)
    {
        return $this->model->with('jobs')->whereHas('jobs',function ($query){
            $query->where(['audit'=>1]);
        })->where('exid', $exid)->get();
    }

    public function getJobfairCompanyJob($where1,$where,$limit = '')
    {
        if($limit){
            return $this->model->with(['jobfair_company','jobs'])->whereHas('jobs',function ($query) use ($where){
                $query->where($where);
            })->where($where1)->orderBy('updated_at','desc')->paginate($limit);
        }else{
            return $this->model->with(['jobfair_company','jobs'])->whereHas('jobs',function ($query) use ($where){
                $query->where($where);
            })->where($where1)->orderBy('updated_at','desc')->get();
        }
    }

    public function getJobfairCompanyJobPage($where,$where1,$page){
        return $this->model->with(['jobfair_company','jobs'])->has('jobfair_company')->whereHas('jobs',function ($query) use ($where1){
            $query->where($where1);
        })->where($where)->orderBy('position_id')->paginate($page);
    }

    public function aioJobfairJob($where, $orwhere, $limit)
    {
        return $this->model->whereHas('jobfairs', function ($query) {
            $query->where([
                    ['holddate_start', '<', strtotime("+60 minute")],
                    ['holddate_end', '>', time()],
                    ['subsite_id',get_subsite_id()]
                ])
                ->where(function ($query){
                    $query->where('showendtime', '>=', time())->orWhere('showendtime', 0);
                });
        })->whereHas('jobs',function ($query) use ($where,$orwhere) {
            $query->whereHas('company')->where($where);
            if ($orwhere) {
                $query->where($orwhere[0][0], $orwhere[0][1], $orwhere[0][2])->orWhere($orwhere[1][0], $orwhere[1][1], $orwhere[1][2]);
            }
        })->whereHas('jobfair_company',function ($query) {
            $query->where('audit',1);
        })->paginate($limit);
    }

    public function getAudit($id){
        return $this->model->with(['jobs'=>function($query){
            $query->select('id','audit');
        }])->find($id);
    }

    public function editJob($id)
    {
        return $this->model->with('jobs')->where('id', $id)->first();
    }

    /**
     * 获取单条职位(在招的)
     * @param $data 条件
     * @return bool返回值 如果有返回职位数组,否则返回 false
     */
    public function getAuditJobsOne($data)
    {
        $val = $this->model->where($data)->first();
        if (!$val) {
            return false;
        }
//        $val->contact= JobfairPutJobsContact::where('pid', $val->id)->first();
        $val->deadline_days=($val->deadline-time())>0?"距到期时间还有<strong style=\"color:#FF0000\">".sub_day($val->deadline, time())."</strong>":"<span style=\"color:#FF6600\">目前已过期</span>";
        return $val;
    }

    //招聘会参展职位
    public function jobfairCompanyJob($where,$map1, $limit)
    {
        return $this->model->with(['jobfair_company','jobfairs','jobs'])->whereHas('jobs',function ($query) use ($map1){
            $query->where($map1)->orderByRaw("Field(jobfair_jobs.audit,2,1,3)");
        })->where($where)->orderByDesc('created_at')->paginate($limit);
    }

    public function delJobs($id)
    {
        return $this->model->whereIn('id', $id)->delete();
    }

    public function delJobIds($id)
    {
        return $this->model->whereIn('job_id', $id)->delete();
    }

    public function delWhereJobs($jobs_id,$where)
    {
        return $this->model->whereIn('job_id', $jobs_id)->where($where)->delete();
    }

    public function jobedit($id)
    {
        return $this->model->where('id', $id)->first();
    }
    public function getStatisticsJobCount($where, $whereIn)
    {
        $res = $this->model->where($where);
        if ($whereIn) {
            foreach ($whereIn as $k => $v) {
                $res->whereIn($k, $v);
            }
        }
        return $res->count();
    }
    public function getStatisticsJobAmount($where, $whereIn)
    {
        $res = $this->model->where($where);
        if ($whereIn) {
            foreach ($whereIn as $k => $v) {
                $res->whereIn($k, $v);
            }
        }
        return $res->sum('amount');
    }
    public function getTopClassRand($where, $whereIn, $fields, $group_by, $order_by, $limit)
    {
        $res = $this->model->selectRaw($fields)->where($where);
        if ($whereIn) {
            foreach ($whereIn as $k => $v) {
                $res->whereIn($k, $v);
            }
        }
        if ($group_by) {
            foreach ($group_by as $k => $v) {
                $res->groupBy($v);
            }
        }
        if ($order_by) {
            $res->orderByRaw($order_by);
        }
        if ($limit) {
            $res->offset(0)->limit($limit);
        }
        return $res->get();
    }

    public function getOpenPutJobs($where){
        return $this->model->whereHas('jobfairs',function ($query){
            $query->where('holddate_end','>=',time());
        })->where($where)->get();
    }

    public function getFairJobIds($where)
    {
        return $this->model->where($where)->pluck('job_id')->toArray();
    }

    public function getFairJobInIds($ids)
    {
        return $this->model->whereIn('id',$ids)->pluck('job_id')->toArray();
    }

    public function getPluck($where,$column)
    {
        return $this->model->where($where)->pluck($column);
    }

    public function getWhere($jobs_where,$put_jobs_where)
    {
        return $this->model->whereHas('jobs',function ($query) use($jobs_where){
            $query->where($jobs_where);
        })->where($put_jobs_where)->groupBy('job_id')->get();
    }

    public function isSync($jobs_where,$put_jobs_where)
    {
        return $this->model->whereHas('jobs',function ($query) use($jobs_where){
            $query->where($jobs_where);
        })->where($put_jobs_where)->groupBy('job_id')->first();
    }

    public function getJobIds($where)
    {
        return $this->model->whereHas('jobs',function ($query){
            $query->where('audit',1);
        })->where($where)->pluck('job_id')->toArray();
    }

    public function destroyArr($del_array)
    {
        return $this->model->destroy($del_array);
    }

    public function delList($where,$ids)
    {
        return $this->model->where($where)->whereIn('job_id',$ids)->delete();
    }
}