<?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);
    }

    public function odd_job_other()
    {
        $setting = SettingModel::getConfigValue(SettingModel::ODD_JOB);

        return json($setting);
    }

    public function bi_talent()
    {
        $talent   = SettingModel::getConfigValue(SettingModel::TALENT);
        $industry = SettingModel::getConfigValue(SettingModel::INDUSTRY);

        $data = [];
        //人才数据
        $data['talent_total']         = $talent['talent_total'];
        $data['talent_problem_total'] = $talent['talent_problem_total'];
        $data['talent_problem_deal']  = $talent['talent_problem_deal'];

        //产业园机构
        $data['industry_institution_num']      = $industry['industry_institution_num'];
        $data['industry_institution_turnover'] = $industry['industry_institution_turnover'];
        $data['industry_institution_employee'] = $industry['industry_institution_employee'];

        //产业园服务数据
        $data['industry_enterprise_num'] = $industry['industry_enterprise_num'];
        $data['industry_service_num']    = $industry['industry_service_num'];

        //人才问题分类
        $data['talent_problem_cate'] = [
            ['name' => '人才津贴', 'count' => $talent['talent_problem_talent']],
            ['name' => '人才认定', 'count' => $talent['talent_problem_determine']],
            ['name' => '交通补贴', 'count' => $talent['talent_problem_traffic']],
            ['name' => '购房补贴', 'count' => $talent['talent_problem_house']],
            ['name' => '人才活动', 'count' => $talent['talent_problem_activity']],
            ['name' => '子女就学', 'count' => $talent['talent_problem_children']],
            ['name' => '配偶就业', 'count' => $talent['talent_problem_spouse']],
            ['name' => '其他问题', 'count' => $talent['talent_problem_other']],
        ];

        //人才层次
        $data['talent_level'] = [
            ['name' => '第一层次', 'count' => $talent['talent_level_1']],
            ['name' => '第二层次', 'count' => $talent['talent_level_2']],
            ['name' => '第三层次', 'count' => $talent['talent_level_3']],
            ['name' => '第四层次', 'count' => $talent['talent_level_4']],
            ['name' => '第五层次', 'count' => $talent['talent_level_5']],
            ['name' => '第六层次', 'count' => $talent['talent_level_6']],
            ['name' => '第七层次', 'count' => $talent['talent_level_7']],
        ];

        //产业园机构类型
        $data['industry_institution_type'] = [
            ['name' => '软件技术', 'count' => $industry['industry_institution_type_software']],
            ['name' => '管理咨询', 'count' => $industry['industry_institution_type_consult']],
            ['name' => '培训教育', 'count' => $industry['industry_institution_type_train']],
            ['name' => '灵活用工', 'count' => $industry['industry_institution_type_flexible']],
            ['name' => '派遣外包', 'count' => $industry['industry_institution_type_dispatch']],
            ['name' => '人才猎聘', 'count' => $industry['industry_institution_type_talent']],
            ['name' => '测评背调', 'count' => $industry['industry_institution_type_evaluation']],
            ['name' => '劳务输出', 'count' => $industry['industry_institution_type_labor']],
            ['name' => '商业保险', 'count' => $industry['industry_institution_type_insurance']],
            ['name' => '其他', 'count' => $industry['industry_institution_type_other']],
        ];

        //人才分布
        $data['talent_industry']  = [
            ['name' => '贸易/进出口', 'count' => $talent['talent_industry_trade']],
            ['name' => '消费品(食/饮/烟酒)', 'count' => $talent['talent_industry_goods']],
            ['name' => '服装/纺织/皮革', 'count' => $talent['talent_industry_clothing']],
            ['name' => '制药/生物工程', 'count' => $talent['talent_industry_drug']],
            ['name' => '医疗设备/器械', 'count' => $talent['talent_industry_medical']],
            ['name' => '酒店/旅游', 'count' => $talent['talent_industry_hotel']],
            ['name' => '交通/运输/物流', 'count' => $talent['talent_industry_traffic']],
            ['name' => '其他', 'count' => $talent['talent_industry_other']],
        ];
        $data['talent_age']       = [
            ['name' => '30岁以下', 'count' => $talent['talent_age_1']],
            ['name' => '30-39岁', 'count' => $talent['talent_age_2']],
            ['name' => '40-49岁', 'count' => $talent['talent_age_3']],
            ['name' => '50岁以上', 'count' => $talent['talent_age_4']],
        ];
        $data['talent_education'] = [
            ['name' => '硕士', 'count' => $talent['talent_education_1']],
            ['name' => '博士', 'count' => $talent['talent_education_2']],
            ['name' => '博士后', 'count' => $talent['talent_education_3']],
        ];

        //人才咨询渠道
        $data['talent_channel'] = [
            ['name' => '来电咨询', 'count' => $talent['talent_channel_call']],
            ['name' => '微信咨询', 'count' => $talent['talent_channel_wechat']],
            ['name' => '上门拜访', 'count' => $talent['talent_channel_door']],
            ['name' => '现场咨询', 'count' => $talent['talent_channel_scene']],
            ['name' => '其他渠道', 'count' => $talent['talent_channel_other']],
        ];

        return json($data);
    }
}
