<?php

namespace app\mainapp\controller;

use app\common\model\RensheCode;
use app\mainapp\BaseController;
use app\common\model\Comjobs as ComjobsModel;
use app\common\model\ComjobsCate as ComjobsCateModel;
use app\common\model\User as UserModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\Train as TrainModel;
use app\common\model\TrainJoin as TrainJoinModel;


class Screen extends BaseController
{

    /**
     * 大屏数据
     */
    public function index()
    {
        $mul_min = 1;
        $mul_max = 1;
        $res     = [];

        //街道数据
        $comjobs_community = ComjobsModel::field("count(community),community")->group('community')->column('count(community)', 'community');
        $community         = RensheCode::getList('community')->toArray();
        foreach ($community as &$v) {
            if (!empty($comjobs_community[$v['code']])) {
                $v['count'] = $comjobs_community[$v['code']];
            } else {
                $v['count'] = 0;
            }
            unset($v);
        }
        $community[]      = [
            'id'    => 0,
            'name'  => '其他',
            'code'  => '',
            'count' => $comjobs_community[''],
        ];
        $res['community'] = $community;

        //岗位类型
        $comjobs_cate = ComjobsModel::field("count(cateid),cateid")->group('cateid')->column('count(cateid)', 'cateid');
        $cateids      = array_keys($comjobs_cate);
        $cate         = ComjobsCateModel::where('id', 'in', $cateids)->select()->toArray();
        foreach ($cate as &$v) {
            $v['count'] = $comjobs_cate[$v['id']];
            unset($v);
        }
        $res['cate'] = $cate;

        //岗位数据
        $comjobs_map   = [];
        $comjobs_map[] = ['createtime', '<=', time()];
        $comjobs_map[] = ['status', 'in', '3,4'];
        $orderby       = ['status' => 'asc', 'updatetime' => 'desc', 'id' => 'desc'];
        $comjobs       = [];
        $comjobs_data  = ComjobsModel::with(['worker'])->where($comjobs_map)->order($orderby)->limit(10)->select();
        foreach ($comjobs_data as $v) {
            $comjobs[] = [
                'title'   => $v['title'],
                'company' => $v['worker']['title'],
                'salary'  => $v['zwagall'],
            ];
        }
        $res['comjobs'] = $comjobs;

        //学历
        $user_education = UserModel::field("count(education),education")->group('education')->column('count(education)', 'education');
        $education_data = ['' => '未知', 1 => '初中及以下', 2 => '高中', 3 => '中技', 4 => '中专', 5 => '大专', 6 => '本科', 7 => '硕士', 8 => '博士'];
        $education      = [];
        foreach ($education_data as $k => $v) {
            $item = ['name' => $v];
            if (empty($user_education[$k])) {
                $item['count'] = 0;
            } else {
                $item['count'] = $user_education[$k];
            }
            $education[] = $item;
        }
        $res['eduction'] = $education;


        //注册相关
        $user_gender         = UserModel::field("count(gender),gender")->group('gender')->column('count(gender)', 'gender');
        $res['people_total'] = array_sum($user_gender);
        $res['people_man']   = $user_gender[1];
        $res['people_woman'] = $user_gender[2];
        $res['broker_total'] = BrokerModel::count();

        //岗位相关
        $res['company_total'] = WorkerModel::where('status', 'in', '4,5')->count();
        $res['comjobs_total'] = ComjobsModel::where($comjobs_map)->count();

        //培训相关
        $res['train']      = TrainModel::count();
        $res['train_join'] = TrainJoinModel::count();

        //月注册相关
        $year       = date('Y');
        $month_arr  = [
            strtotime($year . '-01-01'),
            strtotime($year . '-02-01'),
            strtotime($year . '-03-01'),
            strtotime($year . '-04-01'),
            strtotime($year . '-05-01'),
            strtotime($year . '-06-01'),
            strtotime($year . '-07-01'),
            strtotime($year . '-08-01'),
            strtotime($year . '-09-01'),
            strtotime($year . '-10-01'),
            strtotime($year . '-11-01'),
            strtotime($year . '-12-01'),
        ];
        $month_name = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
        $user_month = [];
        foreach ($month_arr as $k => $v) {
            if ($k == 11) {
                $item = [
                    'name'  => $month_name[$k],
                    'count' => UserModel::where('createtime', 'between', [$v, strtotime(($year + 1) . '-01-01')])->count(),
                ];
            } else {
                $item = [
                    'name'  => $month_name[$k],
                    'count' => UserModel::where('createtime', 'between', [$v, $month_arr[$k + 1]])->count(),
                ];
            }
            $user_month[] = $item;
        }

        //倍数
        if ($mul_min != 1 || $mul_max != 1) {
            foreach ($res as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if (!empty($v2['count'])) {
                            $res[$k][$k2]['count'] *= mt_rand($mul_min, $mul_max);
                        }
                    }
                } else {
                    $res[$k] *= mt_rand($mul_min, $mul_max);
                }
            }
        }

        page_result(0, "", $res);
    }

}