<?php
namespace app\mainapp\controller;

use think\facade\Session;
use app\mainapp\BaseController;

use app\common\model\User as UserModel;
use app\common\model\Partjob as PartjobModel;
use app\common\model\PartjobLog as PartjobLogModel;

class Partjob extends BaseController
{
	
	// 报名取消
	public function delLog()
	{	
		$logid = input('logid/d', 0);
		$userid = input('userid/d', 0);
		$log = PartjobLogModel::where(['id'=>$logid, 'userid'=>$userid])->findOrEmpty();
		if ($log->isEmpty()){
			page_result(1, "报名记录信息不存在或已被删除。");
		}
		$log->delete();
		page_result(0, "", array());
	}
	
	// 报名列表
	public function listLog()
	{	
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$userid = input('userid/d', 0);
		$map[] = ['userid','=',$userid];
		$plist = PartjobLogModel::with(['partjob'])->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 setPartjobLog()
	{
		$partjobid = input('partjobid/d', 0);
		$partjob = PartjobModel::findOrEmpty($partjobid);
		if ($partjob->isEmpty()){
			page_result(1, "兼职信息不存在");
		}
		if ( strtotime($partjob->endtime) < time() ){
			page_result(1, "该招聘信息报名已截止，不能再报名了。");
		}
		$userid = input('userid/d', 0);
		$user = UserModel::findOrEmpty($userid);
		if ($user->isEmpty()){
			page_result(1, "用户信息不存在");
		}
		$log = PartjobLogModel::where(['partjobid'=>$partjobid, 'userid'=>$userid])->findOrEmpty();
		if ($log->isEmpty()){
			$log->save([
				'agentid' 		=> $partjob->agentid,
				'partjobid'		=> $partjobid,
				'userid'		=> $userid,
				'remark'		=> "",
				'createtime'	=> time()
			]);
			page_result(0, "", array());
		}else{
			page_result(1, "你已报名过了，无需重复报名。");
		}
	}

	// 兼职详情
	public function getPartjob()
	{
		$partjobid = input('partjobid/d', 0);
		$partjob = PartjobModel::findOrEmpty($partjobid);
		if ($partjob->isEmpty()){
			page_result(1, "兼职信息不存在");
		}
		$partjoblist = PartjobModel::where([['status','=',1],['createtime','<=',time()]])->order(['priority'=>'desc','id'=>'desc'])->limit(5)->select();
		page_result(0, "", array(
			'partjob'		=> $partjob,
			'partjoblist'	=> $partjoblist
			));
	}
	
	 
	// 兼职列表
	public function listPartjob()
	{
		PartjobModel::where('endtime', '<', time())->update(['status' => 2]);		
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map[] = ['createtime','<=',time()];
		$plist = PartjobModel::withCount(['partjobLog'])->where($map)->order(['status'=>'asc','priority'=>'desc','id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}



}