<?php
include_once ('./common.php');
define('AM_DIRa',dirname(__FILE__).DIRECTORY_SEPARATOR);

$table = require(AM_DIRa.'./table.php');

$cm_name = $table['cm_table_name'];
$list_name = $table['list_table_name'];
$vod_name = $table['vod_table_name'];

$cm_filed = $table['cm_filed'];
$list_filed = $table['list_filed'];
$vod_filed = $table['$vod_filed'];

// echo table_filed_trans('cm_filed',"");
// echo table_filed_trans('vod_filed', 'amvod');
echo returnkey('list_filed', 'cname');


//返回key值
function returnkey($table_filed,$arr_val)
{
	global $table;
	$cm_filed = $table[$table_filed];
	$retkey = '';
	foreach ($cm_filed as $key=>$val)
	{
		if($arr_val == $val)
		{
			$retkey = $key;
		}
	}
	return $retkey;
}

// returnkey($table_filed,$arr_val)
// returnkey('list_filed',$table['list_filed']['list_name'])
//转换查询所有key
function table_filed_trans($table_filed,$table_name)
{
	global $table;
	$cm_filed = $table[$table_filed];
	$query = '';
	
	foreach ($cm_filed as $key=>$val)
	{
		if(empty($table_name))
		{
			$query = $query.' '.$val.' as '.$key.',';
		}else {
			$query = $query.' '.$table_name.".".$val.' as '.$key.',';
		}
	}
	
	$query = substr($query, 0,strlen($query)-1);
	return $query;
}


function  add_feed_back($arcid,$ip,$time,$msg)
{
	global $table;
	$query = "insert into ".$table['cm_table_name']." (".$table['cm_filed']['cm_cid'].","
			.$table['cm_filed']['cm_ip'].",".$table['cm_filed']['cm_addtime']." ,".$table['cm_filed']['cm_content'].")
			 values ($arcid,'$ip',$time,'$msg')";
	return $query;
}

function  category_list_1()
{
	global $table;
	
	$query = "SELECT ".table_filed_trans('list_filed', "")." FROM ".$table['list_table_name']." where ".$table['list_filed']['list_pid']."=".$table['list_pid']." and "
			.$table['list_filed']['list_status']."=".$table['list_status']." and ".$table['list_filed']['list_sid']."=".$table['list_sid']." order by ".$table['list_filed']['list_id']." asc";
	
	return $query;
}

function  category_list_2($val)
{
	global $table;
	$query ="SELECT ".table_filed_trans('list_filed', "")." FROM ".$table['list_table_name']."
			 where ".$table['list_filed']['list_pid']."=".$val['list_id']." order by ".$table['list_filed']['list_id']." asc";
	return $query;
}

//查询电影详情
function  get_detail($parm)
{
	global $table;
	$query = "SELECT ".table_filed_trans('vod_filed', 'amvod').",amlist.".$table['list_filed']['list_name']." as ".returnkey('list_filed',$table['list_filed']['list_name'])." 
				FROM ".$table['vod_table_name']." amvod 
					left join ".$table['list_table_name']." amlist 
							on amlist.".$table['list_filed']['list_id']."=amvod.".$table['vod_filed']['vod_cid']." 
									WHERE amvod.".$table['vod_filed']['vod_id']."=".$parm;
	return $query;
}

function  feed_back_1($arcid)
{
	global $table;
	
	$sqlwhere .= " and ".$table['cm_filed']['cm_cid']."=".$arcid;
	
	$query = "Select count(*) totalcnt  
		  From ".$table['cm_table_name']." feedback where ".$table['cm_filed']['cm_id'].">-1 ".$sqlwhere;	
	return $query;
}
function  feed_back_2($arcid,$page,$count,$count)
{
	global $table;
	$sqlwhere .= " and ".$table['cm_filed']['cm_cid']."=".$arcid;
	$sqlwhere .= " order by ".$table['cm_filed']['cm_addtime']." desc limit ".$page*$count.",$count";
	$query = "Select ".table_filed_trans('cm_filed', 'feedback')."
		  From ".$table['cm_table_name']." feedback where ".$table['cm_filed']['cm_id'].">-1 ".$sqlwhere;
	return $query;
}



