<?php


namespace App\Http\Controllers\Web\Hardware\Tv;

use App\Http\Controllers\Web\WebBaseController;
use App\Models\CategoryJobs;
use App\Models\Company;
use App\Models\Jobs;
use App\Models\Member;
use App\Models\PersonalTalent;
use App\Models\Resume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;

class DapingController extends WebBaseController
{
    public $view_data = null;
    public $SUBSITE_ON = null;
    public $subsite_id = null;
    public $request_method = null;
    public $is_ajax = null;
    public $province = null;
    public $city = null;

    public function __construct(Request $request)
    {
        $this->view_data = null;
        $this->SUBSITE_ON = config('aix.SUBSITE_ON');
        $this->subsite_id = session('caiqing_susbite_id');
        $this->request_method = $request->method();
        $this->is_ajax = $request->ajax();
        $this->province = config('aix.province');
        $this->city = config('aix.city');
    }

    public function index(Request $request)
    {
        $news = $this->getNews($request);
        $overview = $this->getOverview($request);
        $talent = $this->getTalent($request);
        $demand = $this->getDemand($request);
        $demand2 = $this->getDemand2();
        $trend = $this->getTrend($request);
        $right_data = $this->get_right_data();
//        return $this->view_data;
        return view('app.hardware.tv.daping', $this->view_data);
    }

    //获取新增数据
    public function getNews(Request $request)
    {
        $mon = $request->input('mon', '0');
//        $mon = I('request.mon','0','intval');
        $where = [];
        $where2 = [];
        //加入分站条件
        if ($this->SUBSITE_ON == true) {
            if (!empty($this->subsite_id)) {
                $where['subsite_id'] = $this->subsite_id;
                $where2['subsite_id'] = $this->subsite_id;
            }
        }
        //php获取今日开始时间戳和结束时间戳
        $end_day = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $end_day = date('Y-m-d H:i:s', $end_day);
        if (($mon >= 0)) {
            if ($mon == '0') { //获取年初到现在的数据
                $pre_start = mktime(0, 0, 0, 1, 1, date('Y'));
            } elseif ($mon > 0) { //获取距离当前日期$mon个月的数据
                $pre_start = strtotime(date('Y-m-d', strtotime("-" . $mon . " month")));
            }
        } else if ($mon == '-1') { //获取所有数据
            $pre_start = null;
            $pre_end = null;
        }
        $pre_start = date('Y-m-d H:i:s', $pre_start);

        //获取个人会员注册数
        if ($mon >= 0) {
            $where_person_add[] = ['created_at', '>=', $pre_start];
            $where_person_add[] = ['created_at', '<=', $end_day];
        } else {
            $where_person_add[] = ['created_at', '<=', $end_day];
        }
//        $personal_member_num = M("members")->where($where_person_add)->where($where)->count();
        $personal_member_num = Member::where($where_person_add)->where($where)->count();

        //获取企业会员注册数
        if ($mon >= 0) {
            $where_company_add[] = ['created_at', '>=', $pre_start];
            $where_company_add[] = ['created_at', '<=', $end_day];
        } else {
            $where_company_add[] = ['created_at', '<=', $end_day];
        }
//        $company_member_num = M("members")->where($where_company_add)->where($where)->count();
        $company_member_num = Company::where($where_company_add)->where($where)->count();

        //新增职位
        $where_job_add = null;
        if ($mon >= 0) {
            $where_job_add[] = ['created_at', '>=', $pre_start];
            $where_job_add[] = ['created_at', '<=', $end_day];
        } else {
            $where_job_add[] = ['created_at', '<=', $end_day];
        }
//        $job_infos = M("jobs")->where($where_job_add)->where($where)->field('COUNT(*) AS job_num ,SUM(amount) as amount_num')->find();
        $job_num = Jobs::where($where_job_add)
            ->where($where)
            ->count();
        $amount_num = Jobs::where($where_job_add)
            ->where($where)
            ->sum('amount');
        if ($request->isMethod('post')) {
            $return_data = array('company_member_num' => $company_member_num, 'personal_member_num' => $personal_member_num, 'job_info' => $job_infos);
            return json_encode($return_data);
        } else {
//            $this->assign('company_member_num', $company_member_num);
//            $this->assign('personal_member_num', $personal_member_num);
//            $this->assign('job_num', $job_infos['job_num']);
//            $this->assign('amount_num', $job_infos['amount_num'] ? $job_infos['amount_num'] : 0);
            $this->view_data['company_member_num'] = $company_member_num;
            $this->view_data['personal_member_num'] = $personal_member_num;
            if ($job_num) {
                $this->view_data['job_num'] = $job_num;
            } else {
                $this->view_data['job_num'] = 0;
            }
            if ($amount_num) {
                $this->view_data['amount_num'] = $amount_num;
            } else {
                $this->view_data['amount_num'] = 0;
            }
        }
    }

