<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\model\jucai\RecruitAppointInfoModel;
use app\common\model\jucai\RecruitModel;
use app\common\model\jucai\RecruitPostModel;
use app\common\model\odd_job\BrokerModel;
use app\common\model\odd_job\JobModel;
use app\common\model\odd_job\RensheCodeModel;
use app\common\model\odd_job\UserModel;
use app\common\model\odd_job\WorkerModel;
use app\common\model\SettingModel;

class Api extends BaseController
{
    public function talent()
    {
        $system = SettingModel::getConfigValue(SettingModel::TALENT);
        $res    = [
            'talent_level'     => [
                ['name' => '第一层次', 'count' => $system['talent_level_1']],
                ['name' => '第二层次', 'count' => $system['talent_level_2']],
                ['name' => '第三层次', 'count' => $system['talent_level_3']],
                ['name' => '第四层次', 'count' => $system['talent_level_4']],
                ['name' => '第五层次', 'count' => $system['talent_level_5']],
                ['name' => '第六层次', 'count' => $system['talent_level_6']],
                ['name' => '第七层次', 'count' => $system['talent_level_7']],
            ],
            'talent_industry'  => [
                ['name' => '贸易/进出口', 'count' => $system['talent_industry_trade']],
                ['name' => '消费品(食/饮/烟酒)', 'count' => $system['talent_industry_goods']],
                ['name' => '服装/纺织/皮革', 'count' => $system['talent_industry_clothing']],
                ['name' => '制药/生物工程', 'count' => $system['talent_industry_drug']],
                ['name' => '医疗设备/器械', 'count' => $system['talent_industry_medical']],
                ['name' => '酒店/旅游', 'count' => $system['talent_industry_hotel']],
                ['name' => '交通/运输/物流', 'count' => $system['talent_industry_traffic']],
                ['name' => '其他', 'count' => $system['talent_industry_other']],
            ],
            'talent_age'       => [
                ['name' => '30岁以下', 'count' => $system['talent_age_1']],
                ['name' => '30-39岁', 'count' => $system['talent_age_2']],
                ['name' => '40-49岁', 'count' => $system['talent_age_3']],
                ['name' => '50岁以上', 'count' => $system['talent_age_4']],
            ],
            'talent_education' => [
                ['name' => '硕士', 'count' => $system['talent_education_1']],
                ['name' => '博士', 'count' => $system['talent_education_2']],
                ['name' => '博士后', 'count' => $system['talent_education_3']],
            ],
            'total_count'      => [
                ['name' => '硕博人才数', 'count' => $system['talent_total']],
            ],
        ];

        return json($res);
    }

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

        //街道数据
        $comjobs_community = JobModel::field("count(community),community")->group('community')->column('count(community)', 'community');
        $community         = RensheCodeModel::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'] = JobModel::where($comjobs_map)->count();
        $res['user_total']    = UserModel::count();

        return json($res);
    }

    public function recruit()
    {
        $data = [];

        //招考数据
        $data['recruit_count'] = RecruitModel::count();
        $data['recruit_post']  = RecruitPostModel::count();
        $data['recruit_apply'] = RecruitAppointInfoModel::count();

        //学历和年龄
        $recruit_apply_data = RecruitAppointInfoModel::field(['birthday', 'education'])->select();
        $recruit_education  = [
            '专科' => ['name' => '专科', 'count' => 0],
            '本科' => ['name' => '本科', 'count' => 0],
            '硕士' => ['name' => '硕士', 'count' => 0],
            '博士' => ['name' => '博士', 'count' => 0],
            '其他' => ['name' => '其他', 'count' => 0],
        ];
        $recruit_age        = [
            '30岁及以下' => ['name' => '30岁及以下', 'count' => 0],
            '30到35岁' => ['name' => '30到35岁', 'count' => 0],
            '35到40岁' => ['name' => '35到40岁', 'count' => 0],
            '40岁以上'  => ['name' => '40岁以上', 'count' => 0],
        ];
        $year               = date('Y');
        foreach ($recruit_apply_data as $v) {
            if (!empty($recruit_education[$v['education']])) {
                $recruit_education[$v['education']]['count']++;
            } else {
                $recruit_education['其他']['count']++;
            }

            $birth = mb_substr($v['birthday'], 0, 4);
            if ($birth >= ($year - 30)) {
                $recruit_age['30岁及以下']['count']++;
            } elseif ($birth >= ($year - 35) && $birth < ($year - 30)) {
                $recruit_age['30到35岁']['count']++;
            } elseif ($birth >= ($year - 40) && $birth < ($year - 35)) {
                $recruit_age['35到40岁']['count']++;
            } else {
                $recruit_age['40岁以上']['count']++;
            }
        }

        $data['recruit_education'] = array_values($recruit_education);
        $data['recruit_age']       = array_values($recruit_age);

        return json($data);
    }
}