<?php
 /*
 * 74cms 管理中心 猎头顾问用户相关函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
 if(!defined('IN_QISHI'))
 {
die('Access Denied!');
 }
 function get_points_rule()
{
	global $db;
	$sql = "select * from ".table('members_points_rule')." WHERE utype='3' order BY id asc";
	$list=$db->getall($sql);
	return $list;
}

function get_level_meal()
{
	global $db;
	$sql = "select * from ".table('hunter_level_meal').$where."  order BY id asc ";
	return $db->getall($sql);
}
function get_level_meal_one($id)
{
	global $db;
	$sql = "select * from ".table('hunter_level_meal')."  WHERE id=".intval($id)."";
	return $db->getone($sql);
}

function get_level_rule()
{
	global $db;
	$sql = "select * from ".table('hunter_level_rule')." order BY id asc";
	$list=$db->getall($sql);
	return $list;
}
function get_hunterjobs($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['companyname_']=cut_str($row['companyname'],18,0,"...");
		$row['companyname_note_']=cut_str($row['companyname_note'],18,0,"...");
		$row['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$row['id']));
		$row_arr[] = $row;
	}
	return $row_arr;
}
function del_hunterjobs($del_id)
{
	global $db;
	$return=0;
	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}) ")) return false;
	$return=$return+$db->affected_rows();
	}
	return $return;
}
function edit_jobs_audit($id,$audit,$reason,$pms_notice='1')
{
	global $db,$_CFG;
	$audit=intval($audit);
	$reason=trim($reason);
	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("update  ".table('jobs')." SET audit={$audit}  WHERE id IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
			//审核原因表
			$reason=$reason==''?'原因：未知':'原因：'.$reason;
			$result = $db->query("SELECT id,company_id,jobs_name FROM ".table('jobs')." WHERE id IN ({$sqlin})");
			while($list = $db->fetch_array($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);
			 }
			//发送站内信
			if ($pms_notice=='1')
			{
					$result = $db->query("SELECT uid,jobs_name FROM ".table('jobs')." WHERE id IN ({$sqlin})");
					$reason=$reason==''?'原因：未知':'原因：'.$reason;
					while($list = $db->fetch_array($result))
					{
						$user_info=get_user($list['uid']);
						$setsqlarr['message']=$audit=='1'?"您发布的高薪职位：{$list['jobs_name']},成功通过网站管理员审核！":"您发布的高薪职位：{$list['jobs_name']},未通过网站管理员审核,{$reason}";
						$setsqlarr['spms_usertype']=3;
						$setsqlarr['uid']=$list['uid'];
						$setsqlarr['spms_type']=2;
						$setsqlarr['dateline']=time();
						inserttable(table('pms_sys'),$setsqlarr);
					 }
			}
			//发送邮件
			$mailconfig=get_cache('mailconfig');
			if ($audit=="1" && $mailconfig['set_hunjobsallow']=="1")//审核通过
			{
					$result = $db->query("SELECT uid,jobs_name 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_hunjobsallow");
						}
					}
			}
			if ($audit=="3")//审核未通过
			{
					$result = $db->query("SELECT uid,jobs_name 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_hunjobsnotallow");
						}
					}
			}
			//sms
			$sms=get_cache('sms_config');		
			if ($audit=="1" && $sms['open']=="1" && $sms['set_hunjobsallow']=="1" )
			{
					$result = $db->query("SELECT uid,jobs_name 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_hunjobsallow");
						}
					}
			}
			if ($audit=="3" && $sms['open']=="1" && $sms['set_hunjobsnotallow']=="1" )//认证未通过
			{
					$result = $db->query("SELECT uid,jobs_name 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_hunjobsnotallow");
						}
					}
			}
		return $return;
	}
	else
	{
	return $return;
	}
}
function refresh_jobs($id)
{
	global $db;
	$return=0;
	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})  ")) return false;
		$return=$return+$db->affected_rows();
	}
	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 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}' and utype=3 {$wheresql} LIMIT 1");
	if (empty($val)) return false;
	$val['user']=get_user($val['uid']);
	$val['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$val['id']));
	$val['contact']=$db->getone("select * from ".table('jobs_contact')." where pid='{$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 get_hunter($offset,$perpage,$get_sql= '',$mode=1)
{
	global $db;
	$colum=$mode==1?'p.points':'p.setmeal_name';
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT c.*,m.reg_time,m.username,m.mobile,m.email as memail,{$colum} FROM ".table('hunter_profile')." AS c ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
		$row['hunter_url']=url_rewrite('QS_huntershow',array('id'=>$row['id']),false);
		$row_arr[] = $row;
	}
	return $row_arr;
}
function del_hunter($uid)
{
	global $db,$hunter_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 avatars FROM ".table('hunter_profile')." WHERE uid IN ({$sqlin})");
		while($row = $db->fetch_array($result))
		{
		@unlink($hunter_dir.$row['avatars']);
		@unlink($hunter_dir.'thumb/'.$row['avatars']);
		}
		if (!$db->query("Delete from ".table('hunter_profile')." WHERE uid IN ({$sqlin})")) return false;
	return true;
	}
	return false;
}
function del_hunter_alljobs($uid)
{
	global $db;
	if (!is_array($uid))$uid=array($uid);
	$sqlin=implode(",",$uid);
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$db->query("Delete from ".table('jobs')." WHERE uid IN ({$sqlin})");		
		return true;
	}
	return false;
}
function edit_hunter_audit($uid,$audit,$reason,$pms_notice)
{
	global $db,$_CFG;	
	$pms_notice=intval($pms_notice);
	$reason=trim($reason);
	if (!is_array($uid)) $uid=array($uid);
	$sqlin=implode(",",$uid);
	// 积分，经验规则
	$points_rule=get_cache('points_rule');
	$level_rule=get_cache('hunter_level_rule');
	if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
	{
		$result = $db->getall("SELECT * FROM ".table('hunter_profile')." WHERE uid IN ({$sqlin})");
		foreach ($result as $key => $value)
		{
			$hunter_mem = get_user($value['uid']);
			if($value['card_audit']!=1)
			{
				if ($audit['card_audit']=='1') 
				{
					// 实名认证成功加经验
					if($level_rule['audit_id_card']['type']==1 && $level_rule['audit_id_card']['value']<>0)
					{
						level_deal($value['uid'],1,$level_rule['audit_id_card']['value']);
						level_meal_deal($value['uid']);
						write_memberslog($value['uid'],3,1400,$hunter_mem['username'],"经验+".$level_rule['audit_id_card']['value'],0,"实名认证成功加经验",$mode=1);
					}
				}
			}
			if($value['belongcompany_audit']!=1)
			{
				if ($audit['belongcompany_audit']=='1') 
				{
					// 从业认证成功加经验
					if($level_rule['audit_qualification']['type']==1 && $level_rule['audit_qualification']['value']<>0)
					{
						level_deal($value['uid'],1,$level_rule['audit_qualification']['value']);
						level_meal_deal($value['uid']);
						write_memberslog($value['uid'],3,1400,$hunter_mem['username'],"经验+".$level_rule['audit_qualification']['value'],0,"从业认证成功加经验",$mode=1);
					}
				}
			}
			if($value['avatars_audit']!=1)
			{
				if ($audit['avatars_audit']=='1') 
				{
					// 照片认证成功加经验
					if($level_rule['audit_work_photo']['type']==1 && $level_rule['audit_work_photo']['value']<>0)
					{
						level_deal($value['uid'],1,$level_rule['audit_work_photo']['value']);
						level_meal_deal($value['uid']);
						write_memberslog($value['uid'],3,1400,$hunter_mem['username'],"经验+".$level_rule['audit_work_photo']['value'],0,"照片认证成功加经验",$mode=1);
					}
				}
			}
			if( $value['card_audit']!=1 || $value['belongcompany_audit']!=1 || $value['avatars_audit']!=1)
			{
				
				if ($audit['card_audit']=='1' && $audit['belongcompany_audit']=='1' && $audit['avatars_audit']=='1') 
				{
					// 认证通过加积分
					if($points_rule['hunter_reg_audit']['type']==1 && $points_rule['hunter_reg_audit']['value']<>0)
					{
						report_deal($value['uid'],$i_type=1,$points_rule['hunter_reg_audit']['value']);
						$porint=get_user_points($value['uid']);
						write_memberslog($value['uid'],3,9003,$hunter_mem['username'],"积分+".$points_rule['hunter_reg_audit']['value'],0,"猎头认证通过",$mode=1);
					}
				}
			}
		}
		//发送站内信
		if ($pms_notice=='1')
		{
			$reason=$reason==''?'无':$reason;
			$note="";
			if($audit['card_audit']=='1') {$note.='实名认证通过,';}elseif($audit['card_audit']=='3'){$note.='实名认证未通过';}
			if($audit['belongcompany_audit']=='1') {$note.='从业认证通过,';}elseif($audit['belongcompany_audit']=='3'){$note.='从业认证未通过';}
			if($audit['avatars_audit']=='1') {$note.='照片认证通过,';}elseif($audit['avatars_audit']=='3'){$note.='照片认证未通过';}
			$result = $db->query("SELECT fullname,uid FROM ".table('hunter_profile')." WHERE uid IN ({$sqlin})");
			
			while($list = $db->fetch_array($result))
			{
				$setsqlarr['message']="您注册的猎头顾问：{$list['fullname']},".$note.'其他说明：'.$reason;
				$setsqlarr['spms_usertype']=3;
				$setsqlarr['uid']=$list['uid'];
				$setsqlarr['spms_type']=2;
				$setsqlarr['dateline']=time();
				inserttable(table('pms_sys'),$setsqlarr);
			 }
		}
		if (!$db->query("update  ".table('hunter_profile')." SET card_audit='{$audit[card_audit]}',belongcompany_audit='{$audit[belongcompany_audit]}',avatars_audit='{$audit[avatars_audit]}'  WHERE uid IN ({$sqlin}) ")) return false;
		return true;
	}
	return false;
}
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,3,9201,$user['username']," 成为已认证猎头顾问({$operator}{$points})，(剩余:{$mypoints})");
				$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,addtime) VALUES ('{$vuid}', '{$gift}','{$time}')");			
				}
			}			
		}
	 }
}
function refresh_hunter($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('hunter_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;
		$return=$return+$db->affected_rows();
		}
	}
	return $return;
}
function get_hunter_one_id($id)
{
	global $db;
	$id=intval($id);
	$sql = "select * from ".table('hunter_profile')." where id='{$id}'";
	$val=$db->getone($sql);
	$val['user']=get_user($val['uid']);
	if($val){
	$val['workyears']=date('Y')-$val['worktime_start'];
	}
	return $val;
}
// 获取猎头 合作过的客户
function get_hunter_collabora($id)
{
	global $db;
	$uid=intval($id);
	$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_member_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT m.*,c.fullname,c.id,c.addtime FROM ".table('members')." as m ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['hunter_url']=url_rewrite('QS_huntershow',array('id'=>$row['id']),false);
	$row_arr[] = $row;
	}
	return $row_arr;
}
function delete_hunter_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_info')." WHERE uid 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_hunter_setmeal')." WHERE uid IN (".$sqlin.")")) return false; 
		return true;		
	}
	return false;
}
function set_members_setmeal($uid,$setmealid)
{
	global $db,$timestamp;
	$setmeal=$db->getone("select * from ".table('hunter_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_add']=$setmeal['jobs_add'];
	$setsqlarr['download_resume_talent']=$setmeal['download_resume_talent'];
	$setsqlarr['interview_manager']=$setmeal['interview_manager'];
	$setsqlarr['added']=$setmeal['added'];
	$setsqlarr['hunter_refresh_jobs_space']=$setmeal['hunter_refresh_jobs_space'];
	$setsqlarr['hunter_refresh_jobs_time']=$setmeal['hunter_refresh_jobs_time'];
	$setmeal_jobs['setmeal_deadline']=$setsqlarr['endtime'];
	$setmeal_jobs['setmeal_id']=$setsqlarr['setmeal_id'];
	$setmeal_jobs['setmeal_name']=$setsqlarr['setmeal_name'];
	if (!updatetable(table('members_hunter_setmeal'),$setsqlarr," uid='{$uid}'")) return false;
	if (!updatetable(table('jobs'),$setmeal_jobs," uid=".intval($uid)." AND add_mode='2' ")) return false;
	return true;
}
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;
}
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_user_setmeal($uid)
{
	global $db;
	$sql = "select * from ".table('members_hunter_setmeal')."  WHERE uid=".intval($uid)." AND  effective=1 LIMIT 1";
	return $db->getone($sql);
}
function get_user($uid)
{
	global $db;
	$sql = "select * from ".table('members')." where uid=".intval($uid)." LIMIT 1";
	return $db->getone($sql);
}
function edit_setmeal_notes($setarr,$setmeal){
	$diff_arr= array_diff_assoc($setarr,$setmeal);
	if($diff_arr){
		foreach($diff_arr as $key=>$value){
			if($key=='jobs_add'){
				$str.="发布职位：{$setmeal['jobs_add']}-{$setarr['jobs_add']}";
			}elseif($key=='download_resume_manager'){
				$str.=",下载经理人才简历：{$setmeal['download_resume_manager']}-{$setarr['download_resume_manager']}";
			}elseif($key=='interview_manager'){
				$str.=",邀请经理人才面试数：{$setmeal['interview_manager']}-{$setarr['interview_manager']}";
			}elseif($key=='endtime'){
				if($setarr['endtime']=='1970-01-01') $setarr['endtime']='无限期';
				$str.=",修改套餐到期时间：{$setmeal['endtime']}~{$setarr['endtime']}";
			}elseif($key=='log_amount' && $value){
				$str.=",收取套餐金额：{$value} 元";
			}
		}
		$strend=$str?"操作人：{$_SESSION['admin_name']}。说明：".$str:'';
		return $strend;
	}else{
		return '';
	}
}
function get_meal_members_log($offset,$perpage,$get_sql= '',$mode='1')
{
	global $db;
	$colum=$mode==1?'b.points':'b.setmeal_name';
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT a.*,{$colum},c.fullname FROM ".table('members_charge_log')." as a ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['log_value_']=$row['log_value'];
	$row['log_value']=cut_str($row['log_value'],20 ,0,"...");
	$row_arr[] = $row;
	}
	return $row_arr;
}
function del_meal_log($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;
	if (!$db->query("Delete from ".table('members_charge_log')." WHERE log_id IN ({$sqlin})")) return false;
	return $db->affected_rows();
}
function get_meal_members_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query("SELECT a.*,b.*,c.fullname FROM ".table('members_hunter_setmeal')." as a ".$get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row_arr[] = $row;
	}
	return $row_arr;
}
 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_hunter_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_hunter_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']}'  LIMIT 1")) return false;
 			}
		}
	}
	return $return;
	
}
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.fullname 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_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_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 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 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['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="操作人：{$_SESSION['admin_name']},说明：确认收款。收款金额：{$order['amount']} 。".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'],3,1400,$user['username'],"积分+{$order['points']},剩余:{$user_points}",0,$notes,$mode=1);			
			}
		// //发送邮件
		// $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 recom_hunter_jobs($id,$recommend,$pms_notice,$notice)
{
	global $db;
	$return=0;
	$recommend=intval($recommend);
	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 recommend='{$recommend}'  WHERE id IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
		//发送站内信
		if ($pms_notice=='1')
		{
				$result = $db->query("SELECT jobs_name,uid FROM ".table('jobs')." WHERE id IN ({$sqlin})");
				$notice=$notice==''?'说明：无':'说明：'.$notice;
				while($list = $db->fetch_array($result))
				{
					$user_info=get_user($list['uid']);
					$setsqlarr['spms_usertype']=3;
					$setsqlarr['uid']=$list['uid'];
					$setsqlarr['spms_type']=2;
					$setsqlarr['message']=$recommend=='1'?"您发布的职位：{$list['jobs_name']},被管理员设置为热招职位！{$notice}":"您发布的职位：{$list['jobs_name']},被管理员取消热招职位！{$notice}";
					$setsqlarr['dateline']=time();
					inserttable(table('pms_sys'),$setsqlarr);
				 }
		}

	}
	return $return;
}
// 操作猎头经验
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_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'],3,$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));
}
?>