<?php
 /*
 * 74cms 申请职位
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once(dirname(__FILE__).'/../include/common.inc.php');
$act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'app';
require_once(QISHI_ROOT_PATH.'include/mysql.class.php');
require_once(QISHI_ROOT_PATH.'include/fun_user.php');
$db = new mysql($dbhost,$dbuser,$dbpass,$dbname);
if ($_SESSION['uid']=='' || $_SESSION['username']=='')
{
	$captcha=get_cache('captcha');
	$smarty->assign('verify_userlogin',$captcha['verify_userlogin']);
	$smarty->display('plus/ajax_login.htm');
	exit();
}
if ($_SESSION['utype']!='3')
{
	exit('<div class="floatContent">
			<div class="float_common_box">
				<div class="float_tips">
					<div>必须是猎头会员可以推荐人才！</div>
				</div>
			</div>
		</div>');
}
require_once(dirname(__FILE__).'/../include/fun_hunter.php');
$user=get_user_info($_SESSION['uid']);
if ($user['status']=="2") 
{
	exit('<div class="floatContent">
			<div class="float_common_box">
				<div class="float_tips">
					<div>您的账号处于暂停状态，请先联系网站管理员设为正常后进行操作！</div>
				</div>
			</div>
		</div>');
}
if ($act=="app")
{		
		$jobs_id=isset($_GET['id'])?$_GET['id']:exit("id 丢失");
		$jobs=get_jobs_one($jobs_id);
		if (empty($jobs))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>推荐人才失败</div>
					</div>
				</div>
			</div>');
		}
		$hunter=get_hunter(intval($_SESSION['uid']));
		if($hunter['card_audit']!=1 || $hunter['belongcompany_audit']!=1 || $hunter['avatars_audit']!=1)
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>您没有认证通过，请先认证后在进行此操作！</div>
					</div>
				</div>
			</div>');
		}
		// 获取人才库中的简历
		$resume_list=get_favorites_resume($hunter['uid']);
		if(empty($resume_list))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>您的人才库中没有可用简历,不能经行操作！</div>
					</div>
				</div>
			</div>');
		}
?>
<script type="text/javascript">
$(".but80").hover(function(){$(this).addClass("but80_hover")},function(){$(this).removeClass("but80_hover")});
//验证
$("#ajax_app").die().live("click",function() {
		$("#app").hide();
		$("#waiting").show();
		var tsTimeStamp= new Date().getTime();
		var resume_id = $("#select_resume").val();
		//alert(expire);
		$.post("<?php echo $_CFG['site_dir'] ?>user/user_hunter_recommend_resume.php", { "hunter_uid": <?php echo $hunter['uid'];?>,"jobs_id":<?php echo $jobs['id']; ?>,"resume_id":resume_id,"time":tsTimeStamp,"act":"app_save"},
	 	function (data,textStatus)
	 	 {
			if (data=="ok")
			{
				$(".ajax_app_tip").hide();
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
					$("#app_ok .closed").click(function(){
					});
			}
			else if(data=="repeat")
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("您已经推荐过该简历，不能重复推荐！");
			}
			else if(data=="down")
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("企业已经下载过该简历,您不能经行推荐！");
			}
			else if(data=="apply")
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("个人已经申请过该职位,您不能进行推荐！");
			}
			else if(data=="interview")
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("企业已经对该简历经行邀请面试,您不能推荐！");

			}
			else
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("推荐失败！"+data);

			}
	 	 })
});
function DialogClose()
{
	$("#FloatBg").hide();
	$("#FloatBox").hide();
}
</script> 
<div class="floatContent" id="app">
	<div class="float_common_box">
		<div class="float_top_tip ajax_app_tip">您正在为<span>"<?php echo $jobs['companyname']?>"</span>发布的职位：<span><?php echo $jobs['jobs_name']?></span>推荐人才</div>
		<table class="float_auth_table">
			<tbody>
				<tr height="40">
					<td align="right">选择推荐人才</td>
					<td>
						<div class="float_control">
							<select name="resume_id" id="select_resume">
								 <?php 
								 	foreach ($resume_list as $value) {
								 		echo "<option value='".$value['resume_id']."'>".$value['fullname']."</option>";
								 	}
								 ?>
							</select>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
		<div class="float_btn_guoup">
			<input type="submit" value="提交" name="Submit2" class="float_but55lan" id="ajax_app">&nbsp;&nbsp;&nbsp;
			<input type="button" value="取消" class="float_but55hui DialogClose">
		</div>
	</div>
</div>
<div class="floatContent" style="display:none" id="waiting" >
	<div class="float_common_box" >
		<img src="<?php echo  $_CFG['site_template']?>images/30.gif" style="margin-left:110px;" border="0"/>
	</div>
</div>
<div class="floatContent" style="display:none" id="app_ok" >
	<div class="float_common_box" >
		<div class="float_auth_success">推荐成功！<a href="<?php echo get_member_url(3,true)?>hunter_recommend.php?act=recommend_by_resume" >查看推荐记录</a></div>
	</div>
</div>
<?php
}
elseif ($act=="app_save")
{
	$resume_id=intval($_POST['resume_id']);
	$hunter_uid = intval($_SESSION['uid']);
	$jobs_id=intval($_POST['jobs_id']);
	$hunter=get_hunter($hunter_uid);
	$resume=app_get_resume($resume_id);
	if (empty($resume))
	{
		exit("简历丢失");
	}
	$jobs=get_jobs_one($jobs_id);
	if (empty($jobs))
	{
		exit("职位丢失");
	}
	$resume = array_map("addslashes",$resume);
	$jobs = array_map("addslashes",$jobs);
	if (check_resume_recommend($resume['id'],$hunter_uid,$jobs['id']))
	{
	exit("repeat");
	}
	// 检查下载
	$down=check_company_down($resume_id,$jobs['parentuid']);
	if(!empty($down))
	{
		exit("down");
	}
	// 检查邀请面试
	$interview=check_company_interview($resume_id,$jobs['id']);
	if(!empty($down))
	{
		exit("interview");
	}
	// 检查投递
	$apply=check_pesonal_apply($resume_id,$jobs_id);
	if(!empty($apply))
	{
		exit("apply");
	}
	$addarr['uid']=$hunter_uid;
	$addarr['jobs_id']=$jobs['id'];
	$addarr['jobs_name']=$jobs['jobs_name'];
	$addarr['company_id']=$jobs['company_id'];
	$addarr['company_name']=$jobs['companyname'];
	$addarr['company_uid']=$jobs['uid'];
	$addarr['company_puid']=$jobs['parentuid'];
	$addarr['resume_id']=$resume['id'];
	$addarr['resume_name']=$resume['fullname'];
	$addarr['personal_uid']=$resume['uid'];
	$addarr['recommend_addtime']=time();
	$insert_id = inserttable(table('hunter_recommend'),$addarr,1);
	if ($insert_id)
	{
		// 推荐人才加积分
		$points_rule=get_cache("points_rule");

		if($points_rule['hunter_recommend_resume']['value']>0)
		{
			// 获取当日推荐人才获得的积分总数
			$time=time();
			$today=mktime(0, 0, 0,date('m'), date('d'), date('Y'));
			$info=$db->getone("SELECT sum(points) as num FROM ".table('members_handsel')." WHERE uid ='{$_SESSION['uid']}' AND htype='recommend_resume' AND addtime>{$today}  LIMIT 1");
			if($points_rule['hunter_recommend_resume']['type']==1)
			{
				if($points_rule['hunter_recommend_resume']['daymax']>$info['num'])
				{
					$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,points,addtime) VALUES ('{$_SESSION['uid']}', 'recommend_resume','{$points_rule['hunter_recommend_resume']['value']}','{$time}')");
					report_deal($addarr['uid'],$points_rule['hunter_recommend_resume']['type'],$points_rule['hunter_recommend_resume']['value']);
				}
			}
			else
			{
				report_deal($addarr['uid'],$points_rule['hunter_recommend_resume']['type'],$points_rule['hunter_recommend_resume']['value']);
			}
			$operator=$points_rule['hunter_recommend_resume']['type']=="1"?"+":"-";
			$user_points=get_user_points($_SESSION['uid']);
			write_memberslog($_SESSION['uid'],3,9102,$_SESSION['username'],"积分：".$operator.$points_rule['hunter_recommend_resume']['value'].",剩余：{$user_points}",0,"推荐人才",1);
		}
		exit("ok");
	}
}
function get_favorites_resume($uid)
{
	global $db;
	$sql="select f.*,r.fullname,r.birthdate,r.sex_cn,r.education_cn,r.experience_cn,r.intention_jobs_cn,r.district_cn,r.addtime,r.residence_cn,r.current_cn,r.refreshtime from ".table("hunter_favorites")." as f left join ".table("resume")." as r on f.resume_id=r.id where f.hunter_uid={$uid} ORDER BY f.did DESC ";
	return $db->getall($sql);
}
// 检查简历是否被企业下载过简历 
function check_company_down($resume_id,$puid)
{
	global $db;
	$resume_id=intval($resume_id);
	$puid=intval($puid);
	$sql="select did from ".table("company_down_resume")." where resume_id=$resume_id and company_uid=$puid";
	return $db->getone($sql);
}
// 检查 企业是否已经邀请面试
function check_company_interview($resume_id,$jobs_id)
{
	global $db;
	$resume_id=intval($resume_id);
	$jobs_id=intval($jobs_id);
	$sql="select did from ".table("company_interview")." where resume_id=$resume_id and jobs_id=$jobs_id ";
	return $db->getone($sql);
}
// 检查 个人是否申请过职位
function check_pesonal_apply($resume_id,$jobs_id)
{
	global $db;
	$resume_id=intval($resume_id);
	$jobs_id=intval($jobs_id);
	$sql="select did from ".table("personal_jobs_apply")." where resume_id=$resume_id and jobs_id=$jobs_id ";
	return $db->getone($sql);
}
?>