<?php
function tpl_function_qishi_company_list($params, &$smarty)
{
global $db,$_CFG;
$arrset=explode(',',$params['set']);
foreach($arrset as $str)
{
$a=explode(':',$str);
	switch ($a[0])
	{
	case "列表名":
		$aset['listname'] = $a[1];
		break;
	case "显示数目":
		$aset['row'] = $a[1];
		break;
	case "开始位置":
		$aset['start'] = $a[1];
		break;
	case "企业名长度":
		$aset['companynamelen'] = $a[1];
		break;
	case "描述长度":
		$aset['brieflylen'] = $a[1];
		break;
	case "填补字符":
		$aset['dot'] = $a[1];
		break;
	case "关键字":
		$aset['companyname'] = $a[1];
		break;
	case "行业":
		$aset['trade'] = $a[1];
		break;
	case "地区分类":
		$aset['citycategory'] = $a[1];
		break;
	case "排序":
		$aset['displayorder'] = $a[1];
		break;
	case "分页显示":
		$aset['paged'] = $a[1];
		break;
	case "公司页面":
		$aset['companyshow'] = $a[1];
		break;
	case "列表页":
		$aset['listpage'] = $a[1];
		break;
	}
}
if (is_array($aset)) $aset=array_map("get_smarty_request",$aset);
$aset['listname']=isset($aset['listname'])?$aset['listname']:"list";
$aset['row']=!empty($aset['row'])?intval($aset['row']):10;
$aset['start']=isset($aset['start'])?intval($aset['start']):0;
$aset['companynamelen']=isset($aset['companynamelen'])?intval($aset['companynamelen']):30;
$aset['dot']=isset($aset['dot'])?$aset['dot']:'';
$aset['companyshow']=isset($aset['companyshow'])?$aset['companyshow']:'QS_companyshow';
$aset['listpage']=!empty($aset['listpage'])?$aset['listpage']:'QS_companylist';
if ($aset['displayorder'])
{
	if (strpos($aset['displayorder'],'>'))
	{
		$arr=explode('>',$aset['displayorder']);
		// 排序字段
		if($arr[0]=='sale'){
			$arr[0]="sale";
		}
		elseif($arr[0]=="addtime")
		{
			$arr[0]="addtime";
		}
		else
		{
			$arr[0]="click";
		}
		// 排序方式
		if($arr[1]=='asc'){
			$arr[1]="asc";
		}
		else
		{
			$arr[1]="desc";
		}
		if ($arr[0] && $arr[1])
		{
		$orderbysql=" ORDER BY `".$arr[0]."` ".$arr[1]." ,logo desc";
		}
	}
	else
	{
		$orderbysql=" ORDER BY `".trim($aset['displayorder'])."`  desc,logo desc ";
	}
}else{
	$orderbysql=" ORDER BY click  desc,logo desc";
}
if (!empty($aset['companyname']))
{
	$wheresql.=" AND (companyname  like '%".trim($aset['companyname'])."%') ";
}
if (isset($aset['trade']) && $aset['trade']<>'')
{
	if (strpos($aset['trade'],","))
	{
		$or=$orsql="";
		$arr=explode(",",$aset['trade']);
		if (count($arr)>10) exit();
		$sqlin=implode(",",$arr);
		if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
		{
			$joinwheresql_trade.=" AND trade IN  ({$sqlin}) ";
		}
	}
	else
	{
	$joinwheresql_trade.=" AND trade=".intval($aset['trade']);
	}
	
	if (!empty($joinwheresql_trade))
	{
	$joinwheresql_trade=" WHERE ".ltrim(ltrim($joinwheresql_trade),'AND');
	}
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('company_trade')." {$joinwheresql_trade} )AS t ON  c.id=t.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('company_trade')." {$joinwheresql_trade} )AS t ON  c.id=t.pid ";
}
if (!empty($aset['citycategory']))
{
		$dsql=$xsql="";
		$arr=explode(",",$aset['citycategory']);
		$arr=array_unique($arr);
		if (count($arr)>10) exit();
		foreach($arr as $sid)
		{
			$cat=explode(".",$sid);
			if (intval($cat[1])===0)
			{
			$dsql.= " OR district =".intval($cat[0]);
			}
			else
			{
			$xsql.= " OR sdistrict =".intval($cat[1]);
			}
		}
		$wheresql.=" AND  (".ltrim(ltrim($dsql.$xsql),'OR').") ";
}
if (!empty($wheresql))
{
$wheresql=" WHERE ".ltrim(ltrim($wheresql),'AND');
}
if (isset($aset['paged']))
{
	require_once(QISHI_ROOT_PATH.'include/page.class.php');
	$total_sql="SELECT COUNT(*) AS num FROM ".table('company_profile')." as c ".$joinsql.$wheresql;
	$total_count=$db->get_total($total_sql);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$aset['row'],'alias'=>$aset['listpage'],'getarray'=>$_GET));
	$currenpage=$pagelist->nowindex;
	$aset['start']=($currenpage-1)*$aset['row'];
		// if ($total_count>$aset['row'])
		// {
		$smarty->assign('page',$pagelist->show(7));
		$smarty->assign('pagemin',$pagelist->show(6));
		// }
		$smarty->assign('total',$total_count);
}
$limit=" LIMIT ".abs($aset['start']).','.$aset['row'];
$result = $db->query("SELECT * FROM ".table('company_profile')." as c ".$joinsql.$wheresql.$orderbysql.$limit);
$list= array();
while($row = $db->fetch_array($result))
{
	$row['companyname_']=$row['companyname'];
	$row['companyname']=cut_str($row['companyname'],$aset['companynamelen'],0,$aset['dot']);
	$row['url'] = url_rewrite($aset['companyshow'],array('id'=>$row['id']));
	$row['contents']=str_replace('&nbsp;','',$row['contents']);
	$row['briefly_']=strip_tags($row['contents']);
	$row['briefly']=strip_tags($row['briefly_']);
	if ($aset['brieflylen']>0)
	{
		$row['briefly']=cut_str(strip_tags($row['contents']),$aset['brieflylen'],0,$aset['dot']);
	}
	if ($row['logo'])
	{
		$row['logo']=$_CFG['main_domain']."data/logo/".$row['logo'];
	}
	else
	{
		$row['logo']=$_CFG['main_domain']."data/logo/no_logo.gif";
	}
	$time = time();
	$row['jobs_num'] = $db->get_total("select count(*) as num from ".table('jobs')." where company_id=".$row['id']." and audit=1 and display=1 and deadline>$time and setmeal_deadline>$time and jobs_type!=2");
	//职位大于3个的情况
	if(intval($row['jobs_num'])>3){
		$jobs = $db->getall("select *  from ".table('jobs')." where company_id=".$row['id']." order by refreshtime desc limit 3");
		$row['jobs'] = $jobs;
	}else{
		$jobs = $db->getall("select *  from ".table('jobs')." where company_id=".$row['id']." order by refreshtime desc ");
		$row['jobs'] = $jobs;
	}
	if ($row['tag'])
	{
		$tag=explode('|',$row['tag']);
		$taglist=array();
		if (!empty($tag) && is_array($tag))
		{
			foreach($tag as $t)
			{
			$tli=explode(',',$t);
			$taglist[]=array($tli[0],$tli[1]);
			}
		}
		$row['tag']=$taglist;
	}
	else
	{
	$row['tag']=array();
	}
	$list[] = $row;
}
$smarty->assign($aset['listname'],$list);
}
?>