<?php
include(dirname(__file__)."/../includes.php");

//好友圈基础api函数
function pals_read_base($fields="*",$condition="",$by_col="",$order="",$num="",$cache="",$cache_key=""){
	global $tablePreStr;
	$t_pals=$tablePreStr."pals_mine";
	$plugin_rs=array();
	$dbo=new dbex;
  dbplugin('r');
  if($num==''||$num >= 1000){
  	$limit=" limit 1000 ";
  }else{
  	$limit=" limit $num ";
  }
  if($by_col!=''){
  	$by_col=" order by $by_col ";
  }
  $sql=" select $fields from $t_pals where $condition $by_col $order $limit ";
  if($cache==1&&in_array($num,array(10,20,50))&&$cache_key!=''){
		$key=$cache_key.$order.'_'.$num;
		$key_mt=$cache_key.'mt_'.$order.'_'.$num;
		$plugin_rs=model_cache($key,$key_mt,$dbo,$sql);
	}
	if(empty($plugin_rs)){
		$plugin_rs=$dbo->getRs($sql);
	}
	return $plugin_rs;
}

function pals_self_by_paid($fields="*",$id='',$num=10,$accept=1){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$uid=get_sess_userid();
	$condition=" user_id = $uid ";
	if($accept!=''){
		$accept=intval($accept);
	}
	if($id_str!=''){
		$condition.=" and pals_id in ($id_str) ";
	}
	$condition.=" and accepted >= $accept ";
	return pals_read_base($fields,$condition);
}

function pals_self_by_sort($fields="*",$sort){
	$uid=get_sess_userid();
	$fields=filt_fields($fields);
	$sort_str=filt_str_array($sort);
	$condition=" user_id = $uid and pals_sort_name in ($sort_str) ";
	return pals_read_base($fields,$condition);
}

function pals_self_by_online($fields="*",$num=""){
	$num=intval($num);
	$limit='';
	if($num!=0){
		$limit=" limit $num ";
	}
	$fields=filt_fields($fields);
	$pals_id_str=get_sess_mypals();
	global $tablePreStr;
	$t_online=$tablePreStr."online";
	$plugin_rs=array();
	$dbo=new dbex;
  dbplugin('r');
  $sql=" select $fields from $t_online where user_id in ($pals_id_str) and hidden = 0 order by active_time desc $limit ";
  return $dbo->getRs($sql);
}

function pals_self_by_uid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" user_id in ($id_str) ";
	return pals_read_base($fields,$condition);
}

?>