<?php

namespace app\mainapp\controller;

use app\common\model\WorkerThird;
use app\mainapp\BaseController;

use app\common\model\Comjobs as ComjobsModel;
use app\common\model\ComjobsCate as ComjobsCateModel;
use app\common\model\ComjobsLog as ComjobsLogModel;
use app\common\model\ComjobsReport as ComjobsReportModel;
use app\common\model\Param as ParamModel;
use app\common\model\User as UserModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\UserIntegral as UserIntegralModel;

use app\common\validate\Comjobs as ComjobsValidate;
use think\exception\ValidateException;

use think\facade\Db;

class Wcomjobs extends BaseController
{


    public function pageReport()
    {
        $workerid    = input('workerid/d', 0);
        $comjobslist = ComjobsModel::where(['workerid' => $workerid])->order(['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'])->field('id as value, title, priority')->select()->toArray();
        array_unshift($comjobslist, ['value' => 0, 'title' => '全部']);

        // $catelist = DemandCateModel::field('id as value, title, priority')->order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
        // array_unshift( $catelist, array('value'=>0,'title'=>'全部') );

        page_result(0, "", [
            'comjobslist' => $comjobslist,
            'statuslist'  => [['value' => 0, 'title' => "全部"], ['value' => 1, 'title' => "待审核"], ['value' => 2, 'title' => "待面试"], ['value' => 3, 'title' => "已入职"], ['value' => 4, 'title' => "无效报备"]],
        ]);
    }

