<?php
/*
 * 74cms 企业会员中心
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once(dirname(__FILE__).'/company_common.php');
$smarty->assign('nav',"asset");
$act=trim($_GET["act"])?trim($_GET["act"]):"search_engine_junior";
if ($act=='search_engine_junior')
{
	$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 = '".$_SESSION['parentuid']."' AND r.talent=1";
	$count=$db->get_total($sql);
	$smarty->assign('title','初级人才搜索 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign("search_engine",get_search_resume_engine($_SESSION['uid']));
	$smarty->assign("talent",1);
	$smarty->assign("act",$act);
	$smarty->assign("engine",$_GET);
	$smarty->assign("has_down",intval($count));
	$smarty->assign("left_down",intval($setmeal['down_ordinary_resume']));
	$smarty->display('member_company/company_search_engine.htm');
}
elseif ($act=='search_engine_senior')
{
	$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 = '".$_SESSION['parentuid']."' AND r.talent=2";
	$count=$db->get_total($sql);
	$smarty->assign('title','高级人才搜索 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign("search_engine",get_search_resume_engine($_SESSION['uid']));
	$smarty->assign("talent",2);
	$smarty->assign("act",$act);
	$smarty->assign("engine",$_GET);
	$smarty->assign("has_down",intval($count));
	$smarty->assign("left_down",intval($setmeal['down_senior_resume']));
	$smarty->display('member_company/company_search_engine.htm');
}
elseif ($act=='company_resume_list')
{
	if(($_GET['talent']=="1" && $setmeal['search_ordinary_resume']=="0") || ($_GET['talent']=="2" && $setmeal['search_senior_reusme']=="0")){
		showmsg('您当前的套餐没有权限！',1);
	}
	$smarty->assign('title','找人才 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('act',$act);
	$smarty->display('member_company/company_resume_list.htm');
}
elseif($act == "ajax_save_search_engine")
{
	$setsqlarr = $_POST;
	$setsqlarr['uid'] = intval($_SESSION['uid']);
	$setsqlarr['addtime'] = time();
	$insert_id = inserttable(table("search_resume_engine"),$setsqlarr,1);
	if($insert_id){
		exit("1");
	}else{
		exit("err");
	}
}
elseif($act =="ajax_get_search_engine_one"){
	$id = intval($_GET['id']);
	$engine = get_search_resume_engine_one($id,$_SESSION['uid']);
	if($engine){
		exit(json_encode($engine));
	}else{
		exit("err");
	}
}
elseif($act=='ajax_engine_del'){
	$id=intval($_GET['id']);
	del_search_engine($id,intval($_SESSION['uid']));
	$engine = get_search_resume_engine(intval($_SESSION['uid']));
	$html = "";
	if(!empty($engine)){
		foreach($engine as $value){
			$html .= "<tr height=\"25\">";
			$html .= "<td width=\"295\"><div class=\"temp_name\">".$value['engine_name']."</div></td>";
			$html .= "<td class=\"link_blue\"><a class=\"del_engine\" pid=".$value['id']." href=\"javascript:void(0);\">删除</a></td>";
			$html .= "</tr>";
		}
	}else{
		$html .= "<tr height=\"25\"><td width=\"295\"><div class=\"temp_name\">您还没有创建搜索器！</div></td><td class=\"link_blue\"></td></tr>";
	}
	$html.='<script>$(".del_engine").click(function(){
				var pid = $(this).attr("pid");
				$.get("?act=ajax_engine_del&id="+pid,function(result){
					$(".engine_content").html(result);
				});
			});	</script>';
	exit($html);
}
//搜索list通讯录
elseif($act=='search_list')
{
	$sql = "select count(*) as num from ".table('down_list')." as d left join ".table('list')." as r on d.list_id=r.id WHERE d.uid = '".$_SESSION['uid']."'";
	$count=$db->get_total($sql);
	$smarty->assign('title','通讯录搜索 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign("act",$act);
	$smarty->assign("list",$_GET);
	$smarty->assign("down_num",intval($count));
	$smarty->display('member_company/company_search_list.htm');
}
//搜索--list通讯录
elseif($act=='save_search_list')
{
	$smarty->assign('title','通讯录搜索列表 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('act',$act);
	$smarty->display('member_company/company_search_list_result.htm');
}
//管理自己的list通讯录
elseif($act=='manage_self_list')
{
	$uid=intval($_SESSION['uid']);
	$utype=intval($_SESSION['utype']);
	require_once(QISHI_ROOT_PATH.'include/page.class.php');
	$perpage=10;
	//上传 or 下载  (1->上传   2->下载)
	$type = !empty($_GET['type'])?intval($_GET['type']):2;
	if($type==1)
	{
		$wheresql=" where uid=$uid and utype=$utype ";
		$total_sql="SELECT COUNT(*) AS num FROM ".table('list').$wheresql;
		$total_val=$db->get_total($total_sql);
		$page = new page(array('total'=>$total_val, 'perpage'=>$perpage));
		$offset=($page->nowindex-1)*$perpage;
		if($total_val>$perpage)
		{
			$smarty->assign('page',$page->show(7));
		}
		$smarty->assign('list',get_upload_list($offset,$perpage,$wheresql));
	}
	else
	{
		$wheresql=" where d.uid=$uid ";
		$joinsql="left join ".table("list")." as l on d.list_id=l.id ";
		$total_sql="SELECT COUNT(*) AS num FROM ".table('down_list')." as d ".$joinsql.$wheresql;
		$total_val=$db->get_total($total_sql);
		$page = new page(array('total'=>$total_val, 'perpage'=>$perpage));
		$offset=($page->nowindex-1)*$perpage;
		if($total_val>$perpage)
		{
			$smarty->assign('page',$page->show(7));
		}
		$smarty->assign('list',get_down_list($offset,$perpage,$joinsql.$wheresql));
	}
	$smarty->assign('title'," 我的通讯录列表 - 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('act',$act);
	$smarty->assign('type',$type);
	$smarty->display('member_company/company_self_list.htm');
}
//上传--list通讯录
elseif($act=='upload_list')
{
	$smarty->assign('title','上传通讯录 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('act',$act);
	$smarty->display('member_company/company_upload_list.htm');
}
//提交--上传list通讯录
elseif($act=='save_upload_list')
{
	global $timestamp,$_CFG;
	$setsqlarr['uid']=$uid;
	$setsqlarr['utype']=intval($_SESSION['utype']);
	$setsqlarr['title']=!empty($_POST['title'])?trim($_POST['title']):showmsg('您没有填写通讯录标题！',1);
	$setsqlarr['trade']=!empty($_POST['trade'])?trim($_POST['trade']):showmsg('您没有选择行业！',1);
	$setsqlarr['trade_cn']=trim($_POST['trade_cn']);
	$setsqlarr['price']=!empty($_POST['price'])?trim($_POST['price']):showmsg('您没有填写下载定价！',1);
	$setsqlarr['rank_title']=trim($_POST['rank_title']);
	$setsqlarr['mobile']=!empty($_POST['mobile'])?trim($_POST['mobile']):showmsg('您没有选择手机情况！',1);
	$setsqlarr['mobile_cn']=trim($_POST['mobile_cn']);
	$setsqlarr['email']=!empty($_POST['email'])?trim($_POST['email']):showmsg('您没有选择邮箱情况！',1);
	$setsqlarr['email_cn']=trim($_POST['email_cn']);
	$setsqlarr['list_time']=!empty($_POST['list_time'])?strtotime($_POST['list_time']."-01-01"):showmsg('您没有选择年份！',1);
	$setsqlarr['number']=!empty($_POST['number'])?trim($_POST['number']):showmsg('您没有填写人数！',1);
	if (empty($_FILES['file']['name']))
	{
		showmsg('请上传文件！',1);
	}
	else
	{
		if(intval($_FILES['file']['size']) > intval($_CFG['upload_list_file_size_max'])*1024)
		{
			showmsg('文件大小超过设置上限！',1);
		}
		else
		{
			$setsqlarr['file_size']=intval($_FILES['file']['size']);
		}
		require_once(QISHI_ROOT_PATH.'include/upload.php');
		$entrust_updir="../../data/list/";
		$entrust_dir="data/list/";
		$entrust_updir=$entrust_updir.date("Y/m/");
		make_dir($entrust_updir);
		$setsqlarr['file']=_asUpFiles($entrust_updir,"file",$_CFG['upload_list_file_size_max'],'docx/doc/zip/xls',true);
		if (empty($setsqlarr['file']))
		{
			showmsg('上传文件失败！',1);
		}
		//$setsqlarr['file']=$entrust_dir.date("Y/m/").$setsqlarr['file'];
		$setsqlarr['file']=date("Y/m/").$setsqlarr['file'];
	}
	$setsqlarr['contents']=!empty($_POST['contents'])?trim($_POST['contents']):showmsg('您没有填写描述！',1);
	$setsqlarr['audit']=intval($_CFG['audit_upload_list']);
	$setsqlarr['key']=$setsqlarr['title'].$setsqlarr['trade_cn'].$setsqlarr['contents'];
	require_once(QISHI_ROOT_PATH.'include/splitword.class.php');
	$sp = new SPWord();
	$setsqlarr['key']="{$setsqlarr['title']} {$setsqlarr['trade_cn']} {$setsqlarr['contents']} ".$sp->extracttag($setsqlarr['key']);
	$setsqlarr['addtime']=$timestamp;
	!inserttable(table('list'),$setsqlarr)?showmsg("上传失败！",0):'';
	write_memberslog($uid,$_SESSION['utype'],1601,$_SESSION['username'],"/",$_SESSION['parentuid'],"添加通讯录,ID：{$id}",0);
	//奖励积分
	date_default_timezone_set ('PRC');	//格林威治标准时间和北京时间差了正好8个小时,这个函数回复正常时间
	$time=time();
	$today=strtotime('today');
	$info=$db->getall("SELECT * FROM ".table('members_handsel')." WHERE uid ='{$_SESSION['parentuid']}' AND htype='uploadlist' AND addtime>{$today} ");
	$upload_point = 0;
	foreach ($info as $key => $value) 
	{
		$upload_point = $upload_point+intval($value['points']);
	}
	//没超过当天最大限制
	if((intval($upload_point)+intval($_CFG['company_upload_list_point'])) < intval($_CFG['company_upload_list_point_max']))
	{
		$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,points,addtime) VALUES ('{$_SESSION['parentuid']}', 'uploadlist','{$_CFG['company_upload_list_point']}','{$time}')");
		report_deal($_SESSION['parentuid'],1,$_CFG['company_upload_list_point']);
		$user_points=get_user_points($_SESSION['parentuid']);
		write_memberslog($_SESSION['uid'],1,9101,$_SESSION['username'],"积分：+".$_CFG['company_upload_list_point'].",剩余：{$user_points}",$_SESSION['parentuid'],"上传通讯录",1);
	}
	$link[0]['text'] = "管理我的list通讯录";
	$link[0]['href'] = '?act=manage_self_list';
	$link[1]['text'] = "继续上传通讯录";
	$link[1]['href'] = '?act=upload_list';
	showmsg("发布成功！",2,$link);
}
//修改--list通讯录
elseif($act=='update_list')
{
	$uid=intval($_SESSION['uid']);
	$list=get_list_one(intval($_GET['list_id']),$uid);
	if (empty($list)) showmsg("通讯录丢失！",1);
	$smarty->assign('title','修改通讯录 - 企业会员中心 - '.$_CFG['site_name']);
	$list['list_time']=date("Y",$list['list_time']);
	$list['list_time_cn']=date("Y",$list['list_time_cn'])."年";
	$smarty->assign('list',$list);
	$smarty->assign('act',$act);
	$smarty->display('member_company/company_update_list.htm');
}
//保存--修改list通讯录
elseif($act=='save_update_list')
{
	global $timestamp,$_CFG;
	$uid=intval($_SESSION['uid']);
	$id=intval($_POST['id'])?intval($_POST['id']):showmsg("通讯录id丢失！",1);
	$setsqlarr['title']=!empty($_POST['title'])?trim($_POST['title']):showmsg('您没有填写通讯录标题！',1);
	$setsqlarr['trade']=!empty($_POST['trade'])?trim($_POST['trade']):showmsg('您没有选择行业！',1);
	$setsqlarr['trade_cn']=trim($_POST['trade_cn']);
	$setsqlarr['price']=!empty($_POST['price'])?trim($_POST['price']):showmsg('您没有填写下载定价！',1);
	$setsqlarr['rank_title']=trim($_POST['rank_title']);
	$setsqlarr['mobile']=!empty($_POST['mobile'])?trim($_POST['mobile']):showmsg('您没有选择手机情况！',1);
	$setsqlarr['mobile_cn']=trim($_POST['mobile_cn']);
	$setsqlarr['email']=!empty($_POST['email'])?trim($_POST['email']):showmsg('您没有选择邮箱情况！',1);
	$setsqlarr['email_cn']=trim($_POST['email_cn']);
	$setsqlarr['list_time']=!empty($_POST['list_time'])?strtotime($_POST['list_time']."-01-01"):showmsg('您没有选择年份！',1);
	$setsqlarr['number']=!empty($_POST['number'])?trim($_POST['number']):showmsg('您没有填写人数！',1);
	if (!empty($_FILES['file']['name']))
	{
		if(intval($_FILES['file']['size']) > intval($_CFG['upload_list_file_size_max'])*1024)
		{
			showmsg('文件大小超过设置上限！',1);
		}
		else
		{
			$setsqlarr['file_size']=intval($_FILES['file']['size']);
		}
		require_once(QISHI_ROOT_PATH.'include/upload.php');
		$entrust_updir="../../data/list/";
		$entrust_dir="data/list/";
		$entrust_updir=$entrust_updir.date("Y/m/");
		make_dir($entrust_updir);
		$setsqlarr['file']=_asUpFiles($entrust_updir,"file",$_CFG['upload_list_file_size_max'],'docx/doc/zip/xls',true);
		if (empty($setsqlarr['file']))
		{
			showmsg('更新文件失败！',0);
		}
		//$setsqlarr['file']=$entrust_dir.date("Y/m/").$setsqlarr['file'];
		$setsqlarr['file']=date("Y/m/").$setsqlarr['file'];
		//将旧文件删掉
		$list_info = $db->getone("select  * from ".table('list')." where id =".$id." limit 1");
		$path = QISHI_ROOT_PATH."data/list/".$list_info['file'];
		if(!unlink($path))
		{
			showmsg('更新文件失败！',0);
		}
	}
	$setsqlarr['contents']=!empty($_POST['contents'])?trim($_POST['contents']):showmsg('您没有填写描述！',1);
	$setsqlarr['key']=$setsqlarr['title'].$setsqlarr['trade_cn'].$setsqlarr['contents'];
	require_once(QISHI_ROOT_PATH.'include/splitword.class.php');
	$sp = new SPWord();
	$setsqlarr['key']="{$setsqlarr['title']} {$setsqlarr['trade_cn']} {$setsqlarr['contents']} ".$sp->extracttag($setsqlarr['key']);
	$setsqlarr['addtime']=$timestamp;
	if (!updatetable(table('list'), $setsqlarr," id='{$id}' AND uid='{$uid}' ")) showmsg("更新失败！",0);
	write_memberslog($uid,$_SESSION['utype'],1602,$_SESSION['username'],"/",$_SESSION['parentuid'],"修改通讯录,ID：{$id}",0);
	$link[0]['text'] = "管理我的list通讯录";
	$link[0]['href'] = '?act=manage_self_list';
	showmsg("更新成功！",2,$link);
}
//ajax下载--list通讯录
elseif($act=='ajax_down_list')
{
	$uid=intval($_SESSION['uid']);
	$list_id=intval($_POST['list_id']);
	$sql = "SELECT * FROM ".table('down_list')." WHERE uid=".$uid." and list_id=".$list_id." LIMIT 1";
	$is_down = $db->getone($sql);
	//下载过
	if(!empty($is_down))
	{
		//插入数据
		$searchtab['uid'] = $uid;
		$searchtab['utype'] = $_SESSION['utype'];
		$searchtab['list_id'] = $list_id;
		$searchtab['down_time'] = time();
		if(inserttable(table('down_list'),$searchtab))
		{ 
			write_memberslog($uid,$_SESSION['utype'],1604,$_SESSION['username'],"/",$_SESSION['parentuid'],"下载通讯录,ID：{$list_id}",0);
			exit('yes');
		}
		else
		{ 
			exit('插入数据失败 ! ');
		}
	}
	//没有下载过
	else
	{
		$list_sql = "SELECT * FROM ".table('list')." WHERE  id=".$list_id." LIMIT 1";
		$list_info = $db->getone($list_sql);
		if(empty($list_info))
		{
			exit("list丢失！");
		}
		//下载的是自己上传的
		elseif($list_info['uid']==$uid)
		{
			//插入数据
			$searchtab['uid'] = $uid;
			$searchtab['utype'] = $_SESSION['utype'];
			$searchtab['list_id'] = $list_id;
			$searchtab['down_time'] = time();
			if(inserttable(table('down_list'),$searchtab))
			{ 
				write_memberslog($uid,$_SESSION['utype'],1604,$_SESSION['username'],"/",$_SESSION['parentuid'],"下载通讯录,ID：{$list_id}",0);
				exit('yes');
			}
			else
			{ 
				exit('插入数据失败 ! ');
			}
		}
		else
		{
			//下载人支付的费用 = 平台应收费用 + 上传人应收费用
			$user_account = get_user_account($_SESSION['parentuid']);
			//平台应收费用
			$website_charge = intval($list_info['price'])*round($_CFG['charge_ratio']/100,2);
			//上传人应收费用
			$uplode_charge = intval($list_info['price']) - intval($website_charge);
			if(!empty($user_account))
			{
				//账户中的资金不够
				if(intval($user_account['available_funds']) < intval($list_info['price']))
				{
					exit("资金不够！");
				}
				else
				{
					//插入数据
					$searchtab['uid'] = $uid;
					$searchtab['utype'] = $_SESSION['utype'];
					$searchtab['list_id'] = $list_id;
					$searchtab['down_time'] = time();
					if(inserttable(table('down_list'),$searchtab))
					{
						//下载人资金操作
						action_user_account($_SESSION['parentuid'],$_SESSION['utype'],$list_info['price'],2);
						$user_info = $db->getone("select * from ".table('members')." where uid=".$uid." limit 1");
						write_account_log($uid,$user_info['parentuid'],$_SESSION['utype'],$user_info['username'],"下载通讯录扣除金额：".$list_info['price']."元","操作成功");
						//上传人资金操作
						$mem_info = $db->getone("select * from ".table('members')." where uid=".$list_info['uid']);
						//假如上传的人是企业会员 则账户需要parentuid
						if(intval($list_info['utype'])==1)
						{
							if(empty($mem_info))
							{
								exit("查找上传人用户信息发生错误 !");
							}
							else
							{
								$acc_uid = intval($mem_info['parentuid']);
							}
						}
						else
						{
							$acc_uid = intval($list_info['uid']);
						}
						action_user_account($acc_uid,$list_info['utype'],$uplode_charge,1);
						write_account_log($list_info['uid'],$mem_info['parentuid'],$mem_info['utype'],$mem_info['username'],"通讯录收入金额：".$uplode_charge."元","充值成功");
						write_memberslog($uid,$_SESSION['utype'],1604,$_SESSION['username'],"/",$_SESSION['parentuid'],"下载通讯录,ID：{$list_id}",0);

						exit("yes");	
					}
					else
					{
						exit("插入数据失败 !");
					}
				}
			}
			else
			{
				exit("账户资金问题！");
			}
		}
	}
}
//删除--list通讯录
elseif($act=='del_list')
{
	$uid=intval($_SESSION['uid']);
	$list_id=!empty($_GET['list_id'])?intval($_GET['list_id']):showmsg("通讯录id丢失！",1);
	$del_type=intval($_GET['del_type']);
	//2->删除 我下载的   1->删除 我上传的
	if (del_list($list_id,$uid,$del_type)) 
	{
		write_memberslog($uid,$_SESSION['utype'],1603,$_SESSION['username'],"/",$_SESSION['parentuid'],"删除通讯录,ID：{$id}",0);
		showmsg("删除成功！",2);
	}
	else
	{
		showmsg("删除失败",0);
	}
}
unset($smarty);
?>