<?php
 /*
 * 74cms 猎头会员中心函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
if(!defined('IN_QISHI'))
{
	die('Access Denied!');
}
/*
积分相关的方法 
*/
// 获取会员的积分
function get_user_points($uid)
{
	global $db;
	$uid=intval($uid);
	$points=$db->getone("select points from ".table('members_points')." where uid ='{$uid}' LIMIT 1");
	return $points['points'];
}
//积分操作
function report_deal($uid,$i_type=1,$points=0)
{
	global $db,$timestamp;
	$points=intval($points);
	$uid=intval($uid);
	$points_val=get_user_points($uid);
	if ($i_type==1)
	{
	$points_val=$points_val+$points;
	}
	if ($i_type==2)
	{
	$points_val=$points_val-$points;
	$points_val=$points_val<0?0:$points_val;
	}
	$sql = "UPDATE ".table('members_points')." SET points= '{$points_val}' WHERE uid='{$uid}' LIMIT 1";
	if (!$db->query($sql))return false;
	return true;
}
/*
	猎头等级经验相关操作
*/
// 操作猎头经验
function level_deal($uid,$option,$value)
{
	global $db;
	if (!is_array($uid)) $uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if($option==1)
		{
			$setsql=" set level=level+$value ";
		}else{
			$setsql=" set level=level-$value ";
		}
		$sql="update ".table("hunter_profile").$setsql." where uid in ($sqlin) ";
		$db->query($sql);
	}
}
// 增减经验后调用此方法 设置猎头等级
function level_meal_deal($uid)
{
	global $db;
	$hunter=get_hunter($uid);
	$level = $hunter['level'];//经验值
	$level_meal = $hunter['level_meal'];//等级 0,1,2 
	$level_config = get_cache('config');
	switch($level)
	{
		case ($level < $level_config['hunter_level_1'])://300
			$now_Level = 1;
		break;
		// $level>=$level_config['hunter_level_1'] && 
		case ($level < $level_config['hunter_level_2']):  //2500
			$now_Level = 2;
		break;
		//$level >= $level_config['hunter_level_2'] &&
		case ( $level < $level_config['hunter_level_3']):
			$now_Level = 3;
		break;
		//$level >= $level_config['hunter_level_3'] && 
		case ($level < $level_config['hunter_level_4']):
			$now_Level = 4;
		break;
		//$level >= $level_config['hunter_level_4'] && 
		case ($level < $level_config['hunter_level_5']):
			$now_Level = 5;
		break;

		case ($level >= $level_config['hunter_level_5']):
			$now_Level = 6;
		break;
	}	 
	if($level_meal!= $now_Level)
	{
		$db->query("UPDATE ".table('hunter_profile')."  SET `level_meal` = ".$now_Level." WHERE `uid` = ".$uid." LIMIT 1;");
	}
}