    public function getEdvisiteducations(Request $request)
    {
        if (config('aix.SUBSITE_ON') == true) {
            $subsite_id = session('caiqing_susbite_id');
            if ($subsite_id >= 0) {
                $where_subsite = " and subsite_id = " . $subsite_id . "";
            } else {
                $where_subsite = "";
            }
        } else {
            $where_subsite = '';
        }
        $sql = "SELECT COUNT(*) AS num,b.c_name as c_name from resumes as a join categorys as b on a.education=b.c_id where b.c_alias='AIX_education' $where_subsite GROUP BY a.education order by num desc";
        $dataObj = DB::select($sql);
        $i = 0;
        $edata_str = "";
        $edata_pic = "";

        if ($request->isMethod('post')) {
            $str_arr = array();
            $edu_arr = array();
            foreach ($dataObj as $key => $value) {
                $edu_arr[] = array('value' => intval($value['num']), 'name' => $value['c_name']);
                $str_arr[] = $value['c_name'];
            }
            $data['edu_str'] = $str_arr;
            $data['edu_arr'] = $edu_arr;
            if ($str_arr && $edu_arr) {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            return json_encode($data);
        } else {
            foreach ($dataObj as $key => $value) {
                if (empty($edata_pic)) {
                    $edata_pic = "{value:" . intval($value['num']) . ",name:'" . $value['c_name'] . "'}";
                } else {
                    $edata_pic = $edata_pic . "," . "{value:" . intval($value['num']) . ",name:'" . $value['c_name'] . "'}";
                }
                if (empty($edata_str)) {
                    $edata_str = "'" . $value['c_name'] . "'";
                } else {
                    $edata_str = $edata_str . ',' . "'" . $value['c_name'] . "'";
                }
                $i++;
            }
//            $this->assign('edata_str',$edata_str);
//            $this->assign('edata',$edata_pic);
            $this->view_data['edata_str'] = $edata_str;
            $this->view_data['edata'] = $edata_pic;
        }
    }

    public function getTalent(Request $request)
    {
        //加入分站条件
        if ($this->SUBSITE_ON == true) {
            if ($this->subsite_id === false) {
                $where = [];
            } else {
                if (!empty($this->subsite_id)) {
                    $where[] = ['subsite_id', '=', $this->subsite_id];
                }
            }
        } else {
            $where = [];
        }

        //日期范围
//        $mon = I('request.mon', '0', 'intval');
        $mon = $request->input('request.mon', '0');
        $now_date = date('Y-m-d', time());
        if ($mon == 0) {
            $start_time = mktime(0, 0, 0, 1, 1, date('Y'));
        } else if ($mon > 0) {
            $start_time = date('Y-m-d', strtotime("-" . $mon . " month"));
        } else {
            $start_time = null;
        }

        $min = date('Y-m-d H:i:s', $start_time);
        $max = date('Y-m-d H:i:s', strtotime($now_date) + 60 * 60 * 24 - 1);


//        $category = M('category_jobs')->where("parentid=0")->select();
        $category = CategoryJobs::where('parent_id', '0')->get();

        $minNum = 0;
        $minusArr = array(); //职位分类和差异数
        foreach ($category as $key => $value) {
            if (strstr($value['categoryname'], '其他') === false && strstr($value['categoryname'], '其它') === false) {
                $minusArr[$key]['label'] = $value['name'];
                if ($min && $max) {
                    $where[] = ['created_at', '>=', $min];
                    $where[] = ['created_at', '<=', $max];
                } else {
                    $where[] = ['created_at', '>=', $min];
                }
                $job_count = Jobs::where($where)
                    ->where('topclass', '=', $value['id'])
                    ->sum('amount');
                $dataObj = Resume::where($where)
                    ->pluck('intention_jobs_id');
                $num = 0;
                foreach ($dataObj as $key1 => $value1) {
                    $j = explode(",", $value1);
                    $c = explode(".", $j[0]);
                    if ($c[0] == $value['id']) {
                        $num++;
                    }
                }
                $resume_jobs = $num;
                $minusArr[$key]['value'] = $job_count - $resume_jobs;
                if ($minusArr[$key]['value'] < $minNum) {
                    $minNum = $minusArr[$key]['value'];
                }
            }
        }
        $dArr = array();
        $item = 0;
        foreach ($minusArr as $key => $value) {
            $dArr[$item]['label'] = $value['label'];
            $dArr[$item]['value'] = $value['value'];
            $item++;
        }
        usort($dArr, function ($a, $b) {
            $al = $a['value'];
            $bl = $b['value'];
            if ($al == $bl)
                return 0;
            return ($al > $bl) ? -1 : 1;
        });
        $cate_names = array();
        $cate_values = array();
        foreach ($dArr as $k => $v) {
            $cate_names[] = $v['label'];
            $cate_values[] = $v['value'];
        }
        if ($this->is_ajax || $this->request_method == 'post') {
            return json_encode($dArr);
        } else {
//            $this->assign('category', $category);
//            $this->assign("minNum", $minNum);
//            $this->assign('data', $dArr);
//            $this->assign('cate_names', json_encode($cate_names));
//            $this->assign('cate_values', json_encode($cate_values));
            $this->view_data['category'] = $category;
            $this->view_data['minNum'] = $minNum;
            $this->view_data['data'] = $dArr;
            $this->view_data['cate_names'] = json_encode($cate_names);
            $this->view_data['cate_values'] = json_encode($cate_values);
        }
    }

    /*趋势分析
     * 开始时间和结束时间不在同一年时显示12个月的数据
     * 逻辑:没间隔一年显示一组数据
     */
    public function getTrend(Request $request)
    {
//        $mon = I('request.mon', '0', 'intval');
        $mon = $request->input('request.mon', '0');
        $now_date = date('Y-m-d', time());
        if ($mon == 0) {
            $start_time = mktime(0, 0, 0, 1, 1, date('Y'));
        } else if ($mon > 0) {
            $start_time = date('Y-m-d', strtotime("-" . $mon . " month"));
        } else {
            $start_time = null;
        }
        $min = strtotime($start_time);
        $max = strtotime($now_date) + 60 * 60 * 24 - 1;
        $start_year = date('Y', $min);
        $start_month = date('n', $min);
        $end_year = date('Y', $max);
        $end_month = date('n', $max);
        //同一年显示一组数据,不同年显示多条12个的数据
        $month_data = array();
        $legend_data = array();
        $chart_data = array();
        if ($start_year == $end_year) {
            $date_where = array(
                'start_date' => $min,
                'end_date' => $max,
            );
            $res = $this->getTrendData($date_where);
            $chart_data[] = $res;
            $legend_data[] = $start_year . '年求职人数';
            //X轴坐标
            for ($j = $start_month; $j <= $end_month; $j++) {
                $month_data[] = $j . '月';
            }
        } else {
            $y_num = $end_year - $start_year; //间隔年数
            for ($i = 0; $i <= $y_num; $i++) {
                if ($i == 4) {
                    break;
                }
                $start = (string)($end_year - $i) . '-01-01';
                $end = (string)($end_year - $i + 1) . '-01-01';

                $date_where = array(
                    'start_date' => strtotime($start),
                    'end_date' => strtotime($end) - 1,
                );
                $res = $this->getTrendData($date_where);
                $chart_data[] = $res;
                $legend_data[] = ($end_year - $i) . '年求职人数';
            }
            $month_data = array('1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月');
        }
        $chart_data_str = '[';
        $chart_data_arr = array();
        if ($chart_data) {
            foreach ($chart_data as $k => $v) {

                $chart_data_arr[] = "{name:'" . $v['name'] . "',type:'" . $v['type'] . "',data:[" . implode(',', $v['data']) . "], symbolSize:'" . $v['symbolSize'] . "',}";
            }
        }
        $chart_data_str .= implode(',', $chart_data_arr) . ']';
        if ($this->request_method == 'post' || $this->is_ajax) {
            $return_data = array('legend_data' => $legend_data, 'chart_data' => $chart_data, 'month_data' => $month_data);
            return json_encode($return_data);
        } else {
//            $this->assign('legend_data', json_encode($legend_data));
//            $this->assign('chart_data', $chart_data_str);
//            $this->assign('month_data', json_encode($month_data));
            $this->view_data['legend_data'] = $legend_data;
            $this->view_data['chart_data'] = $chart_data_str;
            $this->view_data['month_data'] = $month_data;
        }
    }

    public function getTrendData($map)
    {
        $where = [];
        $where2 = [];
        if ($this->SUBSITE_ON == true) {
            if ($this->subsite_id !== false) {
                if ($this->subsite_id >= 0) {
                    $where['subsite_id'] = $this->subsite_id;
                    $where2['subsite_id'] = $this->subsite_id;
                }
            }

        }
        $min_date = $map['start_date'];
        $max_date = $map['end_date'];

        //只选了一个时间的情况,不做对比
        $start_year = date('Y', $min_date);
        $end_year = date('Y', $max_date);
        $start_month = date('n', $min_date);
        $end_month = date('n', $max_date);
        $val_data = array();
        for ($i = $start_month; $i <= $end_month; $i++) {
            $min = strtotime($start_year . '-' . $i);
            if ($i == 12) {
                $max = strtotime(($end_year + 1) . '-1');
            } else {
                $max = strtotime($end_year . '-' . ($i + 1));
            }
            $admin = Admin::user();
            if (!empty($admin)) {
                if ($admin->role_id == 3) {
                    $where['district'] = $this->province;
                } else if ($admin->role_id == 4) {
                    $where['sdistrict'] = $this->city;
                } else if ($admin->role_id == 5) {
                    $where['user_id'] = $admin->id;
                }
            }

//            $where['addtime'] = array(array('>=', $min), array('<=', $max), 'and');
            $where[] = ['created_at', '>=', $min];
            $where[] = ['created_at', '<=', $max];
            $resume_count = Resume::where($where)->count();
            $val_data[] = $resume_count;
        }
        $resumeArr = array(
            'name' => $start_year . '年求职人数',
            'type' => 'line',
            'data' => $val_data,
            'symbolSize' => '10',
        );
        return $resumeArr;
    }

    public function getDemand(Request $request)
    {
//        $mon = I('request.mon', '0', 'intval');
        //加入分站条件
        $where = [];
        if ($this->SUBSITE_ON == true) {
            if (!empty($this->subsite_id)) {
                $where['subsite_id'] = $this->subsite_id;
            }
        }
        $category = CategoryJobs::where('parent_id', '=', '0')->limit(7)->get();
        $month_array = ['1' => '一月', '2' => '二月', '3' => '三月', '4' => '四月', '5' => '五月', '6' => '六月', '7' => '七月', '8' => '八月', '9' => '九月', '10' => '十月', '11' => '十一月', '12' => '十二月'];
        $prev_date_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
        $prev_date_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));
        $prev_two_date_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 2, 1, date("Y")));
        $prev_two_date_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), -1, date("Y")));
        $prev_two_month_str = $month_array[date('n', strtotime($prev_two_date_start))];
        $prev_month_str = $month_array[date('n', strtotime($prev_date_start))];

