<?php 
class Sort_mod  extends HC_Controller
{
	public function __construct()
	{
		parent::__construct();
		if(empty($this -> db)){
			$this -> load_db();
		}
	}
	
	public function get_sort( $table = 'dow_sort',$prefix = 'sort_', $limit = 2000 )
	{
		if($table!='' && $prefix){
		$sql_query = " SELECT ".$prefix."id, ".$prefix."name,".$prefix."parent_id  FROM  ".$this -> db -> prefix.$table."  WHERE 1 and ".$prefix."enabled = 1 ORDER BY `".$prefix."list` , ".$prefix."id ASC limit $limit ";
		return $infosm = $this -> db -> select_info($sql_query);
		}else{ return false; }
	}
	

	//无数据查询及处理
	public function select_son_prs_id( $parent_id = 0 )
	{
		$sql_query = " SELECT prs_id, prs_parent_id  FROM  ".$this -> db -> prefix."pro_sort  WHERE 1 and prs_enabled = 1 GROUP BY prs_id ORDER BY prs_parent_id ASC , prs_id ASC , `prs_list` limit 1000 ";

		$sort_info = $this -> db -> select_info($sql_query);
		$ret_info  = $this -> build_son_id_str( $sort_info , $parent_id );
		return $ret_info;
	}
	
	public function build_son_id_str( $sort_array = array(), $check_parent_id = 0 )
	{
		foreach((array)$sort_array as $key => $val)
		{
			$all_array[$val['prs_id']]['prs_id'] 		= $val['prs_id'];
			$all_array[$val['prs_id']]['prs_parent_id'] = $val['prs_parent_id'];
		}
		if($check_parent_id!=0){
			$re_arr[0] = $check_parent_id ;	
		}else{
			$re_arr[0] = 0 ;	
		}
		return $this -> get_son_id( $all_array  , $re_arr ,  $grade_val = 6   );
			
	}
	
	public function get_prs_son_id( $parent_id , $prs_key_val )
	{
		if($parent_id!=0){
			$re_arr[0] = $parent_id ;	
		}else{
			$re_arr[0] = 0 ;	
		}
		
		return $this -> get_son_id( $prs_key_val  , $re_arr ,  $grade_val = 6   );
	}
	
	
	public function get_son_id( $big_arr = array() , $re_arr = array(),  $grade_val = 6  )
	{
		
		if( $grade_val >= 0 ){
			foreach((array) $big_arr as $akey => $aval ){
						if( in_array( $aval['prs_parent_id'] , $re_arr ) ){
							$re_arr[] = $aval['prs_id'];

							unset( $big_arr[$akey] );

							$a = $this -> get_son_id( $big_arr  , $re_arr ,  $grade_val -1  );	

							if(is_array($a)){
								$re_arr = array_merge($re_arr,$a);
							}

						}

			}
			
		}
		
		if($re_arr[0]==0){ unset($re_arr[0]); }

		if(count($re_arr)<1){ return false; }else{	return array_unique($re_arr); }
	}


} 