<?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();
        $broker_town       = BrokerModel::field("count(town),town")->group('town')->column('count(town)', 'town');
        foreach ($community as &$v) {
            if (!empty($comjobs_community[$v['code']])) {
                $v['count'] = $comjobs_community[$v['code']];
            } else {
                $v['count'] = 0;
            }
            if (!empty($broker_town[$v['name']])) {
                $v['broker_count'] = $broker_town[$v['name']];
            } else {
                $v['broker_count'] = 0;
            }
            unset($v);
        }
        $community[]      = [
            'id'           => 0,
            'name'         => '其他',
            'code'         => '',
            'count'        => $comjobs_community[''] ?? 0,
            'broker_count' => $broker_town[''] ?? 0,
        ];*/
        $community = [
            [
                "id"=> 2,
                "name"=> "青阳街道",
                "code"=> "350582001",
                "count"=> 639,
                "broker_count"=> 8
            ],
            [
                "id"=> 3,
                "name"=> "梅岭街道",
                "code"=> "350582002",
                "count"=> 5487,
                "broker_count"=> 5
            ],
            [
                "id"=> 4,
                "name"=> "西园街道",
                "code"=> "350582003",
                "count"=> 81,
                "broker_count"=> 0
            ],
            [
                "id"=> 5,
                "name"=> "罗山街道",
                "code"=> "350582004",
                "count"=> 542,
                "broker_count"=> 1
            ],
            [
                "id"=> 6,
                "name"=> "新塘街道",
                "code"=> "350582005",
                "count"=> 273,
                "broker_count"=> 1
            ],
            [
                "id"=> 7,
                "name"=> "灵源街道",
                "code"=> "350582006",
                "count"=> 257,
                "broker_count"=> 2
            ],
            [
                "id"=> 8,
                "name"=> "安海镇",
                "code"=> "350582101",
                "count"=> 526,
                "broker_count"=> 94
            ],
            [
                "id"=> 9,
                "name"=> "磁灶镇",
                "code"=> "350582102",
                "count"=> 201,
                "broker_count"=> 0
            ],
            [
                "id"=> 10,
                "name"=> "陈埭镇",
                "code"=> "350582103",
                "count"=> 735,
                "broker_count"=> 0
            ],
            [
                "id"=> 11,
                "name"=> "东石镇",
                "code"=> "350582104",
                "count"=> 2784,
                "broker_count"=> 2
            ],
            [
                "id"=> 12,
                "name"=> "深沪镇",
                "code"=> "350582105",
                "count"=> 315,
                "broker_count"=> 0
            ],
            [
                "id"=> 13,
                "name"=> "金井镇",
                "code"=> "350582106",
                "count"=> 102,
                "broker_count"=> 0
            ],
            [
                "id"=> 14,
                "name"=> "池店镇",
                "code"=> "350582107",
                "count"=> 1024,
                "broker_count"=> 1
            ],
            [
                "id"=> 15,
                "name"=> "内坑镇",
                "code"=> "350582109",
                "count"=> 72,
                "broker_count"=> 1
            ],
            [
                "id"=> 17,
                "name"=> "龙湖镇",
                "code"=> "350582110",
                "count"=> 99,
                "broker_count"=> 0
            ],
            [
                "id"=> 18,
                "name"=> "永和镇",
                "code"=> "350582111",
                "count"=> 175,
                "broker_count"=> 0
            ],
            [
                "id"=> 19,
                "name"=> "英林镇",
                "code"=> "350582112",
                "count"=> 54,
                "broker_count"=> 0
            ],
            [
                "id"=> 20,
                "name"=> "紫帽镇",
                "code"=> "350582113",
                "count"=> 105,
                "broker_count"=> 0
            ],
            [
                "id"=> 21,
                "name"=> "西滨镇",
                "code"=> "350582114",
                "count"=> 9,
                "broker_count"=> 0
            ],
            [
                "id"=> 22,
                "name"=> "经济开发区",
                "code"=> "350582501",
                "count"=> 167,
                "broker_count"=> 0
            ],
            [
                "id"=> 0,
                "name"=> "其他",
                "code"=> "",
                "count"=> 289,
                "broker_count"=> 1
            ]
        ];
        $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);
        }*/
        $cate = [
            [
                "id"=> 26,
                "pid"=> 0,
                "title"=> "生产管理",
                "priority"=> 22,
                "code"=> "2022803",
                "count"=> 363
            ],
            [
                "id"=> 27,
                "pid"=> 0,
                "title"=> "送餐员",
                "priority"=> 0,
                "code"=> "4030200",
                "count"=> 102
            ],
            [
                "id"=> 28,
                "pid"=> 0,
                "title"=> "物流",
                "priority"=> 2,
                "code"=> "2023002",
                "count"=> 33
            ],
            [
                "id"=> 29,
                "pid"=> 0,
                "title"=> "仓储",
                "priority"=> 3,
                "code"=> "4020600",
                "count"=> 246
            ],
            [
                "id"=> 30,
                "pid"=> 0,
                "title"=> "司机",
                "priority"=> 4,
                "code"=> "6300505",
                "count"=> 69
            ],
            [
                "id"=> 31,
                "pid"=> 0,
                "title"=> "家政保洁",
                "priority"=> 5,
                "code"=> "4090801",
                "count"=> 75
            ],
            [
                "id"=> 32,
                "pid"=> 0,
                "title"=> "家政保姆（照顾小孩）",
                "priority"=> 6,
                "code"=> "4100106",
                "count"=> 12
            ],
            [
                "id"=> 33,
                "pid"=> 0,
                "title"=> "家政保姆（照顾正常老人）",
                "priority"=> 7,
                "code"=> "4100106",
                "count"=> 3
            ],
            [
                "id"=> 35,
                "pid"=> 0,
                "title"=> "家政月嫂",
                "priority"=> 9,
                "code"=> "4100106",
                "count"=> 3
            ],
            [
                "id"=> 36,
                "pid"=> 0,
                "title"=> "酒店餐饮服务员",
                "priority"=> 10,
                "code"=> "4030200",
                "count"=> 42
            ],
            [
                "id"=> 37,
                "pid"=> 0,
                "title"=> "酒店餐饮保洁",
                "priority"=> 11,
                "code"=> "4030200",
                "count"=> 6
            ],
            [
                "id"=> 38,
                "pid"=> 0,
                "title"=> "保安",
                "priority"=> 12,
                "code"=> "4070501",
                "count"=> 126
            ],
            [
                "id"=> 39,
                "pid"=> 0,
                "title"=> "水电工",
                "priority"=> 13,
                "code"=> "6310103",
                "count"=> 87
            ],
            [
                "id"=> 40,
                "pid"=> 0,
                "title"=> "工程师",
                "priority"=> 14,
                "code"=> "6310103",
                "count"=> 77
            ],
            [
                "id"=> 41,
                "pid"=> 0,
                "title"=> "大学生实习（兼职）",
                "priority"=> 15,
                "code"=> "4000000",
                "count"=> 115
            ],
            [
                "id"=> 42,
                "pid"=> 0,
                "title"=> "餐饮后厨",
                "priority"=> 16,
                "code"=> "4030200",
                "count"=> 87
            ],
            [
                "id"=> 43,
                "pid"=> 0,
                "title"=> "生活\/ 服务业",
                "priority"=> 17,
                "code"=> "4000000",
                "count"=> 2166
            ],
            [
                "id"=> 44,
                "pid"=> 0,
                "title"=> "市场\/媒介\/广告\/设计",
                "priority"=> 18,
                "code"=> "4000000",
                "count"=> 252
            ],
            [
                "id"=> 45,
                "pid"=> 0,
                "title"=> "发货员",
                "priority"=> 19,
                "code"=> "4000000",
                "count"=> 130
            ],
            [
                "id"=> 46,
                "pid"=> 0,
                "title"=> "车间工人",
                "priority"=> 20,
                "code"=> "6180101",
                "count"=> 125
            ],
            [
                "id"=> 47,
                "pid"=> 0,
                "title"=> "采购",
                "priority"=> 21,
                "code"=> "6180102",
                "count"=> 27
            ],
            [
                "id"=> 48,
                "pid"=> 0,
                "title"=> "缝纫工",
                "priority"=> 22,
                "code"=> "6050103",
                "count"=> 30
            ],
            [
                "id"=> 49,
                "pid"=> 0,
                "title"=> "销售",
                "priority"=> 23,
                "code"=> "6050401",
                "count"=> 57
            ],
            [
                "id"=> 54,
                "pid"=> 0,
                "title"=> "普工",
                "priority"=> 24,
                "code"=> "",
                "count"=> 300
            ],
            [
                "id"=> 55,
                "pid"=> 0,
                "title"=> "行政\/文员",
                "priority"=> 20,
                "code"=> "",
                "count"=> 26
            ]
        ];
        $res['cate'] = $cate;

        //岗位类型
