<?php
 /*
 * 74cms 管理中心 企业用户相关函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
 if(!defined('IN_QISHI'))
 {
die('Access Denied!');
 }
 //******************************职位部分**********************************
//获取职位信息列表
function get_jobs($offset,$perpage,$get_sql= '')
{
	global $db,$timestamp;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query($get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['jobs_name']=cut_str($row['jobs_name'],12,0,"...");
	if (!empty($row['highlight']))
	{
	$row['jobs_name']="<span style=\"color:{$row['highlight']}\">{$row['jobs_name']}</span>";
	}
	$row['companyname']=cut_str($row['companyname'],18,0,"...");
	$row['company_url']=url_rewrite('QS_companyshow',array('id'=>$row['company_id']));
	$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['id']));
	
	$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);
	$row_arr[] = $row;
	}
	return $row_arr;
}
function distribution_jobs($pid)
{
	global $db,$_CFG;
	if (!is_array($pid))$pid=array($pid);
	$time=time();
	foreach($pid as $v)
	{
		$pid=intval($v);
	
		$t1=$db->getone("select * from ".table('jobs')." where id='{$pid}' LIMIT 1");
  		$db->query("Delete from ".table('jobs_search_hot')." WHERE id='{$v}' LIMIT 1");
		$db->query("Delete from ".table('jobs_search_key')." WHERE id='{$v}' LIMIT 1");
		$db->query("Delete from ".table('jobs_search_rtime')." WHERE id='{$v}' LIMIT 1");
		$db->query("Delete from ".table('jobs_search_scale')." WHERE id='{$v}' LIMIT 1");
		$db->query("Delete from ".table('jobs_search_recommend')." WHERE id='{$v}' 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}");

			$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}");

		}
	}
}
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);
	}
}
function get_jobs_one($id)
{
	global $db;
	$id=intval($id);
	$tb1=$db->getone("select * from ".table('jobs')." where id='{$id}' LIMIT 1");
	$val=!empty($tb1)?$tb1:$tb2;
	$val['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$val['id']));
	$val['company_url']=url_rewrite('QS_companyshow',array('id'=>$val['company_id']));
	$val['user']=get_user($val['uid']);
	$val['contact']=$db->getone("select * from ".table('jobs_contact')." where pid='{$id}' LIMIT 1");
	return $val;
}
//删除职位
function del_jobs($id)
{
	global $db;
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	$return=0;
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("Delete from ".table('jobs')." WHERE id IN ({$sqlin})")) 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})")) return false;
		if (!$db->query("Delete from ".table('jobs_search_key')." WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("Delete from ".table('jobs_search_rtime')." WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("Delete from ".table('jobs_search_scale')." WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("Delete from ".table('jobs_search_recommend')." WHERE id IN ({$sqlin})")) return false;
		return $return;
	}
	else
	{
	return false;
	}
}
//修改职位审核状态
function edit_jobs_audit($id,$audit)
{
	global $db,$_CFG;
	$audit=intval($audit);
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	$return=0;
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$jobsarry = $db->getall("SELECT * FROM ".table('jobs')." WHERE id IN ({$sqlin}) ");
		foreach ($jobsarry as $key => $jobs)
		{
			if($jobs['jobs_type']!=2)
			{
				$db->query("update  ".table('jobs')." SET audit={$audit}  WHERE id=$jobs[id]");
				$return++;
			}
			else
			{
				$user_account=get_user_account($jobs['parentuid']);
				if($audit=="1")
				{
					if($jobs['reward']>$user_account['available_funds'])
					{
						break;
					}
					else
					{
						if($jobs['is_bloked']!=1)
						{
							action_user_account_blocked($jobs['parentuid'],$jobs['reward'],1);
							write_account_log($jobs['uid'],$jobs['parentuid'],1,"管理员","您发布的委托职位：".$jobs['jobs_name']."审核通过,冻结保证金:".$jobs['reward']."元","操作成功");
							$db->query("update ".table("jobs")." set is_bloked=1 where id=$jobs[id]");
						}
						$db->query("update  ".table('jobs')." SET audit={$audit} WHERE id=$jobs[id]");
						$return++;
					}
				}
				else
				{
					if($jobs['is_bloked']==1)
					{
						action_user_account_blocked($jobs['parentuid'],$jobs['reward'],2);
						write_account_log($jobs['uid'],$jobs['parentuid'],1,"管理员","您发布的委托职位：".$jobs['jobs_name']."审核未通过,退还保证金:".$jobs['reward']."元","操作成功");
						$db->query("update ".table("jobs")." set is_bloked=0 where id=$jobs[id]");
					}
					$db->query("update  ".table('jobs')." SET audit={$audit} WHERE id=$jobs[id]");
					$return++;
				}
			}
			distribution_jobs($jobs['id']);
		}
			//审核原因表
			$reason=$reason==''?'原因：未知':'原因：'.$reason;
			$reason_result = $db->query("SELECT  uid,id,company_id,jobs_name FROM ".table('jobs')." WHERE id IN ({$sqlin})");
			while($list = $db->fetch_array($reason_result))
			{
				//审核原因表
				$auditsqlarr['jobs_id']=$list['id'];
				$auditsqlarr['company_id']=$list['company_id'];
				$auditsqlarr['reason']=$audit=='1'?"您发布的职位：{$list['jobs_name']},成功通过网站管理员审核！":"您发布的职位：{$list['jobs_name']},未通过网站管理员审核,{$reason}";
				$auditsqlarr['addtime']=time();
				inserttable(table('audit_reason'),$auditsqlarr);
				//消息通知
				$user_info=get_user($list['uid']);
				$pmssql['message']=$audit=='1'?"您发布的职位：{$list['jobs_name']},成功通过网站管理员审核！":"您发布的职位：{$list['jobs_name']},未通过网站管理员审核,{$reason}";
				$pmssql['spms_usertype']=1;
				$pmssql['uid']=$list['uid'];
				$pmssql['spms_type']=2;
				$pmssql['dateline']=time();
				inserttable(table('pms_sys'),$pmssql);
			 }
			//发送邮件
			$mailconfig=get_cache('mailconfig');
			$sms=get_cache('sms_config');
			if ($audit=="1" && $mailconfig['set_jobsallow']=="1")//审核通过
			{
					$result = $db->query("SELECT * FROM ".table('jobs')." WHERE id IN ({$sqlin}) ");
					while($list = $db->fetch_array($result))
					{
						$user_info=get_user($list['uid']);
						if ($user_info['email_audit']=="1")
						{				
						dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&jobs_name=".$list['jobs_name']."&act=set_jobsallow");
						}
					}
			}
			if ($audit=="3" && $mailconfig['set_jobsnotallow']=="1")//审核未通过
			{
					$result = $db->query("SELECT * FROM ".table('jobs')." WHERE id IN ({$sqlin}) ");
					while($list = $db->fetch_array($result))
					{
						$user_info=get_user($list['uid']);
						if ($user_info['email_audit']=="1")
						{
						dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&jobs_name=".$list['jobs_name']."&act=set_jobsnotallow");
						}
					}
			}
			//sms		
			if ($audit=="1" && $sms['open']=="1" && $sms['set_jobsallow']=="1" )
			{
				$result = $db->query("SELECT * FROM ".table('jobs')." WHERE id IN ({$sqlin})  ");
					while($list = $db->fetch_array($result))
					{
						$user_info=get_user($list['uid']);
						if ($user_info['mobile_audit']=="1")
						{
						dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_sms.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&jobs_name=".$list['jobs_name']."&act=set_jobsallow");
						}
					}
			}
			//sms
			if ($audit=="3" && $sms['open']=="1" && $sms['set_jobsnotallow']=="1" )//认证未通过
			{
				$result = $db->query("SELECT * FROM ".table('jobs')." WHERE id IN ({$sqlin})  ");
					while($list = $db->fetch_array($result))
					{
						$user_info=get_user($list['uid']);
						if ($user_info['mobile_audit']=="1")
						{
						dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_sms.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&jobs_name=".$list['jobs_name']."&act=set_jobsnotallow");
						}
					}
			}
			//sms
		return $return;
	}
	else
	{
	return $return;
	}
}
function edit_jobs_display($id,$display)
{
	global $db;
	$display=intval($display);
	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('jobs')." SET display='{$display}'  WHERE id IN ({$sqlin})")) return false;
		distribution_jobs($id);
		return true;
	}
	return false;
}
function get_user($uid)
{
	global $db;
	$sql = "select * from ".table('members')." where uid=".intval($uid)." LIMIT 1";
	return $db->getone($sql);
}
function refresh_company($uid,$refresjobs=false)
{
	global $db;
	$return=0;
	if (!is_array($uid))$uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("update  ".table('company_profile')." SET refreshtime='".time()."'  WHERE uid IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
		if ($refresjobs)
		{
		if (!$db->query("update  ".table('jobs')." SET refreshtime='".time()."'  WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_hot')."  SET refreshtime='{$time}' WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_key')."  SET refreshtime='{$time}' WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_rtime')."  SET refreshtime='{$time}' WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_scale')."  SET refreshtime='{$time}' WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_recommend')."  SET refreshtime='{$time}' WHERE uid IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
		}
	}
	return $return;
}
function refresh_jobs($id)
{
	global $db;
	$return=0;
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	$time=time();
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("update  ".table('jobs')." SET refreshtime='{$time}'  WHERE id IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
		if (!$db->query("update  ".table('jobs_search_hot')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_key')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_rtime')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_scale')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs_search_recommend')."  SET refreshtime='{$time}' WHERE id IN ({$sqlin})")) return false;
	}
	return $return;
}
function delay_jobs($id,$days)
{
	global $db;
	$days=intval($days);
	$return=0;
	if (empty($days)) return false;
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	$time=time();
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$result = $db->query("SELECT id,deadline FROM ".table('jobs')." WHERE id IN ({$sqlin}) ");
		while($row = $db->fetch_array($result))
		{
			if ($row['deadline']>$time)
			{
			$deadline=strtotime("{$days} day",$row['deadline']);
			}
			else
			{
			$deadline=strtotime("{$days} day");
			}
			if (!$db->query("update  ".table('jobs')." SET deadline='{$deadline}'  WHERE id='{$row['id']}'  LIMIT 1")) return false;
			$return=$return+$db->affected_rows();
		}
	}
	return $return;
	
}
function delay_meal($id,$days)
{
	global $db;
	$days=intval($days);
	$return=0;
	if (empty($days)) return false;
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	$time=time();
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$result = $db->query("SELECT id,uid,endtime FROM ".table('members_setmeal')." WHERE uid IN ({$sqlin})");
		while($row = $db->fetch_array($result))
		{
			if($row['endtime']=="0")
			{
			continue;
			}
			else
			{
				if ($row['endtime']>$time)
				{
				$deadline=strtotime("{$days} day",$row['endtime']);
				}
				else
				{
				$deadline=strtotime("{$days} day");
				}
				if (!$db->query("update  ".table('members_setmeal')." SET endtime='{$deadline}'  WHERE id='{$row['id']}'  LIMIT 1")) return false;
				$return=$return+$db->affected_rows();
				if (!$db->query("update  ".table('jobs')." SET setmeal_deadline='{$deadline}'  WHERE uid='{$row['uid']}' ")) return false;
			}
		}
	}
	return $return;
	
}
//******************************企业部分**********************************
 //获取企业列表
function get_company($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT c.*,m.username FROM ".table('company_profile')." AS c ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['company_url']=url_rewrite('QS_companyshow',array('id'=>$row['id']));
	$row_arr[] = $row;
	}
	return $row_arr;
}
//获取单条企业资料
function get_company_one_id($id)
{
	global $db;
	$id=intval($id);
	$sql = "select * from ".table('company_profile')." where id='{$id}'";
	$val=$db->getone($sql);
	$val['user']=get_user($val['uid']);
	return $val;
}
function get_company_one_uid($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('company_profile')." where uid={$uid}";
	$val=$db->getone($sql);
	return $val;
}
//更改企业认证状态
function edit_company_audit($uid,$audit)
{
	global $db,$_CFG;	
	$audit=intval($audit);
	if (!is_array($uid)) $uid=array($uid);
	$sqlin=implode(",",$uid);	
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("update  ".table('company_profile')." SET audit='{$audit}'  WHERE uid IN ({$sqlin})")) return false;
		if (!$db->query("update  ".table('jobs')." SET company_audit='{$audit}'  WHERE uid IN ({$sqlin})")) return false;
		if ($audit=="1")
		{
			$points_rule=get_cache('points_rule');
			if ($points_rule['company_register_pass']['value']>0)//如果设置了认证赠送积分
			{
				gift_points($sqlin,'companyauth',$points_rule['company_register_pass']['type'],$points_rule['company_register_pass']['value']);
			}
			//设置套餐为基本版
			foreach ($uid as $key => $value) {
				set_members_setmeal($value,2);
			}		
		}
		$mailconfig=get_cache('mailconfig');
		$sms=get_cache('sms_config');
		if ($audit=="1" && $mailconfig['set_licenseallow']=="1")//认证通过
		{
			$result = $db->query("SELECT * FROM ".table('company_profile')." WHERE uid IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
					$user_info=get_user($list['uid']);
					if ($user_info['email_audit']=="1")
					{
					dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid={$list['uid']}&key=".asyn_userkey($list['uid'])."&act=set_licenseallow");
					}
				}
		}
		if ($audit=="3" && $mailconfig['set_licensenotallow']=="1")//认证未通过
		{
			$result = $db->query("SELECT * FROM ".table('company_profile')." WHERE uid IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
					$user_info=get_user($list['uid']);
					if ($user_info['email_audit']=="1")
					{
					dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid={$list['uid']}&key=".asyn_userkey($list['uid'])."&act=set_licensenotallow");
					}
				}
		}
		//sms		
		if ($audit=="1" && $sms['open']=="1" && $sms['set_licenseallow']=="1" )
		{
			$result = $db->query("SELECT * FROM ".table('company_profile')." WHERE uid IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
					$user_info=get_user($list['uid']);
					if ($user_info['mobile_audit']=="1")
					{
					dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_sms.php?uid={$list['uid']}&key=".asyn_userkey($list['uid'])."&act=set_licenseallow");
					}
				}
		}
		//sms
		if ($audit=="3" && $sms['open']=="1" && $sms['set_licensenotallow']=="1" )//认证未通过
		{
			$result = $db->query("SELECT * FROM ".table('company_profile')." WHERE uid IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
					$user_info=get_user($list['uid']);
					if ($user_info['mobile_audit']=="1")
					{
					dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_sms.php?uid={$list['uid']}&key=".asyn_userkey($list['uid'])."&act=set_licensenotallow");
					}
				}
		}
		//sms
	distribution_jobs_uid($uid);
	return true;
	}
	return false;
}
//删除企业资料，uid=array
function del_company($uid)
{
	global $db,$certificate_dir;
	if (!is_array($uid))$uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$result = $db->query("SELECT certificate_img FROM ".table('company_profile')." WHERE uid IN ({$sqlin})");
		while($row = $db->fetch_array($result))
		{
		@unlink($certificate_dir.$row['certificate_img']);
		}
		if (!$db->query("Delete from ".table('company_profile')." WHERE uid IN ({$sqlin})")) return false;
	return true;
	}
	return false;
}
function del_company_alljobs($uid)
{
	global $db;
	if (!is_array($uid))$uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$result = $db->query("SELECT id FROM ".table('jobs')." WHERE uid IN ({$sqlin}) ");
		while($row = $db->fetch_array($result))
		{
		$db->query("Delete from ".table('jobs_contact')." WHERE pid IN ({$row['id']})");	
		}
		$db->query("Delete from ".table('jobs')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_category')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_trade')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_search_hot')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_search_key')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_search_rtime')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_search_scale')." WHERE uid IN ({$sqlin})");
		$db->query("Delete from ".table('jobs_search_recommend')." WHERE uid IN ({$sqlin})");
		return true;
	}
	return false;
}
//******************************订单管理**********************************
//订单列表
function get_order_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT o.*,m.username,m.email,c.companyname FROM ".table('order')." as o ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['payment_name']=get_payment_info($row['payment_name'],true);
	$row_arr[] = $row;
	}
	return $row_arr;
}
//获取订单
function get_order_one($id=0)
{
	global $db;
	$sql = "select * from ".table('order')." where id=".intval($id)." LIMIT 1";
	$val=$db->getone($sql);
	$val['payment_name']=get_payment_info($val['payment_name'],true);
	$val['payment_username']=get_user($val['uid']);
	return $val;
}
//取消订单
function del_order($id)
{
	global $db;
	if (!is_array($id))$id=array($id);
	$sqlin=implode(",",$id);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if (!$db->query("Delete from ".table('order')." WHERE id IN (".$sqlin.")  AND is_paid=1 ")) return false;		
		return true;
	}
	return false;
}
//获取充值支付方式名称
function get_payment_info($typename,$name=false)
{
	global $db;
	$sql = "select * from ".table('payment')." where typename ='".$typename."'";
	$val=$db->getone($sql);
	if ($name)
	{
	return $val['byname'];
	}
	else
	{
	return $val;
	}
}
function get_meal_members_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT * FROM ".table('members_setmeal')." as a ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row_arr[] = $row;
	}
	return $row_arr;
}
function get_member_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT m.*,c.companyname,c.id,c.addtime FROM ".table('members')." as m ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['company_url']=url_rewrite('QS_companyshow',array('id'=>$row['id']));
	if($row['uid']==$row['parentuid']){
		$row['main_user'] = 1;
		$row['main'] = array();
	}else{
		$row['main_user'] = 0;
		$row['main'] = get_user($row['parentuid']);
	}
	$row_arr[] = $row;
	}
	return $row_arr;
}
function delete_company_user($uid)
{
	global $db;
	if (!is_array($uid))$uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		if(defined('UC_API'))
		{
			include_once(QISHI_ROOT_PATH.'uc_client/client.php');
			foreach($uid as $tuid)
			{
			$userinfo=get_user($tuid);
			$uc_user=uc_get_user($userinfo['username']);
			$uc_uid_arr[]=$uc_user[0];
			}
			uc_user_delete($uc_uid_arr);
		}
		if (!$db->query("Delete from ".table('members')." WHERE uid IN (".$sqlin.")")) return false;
		if (!$db->query("Delete from ".table('members')." WHERE parentuid IN (".$sqlin.")")) return false;
		if (!$db->query("Delete from ".table('members_log')." WHERE log_uid IN (".$sqlin.")")) return false;
		if (!$db->query("Delete from ".table('members_points')." WHERE uid IN (".$sqlin.")")) return false;
		if (!$db->query("Delete from ".table('order')." WHERE uid IN (".$sqlin.")")) return false;
		if (!$db->query("Delete from ".table('members_setmeal')." WHERE uid IN (".$sqlin.")")) return false; 
		return true;		
	}
	return false;
}
//******************************其他**********************************
//获取会员信息，返回用户名等相关信息
function get_user_points($uid)
{
	global $db;
	$sql = "select * from ".table('members_points')." where uid = ".intval($uid)."  LIMIT 1 ";
	$points=$db->getone($sql);
	return $points['points'];
}
//获取积分规则
function get_points_rule($utype = '')
{
	global $db; 
	if(!empty($utype)){
		$sql = "select * from ".table('members_points_rule')." where utype='{$utype}' order BY id asc";
	}else{
		$sql = "select * from ".table('members_points_rule')."  order BY id asc";
	}
	$list=$db->getall($sql);
	return $list;
}
//-------------------------------------------------------
//付款后开通
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($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['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'],1,9001,$user['username'],$notes);
				
			}
			if ($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($user['uid'],1,9003,$user['username'],"积分:-{$order['amount_points']},剩余:{$user_points}",$user['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'],"已充值");
			}
		//发送邮件
		$mailconfig=get_cache('mailconfig');
		if ($mailconfig['set_payment']=="1" && $user['email_audit']=="1")
		{
		dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid=".$order['uid']."&key=".asyn_userkey($order['uid'])."&act=set_payment");
		}
		//发送邮件完毕
		//sms
		$sms=get_cache('sms_config');
		if ($sms['open']=="1" && $sms['set_payment']=="1"  && $user['mobile_audit']=="1")
		{
		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 report_deal($uid,$i_type=1,$points=0)
{
		global $db,$timestamp;
		$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 ";
		return $db->query($sql);
}
function gift_points($uid,$gift,$ptype,$points)
{
	 global $db;
	 $operator=$ptype=="1"?"+":"-";
	 $time=time();
	 if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$uid))
	 {
		$uid=explode(',',$uid);
	 }
	 if (!is_array($uid))$uid=array($uid);
	 if (!empty($uid) && is_array($uid))
	 {
	 	foreach($uid as $vuid)
		{
			$vuid=intval($vuid);
			if ($gift=='companyauth')
			{
				$com=$db->getone("SELECT uid FROM ".table('members_handsel')." WHERE uid ='{$vuid}' AND htype='{$gift}'  LIMIT 1");
				if(empty($com))
				{
				report_deal($vuid,$ptype,$points);
				$user=get_user($vuid);
				$mypoints=get_user_points($vuid);
				write_memberslog($vuid,1,9001,$user['username']," 成为已认证企业({$operator}{$points})，(剩余:{$mypoints})");
				$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,addtime) VALUES ('{$vuid}', '{$gift}','{$time}')");			
				}
			}			
		}
	 }
}
function get_setmeal($apply=true)
{
	global $db;
	if ($apply==true)
	{
	$where="";
	}
	else
	{
	$where=" WHERE display=1 ";
	} 
	$sql = "select * from ".table('setmeal').$where."  order BY display desc,show_order desc,id asc";
	return $db->getall($sql);
}
function get_setmeal_one($id)
{
	global $db;
	$sql = "select * from ".table('setmeal')."  WHERE id=".intval($id)."";
	return $db->getone($sql);
}
function get_user_setmeal($uid)
{
	global $db;
	$sql = "select * from ".table('members_setmeal')."  WHERE uid=".intval($uid)." AND  effective=1 LIMIT 1";
	return $db->getone($sql);
}
function del_setmeal_one($id)
{
	global $db;
	if (!$db->query("Delete from ".table('setmeal')." WHERE id=".intval($id)." ")) return false;
	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," parentuid='{$parentuid}'")) return false;
	distribution_jobs_uid($uid);
	return true;
}
 function get_consultant($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT * FROM ".table('consultant').$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
		$row_arr[] = $row;
	}
	return $row_arr;
}
 function get_consultant_one($id)
{
	global $db;
	$result = $db->getone("SELECT * FROM ".table('consultant')." where id=".$id);
	return $result;
}
function del_consultant($id){
	global $db;
	$db->query("delete from ".table('consultant')." where id=".$id);
	return true;
}
 //获取企业图片
function get_company_img($company_id)
{
	global $db;
	$result = $db->getall("SELECT * FROM ".table('company_img')." where company_id=".$company_id);
	return $result;
}
//删除公司图片
function del_company_img($id)
{
	global $db;
	if (!is_array($id)) $id=array($id);
	$sqlin=implode(",",$id);
	if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin)) return false;
	
	$result = $db->query("SELECT * FROM ".table('company_img')." WHERE id IN ({$sqlin})");
	while($list = $db->fetch_array($result))
	{
		@unlink("../data/companyimg/original/".$list['img']);//删除原图
		@unlink("../data/companyimg/thumb/".$list['img']);//删除缩略图
	}
	if (!$db->query("Delete from ".table('company_img')." WHERE id IN ({$sqlin})")) return false;
	return $db->affected_rows();
}
//添加职位多选行业
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_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);
	$action=intval($action);
	$utype=intval($utype);
	//判断账户,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_withdrawal_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT o.*,m.username,m.email,c.companyname FROM ".table('account_withdrawal')." as o ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['payment_name']=get_payment_info($row['extract_pay_type'],true);
	// 当前账户情况
	$row['user_account']=get_user_account($row['uid']);
	$row_arr[] = $row;
	}
	return $row_arr;
}
// 单条提现记录
function get_withdrawal_one($id=0)
{
	global $db;
	$sql = "select * from ".table('account_withdrawal')." where id=".intval($id)." LIMIT 1";
	$val=$db->getone($sql);
	$val['payment_name']=get_payment_info($val['extract_pay_type'],true);
	$val['payment_username']=get_user($val['uid']);
	$val['user_account']=get_user_account($val['uid']);
	return $val;
}
/*
	@后台设置提现成功！
	@先判断用户当前余额,扣除余额	
*/
function withdrawal_set($id)
{	
	global $db;
	$id=intval($id);
	$withdrawal_one=get_withdrawal_one($id);
	$user=get_user($withdrawal_one['uid']);
	// 根据当前账户余额判断是否能够发放提现资金
	$user_account=get_user_account($withdrawal_one['uid']);

	if($withdrawal_one['extract_money']>$user_account['available_funds'])
	{
		return false;
	}
	else
	{
		$time=time();
		action_user_account($withdrawal_one['uid'],1,$withdrawal_one['extract_money'],2);
		$db->query("update ".table("account_withdrawal")." set state=2,payment_time=$time where id=$id and uid=$withdrawal_one[uid]" );
		write_account_log($user['uid'],$user['parentuid'],$user['utype'],$user['username'],"申请提现,提现金额：".$withdrawal_one['extract_money'],"已完成");
		return true;
	}	
}
/*
	@账户 冻结资金操作
	@$uid 会员uid(主uid)
	@$num(操作金额)
	@$action操作类型(1==>加,2==>减)
*/
function action_user_account_blocked($uid,$num,$action="1",$blocked_type=false)
{
	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;
		if($blocked_type)
		{
			$update_arr['available_funds']=$account_arr['available_funds']+$num;
		}
	}
	updatetable(table('account'),$update_arr,array("uid"=>$uid));
}
// 操作猎头经验
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_one_uid($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_one_uid($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('hunter_profile')." where uid={$uid}";
	$val=$db->getone($sql);
	return $val;
}
?>