function  gallary_1()
{
	global $table;
	$query = "SELECT ".table_filed_trans('list_filed', '')." FROM ".$table['list_table_name']." 
			where ".$table['list_filed']['list_pid']."=0 and ".$table['list_filed']['list_status']."=".$table['list_status']." and 
					".$table['list_filed']['list_sid']."=".$table['list_sid']." order by ".$table['list_filed']['list_id']." asc";
	return $query;
}
function  gallary_2($val)
{
	global $table;
	$query ="SELECT ".table_filed_trans('vod_filed', '')." FROM ".$table['vod_table_name']." 
			where ".$table['vod_filed']['vod_cid']." in (
					select ".$table['list_filed']['list_id']." from ".$table['list_table_name']." 
							where ".$table['list_filed']['list_pid']."=".$val["list_id"].") 
									or ".$table['vod_filed']['vod_cid']." = ".$val["list_id"]." order 
											by ".$table['vod_filed']['vod_stars']." desc , ".$table['vod_filed']['vod_hits_week']." desc limit 1";
	return $query;
}


function  indexlist_1($page,$pagesize,$pagesize)
{
	global $table;
	$query = "SELECT ".table_filed_trans('vod_filed', '')." FROM ".$table['vod_table_name']." order by ".$table['vod_filed']['vod_hits_day']." desc limit ".$page*$pagesize.",".$pagesize;
	return $query;
}
function  indexlist_2()
{
	global $table;
	$query = "SELECT ".table_filed_trans('list_filed', '')." FROM ".$table['list_table_name']." where ".$table['list_filed']['list_pid']."=".$table['list_pid']." 
			and ".$table['list_filed']['list_status']."=".$table['list_status']." and ".$table['list_filed']['list_sid']."=".$table['list_sid']." 
					order by ".$table['list_filed']['list_id']." asc";
	return $query;
}
function  indexlist_3($val,$page,$pagesize,$pagesize)
{
	global $table;
	$query = "SELECT ".table_filed_trans('vod_filed', '')." FROM ".$table['vod_table_name']." where ".$table['vod_filed']['vod_cid']." in 
			(select ".$table['list_filed']['list_id']." from ".$table['list_table_name']." where ".$table['list_filed']['list_pid']."=".$val["list_id"].") 
					or ".$table['vod_filed']['vod_cid']." = ".$val["list_id"]." order by ".$table['vod_filed']['vod_addtime']." desc 
							limit ".$page*$pagesize.",".$pagesize;
	return $query;
}



function  itemlist_1($classpath)
{
	global $table;
	$query = "SELECT ".table_filed_trans('list_filed', '')." FROM ".$table['list_table_name']." where ".$table['list_filed']['list_id']."=$classpath ";
	
	return $query;
}
function  itemlist_2($classpath,$page,$pagesize,$pagesize)
{
	global $table;
	
	$query = "SELECT ".table_filed_trans('vod_filed', 'amvod').",amlist.".$table['list_filed']['list_name']." as ".returnkey('list_filed',$table['list_filed']['list_name'])." 
				FROM ".$table['vod_table_name']." amvod left join ".$table['list_table_name']." amlist 
					on amlist.".$table['list_filed']['list_id']."=amvod.".$table['vod_filed']['vod_cid']." 
				where ".$table['vod_filed']['vod_cid']." in (
							select ".$table['list_filed']['list_id']." from ".$table['list_table_name']." 
							where ".$table['list_filed']['list_pid']."=$classpath) or ".$table['vod_filed']['vod_cid']."=$classpath  order 
				by ".$table['vod_filed']['vod_addtime']." desc,".$table['vod_filed']['vod_gold']." desc limit ".$page*$pagesize.",".$pagesize;
	return $query;
}
function  itemlist_3($classpath)
{
	global $table;
	
	$query = "SELECT count(*) as t FROM ".$table['vod_table_name']." where ".$table['vod_filed']['vod_cid']." in (
					select ".$table['list_filed']['list_id']." from ".$table['list_table_name']." 
					where ".$table['list_filed']['list_pid']."=$classpath) or ".$table['vod_filed']['vod_cid']."=$classpath";
	return $query;
}
function  itemlist_4($classpath,$page,$pagesize,$pagesize)
{
	global $table;
	$query = "SELECT ".table_filed_trans('vod_filed', 'amvod').",amlist.".$table['list_filed']['list_name']." as ".returnkey('list_filed',$table['list_filed']['list_name'])." 
				FROM ".$table['vod_table_name']." amvod left join ".$table['list_table_name']." amlist on
					 amlist.".$table['list_filed']['list_id']."=amvod.".$table['vod_filed']['vod_cid']." where ".$table['vod_filed']['vod_cid']."=$classpath  
			order by ".$table['vod_filed']['vod_addtime']." desc,".$table['vod_filed']['vod_gold']." desc limit ".$page*$pagesize.",".$pagesize;
	return $query;
}

