<?php

namespace app\common\service;


use app\common\model\RensheCode;
use app\common\model\Comjobs as ComjobsModel;
use app\common\model\ComjobsCate as ComjobsCateModel;

class ComjobsService extends BaseService
{

    public function importComjobs($file_url, $worker_id)
    {
        $data       = ['title', 'cate', 'address', 'community', 'agegroup', 'enddate', 'comdetails', 'requirement', 'wtype', 'zwagall', 'telephone', 'remark', 'recruit_num', 'education'];
        $list       = importExecl($file_url, $data, 1);
        $data       = [];
        $wtype      = ['按月' => 1, '按时' => 2, '按件' => 3, '按项目' => 4, '其他' => 5];
        $rensheCode = RensheCode::select();
        $renshe     = [];
        foreach ($rensheCode as $v) {
            $renshe[$v['type']][$v['name']] = $v['code'];
        }
        $cateList = ComjobsCateModel::column('id', 'title');

        foreach ($list as $k => $v) {
            $empty_check = [
                'title'       => '标题',
                'cate'        => '岗位',
                'address'     => '地址',
                'community'   => '社区',
                'agegroup'    => '招工年龄',
                'enddate'     => '截止日期',
                'wtype'       => '结算类型',
                'zwagall'     => '薪酬',
                'telephone'   => '咨询电话',
                'recruit_num' => '招聘人数',
                'education'   => '最低学历',
            ];
            foreach ($empty_check as $key => $value) {
                if (empty($v[$key])) {
                    return $this->error('第' . ($k + 2) . '行的' . $value . '不能为空');
                }
            }

            $item = [];
            if (empty($cateList[$v['cate']])) {
                return $this->error('第' . ($k + 2) . '行的岗位选择有误');
            }
            $item['cateid']   = $cateList[$v['cate']];
            $item['title']    = $v['title'];
            $item['province'] = '福建省';
            $item['city']     = '泉州市';
            $item['district'] = '晋江市';
            $item['address']  = $v['address'];
            if (empty($renshe['community'][$v['community']])) {
                return $this->error('第' . ($k + 2) . '行的社区选择有误');
            }
            $item['community'] = $renshe['community'][$v['community']];
            $item['agegroup']  = $v['agegroup'];
            $enddate           = strtotime($v['enddate']);
            if ($enddate <= 0) {
                return $this->error('第' . ($k + 2) . '行的截止日期填写错误，格式2022-10-20');
            }
            $item['enddate'] = strtotime($v['enddate']);

            $item['comdetails']  = $v['comdetails'];
            $item['requirement'] = $v['requirement'];
            if (empty($wtype[$v['wtype']])) {
                return $this->error('第' . ($k + 2) . '行的结算类型选择有误');
            }
            $item['wtype']   = $wtype[$v['wtype']];
            $item['zwagall'] = $item['bwagall'] = $v['zwagall'];
            if (empty($renshe['education'][$v['education']])) {
                return $this->error('第' . ($k + 2) . '行的最低学历选择有误');
            }
            $item['telephone'] = $v['telephone'];
            $item['remark']    = $v['remark'];
            if ($v['recruit_num'] < 1) {
                return $this->error('第' . ($k + 2) . '行的招聘人数需要大于等于1');
            }
            $item['recruit_num']    = (int)$v['recruit_num'];
            $item['education']      = $v['education'];
            $item['is_worry']       = 0;
            $item['picall']         = [];
            $item['tags']           = [];
            $item['emp_time']       = [];
            $item['companydetails'] = '';
            $item['video']          = '';
            $item['priority']       = 0;
            $item['status']         = 3;
            $item['createtime']     = $item['updatetime'] = time();
            $item['workerid']       = $worker_id;
            $data[]                 = $item;
        }

        foreach ($data as $v) {
            ComjobsModel::create($v);
        }

        return $this->success('导入成功');
    }
}