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

namespace App\Http\Controllers\Web\Active;

use App\Exceptions\ResponseException;
use App\Http\Controllers\Web\WebBaseController;
use App\Models\LotteryPrize;
use App\Models\LotteryWin;
use App\Services\Common\CategoryService;
use App\Services\Common\SearchService;
use App\Services\Company\JobsService;
use App\Services\HomeService;
use Illuminate\Http\Request;
use App\Services\Company\CompanyService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class JobfairController extends WebBaseController
{
    protected $companyService;
    protected $jobsService;
    protected $categoryService;
    protected $searchService;
    protected $homeService;

    /*
     * 企业中心
     * */
    /**
     * 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(
        CompanyService $companyService,
        JobsService $jobsService,
        CategoryService $categoryService,
        SearchService $searchService,
        HomeService $homeService
    )
    {
        $this->companyService  = $companyService;
        $this->jobsService     = $jobsService;
        $this->categoryService = $categoryService;
        $this->searchService   = $searchService;
        $this->homeService     = $homeService;
    }

    /**2020新春线上招聘会
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function online2020(Request $request)
    {
        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 20;

        $citycategory = $request->input('citycategory');
        $trade        = $request->input('trade');
        $param_array  = ['citycategory', 'trade', 'nature'];

        $params = [];
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array) && $v) {
                    $params[$k] = $v;
                }
            }
        }

        $subsites = Cache::get('subsites_list');
        if ($subsites) {
            if (!array_has($params, 'citycategory')) {
                if (get_subsite_id() > 0) {
                    $citycategory = $subsites[get_subsite_id()]['district'];
                }
            }
            $is_subsite = 1;
        }
        $citys = $this->categoryService->getCitys($citycategory);

        if (empty($citycategory)) {
            $district_str = "(district like ? or district like ? or district like ? or district like ?)";
            $district_arr = ["%.623%", "%623.%", "%.245%", "%245.%"];
        } else {
            $select_id    = $citys['select']['id'];
            $district_str = "(district like ? or district like ?)";
            $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];
        }

        $companys = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw("jobs.created_at > '2020-01-01 00:00:00'")->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

//        echo '<pre>';
//
//        var_dump($companys);die;

        $where = [];
        //$where[] = array('reg_time', '>', 1577808000);
        $where[] = ['user_status', '=', 1];
        $where[] = ['subsite_id', '=', get_subsite_id()];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];
        if (!empty($trade)) {
            $where[] = ['trade', '=', $trade];
        }
        //$where[] = array('id','in',$companys);
        $order_by = ['id', 'desc'];

        //$list = $this->searchService->search('Company', $where, $order_by, '');

        $list         = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $total        = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->count();
        $total_person = DB::table('resumes')->whereRaw('created_at > "2019-12-01 00:00:00"')->groupBy('uid')->get();

        //$list = DB::table('companys')->where($where)->whereIn('id',$companys)->orderBy('id','desc')->offset($limit*$offset)->limit($limit)->get();

//        echo '<pre>';
//
//        var_dump($list);die;

        $more = count($list) >= $limit ? true : false;

//        if ($list->total() >0) {
//            //处理企业信息
//            $list_items = $this->companyService->dealCompanyFields($list->items());
//        } else {
//            $list_items = array();
//        }

        $res           = [];
        $total_job     = 0;
        $total_job_num = 0;
        foreach ($list as $val) {
            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
            ];

            //$jobs = $this->jobsService->getOtherJobs($jobs_where);
            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if ($jobs) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                    ];
                    array_push($item['jobs'], $job);
                    $total_job_num += $value->amount;
                    $total_job     += 1;
                }
            }

            array_push($res, $item);
        }


        $filter_where = [
            'AIX_trade'        => 100,
            'AIX_company_type' => 100,
        ];
        $categories   = $this->categoryService->getCategories($filter_where);

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('app.active.ajax.ajax_jobfair_com', [
                    'res'        => $res,
                    'city'       => $citys,
                    'params'     => $params,
                    'categories' => $categories,
                    'more'       => $more,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }


        return view('app.active.online2020', [
            'res'          => $res,
            'city'         => $citys,
            'params'       => $params,
            'categories'   => $categories,
            'more'         => $more,
            'total'        => $total,
            'total_person' => count($total_person),
            'total_job'    => $total_job
            //'list_items'    => $list_items,
            //'list'          => $list,
        ]);

    }

    public function test(Request $request)
    {

        $companys     = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw("jobs.created_at > '2020-01-01 00:00:00'")->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();
        $where        = [];
        $where[]      = ['user_status', '=', 1];
        $where[]      = ['subsite_id', '=', get_subsite_id()];
        $where[]      = ['audit', '=', 1];
        $where[]      = ['deleted_at', '=', null];
        $order_by     = ['id', 'desc'];
        $citycategory = $request->input('citycategory');
        $citys        = $this->categoryService->getCitys($citycategory);

        if (empty($citycategory)) {
            $district_str = "(district like ? or district like ? or district like ? or district like ?)";
            $district_arr = ["%.623%", "%623.%", "%.245%", "%245.%"];
        } else {
            $select_id    = $citys['select']['id'];
            $district_str = "(district like ? or district like ?)";
            $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];
        }

        //$list = DB::table('companys')->where($where)->whereRaw("(district like ? or district like ? or district like ? or district like ?)",["%.{$citys['select']['id']}%","%{$citys['select']['id']}.%","%.245%","%245.%"])->whereIn('id',$companys)->limit(1000)->orderBy('id','desc')->get();
        $list         = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('id', 'desc')->get();
        $total        = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->count();
        $total_person = DB::table('resumes')->whereRaw('created_at > "2019-12-01 00:00:00"')->groupBy('uid')->get();

        $res           = [];
        $total_job     = 0;
        $total_job_num = 0;
        foreach ($list as $val) {
            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
            ];

            //$jobs = $this->jobsService->getOtherJobs($jobs_where);
            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if ($jobs) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                    ];
                    array_push($item['jobs'], $job);
                    $total_job_num += $value->amount;
                    $total_job     += 1;
                }
            }

            array_push($res, $item);
        }

        echo "<p>企业数量:" . count($list) . "</p>\r\n";
        echo "<p>求职者数量:" . count($total_person) . "</p>\r\n";
        echo "<p>总招聘职位数量:" . $total_job . "</p>\r\n";
        echo "<p>总需要人员数量:" . $total_job_num . "</p>\r\n";


    }

    public function data(Request $request)
    {
        $companys     = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw("jobs.created_at > '2020-01-01 00:00:00'")->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();
        $where        = [];
        $where[]      = ['user_status', '=', 1];
        $where[]      = ['subsite_id', '=', get_subsite_id()];
        $where[]      = ['audit', '=', 1];
        $where[]      = ['deleted_at', '=', null];
        $order_by     = ['id', 'desc'];
        $citycategory = $request->input('citycategory');
        $citys        = $this->categoryService->getCitys($citycategory);
        if (empty($citycategory)) {
            $citys['select']['id'] = 623;
        }
        $list          = DB::table('companys')->where($where)->whereRaw("(district like ? or district like ? or district like ? or district like ?)", ["%.{$citys['select']['id']}%", "%{$citys['select']['id']}.%", "%.245%", "%245.%"])->whereIn('id', $companys)->limit(1000)->orderBy('id', 'desc')->get();
        $total_person  = DB::table('resumes')->whereRaw('created_at > "2019-12-01 00:00:00"')->groupBy('uid')->get();
        $total_job     = 0;
        $total_job_num = 0;
        foreach ($list as $val) {
            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
            ];

            //$jobs = $this->jobsService->getOtherJobs($jobs_where);
            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if ($jobs) {
                foreach ($jobs as $value) {

                    $total_job_num += $value->amount;
                    $total_job     += 1;
                }
            }
        }

        $org          = DB::table('organization')->whereRaw('id>=10')->get();
        $service      = DB::table('service')->whereRaw('service_type = 3')->get();
        $total_number = 0;

        foreach ($service as $k => $v) {
            $detail       = unserialize($v->detail);
            $total_number += intval($detail[0]['number']);
        }

        $res = [
            'total_enterprise' => count($list),
            'total_job'        => $total_job,
            'total_person'     => count($total_person),
            'total_number'     => $total_number,
            'total_org'        => count($org),
        ];

        echo json_encode($res);
    }

    /**新春线上招聘会
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function spring(Request $request)
    {
        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 18;

        $citycategory = $request->input('citycategory');
        $trade        = $request->input('trade');
        $param_array  = ['citycategory', 'trade', 'nature'];

        $params = [];
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array) && $v) {
                    $params[$k] = $v;
                }
            }
        }

        $subsites = Cache::get('subsites_list');
        if ($subsites) {
            if (!array_has($params, 'citycategory')) {
                if (get_subsite_id() > 0) {
                    $citycategory = $subsites[get_subsite_id()]['district'];
                }
            }
        }
        $citys = $this->categoryService->getCitys($citycategory);

        if (empty($citycategory)) {
            $district_str = "(district like ? or district like ? or district like ? or district like ?)";
            $district_arr = ["%.623%", "%623.%", "%.245%", "%245.%"];
        } else {
            $select_id    = $citys['select']['id'];
            $district_str = "(district like ? or district like ?)";
            $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];
        }

        $whereRaw = "jobs.updated_at > '2023-01-01 00:00:00' and jobs.deleted_at is null and jobs.valid = 1 and jobs.audit = 1 and jobs.display = 1";
        $companys = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw($whereRaw)->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];
        if (!empty($trade)) {
            $where[] = ['trade', '=', $trade];
        }

        $list         = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $total        = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->count();
        $total_person = DB::table('resumes')->whereRaw('created_at > "2019-12-01 00:00:00"')->groupBy('uid')->get();

        $more = count($list) >= $limit ? true : false;

        $res           = [];
        $total_job     = 0;
        $total_job_num = 0;
        foreach ($list as $val) {
            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
                'tag_arr'     => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if ($jobs) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                    ];
                    array_push($item['jobs'], $job);
                    $total_job_num += $value->amount;
                    $total_job     += 1;
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }


        $filter_where = [
            'AIX_trade'        => 100,
            'AIX_company_type' => 100,
        ];
        $categories   = $this->categoryService->getCategories($filter_where);

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('app.active.ajax.ajax_spring_special_com', [
                    'res'        => $res,
                    'city'       => $citys,
                    'params'     => $params,
                    'categories' => $categories,
                    'more'       => $more,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }


        return view('app.active.spring', [
            'res'          => $res,
            'city'         => $citys,
            'params'       => $params,
            'categories'   => $categories,
            'more'         => $more,
            'total'        => $total,
            'total_person' => count($total_person),
            'total_job'    => $total_job,
        ]);

    }

    /**
     * 智能招聘长廊
     */
    public function ai_recruit(Request $request)
    {
        $citycategory = 'jjkfq';
        $whereRaw     = "jobs.updated_at > '2023-01-01 00:00:00' and jobs.deleted_at is null and jobs.valid = 1 and jobs.audit = 1 and jobs.display = 1";
        $companys     = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw($whereRaw)->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

        $citys        = $this->categoryService->getCitys($citycategory);
        $select_id    = $citys['select']['id'];
        $district_str = "(district like ? or district like ?)";
        $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];

        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];
        if (!empty($trade)) {
            $where[] = ['trade', '=', $trade];
        }

        $list = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->get();

        $res = [];
        foreach ($list as $val) {

            $item = [
                'id'           => $val->id,
                'companyname'  => $val->companyname,
                'landline_tel' => $val->landline_tel,
                'mobile'       => $val->mobile,
                'reg_address'  => $val->reg_address,
                'jobs'         => [],
                'tag_arr'      => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if (!$jobs->isEmpty()) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                        'wage_str'  => $value->wage_str,
                    ];
                    array_push($item['jobs'], $job);
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }

        $res = $this->_split_arr($res, 4);

        return view('app.active.ai_recruit', ['res' => $res]);
    }

    private function _split_arr($arr, $num)
    {
        $res  = [];
        $item = [];
        foreach ($arr as $k => $v) {
            if ($k % $num == 0 && $k != 0) {
                $res[] = $item;
                $item  = [];
            }
            $item[] = $v;
        }

        if (!empty($item)) {
            $res[] = $item;
        }

        return $res;
    }

    /**新春线上招聘会经济开发区
     */
    public function spring_jkq(Request $request)
    {
        if (!$request->ajax()) {
            //奖品列表
            $prize = LotteryPrize::all();
        }

        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 18;

        $citycategory = 'jjkfq';
        $whereRaw     = "jobs.updated_at > '2023-01-01 00:00:00' and jobs.deleted_at is null and jobs.valid = 1 and jobs.audit = 1 and jobs.display = 1";
        $companys     = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw($whereRaw)->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

        $citys        = $this->categoryService->getCitys($citycategory);
        $select_id    = $citys['select']['id'];
        $district_str = "(district like ? or district like ?)";
        $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];

        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];

        $list = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $more = count($list) >= $limit ? true : false;

        $res = [];
        foreach ($list as $val) {

            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
                'tag_arr'     => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if (!$jobs->isEmpty()) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                        'wage_str'  => $value->wage_str,
                    ];
                    array_push($item['jobs'], $job);
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('app.active.ajax.ajax_spring_special_com', [
                    'res'  => $res,
                    'city' => $citys,
                    'more' => $more,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }


        return view('app.active.spring_jkq', [
            'params'    => ['citycategory' => 'jjkfq'],
            'prize'     => $prize,
            'res'       => $res,
            'more'      => $more,
            'live_list' => $this->homeService->getSpringLive(),
        ]);

    }

    /**新春线上招聘会经济开发区
     */
    public function spring_main()
    {
        //奖品列表
        $prize = LotteryPrize::all();

        return view('app.active.spring_main', [
            'prize'     => $prize,
            'live_list' => $this->homeService->getSpringLive(),
        ]);

    }

    /**
     * 海灵线上招聘会
     */
    public function online_recruit(Request $request)
    {
        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 18;

        $trade       = $request->input('trade');
        $param_array = ['trade', 'nature'];
        $params      = [];
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array) && $v) {
                    $params[$k] = $v;
                }
            }
        }
        $params['citycategory'] = 'jjkfq';

        $citycategory = 'jjkfq';
        $whereRaw     = "jobs.updated_at > '2022-06-01 00:00:00' and jobs.deleted_at is null and jobs.valid = 1 and jobs.audit = 1 and jobs.display = 1 and education > 68";
        $companys     = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw($whereRaw)->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

        $citys        = $this->categoryService->getCitys($citycategory);
        $select_id    = $citys['select']['id'];
        $district_str = "(district like ? or district like ?)";
        $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];

        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];
        if (!empty($trade)) {
            $where[] = ['trade', '=', $trade];
        }

        $list = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $more = count($list) >= $limit ? true : false;

        $res = [];
        foreach ($list as $val) {

            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
                'tag_arr'     => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
                ['education', '>', 68],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if (!$jobs->isEmpty()) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                        'wage_str'  => $value->wage_str,
                    ];
                    array_push($item['jobs'], $job);
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }

        $filter_where = [
            'AIX_trade'        => 100,
            'AIX_company_type' => 100,
        ];
        $categories   = $this->categoryService->getCategories($filter_where);

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('app.active.ajax.ajax_spring_recruit', [
                    'categories' => $categories,
                    'res'        => $res,
                    'city'       => $citys,
                    'more'       => $more,
                    'params'     => $params,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }

        return view('app.active.online_recruit', [
            'categories' => $categories,
            'res'        => $res,
            'more'       => $more,
            'params'     => $params,
        ]);
    }

    /**薪动餐饮
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Throwable
     */
    public function restaurant(Request $request)
    {
        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 18;


        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['is_restaurant', '=', 1];
        $where[] = ['deleted_at', '=', null];

        $list         = DB::table('companys')->where($where)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $total        = DB::table('companys')->where($where)->count();

        $more = count($list) >= $limit ? true : false;

        $res           = [];
        $total_job     = 0;
        $total_job_num = 0;
        foreach ($list as $val) {
            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
                'tag_arr'     => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if ($jobs) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                    ];
                    array_push($item['jobs'], $job);
                    $total_job_num += $value->amount;
                    $total_job     += 1;
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('app.active.ajax.ajax_restaurant', [
                    'res'        => $res,
                    'more'       => $more,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }

        return view('app.active.restaurant', [
            'res'          => $res,
            'more'         => $more,
            'total'        => $total,
            'total_job'    => $total_job,
        ]);

    }
}