<?php

namespace app\mainapp\controller;

use app\common\model\ComjobsCate as ComjobsCateModel;
use app\common\model\ResumeInvite as ResumeInviteModel;
use app\common\model\User as UserModel;
use app\common\model\UserWill as UserWillModel;
use app\mainapp\BaseController;
use chuanglan\Chuanglan;

class Resume extends BaseController
{

    /**
     * 简历列表
     */
    public function listResume()
    {
        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);

        $model = new UserModel();
        $model = $model->where('status', 2)->where('authstatus', '=', 3);

        $jobintention = input('jobintention/d', 0);
        if (!empty($jobintention)) {
            $model = $model->where('jobintention', $jobintention);
        }
        $cateid = input('cateid/d', 0);
        if (!empty($cateid)) {
            $cate_name = ComjobsCateModel::where('id', $cateid)->value('title');
            if (!empty($cate_name)) {
                $model->whereRaw("json_contains(com_cate,CONCAT('\"',:name,'\"'))", ['name' => $cate_name]);
            }
        }

        $plist = $model->page($ppage)->limit($psize)->append(['jobintention_text', 'education_text', 'worker_text'])->select();
        $year  = date('Y');
        foreach ($plist as $v) {
            if ($v['com_cate_type'] == 2) {
                if (empty($v['com_cate_other'])) {
                    $v['com_cate'] = [];
                } else {
                    $v['com_cate'] = [$v['com_cate_other']];
                }
            }
            if (!empty($v['birthday'])) {
                $v['age'] = $year - date('Y', strtotime($v['birthday']));
            } else {
                $v['age'] = 0;
            }
        }

        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    /**
     * 简历搜索条件
     */
    public function searchItem()
    {
        $catelist = ComjobsCateModel::field('id as value, title, priority')->order(['priority' => 'desc', 'id' => 'desc'])
            ->select()->toArray();
        array_unshift($catelist, ['value' => 0, 'title' => '全部']);
        $jobintentionlist = UserWillModel::field('id as value, title')->select()->toArray();
        array_unshift($jobintentionlist, ['value' => 0, 'title' => '全部']);
        page_result(0, "", [
            'catelist'         => $catelist,
            'jobintentionlist' => $jobintentionlist,
        ]);
    }

    /**
     * 简历详情
     */
    public function detail()
    {
        //简历信息
        $id = input('id/d', 0);
        if (empty($id)) {
            page_result(1, "简历不存在。");
        }

        $info = UserModel::where('id', $id)->find();
        if ($info->isEmpty()) {
            page_result(1, "简历不存在。");
        }

        if ($info->status != 2 || $info->authstatus != 3) {
            page_result(1, "简历未通过系统验证。");
        }

        $info->append(['jobintention_text', 'education_text', 'worker_text']);
        $info->volume++;
        $info->save();

        //邀请记录
        $workerid = input('workerid/d', 0);
        $invite   = ResumeInviteModel::where('userid', $id)->where('workerid', $workerid)->find();

        page_result(0, "", [
            'info'   => $info,
            'invite' => $invite,
        ]);
    }

    /**
     * 邀请面试
     */
    public function invite()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);

        $check = ResumeInviteModel::where('workerid', $workerid)->where('userid', $userid)->find();
        if (!empty($check)) {
            page_result(1, "请勿重复邀请", $check);
        }

        $info = ResumeInviteModel::create([
            'workerid'   => $workerid,
            'userid'     => $userid,
            'createtime' => time(),
        ]);

        $user = UserModel::where('id', $userid)->find();
        if ($user['mobile']) {
            $msg    = '尊敬的求职者你好！您已在“晋江人力”小程序上收到雇主邀请，请您及时查看处理！';
            $mobile = $user['mobile'];
            $sms    = new Chuanglan();
            $sms->send($mobile, ['message' => $msg]);
        }

        page_result(0, "邀请成功", $info);
    }

    /**
     *  收到的邀请
     */
    public function userLog()
    {
        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);

        $userid = input('userid/d', 0);
        $list   = ResumeInviteModel::with(['worker'])->where('userid', $userid)->page($ppage)->limit($psize)->append(['status_text'])->select();

        page_result(0, "", [
            'list'   => $list,
            'status' => $psize > count($list) ? 'noMore' : 'more',
        ]);
    }

    /**
     * 处理邀请
     */
    public function dealInvite()
    {
        $id     = input('id/d', 0);
        $status = input('status/d', 0);
        if (empty($id) || empty($status)) {
            page_result(1, "参数错误");
        }

        $info = ResumeInviteModel::where('id', $id)->find();
        if ($info['status'] != 1) {
            page_result(1, "请勿重复操作");
        }

        $info->status = $status;
        $info->save();

        page_result(0, "操作成功", $info);
    }

    /**
     * 发出的邀请
     */
    public function workerLog()
    {
        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);

        $workerid = input('workerid/d', 0);
        $list     = ResumeInviteModel::with(['user'])->where('workerid', $workerid)->page($ppage)->limit($psize)->append(['status_text'])->select();

        page_result(0, "", [
            'list'   => $list,
            'status' => $psize > count($list) ? 'noMore' : 'more',
        ]);
    }
}