<?php

namespace App\Repositories;

use App\Models\CompanyDownResume;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;

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

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

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

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

    /**下载简历列表
     * @param $where
     * @param $map
     * @param $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function downResume($where, $map, $page)
    {
        return $this->model->with('resumes')->where($map)->where($where)->orderBy("is_reply", "asc")->orderBy('down_addtime', 'desc')->paginate($page, ['*']);
    }

    /**获取简历ID
     * @param $id
     * @return mixed
     */
    public function getResumeId($id)
    {
        return $this->model->whereIn('id', $id)->select(['resume_id'])->get()->toArray();
    }

    /**企业标记简历
     * @param $id
     * @param $data
     * @return mixed
     */
    public function companyLabelResume($id, $data)
    {
        return $this->model->where('id', $id)->update($data);
    }

    //获取已下载简历数量
    public function getDownResumeNums($where)
    {
        return $this->model->where($where)->count();
    }

    //获取简历下载信息集合
    public function getDowmResumes($where, $whereIn = array(), $arr_filed = 'resume_id')
    {
        $list = array();
        if ($whereIn) {
            reset($whereIn);
            $rst = $this->model->where($where)->whereIn(key($whereIn), $whereIn[key($whereIn)])->get();
        } else {
            $rst = $this->model->where($where)->get();
        }
        if ($rst->toArray()) {
            foreach ($rst as $k => $v) {
                $list[$v->$arr_filed] = $v;
            }
        }
        return $list;
    }
    public function getStateArr()
    {
        return $this->model->getStateArr();
    }
    public function updateData($where, $set_data)
    {
        return $this->model->where($where)->update($set_data);
    }
    public function getDownResumes($where = array(), $orderby = array(), $limit = '')
    {
        /*$rst = $this->model->with('resumes')->where($where);
        if ($orderby) {
            foreach ($orderby as $k => $v) {
                $rst->orderBy($k, $v);
            }
        }
        if ($limit) {
            $rst->limit($limit);
        }
        return $rst->get();*/
        $rst = $this->model->with('resumes')->whereHas('subsite_resume', function ($query) { $query->where('subsite_id', get_subsite_id());})->where($where);
        if ($orderby) {
            foreach ($orderby as $k => $v) {
                $rst->orderBy($k, $v);
            }
        }
        if ($limit) {
            $rst->limit($limit);
        }
        return $rst->get();
    }

    public function getResumeNumGroup($where, $group_by, $subsite_id)
    {
        return $this->model->where($where)->select(DB::raw('count(id),DATE(created_at)'))->when($subsite_id!=-1, function($query) use($subsite_id) {
            $query->whereHas('companys', function ($query) use($subsite_id) {
                $query->where(['subsite_id'=>$subsite_id]);
            });
        })->groupBy($group_by)->get();
    }

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