<?php

namespace app\common\service;


use app\common\model\RensheCode;
use app\common\model\User as UserModel;
use app\common\model\ComjobsCate as ComjobsCateModel;

class UserService extends BaseService
{

    public function importComjobs($file_url)
    {
        $data           = ['nickname', 'mobile', 'idcard', 'gender', 'birthday', 'address', 'education', 'jobintention', 'workexperience', 'eduexperience', 'emp_time', 'com_cate', 'com_cate_other', 'work_place'];
        $list           = importExecl($file_url, $data, 1);
        $data           = [];
        $gender         = ['男' => 1, '女' => 2];
        $education      = ['初中及以下' => 1, '高中' => 2, '中技' => 3, '中专' => 4, '大专' => 5, '本科' => 6, '硕士' => 7, '博士' => 8];
        $workexperience = ['无经验' => 1, '一年以下' => 2, '1-3年' => 3, '3-5年' => 4, '5-10年' => 5, '10年以上' => 6];
        $jobintention   = ['长期工' => 1, '临时工' => 3];

        $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 = [
                'nickname'       => '姓名',
                'mobile'         => '电话',
                'gender'         => '性别',
                'education'      => '学历',
                'jobintention'   => '求职意向',
                'workexperience' => '工作经验',
            ];
            foreach ($empty_check as $key => $value) {
                if (empty($v[$key])) {
                    return $this->error('第' . ($k + 2) . '行的' . $value . '不能为空');
                }
            }
            $mobile_check = UserModel::where('mobile',$v['mobile'])->find();
            if (!empty($mobile_check)) {
                return $this->error('第' . ($k + 2) . '行的电话已存在');
            }

            $item             = [];
            $item['nickname'] = $item['realname'] = $v['nickname'];
            $item['mobile']   = $v['mobile'];
            $item['idcard']   = $v['idcard'];
            if (empty($gender[$v['gender']])) {
                return $this->error('第' . ($k + 2) . '行的性别选择有误');
            }
            $item['gender']   = $gender[$v['gender']];
            $item['birthday'] = $v['birthday'];
            $item['address']  = $v['address'];
            if (empty($education[$v['education']])) {
                return $this->error('第' . ($k + 2) . '行的学历选择有误');
            }
            $item['education'] = $education[$v['education']];
            if (empty($jobintention[$v['jobintention']])) {
                return $this->error('第' . ($k + 2) . '行的求职意向选择有误');
            }
            $item['jobintention'] = $jobintention[$v['jobintention']];
            if (empty($workexperience[$v['workexperience']])) {
                return $this->error('第' . ($k + 2) . '行的工作经验选择有误');
            }
            $item['workexperience'] = $workexperience[$v['workexperience']];
            $item['eduexperience']  = $v['eduexperience'];
            $item['emp_time']       = [];
            if (!empty($v['emp_time'])) {
                $emp_time = explode(',', str_replace('，', ',', $v['emp_time']));
                foreach ($emp_time as $time) {
                    if (!empty($renshe['emp_time'][$time])) {
                        $item['emp_time'][] = $time;
                    }
                }
            }
            $item['com_cate_type'] = 1;
            $item['com_cate']      = [];
            if (!empty($v['com_cate'])) {
                $com_cate = explode(',', str_replace('，', ',', $v['com_cate']));
                foreach ($com_cate as $cate) {
                    if (!empty($cateList[$cate])) {
                        $item['com_cate'][] = $cate;
                    }
                }
            }
            if (empty($item['com_cate']) && !empty($v['com_cate_other'])) {
                $item['com_cate_type']  = 2;
                $item['com_cate_other'] = $v['com_cate_other'];
            }
            $item['work_place'] = [];
            if (!empty($v['work_place'])) {
                $work_place = explode(',', str_replace('，', ',', $v['work_place']));
                foreach ($work_place as $place) {
                    if (!empty($renshe['community'][$place])) {
                        $item['work_place'][] = $place;
                    }
                }
            }

            $item['groupsid']   = 7;
            $item['status']     = 2;
            $item['authstatus'] = 3;
            $item['createtime'] = time();
            $data[]             = $item;
        }

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

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