//        $model_jobs = M('jobs', 'qs_', 'DB_PRIMARY');
        $data = [];
        foreach ($category as $key => $value) {
            switch ($key) {
                case 0;
                    $catename = '单位负责人';
                    break;
                case 1;
                    $catename = '专业技术人员';
                    break;
                case 2;
                    $catename = '办事人员和有关人员';
                    break;
                case 3;
                    $catename = '商业和服务业人员';
                    break;
                case 4;
                    $catename = '农林牧渔水利生产人员';
                    break;
                case 5;
                    $catename = '生产运输设备操作工';
                    break;
                case 6;
                    $catename = '其他';
                    break;
                default;
                    $catename = '未知';
                    break;
            }
            $data[] = [
                'catename' => $catename,
                'name' => $value->name,
                'total_jobs' => Jobs::where($where)
                    ->where('topclass', '=', $value->id)
                    ->where('created_at', '>=', $prev_date_start)
                    ->where('created_at', '<=', $prev_date_end)
                    ->count(),
                'total_resume' => Jobs::where($where)
                    ->where('created_at', '>=', $prev_two_date_start)
                    ->where('created_at', '<=', $prev_two_date_end)
                    ->count(),
            ];
        }


        //计算需求合计
        $total_jobs = 0;
        foreach ($data as $k => $v) {
            $total_jobs += $v['total_jobs'];
        }
        //计算求职合计
        $total_resume = 0;
        foreach ($data as $k => $v) {
            $total_resume += $v['total_resume'];
        }


        //格式化图表分类数据
        $categories = array();
        foreach ($data as $k => $v) {
            $categories[] = $v['catename'];
        }
        //格式化图表需要的数据
        $dataValue = array();
        $resume_value = array();
        $multiple_value = array();

        foreach ($data as $k => $v) {
            $dataValue[] = $v['total_jobs'];
            $resume_value[] = $v['total_resume'];
            $total_name[] = $v['name'];
        }
        if ($this->request_method == 'post' || $this->is_ajax) {
            $return_data = array('data_charts' => $dataValue, 'data_charts2' => $resume_value, 'categories' => $categories, 'multiple' => $multiple_value);
            return $return_data;
        } else {
//            $this->assign('data_charts', json_encode($dataValue));
//            $this->assign('data_charts2', json_encode($resume_value));
//            $this->assign('total_name', json_encode($total_name));
//            $this->assign('prev_month_str', json_encode($prev_month_str));
//            $this->assign('prev_two_month_str', json_encode($prev_two_month_str));
//            $this->assign('multiple', json_encode($multiple_value));
//            $this->assign('categories', json_encode($categories));
            $this->view_data['data_charts'] = json_encode($dataValue);
            $this->view_data['data_charts2'] = json_encode($resume_value);
            $this->view_data['total_name'] = json_encode($total_name);
            $this->view_data['prev_month_str'] = json_encode($prev_month_str);
            $this->view_data['prev_two_month_str'] = json_encode($prev_two_month_str);
            $this->view_data['multiple'] = json_encode($multiple_value);
            $this->view_data['categories'] = $categories;
        }
    }

    public function getDemand2()
    {
//        $mon = I('request.mon', '0', 'intval');

        //加入分站条件
        $where = [];
        if ($this->SUBSITE_ON == true) {
            if (!empty($this->subsite_id)) {
                if ($this->subsite_id >= 0) {
                    $where['subsite_id'] = $this->subsite_id;
                } else {
                    $where = [];
                }
            }
        }

        $month_array = ['1' => '一月', '2' => '二月', '3' => '三月', '4' => '四月', '5' => '五月', '6' => '六月', '7' => '七月', '8' => '八月', '9' => '九月', '10' => '十月', '11' => '十一月', '12' => '十二月'];
        $prev_date_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
        $prev_date_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));
        $prev_two_date_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 2, 1, date("Y")));
        $prev_two_date_end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), -1, date("Y")));
        $prev_two_month_str2 = $month_array[date('n', strtotime($prev_two_date_start))];
        $prev_month_str2 = $month_array[date('n', strtotime($prev_date_start))];

        $ids = [
            802 => '第一类人才',
            803 => '第二类人才',
            804 => '第三类人才',
            805 => '第四类人才',
            806 => '第五类人才',
            0 => '不符合人才标准'
        ];
        foreach ($ids as $key => $value) {
            $data[] = [
                'name' => $value,
                'total_jobs' => PersonalTalent::where($where)
                    ->where("rc_level_id", '=', $key)
                    ->where('created_at', '>=', $prev_date_start)
                    ->where('created_at', '<=', $prev_date_end)
                    ->count(),
                'total_resume' => PersonalTalent::where($where)
                    ->where("rc_level_id", '=', $key)
                    ->where('created_at', '>=', $prev_two_date_start)
                    ->where('created_at', '<=', $prev_two_date_end)
                    ->count(),
            ];
        }

        //计算需求合计
        $total_jobs = 0;
        foreach ($data as $k => $v) {
            $total_jobs += $v['total_jobs'];
        }
        //计算求职合计
        $total_resume = 0;
        foreach ($data as $k => $v) {
            $total_resume += $v['total_resume'];
        }


        //格式化图表分类数据

        //格式化图表需要的数据
        $dataValue = array();
        $resume_value = array();
        $multiple_value = array();

        foreach ($data as $k => $v) {
            $dataValue[] = $v['total_jobs'];
            $resume_value[] = $v['total_resume'];
            $total_name[] = $v['name'];
        }
