<?php
 /*
 * 74cms 企业会员中心函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
if(!defined('IN_QISHI'))
{
	die('Access Denied!');
}
//分配客服顾问
function set_consultant($uid){
	global $db;
	$consultant = $db->getall("select id from ".table('consultant'));
	if($consultant){
		foreach ($consultant as $key => $value) {
			$arr[$value['id']] = $value['id'];
		}
		$rand = array_rand($arr,1);
		updatetable(table('members'),array("consultant"=>$rand)," uid=".$uid." ");
	}
	return true;
}
//设置企业套餐
function set_members_setmeal($parentuid,$setmealid)
{
	global $db,$timestamp;
	$setmeal=$db->getone("select * from ".table('setmeal')." WHERE id = ".intval($setmealid)." AND display=1 LIMIT 1");
	if (empty($setmeal)) return false;
	$setsqlarr['effective']=1;
	$setsqlarr['setmeal_id']=$setmeal['id'];
	$setsqlarr['setmeal_name']=$setmeal['setmeal_name'];
	$setsqlarr['days']=$setmeal['days'];
	$setsqlarr['starttime']=$timestamp;
	if ($setmeal['days']>0)
	{
	$setsqlarr['endtime']=strtotime("".$setmeal['days']." days");
	}
	else
	{
	$setsqlarr['endtime']="0";	
	}
	$setsqlarr['expense']=$setmeal['expense'];
	$setsqlarr['jobs_ordinary']=$setmeal['jobs_ordinary'];
	$setsqlarr['jobs_ordinary_porint_all']=$setmeal['jobs_ordinary_porint_all'];
	$setsqlarr['jobs_senior']=$setmeal['jobs_senior'];
	$setsqlarr['jobs_senior_porint_all']=$setmeal['jobs_senior_porint_all'];
	$setsqlarr['jobs_entrust']=$setmeal['jobs_entrust'];
	$setsqlarr['jobs_entrust_porint_all']=$setmeal['jobs_entrust_porint_all'];
	$setsqlarr['search_ordinary_resume']=$setmeal['search_ordinary_resume'];
	$setsqlarr['see_ordinary_resume']=$setmeal['see_ordinary_resume'];
	$setsqlarr['down_ordinary_resume']=$setmeal['down_ordinary_resume'];
	$setsqlarr['down_ordinary_resume_day']=$setmeal['down_ordinary_resume_day'];
	$setsqlarr['search_senior_reusme']=$setmeal['search_senior_reusme'];
	$setsqlarr['see_senior_resume']=$setmeal['see_senior_resume'];
	$setsqlarr['down_senior_resume']=$setmeal['down_senior_resume'];
	$setsqlarr['down_senior_resume_day']=$setmeal['down_senior_resume_day'];
	$setsqlarr['interview_ordinary']=$setmeal['interview_ordinary'];
	$setsqlarr['interview_senior']=$setmeal['interview_senior'];
	$setsqlarr['account']=$setmeal['account'];
	$setsqlarr['company_web']=$setmeal['company_web'];
	$setsqlarr['save_resume']=$setmeal['save_resume'];
	$setsqlarr['report_ordinary_resume']=$setmeal['report_ordinary_resume'];
	$setsqlarr['report_senior_resume']=$setmeal['report_senior_resume']; 
	$setsqlarr['is_entrust_hunter']=$setmeal['is_entrust_hunter']; 
	$setsqlarr['added']=$setmeal['added'];
	if (!updatetable(table('members_setmeal'),$setsqlarr," uid='{$parentuid}'")) return false;
	$setmeal_jobs['setmeal_deadline']=$setsqlarr['endtime'];
	$setmeal_jobs['setmeal_id']=$setsqlarr['setmeal_id'];
	$setmeal_jobs['setmeal_name']=$setsqlarr['setmeal_name'];
	if (!updatetable(table('jobs'),$setmeal_jobs," uid='{$parentuid}'")) return false;
	distribution_jobs_uid($parentuid);
	return true;
}
//整理职位索引表 
function distribution_jobs($pid,$parentuid)
{
	global $db,$_CFG,$timestamp;
	$parentuid=intval($parentuid);
	$uidsql=" AND parentuid='{$parentuid}' ";
	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) && ($t1['setmeal_deadline']>$timestamp || $t1['setmeal_deadline']=0))
		{  
			//若是有效职位把职位中的job_status(职位状态->有效/无效)修改成1(有效职位)
			updatetable(table('jobs') , array('job_status'=>'1') , " id='{$pid}' {$uidsql}");

			$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);
		}
		else
		{
			//若是无效职位把职位中的job_status(职位状态->有效/无效)修改成2(无效职位)
			updatetable(table('jobs') , array('job_status'=>'2') , " id='{$pid}' {$uidsql}");
		}
	}
}
//根据企业parentuid整理职位索引表
function distribution_jobs_uid($parentuid)
{
	global $db;
	$parentuid=intval($parentuid);
	$result = $db->query("select id from ".table('jobs')." where parentuid={$parentuid} ");
	while($row = $db->fetch_array($result))
	{
	$id[] = $row['id'];
	}
	if (!empty($id))
	{
	return distribution_jobs($id,$parentuid);
	}
}
/*
	积分相关的方法 
*/
// 获取会员的积分
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 count_jobs($uid,$parentuid,$sql)
{
	global $db;
	if($uid==$parentuid){
		$wheresql=" where parentuid='{$parentuid}' ".$sql;
	}else{
		$wheresql=" where uid='{$uid}' ".$sql;
	}
	$total_sql="SELECT COUNT(*) AS num FROM ".table('jobs_search_rtime').$wheresql;
	return $db->get_total($total_sql);
}
//把简历添加到已下载中
function add_down_resume($resume_id,$company_parentuid,$company_uid,$resume_uid,$resume_name,$talent)
{
	global $db,$timestamp;
	$resume_id=intval($resume_id);
	$company_parentuid=intval($company_parentuid);
	$company_uid=intval($company_uid);
	$resume_uid=intval($resume_uid);
	$resume_name=trim($resume_name);
	$talent=intval($talent);
	$company=get_company($company_parentuid);
	$sql = "INSERT INTO ".table('company_down_resume')." (resume_id,resume_uid,resume_name,company_parentuid,company_uid,company_name,down_addtime,talent) VALUES ('{$resume_id}','{$resume_uid}','{$resume_name}','{$company_parentuid}','{$company_uid}','{$company['companyname']}','{$timestamp}','{$talent}')";
	return $db->query($sql);
}
//已下载简历总数
function count_down_resume($uid,$parentuid)
{
	global $db;
	if($uid==$parentuid){
		$wheresql=" where d.company_parentuid='{$parentuid}' ";
	}else{
		$wheresql=" where d.company_uid='{$uid}' ";
	}
	$total_sql="SELECT COUNT(*) AS num FROM ".table('company_down_resume')." AS d ".$wheresql;
	return $db->get_total($total_sql);
}
//收到的职位申请总数
function count_jobs_apply($uid,$parentuid,$look='')
{
	global $db;
	$look=intval($look);
	if($uid==$parentuid){
		$wheresql=" where a.belong_parentuid=".$parentuid." ";
	}else{
		$wheresql=" where a.belong_uid=".$uid." ";
	}
	
	if($look>0)
	{
	$wheresql.=" AND a.personal_look='{$look}' ";
	}
	$total_sql="SELECT COUNT(*) AS num FROM ".table('personal_jobs_apply')." AS a {$wheresql}";
	return $db->get_total($total_sql);
}
//获取招聘意向
function get_concern_id($uid)
{
	global $db;
	$wheresql = " where uid=".$uid." ";
	$info=$db->getall("select id,topclass,category from ".table('jobs_category').$wheresql." LIMIT 10");
	if (!empty($info) && is_array($info))
	{
		foreach($info as $s)
		{
		$str[]=$s['topclass'];
		}
		return implode("-",array_unique($str));
	}
	return "";
}
//获取客服顾问
function get_consultant($id){
	global $db;
	$sql = "select * from ".table('consultant')." where id=".$id;
	return $db->getone($sql);
}
//获取会员套餐
function get_user_setmeal($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members_setmeal')."  WHERE uid='{$uid}' AND  effective=1 LIMIT 1";
	return $db->getone($sql);
}
// 以套餐ID获取套餐信息
function get_setmeal_one($id)
{
	global $db;
	$id=intval($id);
	$sql = "select * from ".table('setmeal')."  WHERE id='{$id}' LIMIT 1";
	return $db->getone($sql);
}
//获取企业基本信息
function get_company($uid)
{
	global $db;
	$sql = "select * from ".table('company_profile')." where uid=".intval($uid)." LIMIT 1 ";
	return $db->getone($sql);
}
//获取职位列表
function get_jobs($offset,$perpage,$get_sql= '',$countresume=false,$countrecommend=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'],5,0,"...");
		$row['district_cn_']=cut_str($row['district_cn'],6,0,"...");

		$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['id']));
		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);
		}else{
			$row['countresume'] = 0;
		}
		if ($countrecommend)
		{
		$wheresql=" WHERE company_uid='{$row['uid']}' AND jobs_id= '{$row['id']}'";
		$row['countrecommend']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('hunter_recommend').$wheresql);
		}else{
			$row['countrecommend'] = 0;
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
//获取单条职位
function get_jobs_one($id,$parentuid='')
{
	global $db,$timestamp;
	$id=intval($id);
	if (!empty($parentuid)) $wheresql=" AND parentuid=".intval($parentuid);
	$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>";
	return $val;
}
//删除职位
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_tmp')." 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;
	if (!$db->query("Delete from ".table('jobs_category')." WHERE pid IN ({$sqlin}) {$uidsql}")) return false;
	if (!$db->query("Delete from ".table('jobs_trade')." WHERE pid IN ({$sqlin}) {$uidsql}")) return false;
	write_memberslog($_SESSION['uid'],1,2003,$_SESSION['username'],"/",$_SESSION['parentuid'],"删除职位({$sqlin})",0);
	}
	return $return;
}
//激活或者结束职位
function activate_jobs($idarr,$display,$parentuid)
{
	global $db,$timestamp;
	$display=intval($display);	
	$parentuid=intval($parentuid);
	$uidsql=" AND parentuid='{$parentuid}'";
	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 display='{$display}',finishtime='{$timestamp}' WHERE id IN ({$sqlin}) {$uidsql}")) return false;
		distribution_jobs($idarr,$parentuid);
		write_memberslog($_SESSION['uid'],1,2005,$_SESSION['username'],"/",$_SESSION['parentuid'],"将职位激活状态设为:{$display},职位ID为：{$sqlin}",0);
		return true;
	}
	return false;
}
//推广或取消职位推广
function recommend_jobs($idarr,$recommend,$parentuid)
{
	global $db,$timestamp;
	$recommend=intval($recommend);	
	$parentuid=intval($parentuid);
	$uidsql=" AND parentuid='{$parentuid}'";
	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;
		
		write_memberslog($_SESSION['uid'],1,2005,$_SESSION['username'],"/",$_SESSION['parentuid'],"将职位推广状态设为:{$recommend},职位ID为：{$sqlin}",0);
		return true;
	}
	return false;
}
function refresh_jobs($id,$uid)
{
	global $db,$timestamp;
	$uid=intval($uid);
	if (!is_array($id)) $id=array($id);
	$sqlin=implode(",",$id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
	if (!$db->query("update  ".table('company_profile')."  SET refreshtime='{$timestamp}' WHERE uid='{$uid}' LIMIT 1 ")) return false;
	if (!$db->query("update  ".table('jobs')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_hot')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_key')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_rtime')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_scale')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	if (!$db->query("update  ".table('jobs_search_recommend')."  SET refreshtime='{$timestamp}' WHERE id IN ({$sqlin})  AND uid='{$uid}'")) return false;
	return true;
	}
	return false;
}
//检测是否有职位操作权限
function check_jobs_permission($jobs_id,$uid){
	global $db;
	if (!is_array($jobs_id))$jobs_id=array($jobs_id);
	$uid = intval($uid);
	$user_info = get_user_info($uid);
	if($user_info['uid']==$user_info['parentuid']){
		return true;
	}else{
		foreach ($jobs_id as $value) {
			$jobs_info = $db->getone("select 1 from ".table('jobs')." where id=".$value." and uid=".$uid." limit 1");
			if(!$jobs_info){
				return false;
			}
		}
		return true;
	}
}
//管理模板
function get_company_jobs_templates($uid){
	global $db;
	$list = $db->getall("select * from ".table('company_jobs_templates')." where uid=".$uid);
	foreach ($list as $key => $value) {
		$list[$key]['title'] = cut_str($list[$key]['title'],12,0,"...");
	}
	return $list;
}
//获取单条职位模板
function get_company_jobs_templates_one($id,$uid){
	global $db;
	$list = $db->getone("select * from ".table('company_jobs_templates')." where id=".$id." and uid=".$uid);
	$tag_cn = "";
	if($list['tag']!=""){
		$arr = explode("|", $list['tag']);
		foreach ($arr as $key => $value) {
			$t_arr = explode(",",$value);
			$tag_cn .= $t_arr[1].",";
		}
	}
	$list['tag_cn'] = trim($tag_cn,",");
	return $list;
}
//删除职位模板
function del_company_jobs_templates($id,$uid){
	global $db;
	$id = intval($id);
	$db->query("delete from ".table('company_jobs_templates')." where id=".$id." and uid=".$uid." limit 1");
	return true;
}
//操作套餐
function action_user_setmeal($uid,$action)
{
	global $db;
	$sql="update ".table('members_setmeal')." set `".$action."`=".$action."-1  WHERE uid=".intval($uid)."  AND  effective=1 LIMIT 1";
    	return $db->query($sql);
}
//操作积分
function action_user_points($uid,$points,$action)
{
	global $db;
	if($action=='+'){
		$sql="update ".table('members_points')." set  points =points+".$points." WHERE uid=".intval($uid)." ";
	}else{
		$sql="update ".table('members_points')." set  points =points-".$points." WHERE uid=".intval($uid)." ";
	}
              return $db->query($sql);
}
//添加多选职位分类
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 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 company_trade_save($uid,$pid,$str){
 	global $db;
	$db->query("Delete from ".table('company_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('company_trade'),$setsqlarr))return false;
		}
	}
	return true;
}
function get_points_rule()
{
	global $db;
	$sql = "select * from ".table('members_points_rule')."  ORDER BY id asc";
	return $db->getall($sql);
}
/////*****************************招聘管理部分
function get_auditjobs($uid,$parent=false)
{
	global $db;
	$time = time();
	if($parent){
		$where = " and parentuid=".$uid." ";
	}else{
		$where = " and uid=".$uid." ";
	}
	return $db->getall("select * from ".table('jobs')." WHERE audit=1 and display=1 and (deadline>".$time." or deadline=0) and (setmeal_deadline>".$time." or setmeal_deadline=0) ".$where);
}
//收到应聘记录
function get_apply_jobs($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT {$offset},{$perpage}";
	$selectstr=" a.*,r.current_cn,r.fullname,r.sex_cn,r.birthdate,r.education_cn,r.experience_cn,r.residence_cn,r.refreshtime ";
	$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['jobs_name_']=cut_str($row['jobs_name'],10,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],6,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['age'] = date("Y")-$row["birthdate"];
		$row_arr[] = $row;
	}
	return $row_arr;
}
//删除 -收到应聘记录
function del_apply_jobs($del_id,$uid,$parentuid)
{
	global $db;
	if($uid==$parentuid){
		$uidsql=" AND belong_parentuid=".intval($parentuid)."";
	}else{
		$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;
	write_memberslog($_SESSION['uid'],1,1302,$_SESSION['username'],"/",$_SESSION['parentuid'],"删除收到的简历({$sqlin})",0);
	return true;
}
//获取推荐简历
function get_recommend_resume($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="c.*,r.current_cn,r.sex_cn,r.birthdate,r.education_cn,r.experience_cn,r.refreshtime ";
	$result = $db->query("SELECT ".$selectstr."  FROM ".table('hunter_recommend')." AS c ".$get_sql." ORDER BY c.did DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['jobs_name_']=$row['jobs_name'];
		$row['jobs_name']=cut_str($row['jobs_name'],10,0,"...");
		$jobs_info = $db->getone("select * from ".table('jobs')." where id = {$row['jobs_id']}");
		$row['reward'] = $jobs_info['reward'];

		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['jobs_id']));
		$row['age'] = date("Y")-$row['birthdate'];
		$hunter_profile=get_hunter($row['uid']);
		$row['hunter_name'] =$hunter_profile['fullname'];
		$row['hunter_name_']=$row['hunter_name'];
		$row['hunter_name']=cut_str($row['hunter_name'],3,0,"...");
		if($row['recommend_state']==0)
		{	
			$row['recommend_state_cn']="未处理";
		}
		elseif($row['recommend_state']==1)
		{
			$row['recommend_state_cn']="已邀请面试";
		}
		elseif($row['recommend_state']==2)
		{
			$row['recommend_state_cn']="已邀请入职";
		}
		else
		{
			$row['recommend_state_cn']="已拒绝";
		}
		$row_arr[] = $row;
	}
	return $row_arr;
}
//获取推荐人列表
function get_recommend_person_arr($uid)
{
	global $db;
	$result = $db->query("SELECT c.uid,p.fullname FROM ".table('hunter_recommend')." AS c left join ".table('hunter_profile')." as p on c.uid=p.uid where c.company_uid=".$uid." ORDER BY c.did DESC ");
	while($row = $db->fetch_array($result))
	{
		$row_arr[] = $row;
	}
	return $row_arr;
}
//已下载的简历列表
function get_down_resume($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT ".intval($offset).','.intval($perpage);
	$selectstr=" d.*,r.fullname,r.sex_cn,r.birthdate,r.education_cn,r.experience_cn,r.residence_cn,r.currentjobs,r.refreshtime ";
	$result = $db->query("SELECT ".$selectstr." FROM ".table('company_down_resume')." as d {$get_sql} ORDER BY d.down_addtime 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['currentjobs_']=cut_str($row['currentjobs'],7,0,"...");

		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		$row['age'] = date('Y')-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
function del_down_resume($id,$uid,$parentuid)
{
	global $db;
	if($parentuid==$uid){
		$wheresql=" AND company_parentuid={$uid}";
	}else{
		$wheresql=" AND company_uid={$uid}";
	}
	if (!is_array($id)) $id=array($id);
	$sqlin=implode(",",$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('company_down_resume')." WHERE did IN ({$sqlin}) {$wheresql}");
	$return=$return+$db->affected_rows();
	return $return;
	}
}
function check_down_resumeid($resume_id,$company_parentuid)
{
	global $db;
	$company_parentuid=intval($company_parentuid);
	$resume_id=intval($resume_id);
	$sql = "select did from ".table('company_down_resume')." WHERE company_parentuid = '{$company_parentuid}' 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 get_favorites($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="f.*,r.sex_cn,r.birthdate,r.education_cn,r.experience_cn,r.residence_cn,r.currentjobs,r.refreshtime ";
	$result = $db->query("SELECT ".$selectstr."  FROM ".table('company_favorites')." AS f ".$get_sql." ORDER BY f.did DESC ".$limit);
	while($row = $db->fetch_array($result))
	{
		$row['currentjobs_']=cut_str($row['currentjobs'],7,0,"...");
		$row['residence_cn_']=cut_str($row['residence_cn'],7,0,"...");

		$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['resume_id']));
		$row['age'] = date("Y")-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
function add_favorites($id,$company_parentuid,$company_uid)
{
	global $db,$_CFG;
	$timestamp=time();
	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,$company_parentuid))
			{
				$db->query("INSERT INTO ".table('company_favorites')." (resume_id,company_parentuid,company_uid,favorites_addtime) VALUES ('{$v}','{$company_parentuid}','{$company_uid}','{$timestamp}')");
				write_memberslog($_SESSION['uid'],1,5001,$_SESSION['username'],"/",$company_parentuid,"收藏简历{$v}",0);
				$i++;
			}
		}
		return $i;
}
//检测人才库中是否已经存在
function check_favorites($resume_id,$company_parentuid)
{
	global $db;
	$company_parentuid=intval($company_parentuid);
	$resume_id=intval($resume_id);
	$sql = "select * from ".table('company_favorites')." WHERE company_parentuid ='{$company_parentuid}' AND resume_id='{$resume_id}' LIMIT 1";
	$info=$db->getone($sql);
	if (empty($info))
	{
	return false;
	}
	else
	{
	return true;
	}
}
//删除 -人才库中的简历
function del_favorites($del_id,$uid,$parentuid)
{
	global $db;
	$uid=intval($uid);
	if($parentuid==$uid){
		$uidsql=" AND company_parentuid='{$parentuid}'";
	}else{
		$uidsql=" AND company_uid='{$uid}'";
	}
	
	if (!is_array($del_id)) $del_id=array($del_id);
	$sqlin=implode(",",$del_id);
	$return=0;
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("Delete from ".table('company_favorites')." WHERE did IN ({$sqlin}) {$uidsql}")) return false;
		$return=$return+$db->affected_rows();
		write_memberslog($_SESSION['uid'],$_SESSION['utype'],5002,$_SESSION['username'],"/",$parentuid,"删除收藏简历({$sqlin})",0);		
		return $return;
	}
}
//邀请记录列表
function get_interview($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	if(isset($offset)&&!empty($perpage)) $limit=" LIMIT ".$offset.','.$perpage;
	$selectstr="i.*,r.sex_cn,r.birthdate,r.education_cn,r.experience_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'],7,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['age'] = date("Y")-$row['birthdate'];
		$row_arr[] = $row;
	}
	return $row_arr;
}
function check_interview($resume_id,$jobs_id,$company_parentuid)
{
	global $db;
	$resume_id=intval($resume_id);
	$jobs_id=intval($jobs_id);
	$company_parentuid=intval($company_parentuid);
	$sql = "select * from ".table('company_interview')." WHERE belong_parentuid ='{$company_parentuid}' AND resume_id='{$resume_id}' AND jobs_id='{$jobs_id}' LIMIT 1";
	return $db->getone($sql);
}
//删除 -邀请记录
function del_interview($del_id,$uid,$parentuid)
{
	global $db;
	if($uid==$parentuid){
		$uidsql=" AND belong_parentuid=".intval($parentuid)."";
	}else{
		$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('company_interview')." WHERE did IN ({$sqlin}) {$uidsql}")) return false;
	write_memberslog($_SESSION['uid'],1,6002,$_SESSION['username'],"/",$parentuid,"删除面试邀请({$sqlin})",0);
	return true;
}
//获取全部搜索器
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 del_search_engine($id,$uid){
	global $db;
	$id = intval($id);
	$db->query("delete from ".table('search_resume_engine')." where id=".$id." and uid=".$uid." limit 1");
	return true;
}
// 获取企业 合作猎头
function get_company_hunter($parentuid)
{
	global $db;
	$parentuid=intval($parentuid);
	$sql="select * from ".table("entrust_hunter")." where entrust_type=1 and state=1 and company_uid=$parentuid ";
	return $db->getall($sql);
}
//得到所有企业子帐号会员信息
function get_com_user_info($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members')." where parentuid = '{$uid}' and uid != '{$uid}'";
	return $db->getall($sql);
}
//检查今天下载简历数是否达到
function check_today_down_resume($down_per_day,$company_parentuid,$talent)
{
	global $db;
	$down_per_day=intval($down_per_day);
	$company_parentuid=intval($company_parentuid);
	$starttime = strtotime(date("Y-m-d"));
	$endtime = strtotime(date("Y-m-d"))+3600*24;
	$sql = "select count(*) as num from ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_parentuid = '{$company_parentuid}' AND d.down_addtime>=".$starttime." AND d.down_addtime<=".$endtime." AND r.talent=".$talent;
	$count=$db->get_total($sql);
	if ($count>=$down_per_day)
	{
	return true;//达到
	}
	else
	{
	return false;//未达到
	}
}
//检查该猎头是否委托过
function check_hunter_entrust($hunter_id,$jobdid)
{
	global $db;
	$sql = "select id from ".table('entrust_hunter')." WHERE hunter_id = '".intval($hunter_id)."' AND jobs_id='".intval($jobdid)."' 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 count_down_resume_times_today($uid,$talent){
	global $db;
	$starttime = strtotime(date("Y-m-d"));
	$sql = "select count(*) as num from ".table('company_down_resume')." where company_parentuid=".$uid." and talent=".$talent." and down_addtime>=".$starttime;
	return $db->get_total($sql);
}
//检查举报简历
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 del_account($uid){
	global $db;
	$uid = intval($uid);
	$db->query("delete from ".table('members')." where uid=".$uid." limit 1");
	return true;
}
// 购买套餐,充值
/* ======积分冲值部分====== */
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,$amount_points,$payment_name,$description,$addtime,$points='',$setmeal='',$utype='1')
{
	global $db;
	$setsqlarr['uid']=intval($uid);
	$setsqlarr['oid']=$oid;
	$setsqlarr['amount']=$amount;
	$setsqlarr['amount_points']=$amount_points;
	$setsqlarr['payment_name']=$payment_name;
	$setsqlarr['description']=$description;
	$setsqlarr['addtime']=$addtime;
	$setsqlarr['points']=$points;
	$setsqlarr['setmeal']=$setmeal;
	$setsqlarr['utype']=$utype;
	write_memberslog($uid,$_SESSION['utype'],3001,$_SESSION['username'],"添加订单，编号{$oid}，金额{$amount}元,购买套餐",$_SESSION['parentuid'],"添加订单",0);
	$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_list($offset,$perpage,$get_sql='')
{
	global $db;
	if(isset($offset)&&!empty($perpage))
	{
	$limit=" LIMIT {$offset},{$perpage}";
	}
	$arr = $db->getall($get_sql.$limit);
	foreach ($arr as $key => $value)
	{
		$sql="select * from ".table("payment")." where typename='{$value[payment_name]}' limit 1 ";
		$payment = $db->getone($sql);
		$value['payment_name_cn']=$payment['byname'];
		$value['description_']=cut_str($value['description'], 6, $start=0);
		$arr[$key]=$value;
	}
	return $arr;
}
//获取单条订单
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($order['uid'],$user['utype'],9001,$user['username'],$notes,0,"冲值积分",1);			
			}
			elseif ($order['setmeal']>0)
			{
					// 积分抵现
					if($order['amount_points']>0)
					{
						report_deal($order['uid'],2,$order['amount_points']);
						$user_points=get_user_points($order['uid']);
						write_memberslog($_SESSION['uid'],1,9003,$_SESSION['username'],"积分:-{$order['amount_points']},剩余:{$user_points}",$_SESSION['parentuid'],"积分抵现",1);
					}
					set_members_setmeal($order['uid'],$order['setmeal']);
					$setmeal=get_setmeal_one($order['setmeal']);
					$notes=date('Y-m-d H:i',time())."通过：".get_payment_info($order['payment_name'],true)." 成功充值 ".$order['amount']."元并开通{$setmeal['setmeal_name']}";
					write_memberslog($order['uid'],1,9002,$user['username'],$notes);
			}
			else
			{
				action_user_account($order['uid'],1,$order['amount'],1);
				write_account_log($user['uid'],$user['parentuid'],$user['utype'],$user['username'],"充值账户金额：".$order['amount'],"已充值");
			}
		//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_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($account_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);
}
/*
	@账户 冻结资金操作
	@$uid 会员uid(主uid)
	@$num(操作金额)
	@$action操作类型(1==>加,2==>减)
*/
function action_user_account_blocked($uid,$num,$action="1")
{
	global $db;
	$uid=intval($uid);
	$num=floatval($num);
	$action=intval($action);
	$account_arr=get_user_account($uid);
	if($action==1)
	{
		$update_arr['blocked_funds']=$account_arr['blocked_funds']+$num;
		$update_arr['available_funds']=$account_arr['available_funds']-$num;
	}
	else
	{
		$update_arr['blocked_funds']=$account_arr['blocked_funds']-$num;
		$update_arr['available_funds']=$account_arr['available_funds']+$num;
	}
	updatetable(table('account'),$update_arr,array("uid"=>$uid));
}
/*
	@删除委托信息
	@$del_id 记录id
	@$uid  企业会员uid
*/
function del_entrust($del_id,$uid)
{
	global $db;
	$return=0;
	$uidsql=" AND company_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;
}
// 获取猎头信息
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;
}
/*
	@获取 		下载的通讯录信息
	@$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);
}
?>