<?php

namespace App\Http\Controllers\Api\Test;

use App\Http\Controllers\Api\ApiBaseController;
use App\Models\Category;
use App\Models\Company;
use App\Models\Jobs;
use App\Models\JobsContact;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;

class JobController extends ApiBaseController
{

    public function import(Request $request)
    {
        return '完成';
        $spreadsheet = IOFactory::load(public_path() . '/job.xls');
        $sheet       = $spreadsheet->getActiveSheet();
        $rowCount    = $sheet->getHighestRow();
        $record      = [];
        for ($row = 5; $row <= $rowCount; $row++) {
            if (empty($sheet->getCell("D{$row}")->getValue())) {
                continue;
            }
            $record[] = [
                'company_name' => $sheet->getCell("D{$row}")->getValue(),
                'job_name'     => $sheet->getCell("G{$row}")->getValue(),
                'amount'       => $sheet->getCell("I{$row}")->getValue(),
                'max_age'      => $sheet->getCell("K{$row}")->getValue(),
                'sex'          => $sheet->getCell("L{$row}")->getValue(),
                'education'    => $sheet->getCell("O{$row}")->getValue(),
                'xuewei'       => $sheet->getCell("P{$row}")->getValue(),
                'major'        => $sheet->getCell("Q{$row}")->getValue(),
                'jobs_content' => $sheet->getCell("R{$row}")->getValue(),
                'contacts'     => $sheet->getCell("T{$row}")->getValue(),
            ];
        }
        $company_name_list = array_unique(array_column($record, 'company_name'));
        $company_list      = Company::whereIn('companyname', $company_name_list)->get()->keyBy('companyname');

        $education = Category::categoryTypeByDemand('AIX_education');
        foreach ($record as $k => $v) {
            if (empty($company_list[$v['company_name']])) {
                return $v['company_name'] . '还未创建';
            }

            $item                    = [];
            $item['valid']           = 1;
            $item['jobs_name']       = $v['job_name'];
            $item['company_id']      = $company_list[$v['company_name']]['id'];
            $item['company_name']    = $company_list[$v['company_name']]['companyname'];
            $item['company_addtime'] = strtotime($company_list[$v['company_name']]['created_at']);
            $item['company_audit']   = 1;
            $item['amount']          = $v['amount'];
            $item['topclass']        = 225;
            $item['category']        = 226;
            $item['subclass']        = 992;
            $item['trade']           = $company_list[$v['company_name']]['trade'];
            $item['scale']           = $company_list[$v['company_name']]['scale'];
            $item['district']        = $company_list[$v['company_name']]['district'];
            $item['education']       = $education[$v['education']];
            $item['wage']            = -1;
            $item['age']             = [18, $v['max_age']];
            $item['jobs_content']    = "性别要求:" . $v['sex'] . "学位要求:" . $v['xuewei'] . ";专业要求:" . $v['major'] . $v['jobs_content'] . "\n";
            $item['audit']           = 1;
            $item['setmeal_id']      = 1;
            $item['setmeal_name']    = "免费会员";
            $item['display']         = 1;
            $item['is_health']       = 1;
            $item['health_type']     = 1;
            $jobs_contacts = explode(':',$v['contacts']);
            $jobs = Jobs::create($item);
            JobsContact::create([
                'job_id' => $jobs['id'],
                'contact' => $jobs_contacts[0],
                'telephone' => $company_list[$v['company_name']]['mobile'],
                'landline_tel' => explode('-', $jobs_contacts[1]),
                'address' => $company_list[$v['company_name']]['address'],
                'email' => $company_list[$v['company_name']]['email'],
                'notify_mobile' => 0,
                'contact_show' => 1,
                'telephone_show' => 0,
                'email_show' => 1,
                'landline_tel_show' => 1,
            ]);
        }

        return '完成';
    }
}