//获取 猎头信息
function get_hunter($uid)
{
	global $db;
	$uid=intval($uid);
	$level_config = get_cache('config'); 
	$sql="select * from ".table("hunter_profile")." where uid=$uid limit 1";
	$levelarr=$db->getone($sql);
	$level = $levelarr['level'];  
	switch($level){
		case ($level < $level_config['hunter_level_1']):   ///300
			$levelarr['left'] = 170;
			$levelarr['now_Level'] = 1;
			$levelarr['next_Level'] = 2;
			$levelarr['surplus_level'] = $level_config['hunter_level_1']-$level;
		break;
		case ($level < $level_config['hunter_level_2']):  //2500
			$levelarr['left'] = 308;
			$levelarr['now_Level'] =2;
			$levelarr['next_Level'] =3;
			$levelarr['surplus_level'] = $level_config['hunter_level_2']-$level;
		break;
		case ($level < $level_config['hunter_level_3']):
			$levelarr['left'] = 442;
			$levelarr['now_Level'] = 3;
			$levelarr['next_Level'] = 4;
			$levelarr['surplus_level'] = $level_config['hunter_level_3']-$level;
		break;
		case ($level < $level_config['hunter_level_4']):
			$levelarr['left'] = 576;
			$levelarr['now_Level'] = 4;
			$levelarr['next_Level'] = 5;
			$levelarr['surplus_level'] = $level_config['hunter_level_4']-$level;
		break;
		case ($level < $level_config['hunter_level_5']):
			$levelarr['left'] = 710;
			$levelarr['now_Level'] = 5;
			$levelarr['next_Level'] = 6;
			$levelarr['surplus_level'] = $level_config['hunter_level_5']-$level;
		break;
		case ($level > $level_config['hunter_level_5']):
			$levelarr['left'] = 844;
			$levelarr['now_Level'] = 6;
			$levelarr['next_Level'] = 6;
			$levelarr['surplus_level'] = $level_config['hunter_level_5']-$level;
		break;
	}
	return $levelarr;
}
//增加猎头擅长行业
function hunter_trade_save($uid,$pid,$str){
	global $db;
	$db->query("Delete from ".table('hunter_trade')." WHERE uid = '".intval($uid)."' and pid='".intval($pid)."'");
	$str=trim($str);
	$arr=explode(",",$str);
	if (is_array($arr) && !empty($arr))
	{
		foreach($arr as $a)
		{
		$a=explode(".",$a);
		$setsqlarr['uid']=intval($uid);
		$setsqlarr['pid']=intval($pid);
		$setsqlarr['trade']=intval($a[0]); 
			if (!inserttable(table('hunter_trade'),$setsqlarr))return false;
		}
	}
	return true;
}
//增加猎头擅长职能
function hunter_category_save($uid,$pid,$str){
	global $db;
	$db->query("Delete from ".table('hunter_category')." WHERE uid = '".intval($uid)."' and pid='".intval($pid)."'");
	$str=trim($str);
	$arr=explode(",",$str);
	if (is_array($arr) && !empty($arr))
	{
		foreach($arr as $a)
		{
		$a=explode(".",$a);
		$setsqlarr['uid']=intval($uid);
		$setsqlarr['pid']=intval($pid);
		$setsqlarr['topclass']=intval($a[0]); 
		$setsqlarr['category']=intval($a[1]); 
		$setsqlarr['subclass']=intval($a[2]); 
			if (!inserttable(table('hunter_category'),$setsqlarr))return false;
		}
	}
	return true;
}
// 获取猎头 合作过的客户
function get_hunter_collaborative($uid)
{
	global $db;
	$uid=intval($uid);
	$sql="select * from ".table("hunter_collaborative")." where hunter_uid=$uid ORDER by id desc ";
	$arr=$db->getall($sql);
	foreach ($arr as $key => $value)
	{
		$value["company_name_"]=cut_str($value['company_name'],10,0,'...');
		$arr[$key]=$value;
	}
	return $arr;
}
/* ======积分冲值部分====== */
function get_payment()
{
	global $db;
	$sql = "select * from ".table('payment')." where p_install='2' ORDER BY listorder desc";
	$list=$db->getall($sql);
	return $list;
}
function get_payment_info($typename,$name=false)
{
	global $db;
	$sql = "select * from ".table('payment')." where typename ='".$typename."' AND p_install='2' LIMIT 1";
	$val=$db->getone($sql);
	if ($name)
	{
	return $val['byname'];
	}
	else
	{
	return $val;
	}
}
//增加订单
function add_order($uid,$oid,$amount,$payment_name,$description,$addtime,$points='',$setmeal='',$utype='1')
{
	global $db;
	$setsqlarr['uid']=intval($uid);
	$setsqlarr['oid']=$oid;
	$setsqlarr['amount']=$amount;
	$setsqlarr['payment_name']=$payment_name;
	$setsqlarr['description']=$description;
	$setsqlarr['addtime']=$addtime;
	$setsqlarr['points']=$points;
	$setsqlarr['setmeal']=$setmeal;
	$setsqlarr['utype']=$utype;
	// write_memberslog($uid,$utype,$type,$username,$str,$parentuid,$note,$mode=0)
	write_memberslog($uid,$_SESSION['utype'],3001,$_SESSION['username'],"添加订单，编号{$oid}，金额{$amount}元,积分{$points}",0,"添加订单",1);
	$userinfo=get_user_info($uid);
		//sendemail
		$mailconfig=get_cache('mailconfig');
		if ($mailconfig['set_order']=="1" && $userinfo['email_audit']=="1" && $amount>0)
		{
		global $_CFG;
		$paymenttpye=get_payment_info($payment_name);
		dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_mail.php?uid={$uid}&key=".asyn_userkey($uid)."&act=set_order&oid={$oid}&amount={$amount}&paymenttpye={$paymenttpye['byname']}");
		}
		//sendemail
		//sms
		$sms=get_cache('sms_config');
		if ($sms['open']=="1" && $sms['set_order']=="1"  && $userinfo['mobile_audit']=="1" && $amount>0)
		{
		global $_CFG;
		$paymenttpye=get_payment_info($payment_name);
		dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_sms.php?uid={$uid}&key=".asyn_userkey($uid)."&act=set_order&oid={$oid}&amount={$amount}&paymenttpye={$paymenttpye['byname']}");
		}
		//sms
	return inserttable(table('order'),$setsqlarr,true);
}
//获取指点会员订单
function get_user_order($uid,$is_paid)
{
	global $db;
	$sql = "select * from ".table('order')." WHERE uid=".intval($uid)." AND  is_paid='".intval($is_paid)."' ORDER BY id desc";
	return $db->getall($sql);
}
//获取单条订单
function get_order_one($uid,$id)
{
	global $db;
	$sql = "select * from ".table('order')." where id =".intval($id)." AND uid = ".intval($uid)."  AND is_paid =1  LIMIT 1 ";
	return $db->getone($sql);
}
//取消订单
function del_order($uid,$id)
{
	global $db;
	// write_memberslog($uid,$utype,$type,$username,$str,$parentuid,$note,$mode=0)
	write_memberslog($_SESSION['uid'],$_SESSION['utype'],3002,$_SESSION['username'],"取消订单，订单id{$id}",0,"取消订单",0);
	return $db->query("Delete from ".table('order')." WHERE id='".intval($id)."' AND uid=".intval($uid)." AND is_paid=1  LIMIT 1 ");
}
//付款后开通
function order_paid($v_oid)
{
	global $db,$timestamp,$_CFG;
	$order=$db->getone("select * from ".table('order')." WHERE oid ='{$v_oid}' AND is_paid= '1' LIMIT 1 ");
	if ($order)
	{
		$user=get_user_info($order['uid']);
		$sql = "UPDATE ".table('order')." SET is_paid= '2',payment_time='{$timestamp}' WHERE oid='{$v_oid}' LIMIT 1 ";
			if (!$db->query($sql)) return false;
			if($order['amount']=='0.00'){
				$ismoney=1;
			}else{
				$ismoney=2;
			}
			if ($order['points']>0)
			{
					report_deal($order['uid'],1,$order['points']);				
					$user_points=get_user_points($order['uid']);
					$notes=date('Y-m-d H:i',time())."通过：".get_payment_info($order['payment_name'],true)." 成功充值 ".$order['amount']."元，(+{$order['points']})，(剩余:{$user_points}),订单:{$v_oid}";	
					//write_memberslog($uid,$utype,$type,$username,$str,$parentuid,$note,$mode=0)				
					write_memberslog($order['uid'],$user['utype'],9001,$user['username'],$notes,0,"冲值积分",1);			
			}
		//sendemail
		$mailconfig=get_cache('mailconfig');
		if ($mailconfig['set_payment']=="1" && $user['email_audit']=="1" && $order['amount']>0)
		{
		dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_mail.php?uid={$order['uid']}&key=".asyn_userkey($order['uid'])."&act=set_payment");
		}
		//sendemail
		//sms
		$sms=get_cache('sms_config');
		if ($sms['open']=="1" && $sms['set_payment']=="1"  && $user['mobile_audit']=="1" && $order['amount']>0)
		{
		dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_sms.php?uid={$order['uid']}&key=".asyn_userkey($order['uid'])."&act=set_payment");
		}
		//sms
		return true;
	}
return true;
}
//获取猎头等级特权
function get_hunter_level_meal($id){
	global $db;
	$sql = "select * from ".table('hunter_level_meal')." where id=".intval($id)." LIMIT 1 ";
	return $db->getone($sql);  
}
// 获取 简历列表 精英推荐
function get_talent_resume_list($num)
{
	global $db;
	$sql="select * from ".table("resume")." where talent=2 order by refreshtime desc limit $num ";
	return $db->getall($sql);
}
//添加多选行业
function jobs_trade_save($uid,$pid,$str){
 	global $db;
	$db->query("Delete from ".table('jobs_trade')." WHERE uid = '".intval($uid)."' and pid='".intval($pid)."'");
	$str=trim($str);
	$arr=explode(",",$str);
	if (is_array($arr) && !empty($arr))
	{
		foreach($arr as $a)
		{
		$a=explode(".",$a);
		$setsqlarr['uid']=intval($uid);
		$setsqlarr['pid']=intval($pid);
		$setsqlarr['trade']=intval($a[0]); 
			if (!inserttable(table('jobs_trade'),$setsqlarr))return false;
		}
	}
	return true;
}
//添加多选职位分类
function jobs_category_save($uid,$pid,$str){
	global $db;
	$db->query("Delete from ".table('jobs_category')." WHERE uid = '".intval($uid)."' and pid='".intval($pid)."'");
	$str=trim($str);
	$arr=explode(",",$str);
	if (is_array($arr) && !empty($arr))
	{
		foreach($arr as $a)
		{
		$a=explode(".",$a);
		$setsqlarr['uid']=intval($uid);
		$setsqlarr['pid']=intval($pid);
		$setsqlarr['topclass']=intval($a[0]); 
		$setsqlarr['category']=intval($a[1]); 
		$setsqlarr['subclass']=intval($a[2]); 
			if (!inserttable(table('jobs_category'),$setsqlarr))return false;
		}
	}
	return true;
}
//获取职位列表
function get_jobs($offset,$perpage,$get_sql= '',$countresume=false)
{
	global $db,$timestamp;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage))
	{
	$limit=" LIMIT {$offset},{$perpage}";
	}
	$result = $db->query($get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
		$row['jobs_name_']=cut_str($row['jobs_name'],6,0,"...");
		$row['companyname_']=cut_str($row['companyname'],5,0,"...");
		$row['district_cn_']=cut_str($row['district_cn'],7,0,"...");
		
		$row['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$row['id']));
		if($row['audit']==3){
			$row['status'] = 4;
			$row['status_cn'] = '未通过';
			$sql = "select reason from ".table('audit_reason')." where jobs_id=$row[id] ";
			$reason_arr=$db->getone($sql);
			$row['reasonmsg']=$reason_arr['reason'];
			$row['reason']=cut_str($reason_arr["reason"],5,0,"...");
			$row['company']=$row['companyname'];
		}
		elseif($row['audit']==2){
			$row['status'] = 3;
			$row['status_cn'] = '审核中';
		}
		elseif($row['deadline']<time() || $row['display']!=1){
			$row['status'] = 2;
			$row['status_cn'] = '已结束';
		}
		else{
			$row['status'] = 0;
			$row['status_cn'] = '正在发布';
		}
		if ($countresume)
		{
		$wheresql=" WHERE belong_uid='{$row['uid']}' AND jobs_id= '{$row['id']}'";
		$row['countresume']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('personal_jobs_apply').$wheresql);
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
//获取单条职位
function get_jobs_one($id,$uid='')
{
	global $db,$timestamp;
	$id=intval($id);
	if (!empty($uid)) $wheresql=" AND uid=".intval($uid);
	$val=$db->getone("select * from ".table('jobs')." where id='{$id}' {$wheresql} LIMIT 1");
	if (empty($val)) return false;
	$val['contact']=$db->getone("select * from ".table('jobs_contact')." where pid='{$val['id']}' LIMIT 1 ");
	$val['deadline_days']=($val['deadline']-$timestamp)>0?"距到期时间还有<strong style=\"color:#FF0000\">".sub_day($val['deadline'],$timestamp)."</strong>":"<span style=\"color:#FF6600\">目前已过期</span>";
	$val['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$val['id']));
	return $val;
} 
// 获取 职位的 公司备注名称 （猎头职位管理部分）
function get_hunter_jobs_companyname($get_sql)
{
	global $db;
	$result = $db->getall($get_sql);
	$companyname_arr=array();
	foreach ($result as $key => $value) {
		$companyname_arr[$key]=$value['companyname_note'];
	}
	if(empty($companyname_arr))
	{
		return $companyname_arr;
	}
	else
	{
		return $companyname_arr=array_unique($companyname_arr);
	}
}
// 获取推荐记录中企业名称
function get_hunter_recommend_companyname($get_sql)
{
	global $db;
	$result = $db->getall($get_sql);
	$companyname_arr=array();
	foreach ($result as $key => $value) {
		$companyname_arr[$key]['companyname']=$value['companyname'];
	}
	if(empty($companyname_arr))
	{
		return $companyname_arr;
	}
	else
	{
		return $companyname_arr=array_unique($companyname_arr);
	}
}
//刷新职位
function refresh_jobs($id,$uid)
{
	global $db;
	$uid=intval($uid);
	if (!is_array($id)) $id=array($id);
	$time=time();
	$sqlin=implode(",",$id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	if (!$db->query("update  ".table('jobs')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_hot')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_key')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_rtime')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_scale')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_recommend')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	return true;
	}
	return false;
}
//激活或者结束职位
function activate_jobs($idarr,$display,$uid)
{
	global $db;
	$time= time();
	$display=intval($display);	
	$uid=intval($uid);
	$uidsql=" AND uid='{$uid}'";
	if (!is_array($idarr)) $idarr=array($idarr);
	$sqlin=implode(",",$idarr);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		//恢复
		if($display==1){
			if (!$db->query("update ".table('jobs')."  SET display='{$display}',finishtime='0' WHERE id IN ({$sqlin}) {$uidsql}")) return false;
			distribution_jobs($idarr,$uid);
			write_memberslog($_SESSION['uid'],3,2005,$_SESSION['username'],"将职位激活状态设为:{$display},职位ID为：{$sqlin}",0,"修改职位状态",1);
			return true;
		}else{
			if (!$db->query("update ".table('jobs')."  SET display='{$display}',finishtime='{$time}' WHERE id IN ({$sqlin}) {$uidsql}")) return false;
			distribution_jobs($idarr,$uid);
			write_memberslog($_SESSION['uid'],3,2005,$_SESSION['username'],"将职位激活状态设为:{$display},职位ID为：{$sqlin}",0,"修改职位状态",1);
			return true;
		}
	
	}
	return false;
}
//删除职位
function del_jobs($del_id,$uid)
{
	global $db;
	$return=0;
	$uidsql=" AND uid=".intval($uid)."";
	if (!is_array($del_id)) $del_id=array($del_id);
	$sqlin=implode(",",$del_id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	if (!$db->query("Delete from ".table('jobs')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	$return=$return+$db->affected_rows();
	if (!$db->query("Delete from ".table('jobs_contact')." WHERE pid IN ({$sqlin}) ")) return false;
	if (!$db->query("Delete from ".table('jobs_search_hot')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	if (!$db->query("Delete from ".table('jobs_search_key')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	if (!$db->query("Delete from ".table('jobs_search_rtime')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	if (!$db->query("Delete from ".table('jobs_search_scale')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	if (!$db->query("Delete from ".table('jobs_search_recommend')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	write_memberslog($_SESSION['uid'],1,2003,$_SESSION['username'],"删除职位({$sqlin})");
	}
	return $return;
}
//推广或取消职位推广
function recommend_jobs($idarr,$recommend,$uid)
{
	global $db;
	$time= time();
	$recommend=intval($recommend);	
	$uid=intval($uid);
	$uidsql=" AND uid='{$uid}'";
	if (!is_array($idarr)) $idarr=array($idarr);
	$sqlin=implode(",",$idarr);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("update ".table('jobs')."  SET recommend='{$recommend}' WHERE id IN ({$sqlin}) {$uidsql}")) return false;
		if (!$db->query("update ".table('jobs_search_recommend')."  SET recommend='{$recommend}' WHERE id IN ({$sqlin}) {$uidsql}")) return false;
		return true;
	}
	return false;
}
//获取  推荐记录
function get_hunter_recommend($offset,$perpage,$get_sql='',$countresume=false)
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".intval($offset).','.intval($perpage);
	$select="h.*,j.id,j.companyname,j.addtime,j.district_cn,j.trade_cn,j.audit,j.jobs_type,j.deadline,j.display,j.setmeal_deadline ";
	$sql=" SELECT {$select} from ".table('hunter_recommend')." AS h {$get_sql}  {$limit}";

	$result = $db->query($sql);
	while($row = $db->fetch_array($result))
	{
		$row['jobs_name_']=cut_str($row['jobs_name'],10,0,"...");
		$row['companyname_']=cut_str($row['companyname'],10,0,"...");
		$row['district_cn_']=cut_str($row['district_cn'],7,0,"...");

		if($row['audit']==3){
			$row['status'] = 4;
			$row['status_cn'] = '未通过';
		}
		elseif($row['audit']==2){
			$row['status'] = 3;
			$row['status_cn'] = '审核中';
		}
		elseif($row['deadline']<time() || $row['display']==2 || ($row['setmeal_deadline']<time() && $row['setmeal_deadline']!=0)){
			$row['status'] = 2;
			$row['status_cn'] = '已结束';
		}else{
			$row['status'] = 0;
			$row['status_cn'] = '正在招聘';
		}
		if ($countresume)
		{
		$wheresql=" WHERE company_uid='{$row['company_uid']}' AND jobs_id= '{$row['jobs_id']}'";
		$row['countresume']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('hunter_recommend').$wheresql);
		$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['jobs_id']));
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
//整理职位索引表 
function distribution_jobs($pid,$uid)
{
	global $db,$_CFG,$timestamp;
	$uid=intval($uid);
	$uidsql=" AND uid='{$uid}' ";
	if (!is_array($pid))$pid=array($pid);
	foreach($pid as $v)
	{
		$pid=intval($v);
		$t1=$db->getone("select * from ".table('jobs')." where id='{$pid}' {$uidsql} LIMIT 1");
  		$db->query("Delete from ".table('jobs_search_hot')." WHERE id='{$v}' {$uidsql} LIMIT 1");
		$db->query("Delete from ".table('jobs_search_key')." WHERE id='{$v}' {$uidsql} LIMIT 1");
		$db->query("Delete from ".table('jobs_search_rtime')." WHERE id='{$v}' {$uidsql} LIMIT 1");
		$db->query("Delete from ".table('jobs_search_scale')." WHERE id='{$v}' {$uidsql} LIMIT 1");
		$db->query("Delete from ".table('jobs_search_recommend')." WHERE id='{$v}' {$uidsql} LIMIT 1");
		if ($t1['audit']=="1" && $t1['display']=="1" && $t1['user_status']=="1" && ($t1['deadline']>$timestamp || $t1['deadline']==0))
		{  
			$searchtab['id']=$t1['id'];
			$searchtab['uid']=$t1['uid'];
			$searchtab['parentuid']=$t1['parentuid'];
			$searchtab['utype']=$t1['utype'];
			$searchtab['jobs_type']=$t1['jobs_type'];
			$searchtab['sex']=$t1['sex'];
			$searchtab['companynature']=$t1['companynature'];
			$searchtab['district']=$t1['district'];
			$searchtab['sdistrict']=$t1['sdistrict'];	
			$searchtab['education']=$t1['education'];
			$searchtab['experience']=$t1['experience'];
			$searchtab['wage_min']=$t1['wage_min'];
			$searchtab['wage_max']=$t1['wage_max'];
			$searchtab['refreshtime']=$t1['refreshtime'];
			$searchtab['scale']=$t1['scale'];	
			$searchtab['addtime']=$t1['addtime'];	
			//按规模
			inserttable(table('jobs_search_scale'),$searchtab);
			//按刷新时间
			inserttable(table('jobs_search_rtime'),$searchtab);
			//推荐职位
			$searchtab['recommend']=$t1['recommend'];
			inserttable(table('jobs_search_recommend'),$searchtab);
			unset($searchtab['recommend']);
			//按热度
			$searchtab['click']=$t1['click'];
			inserttable(table('jobs_search_hot'),$searchtab);
			unset($searchtab['click']);
			//按关键词
			$searchtab['key']=$t1['key'];
			$searchtab['likekey']=$t1['jobs_name'].','.$t1['companyname'];
			inserttable(table('jobs_search_key'),$searchtab);
			unset($searchtab);
		}
	}
}
//根据猎头uid整理职位索引表
function distribution_jobs_uid($uid)
{
	global $db;
	$uid=intval($uid);
	$result = $db->query("select id from ".table('jobs')." where uid={$uid} ");
	while($row = $db->fetch_array($result))
	{
	$id[] = $row['id'];
	}
	if (!empty($id))
	{
	return distribution_jobs($id,$uid);
	}
}
// 猎头申请推荐
function app_get_jobs($id)
{
	global $db;
	$id=intval($id);
	$sql = "select * from ".table('jobs')." WHERE id=$id ";
	return $db->getone($sql);
}
//检查该职位是否申请过
function check_jobs_apply($jobs_id,$hunter_uid)
{
	global $db;
	$sql = "select id from ".table('entrust_hunter')." WHERE hunter_uid = '".intval($hunter_uid)."' AND jobs_id='".intval($jobs_id)."' LIMIT 1";
	return $db->getone($sql);
}
// 猎头申请成为个人的顾问
function app_get_resume($id)
{
	global $db;
	$id=intval($id);
	$sql = "select * from ".table('resume')." WHERE id=$id ";
	return $db->getone($sql);
}
//检查该职位是否申请过
function check_resume_apply($resume_id,$hunter_uid)
{
	global $db;
	$sql = "select id from ".table('entrust_hunter')." WHERE hunter_uid = '".intval($hunter_uid)."' AND resume_id='".intval($resume_id)."' LIMIT 1";
	return $db->getone($sql);
}
function get_resume_basic($id)
{
	global $db;
	$id=intval($id);
	$val=$db->getone("select * from ".table('resume')." where id='{$id}' LIMIT 1 ");
	return $val;
}
//获取全部搜索器
function get_search_resume_engine($uid){
	global $db;
	$sql = "select * from ".table('search_resume_engine')." where uid=".$uid;
	return $db->getall($sql);
}
//获取单条搜索器
function get_search_resume_engine_one($id,$uid){
	global $db;
	$sql = "select * from ".table('search_resume_engine')." where uid=".$uid." AND id=".$id." limit 1 ";
	return $db->getone($sql);
}
/*
	@简历管理
	@正在应聘部分
*/
function get_apply_hunterjobs($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT {$offset},{$perpage}";
	$selectstr=" a.*,r.fullname,r.residence_cn,r.sex_cn,r.birthdate,r.experience_cn,r.district_cn,r.education_cn,r.intention_jobs_cn,r.evaluation,r.click,r.refreshtime,r.addtime as resume_addtime";
	$result = $db->query("SELECT {$selectstr} FROM ".table('personal_jobs_apply')." as a {$get_sql} ORDER BY a.did DESC {$limit}");
	while($row = $db->fetch_array($result))
	{
		$row['fullname_']=cut_str($row['fullname'],3,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");
		$row['jobs_name_']=cut_str($row['jobs_name'],6,0,"...");
		$Y=date("Y",time());
		$row['age']=$Y-$row['birthdate'];
		$row['specialty_']=$row['evaluation'];
		$row['specialty']=cut_str($row['evaluation'],30,0,"...");
		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		if(intval($row['belong_parentuid'])>0){
			$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['jobs_id']));
		}else{
			$row['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$row['jobs_id']));
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
function get_apply_hunterjobs_jobs_arr($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT {$offset},{$perpage}";
	$selectstr=" a.*";
	$result = $db->query("SELECT {$selectstr} FROM ".table('personal_jobs_apply')." as a {$get_sql} ORDER BY a.did DESC {$limit}");
	while($row = $db->fetch_array($result))
	{
		$row_arr[] = $row;
	}
	return $row_arr;
}
//删除 -收到简历记录
function del_apply_jobs($del_id,$uid)
{
	global $db;
	$uidsql=" AND belong_uid=".intval($uid)."";
	if (!is_array($del_id)) $del_id=array($del_id);
	$sqlin=implode(",",$del_id);
	if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin)) return false;
	if (!$db->query("Delete from ".table('personal_jobs_apply')." WHERE did IN ({$sqlin}) {$uidsql}")) return false;
	return true;
}
//设置已看
function set_apply($id,$uid,$setlook)
{
	global $db;
	if (!is_array($id)) $id=array($id);
	$sqlin=implode(",",$id);
	if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin)) return false;
	$setsqlarr['is_look']=intval($setlook);
	$wheresql=" did IN (".$sqlin.") AND belong_uid=".intval($uid)."";
	return updatetable(table('personal_jobs_apply'),$setsqlarr,$wheresql);
}
//收到的职位申请总数
function count_hunterjobs_apply($uid,$look='')
{
	global $db;
	$uid=intval($uid);
	$look=intval($look);
	$wheresql="";
	if($look>0)
	{
	$wheresql.=" AND a.is_look='{$look}' ";
	}
	$total_sql="SELECT COUNT(*) AS num FROM ".table('personal_jobs_apply')." AS a WHERE a.belong_uid='{$uid}' {$wheresql}";
	return $db->get_total($total_sql);
}

//得到猎头正在发布中的职位
function get_auditjobs($uid)
{
	global $db;
	$uid=intval($uid);
	$time = time();
	return $db->getall( "select * from ".table('jobs')." WHERE uid={$uid} and utype=3 and  audit=1 and display=1 and deadline>".$time);
}
/*
	@简历管理
	@主动下载
*/
function get_hunter_down_resume($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT ".intval($offset).','.intval($perpage);
	$selectstr=" d.*,r.sex_cn,r.birthdate,r.current_cn,r.residence_cn,r.fullname,r.experience_cn,r.district_cn,r.education_cn,r.intention_jobs_cn,r.talent,r.addtime,r.refreshtime ";
	$result = $db->query("SELECT ".$selectstr." FROM ".table('hunter_down_resume')." as d {$get_sql} ORDER BY d.down_addtime DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$Y=date("Y",time());
		$row['fullname_']=cut_str($row['fullname'],3,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");
		$row['current_cn_']=cut_str($row['current_cn'],7,0,"...");
		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		$row['intention_jobs_cn']=cut_str($row['intention_jobs_cn'],30,0,"...");
		$row['age']=$Y-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
//删除
function del_down_resume($resume_id,$uid)
{
	global $db;
	$wheresql=" AND hunter_uid={$uid}";
	if (!is_array($resume_id)) $resume_id=array($resume_id);
	$sqlin=implode(",",$resume_id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	write_memberslog($_SESSION['uid'],1,4002,$_SESSION['username'],"/",$parentuid,"删除已下载简历({$sqlin})",0);
	$return=0;
	$db->query("Delete from ".table('hunter_down_resume')." WHERE did IN ({$sqlin}) {$wheresql}");
	$return=$return+$db->affected_rows();
	return $return;
	}
}
// 检查是否下载过
function check_down_resumeid($resume_id,$hunter_uid)
{
	global $db;
	$hunter_uid=intval($hunter_uid);
	$resume_id=intval($resume_id);
	$sql = "select did from ".table('hunter_down_resume')." WHERE hunter_uid = '{$hunter_uid}' AND resume_id='{$resume_id}' LIMIT 1";
	$info=$db->getone($sql);
	if (empty($info))
	{
	return false;
	}
	else
	{
	return true;
	}
}
function check_down_list($list_id ,$uid)
{
	global $db;
	$company_parentuid=intval($company_parentuid);
	$resume_id=intval($resume_id);
	$sql = "select * from ".table('down_list')." WHERE uid = '{$uid}' AND list_id ='{$list_id}' LIMIT 1";
	$info=$db->getone($sql);
	if (empty($info))
	{
	return false;
	}
	else
	{
	return true;
	}
}
function check_today_down_resume($down_per_day,$hunter_uid,$talent)
{
	global $db;
	$down_per_day=intval($down_per_day);
	$hunter_uid=intval($hunter_uid);
	$starttime = strtotime(date("Y-m-d"));
	$sql = "select count(*) as num from ".table('hunter_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.hunter_uid = '{$hunter_uid}' AND d.down_addtime>=".$starttime." AND r.talent=".$talent;
	$count=$db->get_total($sql);
	if ($count>=$down_per_day)
	{
	return true;//达到
	}
	else
	{
	return false;//未达到
	}
}
//把简历添加到已下载中
function add_down_resume($resume_id,$hunter_uid,$resume_uid,$resume_name)
{
	global $db,$timestamp;
	$resume_id=intval($resume_id);
	$hunter_uid=intval($hunter_uid);
	$resume_uid=intval($resume_uid);
	$resume_name=trim($resume_name);
	$hunter=get_hunter($hunter_uid);
	$sql = "INSERT INTO ".table('hunter_down_resume')." (resume_id,resume_uid,resume_fullname,hunter_uid,hunter_name,down_addtime) VALUES ('{$resume_id}','{$resume_uid}','{$resume_name}','{$hunter_uid}','{$hunter['fullname']}','{$timestamp}')";
	add_favorites($resume_id,$hunter_uid);
	return $db->query($sql);
}
/*
	@简历管理
	@浏览过的
*/
function get_my_attention($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT {$offset},{$perpage}";
	$selectstr=" a.*,r.fullname,r.birthdate,r.sex_cn,r.current_cn,r.residence_cn,r.education_cn,r.experience_cn,r.intention_jobs_cn ";
	$result = $db->query("SELECT {$selectstr} FROM ".table('view_resume')." as a {$get_sql} ORDER BY a.id DESC {$limit}");
	while($row = $db->fetch_array($result))
	{
		$row['resume_url'] = url_rewrite("QS_resumeshow",array('id'=>$row['resumeid']));
		$row['fullname_']=cut_str($row['fullname'],8,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");
		$row['current_cn_']=cut_str($row['current_cn'],9,0,"...");
		$Y=date("Y",time());
		$row['age']=$Y-$row['birthdate'];
		$sql="select * from ".table("hunter_favorites")." where hunter_uid=$row[uid] and resume_id=$row[resumeid] limit 1 ";
		$arr=$db->getone($sql);
		if(empty($arr))
		{	
			$row['is_favorites']=1;
			$row['is_favorites_cn']="未收藏";
		}
		else
		{
			$row['is_favorites']=2;
			$row['is_favorites_cn']="已收藏";
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
// 删除浏览过的简历
function del_my_attention($yid,$uid)
{
	global $db,$_CFG;
	$uid=intval($uid);
	$uidsql=" AND uid='{$uid}'";
	if (!is_array($yid)) $yid=array($yid);
	$sqlin=implode(",",$yid);
	$return=0;
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("Delete from ".table('view_resume')." WHERE resumeid IN ({$sqlin}) {$uidsql}")) return false;
		$return=$return+$db->affected_rows();
		return $return;
	}
}
/*
	@简历管理
	@人才库
*/
function add_favorites($id,$hunter_uid)
{
	global $db,$_CFG;
	$timestamp=time();
	// $setmeal=get_user_setmeal($hunter_uid);
	$hunter_uid=intval($hunter_uid);
	if (strpos($id,"-")) $id=explode("-",$id);
	if  (!is_array($id)) $id=array($id);
		$i=0;
		foreach ($id as $v) 
		{
			$v=intval($v);
			if (!check_favorites($v,$hunter_uid))
			{
				$db->query("INSERT INTO ".table('hunter_favorites')." (resume_id,hunter_uid,favorites_addtime) VALUES ('{$v}','{$hunter_uid}','{$timestamp}')");
				// write_memberslog($_SESSION['uid'],1,5001,$_SESSION['username'],"将简历({$v})添加至人才库");
				$i++;
			}
		}
		return $i;
}
function get_favorites($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="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";
	$result = $db->query("SELECT ".$selectstr."  FROM ".table('hunter_favorites')." AS f ".$get_sql." ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['fullname_']=cut_str($row['fullname'],3,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");
		$row['current_cn_']=cut_str($row['current_cn'],7,0,"...");

		$row['intention_jobs_cn_']=$row['intention_jobs_cn'];
		$row['intention_jobs_cn']=cut_str($row['intention_jobs_cn'],30,0,"...");
		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		$Y=date("Y",time());
		$row['age']=$Y-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
// 获取 人才库中 简历的期望行业
function get_hunter_favorites_trade($uid)
{
	global $db;
	$sql="select r.trade,r.trade_cn from ".table("hunter_favorites")." as h left join ".table("resume")." as r on h.resume_id=r.id where h.hunter_uid=$uid ";
	$row=$db->getall($sql);
	foreach($row as $key => $value)
	{
		$trade_id_arr=explode(",", $value['trade']);
		$trade_cn_arr=explode(",", $value['trade_cn']);
		if(empty($trade_id_arr))
		{
			$trade_arr[$key]['trade_id']=$trade_id_arr;
			$trade_arr[$key]['trade_cn']=$trade_cn_arr;
		}
		else
		{
			$trade_id_arr=array_unique($trade_id_arr);
			$trade_cn_arr=array_unique($trade_cn_arr);

			foreach ($trade_id_arr as $key1 => $value)
			{
				$trade_arr[$key1]["trade_id"]=$value;
				$trade_arr[$key1]["trade_cn"]=$trade_cn_arr[$key1];
			}
		}
	}
	return $trade_arr;
}
// 删除人才库简历
function del_favorites($resume_id,$hunter_uid)
{
	global $db;
	$hunter_uid=intval($hunter_uid);
	$uidsql=" AND hunter_uid='{$hunter_uid}'";
	if (!is_array($resume_id)) $resume_id=array($resume_id);
	$sqlin=implode(",",$resume_id);
	$return=0;
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("Delete from ".table('hunter_favorites')." WHERE resume_id IN ({$sqlin}) {$uidsql}")) return false;
		$return=$return+$db->affected_rows();
		// write_memberslog($_SESSION['uid'],$_SESSION['utype'],5002,$_SESSION['username'],"删除人才库人才({$sqlin})");		
		return $return;
	}
}
//检测人才库中是否已经存在
function check_favorites($resume_id,$hunter_uid)
{
	global $db;
	$hunter_uid=intval($hunter_uid);
	$resume_id=intval($resume_id);
	$sql = "select * from ".table('hunter_favorites')." WHERE hunter_uid ='{$hunter_uid}' AND resume_id='{$resume_id}' LIMIT 1";
	$info=$db->getone($sql);
	if (empty($info))
	{
	return false;
	}
	else
	{
	return true;
	}
}
/*
	@简历管理
	@面试邀请
*/
function get_interview($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="i.*,r.fullname,r.birthdate,r.sex_cn,r.education_cn,r.experience_cn,r.intention_jobs_cn,r.district_cn,r.residence_cn,r.refreshtime";
	$result = $db->query("SELECT  {$selectstr}  FROM ".table('company_interview')." as i {$get_sql} ORDER BY  i.did DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");
		$row['jobs_name_']=cut_str($row['jobs_name'],12,0,"...");

		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		if(intval($row['belong_parentuid'])>0){
			$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['jobs_id']));
		}else{
			$row['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$row['jobs_id']));
		}
		$row['intention_jobs_cn']=cut_str($row['intention_jobs_cn'],30,0,"...");
		$Y=date("Y",time());
		$row['age']=$Y-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
function check_interview($resume_id,$jobs_id,$hunter_uid)
{
	global $db;
	$resume_id=intval($resume_id);
	$jobs_id=intval($jobs_id);
	$hunter_uid=intval($hunter_uid);
	$sql = "select * from ".table('company_interview')." WHERE belong_uid ='{$hunter_uid}' AND resume_id='{$resume_id}' AND jobs_id='{$jobs_id}' LIMIT 1";
	return $db->getone($sql);
}
// 删除面试邀请
function del_interview($yid,$uid)
{
	global $db;
	$uidsql=" AND belong_uid=".intval($uid)."";
	if (!is_array($yid)) $yid=array($yid);
	$sqlin=implode(",",$yid);
	if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin)) return false;
	if (!$db->query("Delete from ".table('company_interview')." WHERE did IN ({$sqlin})  {$uidsql}")) return false;
	write_memberslog($_SESSION['uid'],3,6002,$_SESSION['username'],"/",0,"删除面试邀请({$sqlin})",0);
	return true;
}
//检查举报简历
function check_resume_report($uid,$resume_id)
{
	global $db;
	$sql = "select id from ".table('report_resume')." WHERE uid = '".intval($uid)."' AND resume_id='".intval($resume_id)."' LIMIT 1";
	return $db->getone($sql);
}
// 检查是否推荐过该简历
function check_resume_recommend($resume_id,$hunter_uid,$jobs_id)
{
	global $db;
	$resume_id=intval($resume_id);
	$hunter_uid=intval($hunter_uid);
	$jobs_id=intval($jobs_id);
	$sql="select * from ".table("hunter_recommend")." where uid=$hunter_uid and jobs_id=$jobs_id and resume_id=$resume_id limit 1";
	return $db->getone($sql);
}
//获取推荐记录 的简历
function get_recom_resume($offset,$perpage,$get_sql='')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".intval($offset).','.intval($perpage);
	$select="h.*,r.id,r.fullname,r.sex_cn,r.birthdate,r.experience_cn,r.education_cn,r.residence_cn,r.current_cn";
	$sql="SELECT {$select} from ".table('hunter_recommend')." AS h {$get_sql}  {$limit}";
	$result = $db->query($sql);
	while($row = $db->fetch_array($result))
	{	
		$row['age'] = intval(date('Y',time()))-intval($row['birthdate']);
		//推荐反馈
		$wheresql = " WHERE  jobs_id={$row['jobs_id']}  AND  belong_id={$row['company_id']}  AND  resume_id={$row['resume_id']}";
		$interview=$db->getone("SELECT did  FROM ".table('company_interview').$wheresql);
		if(empty($interview)){
			$row['status_'] = '等待邀请面试';
		}
		else
		{
			$row['status_'] = '已邀请面试';
		}
		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['id']));
		$row_arr[] = $row;
	}
	return $row_arr;
}
/*
	@删除委托信息
	@$del_id 记录id
	@$uid  猎头会员uid
*/
function del_entrust($del_id,$uid)
{
	global $db;
	$return=0;
	$uidsql=" AND hunter_uid=".intval($uid)."";
	if (!is_array($del_id)) $del_id=array($del_id);
	$sqlin=implode(",",$del_id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	if (!$db->query("Delete from ".table('entrust_hunter')." WHERE id IN ({$sqlin}) {$uidsql}")) return false;
	$return=$return+$db->affected_rows();
	}
	return $return;
}
/*
	@删除推荐记录
	@$del_id 记录id
	@$uid  猎头会员uid
*/
function del_recommend($del_id,$uid)
{
	global $db;
	$return=0;
	$uidsql=" AND uid=".intval($uid)."";
	if (!is_array($del_id)) $del_id=array($del_id);
	$sqlin=implode(",",$del_id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	if (!$db->query("Delete from ".table('hunter_recommend')." WHERE did IN ({$sqlin}) {$uidsql}")) return false;
	$return=$return+$db->affected_rows();
	}
	return $return;
}
// 账户资金情况
function get_user_account($uid)
{	
	global $db;
	$uid=intval($uid);
	$sql="select * from ".table("account")." where uid=$uid limit 1";
	return $db->getone($sql);
}
/*
	@账户余额操作
	@$uid 会员uid(主uid)
	@$num(操作金额)
	@$action操作类型(1==>加,2==>减)
*/
function action_user_account($uid,$utype,$num,$action='1')
{
	global $db;
	$uid=intval($uid);
	$num=floatval($num);
	$utype=intval($utype);
	$action=intval($action);
	//判断账户,insert,update
	$account_arr=get_user_account($uid);
	if(empty($arr))
	{
		$insert_arr['uid']=$uid;
		$insert_arr['utype']=$utype;
		$insert_arr['available_funds']=$num;
		inserttable(table('account'),$insert_arr);
	}
	else
	{
		if($action==1)
		{
			$update_arr['available_funds']=$account_arr['available_funds']+$num;
		}
		else
		{
			$update_arr['available_funds']=$account_arr['available_funds']-$num;
		}
		updatetable(table('account'),$update_arr,array("uid"=>$uid));
	}
}
/*
	@账户资金记录日志
	@$uid
	@$puid
	@$utype
	@$username
	@$text_log
	@$state
*/
function write_account_log($uid,$puid,$utype,$username,$text_log,$state)
{
	$setarr['uid']=intval($uid);
	$setarr['puid']=intval($puid);
	$setarr['utype']=intval($utype);
	$setarr['username']=$username;
	$setarr['text_log']=$text_log;
	$setarr['state']=$state;
	$setarr['addtime']=time();
	inserttable(table('account_log'),$setarr);
}
// 获取账户资金日志
function get_account_log($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage))
	{
	$limit=" LIMIT {$offset},{$perpage}";
	}
	return $db->getall("select * from ".table("account_log").$get_sql." order by addtime desc ".$limit);
}
/*
查询是否有未处理的提现记录
*/
function get_account_withdrawal($uid,$state)
{
	global $db;
	$uid=intval($uid);
	$state=intval($state);
	$sql="select * from ".table("account_withdrawal")." where uid=$uid and state=$state limit 1 ";
	return $db->getone($sql);
}
// 获取会员单条提现申请
function get_account_withdrawa_one($uid,$id)
{
	global $db;
	$sql="select * from ".table("account_withdrawal")." where id=".intval($id)." and uid=".intval($uid)." limit 1 ";
	return $db->getone($sql);
}
/*
	@获取 		下载的通讯录信息
	@$offset      	数据开始位置
	@$perpage 	显示个数
	@$get_sql  	sql外链接语句
	@$row_arr  	返回结果信息
*/
function get_down_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="d.*,l.title,l.trade_cn,l.mobile_cn,l.email_cn,l.number,l.list_time,l.file_size,l.file,l.click,l.price ";
	$result = $db->query("SELECT ".$selectstr."  FROM ".table('down_list')." AS d ".$get_sql." ORDER BY d.down_time DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['title_']=$row['title'];
		$row['title']=cut_str($row['title'],13,0,"...");
		$row['url']=url_rewrite('QS_list_show',array('id'=>$row['list_id']));
		$row['trade_cn_']=$row['trade_cn'];
		$row['trade_cn']=cut_str($row['trade_cn'],13,0,"...");
		if(intval($row['number'])>999)
		{
			$row['number']='999+';
		}
		$row['list_time']=date('Y',$row['list_time']);
		$row['file_size']=filesizerange($row['file_size']);
		$row['time']=date('Y-m-d',$row['down_time']);
		$row_arr[] = $row;
	}
	return $row_arr;
}
/*
	@获取 		上传的通讯录信息
	@$offset      	数据开始位置
	@$perpage 	显示个数
	@$get_sql  	sql条件语句
	@$row_arr  	返回结果信息
*/
function get_upload_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT *  FROM ".table('list').$get_sql." ORDER BY addtime DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['title_']=$row['title'];
		$row['title']=cut_str($row['title'],13,0,"...");
		$row['url']=url_rewrite('QS_list_show',array('id'=>$row['id']));
		$row['trade_cn_']=$row['trade_cn'];
		$row['trade_cn']=cut_str($row['trade_cn'],13,0,"...");
		if(intval($row['number'])>999)
		{
			$row['number']='999+';
		}
		$row['list_time']=date('Y',$row['list_time']);
		$row['file_size']=filesizerange($row['file_size']);
		$row['time']=date('Y-m-d',$row['addtime']);
		$down_sql = "SELECT COUNT(*) AS num FROM ".table('down_list')." WHERE list_id=".$row['id'];
		$row['down_num']=$db->get_total($down_sql);
		$row_arr[] = $row;
	}
	return $row_arr;
}
/*
	@删除 		通讯录信息
	@$list_id      	通讯录id
	@$uid 		会员id
	@$del_type  	删除类型（2->删除我下载的  1->删除我上传的）
	@return  	ture/false
*/
function del_list($list_id,$uid,$del_type)
{
	global $db,$_CFG;
	$list_id = intval($list_id);
	$uid = intval($uid);
	$del_type = intval($del_type);
	if($del_type==2)
	{
		if (!$db->query("Delete from ".table('down_list')." WHERE uid = {$uid} and id = {$list_id}")) return false;
	}
	else
	{
		$list_info = $db->getone("select * from ".table('list')." where id=".$list_id." limit 1");
		if (!$db->query("Delete from ".table('list')." WHERE uid = {$uid} and id = {$list_id}")) return false;
		//还得将相应通讯录文件也得删掉
		$path = QISHI_ROOT_PATH."data/list/".$list_info['file'];
		if(!unlink($path))
		{
			return false;
		}
	}
	return true;
}
/*
	@获取单条list通讯录 		
	@$list_id      	通讯录id
	@$uid 		会员id
	@return  	通讯录信息
*/
function get_list_one($list_id,$uid='')
{
	global $db,$timestamp;
	$list_id=intval($list_id);
	if (!empty($uid)) $wheresql=" AND uid=".intval($uid);
	$list_info=$db->getone("select * from ".table('list')." where id='{$list_id}' {$wheresql} LIMIT 1");
	if (empty($list_info)) return false;
	return $list_info;
}
/*
	@检查举报通讯录 		
	@$list_id      	通讯录id
	@$uid 		会员id
	@return  	返回举报通讯录信息
*/
function check_list_report($uid,$list_id)
{
	global $db;
	$sql = "select id from ".table('report_list')." WHERE uid = '".intval($uid)."' AND list_id='".intval($list_id)."' LIMIT 1";
	return $db->getone($sql);
}
?>