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

//群组基础api函数
function group_read_base($fields="*",$condition="",$by_col="",$order="",$num="",$cache="",$cache_key=""){
	global $tablePreStr;
	$t_group=$tablePreStr."groups";
	$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_group where group_pass = 1 and group_join_type = 0 $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 group_self_by_gid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and group_id in ($id_str) ";
	return group_read_base($fields,$condition);
}

function group_self_get_by_uid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and add_userid in ($id_str) ";
	return group_read_base($fields,$condition);
}

function group_self_get_by_mcount($fields="*",$order="desc",$num=10){
	$num=intval($num);
	$order=filt_order($order);
	return group_read_base($fields,'','member_count',$order,$num,1,"group_mcount_");
}

function group_self_get_new($fields="*",$num=10){
	$num=intval($num);
	return group_read_base($fields,'','group_time','desc',$num,1,"group_new_");
}

function group_self_get_by_subs($fields="*",$num=10){
	$num=intval($num);
	return group_read_base($fields,'','subjects_num','desc',$num,1,"group_sub_");
}

function group_self_by_pals($fields="*"){
	$fields=filt_fields($fields);
	$group_id_str='';
	global $tablePreStr;
	$t_group_members=$tablePreStr."group_members";
	$plugin_rs=array();
	$dbo=new dbex;
  dbplugin('r');
	$pals_id=get_sess_mypals();
	$sql=" select group_id from $t_group_members where user_id in ($pals_id) ";
	$group_data=$dbo->getRs($sql);
	foreach($group_data as $rs){
		$group_id_str.=$rs['group_id'].",";
	}
	$group_id_str=preg_replace("/,$/","",$group_id_str);
	return group_self_by_gid($fields,$group_id_str);
}

?>