function  itemlist_5($classpath)
{
	global $table;
	$query = "SELECT count(*) as t FROM ".$table['vod_table_name']." where ".$table['vod_filed']['vod_cid']."=$classpath";
	return $query;
}


function presearchlist($skey)
{
	global $table;
	$query = "select ".$table['vod_filed']['vod_id']." as ".returnkey('vod_filed',$table['vod_filed']['vod_id']).",".$table['vod_filed']['vod_name']." as 
				".returnkey('vod_filed',$table['vod_filed']['vod_name'])." from ".$table['vod_table_name']." 
			where (".$table['vod_filed']['vod_name']." like '%$skey%') order by ".$table['vod_filed']['vod_hits']." desc limit 10";
	
	return $query;
}


function search_list_1($keyvalue,$page,$pagesize,$pagesize)
{
	global $table;
	$query = "SELECT ".table_filed_trans('vod_filed', 'amvod').",amlist.".$table['list_filed']['list_name']." as ".returnkey('list_filed',$table['list_filed']['list_name'])."
			   FROM ".$table['vod_table_name']." amvod 	left join ".$table['list_table_name']." amlist on
			 	amlist.".$table['list_filed']['list_id']."=amvod.".$table['vod_filed']['vod_cid']."  
			 		where ".$table['vod_filed']['vod_name']." like '%$keyvalue%' or ".$table['vod_filed']['vod_content']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_keywords']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_actor']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_director']." like '%$keyvalue%'  
					order by ".$table['vod_filed']['vod_hits']." desc limit ".$page*$pagesize.",".$pagesize;
	
	return $query;
}
function search_list_2($keyvalue)
{
	global $table;
	
	$query = "SELECT count(*) as t FROM ".$table['vod_table_name']." 
				where ".$table['vod_filed']['vod_name']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_content']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_keywords']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_actor']." like '%$keyvalue%' 
						or ".$table['vod_filed']['vod_director']." like '%$keyvalue%'  
				order by ".$table['vod_filed']['vod_hits']." desc ";
	
	return $query;
}


function hotsearch_list()
{
	global $table;
	
	$query = "SELECT ".$table['vod_filed']['vod_name']." as ".returnkey('vod_filed',$table['vod_filed']['vod_name'])."
			   FROM ".$table['vod_table_name']." 
					order by ".$table['vod_filed']['vod_hits']." desc limit 100";
	
	return $query;
}

function randomlist_1($page, $pagesize){
	global $table;
	
	$query =  "SELECT ".$table['vod_filed']['vod_id']." FROM ".$table['vod_table_name']." order by ".$table['vod_filed']['vod_hits_day']." desc limit ".$pagesize*100;

	

	return $query;
}


function randomlist_2($vodci,$page, $pagesize){
	global $table;

	
	if (sizeof($vodci)<$pagesize){
		$pagesize = sizeof($vodci);
	}
	
	$vodci1  = array_rand($vodci,$pagesize);
	
	$arr_vod  = "";
	foreach($vodci1 as $key=>$val){
		if ($key != 0){
			$arr_vod .= ",";
		}
		$arr_vod .= $vodci[$val][$table['vod_filed']['vod_id']];
	}


	$query = "SELECT ".table_filed_trans('vod_filed', '')." FROM ".$table['vod_table_name']."  where ".$table['vod_filed']['vod_id']." in ($arr_vod) ";
	
	return $query;
}


