<?php
 /*
 * 74cms 管理中心 个人用户相关函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
 if(!defined('IN_QISHI'))
 {
 	die('Access Denied!');
 }
 //******************************简历部分**********************************
function get_resume_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$limit=" LIMIT ".$offset.','.$perpage;
	$result = $db->query($get_sql.$limit);
	while($row = $db->fetch_array($result))
	{
	$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['id']));
	$row_arr[] = $row;
	}
	return $row_arr;
}
function distribution_resume($id)
{
	global $db;
	if (!is_array($id))$id=array($id);
	$time=time();
	foreach($id as $v)
	{
		$v=intval($v);
		$t1=$db->getone("select * from ".table('resume')." where id='{$v}' LIMIT 1");
		if (empty($t1) || !empty($t1))
		{
		continue;
		}
		else
	{
			$j=$t1;
			if (!empty($t1) &&  $j['audit']=="1" && $j['display']=="1" && $j['user_status']=="1" && $j['complete']=="1")
			{
				continue;
			}
			elseif (!empty($t2))
			{
					if ($j['audit']!="1" || $j['display']!="1" || $j['user_status']!="1"  || $j['complete']!="1")
					{
					continue;
					}
			}
			//检测完毕	 
			$db->query("Delete from ".table('resume')." WHERE id='{$v}' LIMIT 1");
			if (inserttable(table('resume'),$j))
			{
				$searchtab['id']=$j['id'];
				$searchtab['uid']=$j['uid'];
				$searchtab['sex']=$j['sex'];
				$searchtab['birthdate']=$j['birthdate'];
				$searchtab['experience']=$j['experience'];
				$searchtab['wage']=$j['wage'];
				$searchtab['education']=$j['education'];
				$searchtab['current']=$j['current'];
				$searchtab['refreshtime']=$j['refreshtime'];
				$searchtab['talent']=$j['talent'];
				$searchtab['display']=$j['display'];
				$searchtab['key']=$j['key'];
				$searchtab['likekey']=$j['likekey'];
				inserttable(table('resume_search'),$searchtab);
				unset($searchtab);
			}
				
		}
	}
}
function del_resume($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('resume')." WHERE id IN ({$sqlin})")) return false;
		$return=$return+$db->affected_rows();
		if (!$db->query("Delete from ".table('resume_district')." WHERE pid IN ({$sqlin}) ")) return false; 
		if (!$db->query("Delete from ".table('resume_education')." WHERE pid IN ({$sqlin}) ")) return false;
		if (!$db->query("Delete from ".table('resume_jobs')." WHERE pid IN ({$sqlin}) ")) return false;
		if (!$db->query("Delete from ".table('resume_language')." WHERE pid IN ({$sqlin}) ")) return false;
		if (!$db->query("Delete from ".table('resume_project')." WHERE pid IN ({$sqlin}) ")) return false; 
		if (!$db->query("Delete from ".table('resume_work')." WHERE pid IN ({$sqlin}) ")) return false;
		if (!$db->query("Delete from ".table('resume_trade')." WHERE pid IN ({$sqlin}) ")) return false;
		if (!$db->query("Delete from ".table('resume_search')." WHERE id IN ({$sqlin})")) return false;
		return $return;
	}
	return $return;
}
function del_resume_for_uid($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('resume')." WHERE uid IN (".$sqlin.")");
		while($row = $db->fetch_array($result))
		{
		$rid[]=$row['id'];
		}
		if (empty($rid))
		{
		return true;
		}
		else
		{
		return del_resume($rid);
		}		
	}
}
function edit_resume_audit($id,$audit)
{
	global $db,$_CFG;
	$audit=intval($audit);
	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('resume')." SET audit='{$audit}'  WHERE id IN ({$sqlin}) ")) return false;
		distribution_resume($id);
			//审核原因表 和 信息通知表
			$reason=$reason==''?'原因：未知':'原因：'.$reason;
			$reason_result = $db->query("SELECT  uid,id,title FROM ".table('resume')." WHERE id IN ({$sqlin})");
			while($list = $db->fetch_array($reason_result))
			{
				//审核原因表
				$auditsqlarr['resume_id']=$list['id'];
				$auditsqlarr['reason']=$audit=='1'?"您的简历：{$list['title']},成功通过网站管理员审核！":"您的简历：{$list['title']},未通过网站管理员审核,{$reason}";
				$auditsqlarr['addtime']=time();
				inserttable(table('audit_reason'),$auditsqlarr);
				//消息通知
				$user_info=get_user($list['uid']);
				$pmssql['message']=$audit=='1'?"您的简历：{$list['title']},成功通过网站管理员审核！":"您的简历：{$list['title']},未通过网站管理员审核,{$reason}";
				$pmssql['spms_usertype']=2;
				$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_resumeallow']=="1")//审核通过
			{
				$result = $db->query("SELECT * FROM ".table('resume')." WHERE id IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
				dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&act=set_resumeallow");
				}
			}
			if ($audit=="3" && $mailconfig['set_resumenotallow']=="1")//审核未通过
			{
				$result = $db->query("SELECT * FROM ".table('resume')." WHERE id IN ({$sqlin})");
				while($list = $db->fetch_array($result))
				{
				dfopen($_CFG['site_domain'].$_CFG['site_dir']."plus/asyn_mail.php?uid=".$list['uid']."&key=".asyn_userkey($list['uid'])."&act=set_resumenotallow");
				}
			}
			//sms		
			if ($audit=="1" && $sms['open']=="1" && $sms['set_resumeallow']=="1" )
			{
				$result = $db->query("SELECT * FROM ".table('resume')." 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'])."&act=set_resumeallow");
					}
				}
			}
			//sms
			if ($audit=="3" && $sms['open']=="1" && $sms['set_resumenotallow']=="1" )//认证未通过
			{
				$result = $db->query("SELECT * FROM ".table('resume')." 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'])."&act=set_resumenotallow");
					}
				}
			}
				
	return true;
	}
	return false;
}
//修改人才等级
function edit_resume_talent($id,$talent)
{
	global $db;
	$talent=intval($talent);
	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('resume')." SET talent={$talent}  WHERE id IN ({$sqlin})")) return false;
	if (!$db->query("update  ".table('resume_search')." SET talent={$talent}  WHERE id IN ({$sqlin})")) return false;
	return true;
	}
	return false;
}
//从UID获取所有简历
function get_resume_uid($uid)
{
	global $db;
	$uid=intval($uid);
	$result = $db->query("select * FROM ".table('resume')." where uid='{$uid}'");
	while($row = $db->fetch_array($result))
	{ 
	$row['resume_url']=url_rewrite('QS_resumeshow',array('id'=>$row['id']));
	$row_arr[] = $row;
	}
	return $row_arr;	
}
function refresh_resume($id)
{
	global $db;
	$return=0;
	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('resume')." SET refreshtime='".time()."'  WHERE id IN (".$sqlin.")")) return false;
		$return=$return+$db->affected_rows();
		if (!$db->query("update  ".table('resume_search')." SET refreshtime='".time()."'  WHERE id IN (".$sqlin.")")) return false;
	}
	return $return;
}
//**************************个人会员列表
function get_member_list($offset,$perpage,$get_sql= '')
{
	global $db;
	$row_arr = array();
	$limit=" LIMIT ".$offset.','.$perpage;	
	$result = $db->query("SELECT * FROM ".table('members')." as m ".$get_sql.$limit);
		while($row = $db->fetch_array($result))
		{
		$row_arr[] = $row;
		}
	return $row_arr;
}
function delete_member($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;
		return true;
		}
	return false;
}
function get_member_one($memberuid)
{
	global $db;
	$sql = "select * from ".table('members')." where uid=".intval($memberuid)." LIMIT 1";
	$val=$db->getone($sql);
	return $val;
}
function get_user($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members')." where uid = '{$uid}' LIMIT 1";
	return $db->getone($sql);
}
?>