//        var_dump(json_encode($total_name));
//        exit();
        if ($this->is_ajax || $this->request_method == 'post') {
            $return_data = array('data_charts' => $dataValue, 'data_charts2' => $resume_value, 'multiple' => $multiple_value);
            return $return_data;
        } else {
//            $this->assign('data_charts3', json_encode($dataValue));
//            $this->assign('data_charts4', json_encode($resume_value));
//            $this->assign('total_name2', json_encode($total_name));
//            $this->assign('prev_month_str2', json_encode($prev_month_str2));
//            $this->assign('prev_two_month_str2', json_encode($prev_two_month_str2));
//            $this->assign('multiple', json_encode($multiple_value));
            $this->view_data['data_charts3'] = json_encode($dataValue);
            $this->view_data['data_charts4'] = json_encode($resume_value);
            $this->view_data['total_name2'] = json_encode($total_name);
            $this->view_data['prev_month_str2'] = json_encode($prev_month_str2);
            $this->view_data['prev_two_month_str2'] = json_encode($prev_two_month_str2);
            $this->view_data['multiple'] = json_encode($multiple_value);
        }
    }

    public function getEducations()
    {
//        $a = [array('value' => 1037, 'name' => "本科"),
//            array('value' => 222, 'name' => "大专"),
//            array('value' => 70, 'name' => "中专"),
//            array('value' => 63, 'name' => "高中"),
//            array('value' => 39, 'name' => "初中"),
//            array('value' => 35, 'name' => "硕士"),
//            array('value' => 17, 'name' => "中技"),
//            array('value' => 3, 'name' => "博士"),
//            array('value' => 2, 'name' => "博后")];
//        $b = ["本科", "大专", "中专", "高中", "初中", "硕士", "中技", "博士", "博后"];
//        return ['edu_arr' => $a, 'edu_str' => $b, 'status' => 1];
        $where_subsite = '';
        if ($this->SUBSITE_ON == true) {
            if (!empty($this->subsite_id)) {
                $where_subsite = " and subsite_id = " . $this->subsite_id . "";
            }
        }
        $sql = "SELECT COUNT(*) AS num,b.demand as demand from resumes as a join categorys as b on a.education=b.id where b.alias='AIX_education' $where_subsite GROUP BY a.education order by num desc";
        $dataObj = DB::select($sql);
        $str_arr = array();
        $edu_arr = array();
        foreach ($dataObj as $key => $value) {
            $edu_arr[] = array('value' => intval($value->num), 'name' => $value->demand);
            $str_arr[] = $value->demand;
        }
        $data['edu_str'] = $str_arr;
        $data['edu_arr'] = $edu_arr;
        if ($str_arr && $edu_arr) {
            $data['status'] = 1;
        } else {
            $data['status'] = 0;
        }
        return json_encode($data);
    }

    // 年龄分布
    public function get_right_data()
    {
        //添加筛选条件
        $where = [];
        if ($this->SUBSITE_ON == true) {
            if (!empty($this->subsite_id)) {
                $where['subsite_id'] = $this->subsite_id;
            }
        }

//        $model_resume = M('resume', 'qs_', 'DB_PRIMARY');
        $now_year = date('Y');

        $data = array();
        $for_data = array(
            ['district_cn' => "晋江市", 'age' => "31-40岁", 'max' => 40, 'min' => 31, 'district' => 568],
            ['district_cn' => "泉州市", 'age' => "26-30岁", 'max' => 30, 'min' => 26, 'district' => 573],
            ['district_cn' => "石狮市", 'age' => "23-25岁", 'max' => 25, 'min' => 23, 'district' => 570],
            ['district_cn' => "福清市", 'age' => "41-50岁", 'max' => 50, 'min' => 41, 'district' => 540],
            ['district_cn' => "长乐市", 'age' => "51-60岁", 'max' => 60, 'min' => 51, 'district' => 539],
            ['district_cn' => "思明区", 'age' => "18-22岁", 'max' => 22, 'min' => 18, 'district' => 551],
        );
        foreach ($for_data as $key => $value) {
            $data[] = [
                'district_cn' => $value['district_cn'],
                'age' => $value['age'],
                'total_man' => Resume::distinct()->where($where)
                    ->where('birthdate', '>', $now_year - $value['max'])
                    ->where('birthdate', '<=', $now_year - $value['min'])
                    ->where('sex', '=', 1)
                    ->count(),
                'total_women' => Resume::distinct()->where($where)
                    ->where('birthdate', '>', $now_year - $value['max'])
                    ->where('birthdate', '<=', $now_year - $value['min'])
                    ->where('sex', '=', 2)
                    ->count(),
                'total_district' => Resume::distinct()->where($where)
                    ->where('district', 'like', '%' . $value['district'] . '%')
                    ->where('display', '1')
                    ->count(),
                'total_district2' => Resume::distinct()->where($where)
                    ->where('district', 'like', '%' . $value['district'] . '%')
                    ->where('display', '0')
                    ->count(),
            ];
        }

        foreach ($data as $k => $v) {
            $district_cn[] = $v['district_cn'];
            $age[] = $v['age'];
            $total_man[] = $v['total_man'];
            $total_women[] = $v['total_women'];
            $total_district[] = $v['total_district'];
            $total_district2[] = $v['total_district2'];
        }

//        $this->assign('district_cn', json_encode($district_cn));
//        $this->assign('age', json_encode($age));
//        $this->assign('total_man', json_encode($total_man));
//        $this->assign('total_women', json_encode($total_women));
//        $this->assign('total_district', json_encode($total_district));
//        $this->assign('total_district2', json_encode($total_district2));
        $this->view_data['district_cn'] = json_encode($district_cn, true);
        $this->view_data['age'] = json_encode($age, true);
        $this->view_data['total_man'] = json_encode($total_man);
        $this->view_data['total_women'] = json_encode($total_women);
        $this->view_data['total_district'] = json_encode($total_district);
        $this->view_data['total_district2'] = json_encode($total_district2);

    }

    public function getCompetition(Request $request)
    {
        $mon = I('request.mon', '0', 'intval');

        $now_date = date('Y-m-d', time());
        if ($mon == 0) {
            $start_date = mktime(0, 0, 0, 1, 1, date('Y'));
        } else if ($mon > 0) {
            $start_time = date('Y-m-d', strtotime("-" . $mon . " month"));
        } else {
            $start_time = null;
        }
        //日期范围
        $min = strtotime($start_time);
        $max = strtotime($now_date) + 60 * 60 * 24 - 1;

        //添加筛选条件
        if (C('SUBSITE_ON') == true) {
            if ($this->subsite_id === false) {
                $where = [];
            } else {
                if ($this->subsite_id >= 0) {
                    $where['subsite_id'] = $this->subsite_id;
                } else {
                    $where['subsite_id'] = array('IN', '0,1,2,3,4,5');
                }
            }
        } else {
            $where = [];
        }

        if ($this->role_id == 3) {
            $condition = " and a.district = " . $this->province . "";
        } else if ($this->role_id == 4) {
            $condition = " and a.sdistrict = " . $this->city . "";
        } else if ($this->role_id == 5) {
            $condition = " and a.user_id = " . $this->uid . "";
        } else {
            $condition = "";
        }

        $tradeData = M('category_jobs')->where("parentid=0")->select();
        foreach ($tradeData as $key => $value) {
            $cidArr[] = $value['id'];
        }
        $cidStr = implode(',', $cidArr);


        $tradeArr = array();
        if ($min && $max) {
            $wheresql = " and apply_addtime>" . $min . " and apply_addtime<" . $max . " ";
        } else {
            $wheresql = " and apply_addtime<" . $max . " ";
        }
        $obj = M("personal_jobs_apply as p")->field("COUNT(*) AS num,j.category")
            ->join("left JOIN qs_jobs AS j ON p.jobs_id=j.id")
            ->where("1=1" . $wheresql . $condition)
            ->where($where)
            ->group("j.category")
            ->order("num desc")
            //->limit('0,6')
            ->select();
        $i = 0;
        foreach ($obj as $key => $value) {
            if ($i == 8) {
                break;
            }
            if ($value['category'] > 0) {
                $cat = M('category_jobs')->where("id=" . $value['category'] . "")->find();
                $tradeArr[$key]['label'] = $cat['categoryname'];
                $tradeArr[$key]['value'] = $value['num'];
                $tabledata[$key]['t_name'] = $cat['categoryname'];
                $tabledata[$key]['num'] = $value['num'];
                $i++;
            }
        }
        $dArr = array();
        $cname_arr = array();
        $cvale_arr = array();
        $item = 0;
        foreach ($tradeArr as $key => $value) {
            $dArr[$item]['label'] = $value['label'];
            $dArr[$item]['value'] = $value['value'];
            $item++;
            $cname_arr[] = $value['label'];
            $cvale_arr[] = $value['value'];
        }

        if (IS_POST || IS_AJAX) {
            echo json_encode($dArr);
        } else {
            $this->assign('cname_arr', json_encode($cname_arr));
            $this->assign('cvale_arr', json_encode($cvale_arr));
        }
    }

    public function getOverview(Request $request)
    {
//        $mon = I('request.mon', '0', 'intval');
        $mon = $request->input('mon', 0);
        $now_time = time();

        $now_date = date('Y-m-d', $now_time);
        if ($mon == 0) {
            $start_time = mktime(0, 0, 0, 1, 1, date('Y'));
        } else if ($mon > 0) {
            $start_time = date('Y-m-d', strtotime("-" . $mon . " month"));
        } else {
            $start_time = null;
        }
        //日期范围
        $min = date('Y-m-d H:i:s', $start_time);
        $max = date('Y-m-d H:i:s', strtotime($now_date) + 60 * 60 * 24 - 1);

        //添加筛选条件
        //加入分站条件
        $wheresql = '';
        $wheresql_d = '';
        if (!empty($this->subsite_id) and $this->subsite_id >= 0) {
            $where['subsite_id'] = $this->subsite_id;
            $where2['subsite_id'] = $this->subsite_id;
            $wheresql .= " and subsite_id = ?";
            $whereVal[] = $this->subsite_id;
        }
        $admin = Admin::user();
        if (!empty($admin)) {
            if ($admin->role_id == 3) {
                $wheresql .= " and a.district = ?";
                $whereVal[] = $this->province;
            } else if ($admin->role_id == 4) {
                $wheresql .= " and a.sdistrict = ?";
                $whereVal[] = $this->city;
            } else if ($admin->role_id == 5) {
                $wheresql .= " and a.user_id = ?";
                $whereVal[] = $admin->id;
            }
        }
        //获取职位大类简历投递前十信息
        $data = array();
        if ($min && $max) {
            $wheresql .= " where a.created_at > ? and a.created_at < ?";
            $wheresql_d .= " where d.down_addtime > ? and d.down_addtime < ?";
            $whereVal[] = $min;
            $whereVal[] = $max;
        } else {
            $wheresql .= " where a.created_at < ?";
            $wheresql_d .= " where d.down_addtime < ? ";
            $whereVal[] = $max;
        }

        $sql = "SELECT COUNT(*) AS num,j.category,c.name FROM personal_jobs_apply  AS a INNER JOIN jobs AS j on j.id=a.jobs_id LEFT JOIN category_jobs as c on c.id = j.category  $wheresql GROUP BY j.category ORDER BY num desc limit 0,10";
        $dataObj = DB::select($sql, $whereVal);
        $i = 0;
        $c_ids = array();
        foreach ($dataObj as $key => $value) {
            if ($value->category > 0) {
                $data[$i]['order'] = $i + 1;
                $data[$i]['t_name'] = $value->name;
                $data[$i]['num'] = $value->num;
                $data[$i]['category'] = $value->category;
                $c_ids[] = $value->category;
                $data[$i]['download_num'] = 0;
                $i++;

            }
        }
        if ($c_ids) {
            $wheresql_d .= ' and j.category in ( ? )';
            $whereVal[] = implode(',', $c_ids);
        }

        $downloads = $this->getDownloadInfo($wheresql_d, $whereVal); //获取简历下载量
        foreach ($data as $k => $v) {
            if (isset($downloads[$v['category']]) && $downloads[$v['category']]) {
                $data[$k]['download_num'] = $downloads[$v->category];
            }
        }
        if ($this->is_ajax || $this->request_method == 'post') {
            echo json_encode($data);
        } else {
//            $this->assign('overviewInfo', $data);
            $this->view_data['overviewInfo'] = $data;
        }
    }

    //获取简历下载信息
    public function getDownloadInfo($where, $whereVal)
    {
        $sql = "SELECT COUNT(*) AS num,j.category FROM company_down_resumes as d INNER join jobs  AS j on j.company_id = d.company_uid  $where GROUP BY j.category ORDER BY num DESC";
        $dataObj = DB::select($sql, $whereVal);
        $data = array();
        foreach ($dataObj as $key => $value) {
            $data[$value->category] = $value->num;
        }
        return $data;
    }
}