//        $comjobs_type = ComjobsModel::field("count(wtype) count,wtype")->group('wtype')->select();
        $comjobs_type = [
            [
                "count" => 2405,
                "wtype" => 1,
            ],
            [
                "count" => 1157,
                "wtype" => 2,
            ],
            [
                "count" => 306,
                "wtype" => 3,
            ],
            [
                "count" => 124,
                "wtype" => 4,
            ],
            [
                "count" => 567,
                "wtype" => 5,
            ],
        ];
        $res['type']  = $comjobs_type;

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

        //学历
        /*$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;
        }*/
        $education = [
            [
                "name"=> "未知",
                "count"=> 11401
            ],
            [
                "name"=> "初中及以下",
                "count"=> 480
            ],
            [
                "name"=> "高中",
                "count"=> 308
            ],
            [
                "name"=> "中技",
                "count"=> 80
            ],
            [
                "name"=> "中专",
                "count"=> 204
            ],
            [
                "name"=> "大专",
                "count"=> 384
            ],
            [
                "name"=> "本科",
                "count"=> 284
            ],
            [
                "name"=> "硕士",
                "count"=> 20
            ],
            [
                "name"=> "博士",
                "count"=> 16
            ]
        ];
        $res['eduction'] = $education;

        //年龄分布
        /*$user_age_other = UserModel::where('idcard', '')->count();
        $user_idcard    = UserModel::field('idcard')->where('idcard', '<>', '')->select();
        $user_age       = [
            ['name' => '16-25', 'count' => 0],
            ['name' => '25-35', 'count' => 0],
            ['name' => '35-45', 'count' => 0],
            ['name' => '45以上', 'count' => 0],
            ['name' => '其他', 'count' => $user_age_other],
        ];
        foreach ($user_idcard as $v) {
            $age = get_age($v['idcard']);
            if ($age >= 16) {
                $user_age[0]['count']++;
            } elseif ($age >= 25) {
                $user_age[1]['count']++;
            } elseif ($age >= 35) {
                $user_age[2]['count']++;
            } elseif ($age >= 45) {
                $user_age[3]['count']++;
            }
        }*/
        $user_age        = [
            ['name' => '16-25', 'count' => 10981],
            ['name' => '25-35', 'count' => 0],
            ['name' => '35-45', 'count' => 0],
            ['name' => '45以上', 'count' => 0],
            ['name' => '其他', 'count' => 2196],
        ];
        $res['user_age'] = $user_age;

        //注册相关
        /*$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['people_total'] = 13177;
        $res['people_man']   = 9224;
        $res['people_woman'] = 3953;
        $res['broker_total'] = 416;

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

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

        //月注册相关
        $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;
        }
        $res['user_month'] = $user_month;

        //倍数
        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);
    }

    public function agg()
    {
        $res = [];

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

        //数量统计
        $comjobs_map          = [];
        $comjobs_map[]        = ['createtime', '<=', time()];
        $comjobs_map[]        = ['status', 'in', '3,4'];
        $res['company_total'] = WorkerModel::where('status', 'in', '4,5')->count();
        $res['comjobs_total'] = ComjobsModel::where($comjobs_map)->count();
        $res['user_total']    = UserModel::count();

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

}