    public function listReport()
    {
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $workerid = input('workerid/d', 0);
        $map[]    = ['workerid', '=', $workerid];
        $status   = input('status/d', 0);
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $comjobsid = input('comjobsid/d', 0);
        if (!empty($comjobsid)) {
            $map[] = ['comjobsid', '=', $comjobsid];
        }
        $plist = ComjobsReportModel::with(['comjobs', 'broker'])->where($map)->order(['createtime' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function setReportStatus()
    {
        $workerid = input('workerid/d', 0);
        $id       = input('id/d', 0);
        $report   = ComjobsReportModel::with(['comjobs', 'broker'])->where(['workerid' => $workerid, 'id' => $id])->append(['status_text'])->findOrEmpty();
        if ($report->isEmpty()) {
            page_result(1, "报备信息不存在。");
        }
        $status    = input('status/d', 1);
        $retremark = $status == 4 ? input('retremark/s', "") : "";
        $report->save([
            'status'    => $status,
            'retremark' => $retremark,
        ]);
        page_result(0, "", [
            'report' => $report,
        ]);
    }


    public function pageComlog()
    {
        $workerid    = input('workerid/d', 0);
        $comjobslist = ComjobsModel::where(['workerid' => $workerid])->order(['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'])->field('id as value,title')->select()->toArray();
        array_unshift($comjobslist, ['value' => 0, 'title' => '全部']);
        page_result(0, "", [
            'comjobslist' => $comjobslist,
            'statuslist'  => [['value' => 0, 'title' => "全部"], ['value' => 1, 'title' => "未跟进"], ['value' => 2, 'title' => "未面试"], ['value' => 3, 'title' => "面试通过"], ['value' => 4, 'title' => "面试未通过"], ['value' => 5, 'title' => "用户放弃"], ['value' => 6, 'title' => "已入职"], ['value' => 7, 'title' => "已离职"]],
        ]);
    }

    public function listComlog()
    {
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $workerid = input('workerid/d', 0);
        $map[]    = ['workerid', '=', $workerid];
        $status   = input('status/d', 0);
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $comjobsid = input('comjobsid/d', 0);
        if (!empty($comjobsid)) {
            $map[] = ['comjobsid', '=', $comjobsid];
        }
        $plist = ComjobsLogModel::with(['comjobs', 'user'])->where($map)->order(['createtime' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function delLog()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);
        $user     = UserModel::where(1)->findOrEmpty($userid);
        $worker   = WorkerModel::where(['userid' => $userid])->findOrEmpty($workerid);
        if ($user->isEmpty() || $worker->isEmpty()) {
            page_result(1, "用户或公司信息不存在。");
        }
        $logid      = input('logid/d', 0);
        $comjobslog = ComjobsLogModel::where(['workerid' => $workerid])->findOrEmpty($logid);
        if ($comjobslog->isEmpty()) {
            page_result(1, "报名记录信息不存在。");
        }
        $comjobslog->delete();
        page_result(0, "", []);
    }

    public function statusLog()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);
        $user     = UserModel::where(1)->findOrEmpty($userid);
        $worker   = WorkerModel::where(['userid' => $userid])->findOrEmpty($workerid);
        if ($user->isEmpty() || $worker->isEmpty()) {
            page_result(1, "用户或公司信息不存在。");
        }
        $logid      = input('logid/d', 0);
        $comjobslog = ComjobsLogModel::with(['comjobs', 'user', 'comjobs.comjobsCate'])->where(['workerid' => $workerid])->append(['status_text'])->findOrEmpty($logid);
        if ($comjobslog->isEmpty()) {
            page_result(1, "报名记录信息不存在。");
        }
        if (!empty($comjobslog['comjobs']['third_id'])) {
            $third = WorkerThird::where('id', $comjobslog['comjobs']['third_id'])->find();
            /*if (empty($third['card_no'])) {
                page_result(1, "雇主身份证号不能为空。");
            }*/
            $comjobslog['comjobs']['third'] = $third;
        }
        /*else {
            if (empty($worker['card_no'])) {
                page_result(1, "公司证件号不能为空。");
            }
        }*/

        $status = input('status/d', 1);
        $comjobslog->save(['status' => $status,]);

        //同步人社局
        $comjobslog['worker'] = $worker;
        event('JobMatch', $comjobslog);

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


    public function statusComjobs()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);
        $user     = UserModel::where(1)->findOrEmpty($userid);
        $worker   = WorkerModel::where(['userid' => $userid])->findOrEmpty($workerid);
        if ($user->isEmpty() || $worker->isEmpty()) {
            page_result(1, "用户或公司信息不存在。");
        }
        $comjobsid = input('comjobsid/d', 0);
        $comjobs   = ComjobsModel::where(1)->findOrEmpty($comjobsid);
        if ($comjobs->isEmpty()) {
            page_result(1, "招聘信息不存在。");
        }
        $status = input('status/d', 3);
        if ($status == 3 && time() > strtotime($comjobs->enddate) + 86400) {
            page_result(1, "招聘时间已截止，请先编辑招聘截止日期。");
        }
        $comjobs->save([
            'status' => $status,
        ]);
        page_result(0, "", []);
    }

    public function updateComjobs()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);
        $user     = UserModel::where(1)->findOrEmpty($userid);
        $worker   = WorkerModel::where(['userid' => $userid])->findOrEmpty($workerid);
        if ($user->isEmpty() || $worker->isEmpty()) {
            page_result(1, "用户或公司信息不存在。");
        }
        $param = ParamModel::where(1)->findOrEmpty();
        if ($user->integral < $param->topcomjobs) {
            page_result(1, "每次置顶招聘信息需要扣除" . $param->topcomjobs . "聘豆，你当前聘豆不足。");
        }
        $comjobsid = input('comjobsid/d', 0);
        $comjobs   = ComjobsModel::where(1)->findOrEmpty($comjobsid);
        if ($comjobs->isEmpty()) {
            page_result(1, "招聘信息不存在。");
        }
        $comjobs->save(['updatetime' => time()]);
        $intdata = [
            'userid'      => $userid,
            'title'       => "置顶招聘信息扣除",
            'intvalue'    => 0 - $param->topcomjobs,
            'intmoney'    => 0.00,
            'onlycontent' => "",
            'remark'      => input('title/s', ""),
            'itype'       => 4,
            'status'      => 2,
            'createtime'  => date("Y-m-d H:i:s"),
            'yeartime'    => date("Y"),
            'monthtime'   => date("Ym"),
        ];
        UserIntegralModel::create($intdata);
        $integral = intval($user->integral) - intval($param->topcomjobs);
        $user->save([
            'integral' => $integral,
        ]);
        page_result(0, "", []);
    }

    public function listComjobs()
    {
        $status   = input('status/d', 1);
        $workerid = input('workerid/d', 0);
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $map[]    = ['workerid', '=', $workerid];
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $orderby = ['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'];
        $plist   = ComjobsModel::with(['comjobsCate'])->where($map)->order($orderby)->page($ppage)->limit($psize)->select();
        $param   = ParamModel::where(1)->findOrEmpty();
        page_result(0, "", [
            'param'   => $param,
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function getComjobs()
    {
        $workerid  = input('workerid/d', 0);
        $comjobsid = input('comjobsid/d', 0);
        $comjobs   = ComjobsModel::where('workerid', '=', $workerid)->where('id', '=', $comjobsid)->findOrEmpty();
        if ($comjobs->isEmpty()) {
            $comjobs = "NULL";
        } else {
            $comjobs->tags = implode(" ", $comjobs->tags);
        }
        $catelist     = ComjobsCateModel::order(['priority' => 'desc', 'id' => 'desc'])->select()->toArray();
        $comjobscount = ComjobsModel::where('workerid', '=', $workerid)->count();
        page_result(0, "", [
            'comjobs'      => $comjobs,
            'catelist'     => $catelist,
            'comjobscount' => $comjobscount,
        ]);
    }


    public function editComjobs()
    {
        $userid   = input('userid/d', 0);
        $workerid = input('workerid/d', 0);
        $user     = UserModel::where(1)->findOrEmpty($userid);
        $worker   = WorkerModel::where(['userid' => $userid])->findOrEmpty($workerid);
        if ($user->isEmpty() || $worker->isEmpty()) {
            page_result(1, "用户或公司信息不存在。");
        }
        $param            = ParamModel::where(1)->findOrEmpty();
        $id               = input('id/d', 0);
        $wtype            = input('wtype/d', 1);
        $zwagall          = $wtype == 1 ? input('zwagall/s', "") : '';
        $picall           = input('picall/s', "");
        $recruitment_cate = input('recruitment_cate/d', 1);
        $data             = [
            'workerid'         => input('workerid/d', 0),
            'title'            => input('title/s', ""),
            'cateid'           => input('cateid/d', 0),
            'province'         => input('province/s', ""),
            'city'             => input('city/s', ""),
            'district'         => input('district/s', ""),
            'address'          => input('address/s', ""),
            'agegroup'         => input('agegroup/s', ""),
            'tags'             => explode(" ", input('tags/s', "")),
            'enddate'          => input('enddate/s', ""),
            'requirement'      => input('requirement/s', ""),
            'comdetails'       => input('comdetails/s', ""),
            'retmoney'         => $recruitment_cate == 2 ? input('retmoney/d', 0) : 0,
            'picall'           => empty($picall) ? [] : explode(",", $picall),
            'video'            => input('video/s', ""),
            'companydetails'   => input('companydetails/s', ""),
            'wtype'            => $wtype,
            'bwagall'          => input('bwagall/s', ""),
            'zwagall'          => $zwagall,
            'fwagall'          => input('fwagall/s', ""),
            'telephone'        => input('telephone/s', ""),
            'remark'           => input('remark/s', ""),
            'recruitment_cate' => $recruitment_cate,
            'latitude'         => input('tlatitude/s', ""),
            'longitude'        => input('tlongitude/s', ""),
        ];
        try {
            validate(ComjobsValidate::class)->check($data);
        } catch (ValidateException $e) {
            page_result(1, $e->getError());
        }
        if ($id == 0) {
            $data = array_merge($data, [
                'status'     => 1,
                'priority'   => 0,
                'updatetime' => date("Y-m-d H:i:s"),
                'createtime' => date("Y-m-d H:i:s"),
                'volume'     => 0,
            ]);
            if ($recruitment_cate == 1 && $user->integral < $param->addcomjobs) {
                page_result(1, "每次发布普通招聘信息需要扣除" . $param->addcomjobs . "聘豆，你当前聘豆不足。");
            } elseif ($recruitment_cate == 2 && $user->integral < $param->addcomjobswyp) {
                page_result(1, "每次发布无忧招聘信息需要扣除" . $param->addcomjobswyp . "聘豆，你当前聘豆不足。");
            }
            $comjobs = ComjobsModel::create($data);
            $intdata = [
                'userid'      => $userid,
                'title'       => $recruitment_cate == 1 ? "发布普通招聘信息扣除" : "发布无忧招聘信息扣除",
                'intvalue'    => $recruitment_cate == 1 ? (0 - $param->addcomjobs) : (0 - $param->addcomjobswyp),
                'intmoney'    => 0.00,
                'onlycontent' => "",
                'remark'      => input('title/s', ""),
                'itype'       => 4,
                'status'      => 2,
                'createtime'  => date("Y-m-d H:i:s"),
                'yeartime'    => date("Y"),
                'monthtime'   => date("Ym"),
            ];
            UserIntegralModel::create($intdata);
            if ($recruitment_cate == 1) {
                $integral = intval($user->integral) - intval($param->addcomjobs);
            } else {
                $integral = intval($user->integral) - intval($param->addcomjobswyp);
            }
            $user->save([
                'integral' => $integral,
            ]);
        } else {
            $comjobs = ComjobsModel::where('id', '=', $id)->findOrEmpty();
            $comjobs->save($data);
        }
        page_result(0, "", [
            'comjobs' => $comjobs,
        ]);
    }

    //经纪人
    public function brokerPageReport()
    {
        $userid = input('userid/d', 0);

        if (!empty($userid)) {
            $worker_data = Db::name('worker')->field('id,userid')->where('userid', $userid)->find();
        }
        $comjobslist = ComjobsModel::where(['workerid' => $worker_data['id']])->order(['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'])->field('id as value,title')->select()->toArray();
        array_unshift($comjobslist, ['value' => 0, 'title' => '全部']);
        page_result(0, "", [
            'comjobslist' => $comjobslist,
            'statuslist'  => [['value' => 0, 'title' => "全部"], ['value' => 1, 'title' => "待审核"], ['value' => 2, 'title' => "待面试"], ['value' => 3, 'title' => "已入职"], ['value' => 4, 'title' => "无效报备"]],
        ]);
    }

    ////经纪人
    public function brokerListReport()
    {
        $ppage  = input('ppage/d', 1);
        $psize  = input('psize/d', 20);
        $map    = [];
        $userid = input('userid/d', 0);
        if (!empty($userid)) {
            $worker_data = Db::name('worker')->field('id,userid')->where('userid', $userid)->find();
        }

        $map[]  = ['workerid', '=', $worker_data['id']];
        $status = input('status/d', 0);
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $comjobsid = input('comjobsid/d', 0);
        if (!empty($comjobsid)) {
            $map[] = ['comjobsid', '=', $comjobsid];
        }
        $plist = ComjobsReportModel::with(['comjobs', 'broker'])->where($map)->order(['createtime' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }
}