<?php

namespace app\mainapp\controller;

use app\mainapp\BaseController;

use app\common\model\Demand as DemandModel;
use app\common\model\DemandCate as DemandCateModel;
use app\common\model\DemandLog as DemandLogModel;
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\model\DemandSnatch as DemandSnatchModel;

use app\common\validate\Demand as DemandValidate;
use think\exception\ValidateException;

class Wdemand extends BaseController
{

    public function pageWdemandLog()
    {
        $demandid = input('demandid/d', 0);
        $workerid = input('workerid/d', 0);
        $demand   = DemandModel::where(['workerid' => $workerid])->append(['ftype_text', 'wtype_text'])->findOrEmpty($demandid);
        if ($demand->isEmpty()) {
            page_result(1, "招聘订单信息不存在。");
        }
        $logcount = DemandSnatchModel::where(['demand_id' => $demandid])->where('status','>=',2)->count();
        page_result(0, "", [
            'demand'   => $demand,
            'logcount' => $logcount,
        ]);
    }

    /*public function wdemandLogList()
    {
        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);
        $map = array();
        $workerid = input('workerid/d', 0);
        $map[] = ['workerid','=',$workerid];
        $demandid = input('demandid/d', 0);
        $map[] = ['demandid','=',$demandid];
        $plist = DemandLogModel::with(['gworker'])->where($map)->order(['createtime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->select();
        page_result(0, "", array(
            'plist'		=> $plist,
            'pstatus'	=> $psize > count($plist) ? 'noMore' : 'more'
            ));
    }*/

    public function wdemandLogList()
    {
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $demandid = input('demandid/d', 0);
        $map[]    = ['demand_id', '=', $demandid];
        $map[]    = ['status', '>=', 2];
        $plist    = DemandSnatchModel::with(['worker'])->where($map)->order(['createtime' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function pageComlog()
    {
        $workerid   = input('workerid/d', 0);
        $demandlist = DemandModel::where(['workerid' => $workerid])->order(['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'])->field('id as value,title')->select()->toArray();
        array_unshift($demandlist, ['id' => 0, 'value' => '全部']);
        page_result(0, "", [
            'demandlist' => $demandlist,
            '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];
        }
        $demandid = input('demandid/d', 0);
        if (!empty($demandid)) {
            $map[] = ['demandid', '=', $demandid];
        }
        $plist = DemandLogModel::with(['demand', '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);
        $demandlog = DemandLogModel::where(['workerid' => $workerid])->findOrEmpty($logid);
        if ($demandlog->isEmpty()) {
            page_result(1, "报名记录信息不存在。");
        }
        $demandlog->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);
        $demandlog = DemandLogModel::with(['demand', 'user'])->where(['workerid' => $workerid])->append(['status_text'])->findOrEmpty($logid);
        if ($demandlog->isEmpty()) {
            page_result(1, "报名记录信息不存在。");
        }
        $status = input('status/d', 1);
        $demandlog->save([
            'status' => $status,
        ]);
        page_result(0, "", [
            'demandlog' => $demandlog,
        ]);
    }


    public function statusDemand()
    {
        $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, "用户或公司信息不存在。");
        }
        $demandid = input('demandid/d', 0);
        $demand   = DemandModel::where(1)->findOrEmpty($demandid);
        if ($demand->isEmpty()) {
            page_result(1, "招聘订单信息不存在。");
        }
        $status = input('status/d', 3);
        if ($status == 3 && time() > strtotime($demand->enddate) + 86400) {
            page_result(1, "招聘时间已截止，请先编辑招聘订单截止日期。");
        }
        $demand->save([
            'status' => $status,
        ]);
        page_result(0, "", []);
    }

    public function updateDemand()
    {
        $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->topdemand) {
            page_result(1, "每次置顶招聘订单信息需要扣除" . $param->topdemand . "积分，你当前积分不足。");
        }
        $demandid = input('demandid/d', 0);
        $demand   = DemandModel::where(1)->findOrEmpty($demandid);
        if ($demand->isEmpty()) {
            page_result(1, "招聘信息不存在。");
        }
        $demand->save(['updatetime' => time()]);
        $intdata = [
            'userid'      => $userid,
            'title'       => "置顶招聘订单信息扣除",
            'intvalue'    => 0 - $param->topdemand,
            'intmoney'    => 0.00,
            'onlycontent' => "",
            'remark'      => input('title/s', ""),
            'itype'       => 5,
            '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->topdemand);
        $user->save([
            'integral' => $integral,
        ]);
        page_result(0, "", []);
    }

    public function listDemand()
    {
        $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   = DemandModel::with(['demandCate'])->where($map)->order($orderby)->page($ppage)->limit($psize)->append(['ftype_text'])->select();
        $param   = ParamModel::where(1)->findOrEmpty();
        page_result(0, "", [
            'param'   => $param,
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function getDemand()
    {
        $workerid = input('workerid/d', 0);
        $demandid = input('demandid/d', 0);
        $demand   = DemandModel::where('workerid', '=', $workerid)->where('id', '=', $demandid)->findOrEmpty();
        if ($demand->isEmpty()) {
            $demand = "NULL";
        } else {
            $demand->tags = implode(" ", $demand->tags);
        }
        $catelist = DemandCateModel::order(['priority' => 'desc', 'id' => 'desc'])->select()->toArray();
        page_result(0, "", [
            'demand'   => $demand,
            'catelist' => $catelist,
        ]);
    }


    public function editDemand()
    {
        $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);
        $picall  = input('picall/s', "");
        $zwagall = $wtype == 1 ? input('zwagall/s', "") : '';
        $data    = [
            'workerid'    => input('workerid/d', 0),
            'title'       => input('title/s', ""),
            'num'         => input('num/d', ""),
            'cateid'      => input('cateid/d', 0),
            'province'    => input('province/s', ""),
            'city'        => input('city/s', ""),
            'district'    => input('district/s', ""),
            'agegroup'    => input('agegroup/s', ""),
            'tags'        => explode(" ", input('tags/s', "")),
            'enddate'     => input('enddate/s', ""),
            'requirement' => input('requirement/s', ""),
            'comdetails'  => input('comdetails/s', ""),
            'picall'      => empty($picall) ? [] : explode(",", $picall),
            'video'       => input('video/s', ""),
            'wtype'       => $wtype,
            'bwagall'     => input('bwagall/s', ""),
            'zwagall'     => $zwagall,
            'ftype'       => input('ftype/d', 1),
            'fwagall'     => input('fwagall/s', ""),
            'telephone'   => input('telephone/s', ""),
            'remark'      => input('remark/s', ""),
        ];
        try {
            validate(DemandValidate::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,
                'telearr'    => [],
            ]);
            if ($user->integral < $param->adddemand) {
                page_result(1, "每次发布招聘订单信息需要扣除" . $param->adddemand . "积分，你当前积分不足。");
            }
            $demand  = DemandModel::create($data);
            $intdata = [
                'userid'      => $userid,
                'title'       => "发布招聘订单信息扣除",
                'intvalue'    => 0 - $param->adddemand,
                'intmoney'    => 0.00,
                'onlycontent' => "",
                'remark'      => input('title/s', ""),
                'itype'       => 5,
                '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->adddemand);
            $user->save([
                'integral' => $integral,
            ]);
        } else {
            $demand = DemandModel::where('id', '=', $id)->findOrEmpty();
            $demand->save($data);
        }
        page_result(0, "", [
            'demand' => $demand,
        ]);
    }

}