<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司（TEL-15896098989）
	程序版本：1.0
	更新日期：2015-05-25   
-------------------------------------*/
class industy
{
	private $db;
	private $table;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=LA_PRE.'industy';
	}

	function industylist($parentid=0)
	{
		$parentid=intval($parentid);
		return $this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid ORDER BY `$this->table`.`orderby` ASC");
	}

	function industydata($parentid=0,$m=1)
	{
		$parentid=intval($parentid);
		$m=intval($m);
		return $this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid AND `$this->table`.`m`=$m ORDER BY `$this->table`.`orderby` ASC");
	}

	function info($id)
	{
		return $this->db->fetch_one("SELECT * FROM `$this->table` WHERE `$this->table`.`id`=".intval($id));
	}

	function getchildid($id,$withself=1)
	{
		$id=intval($id);
		$childid=$withself?array($id):array();

		$r=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($id));

		if($r)
		{
			foreach($r as $_r) 
			{
				$childid[]=$_r['id'];
				$r2=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r['id']));
				if($r2)
				{
					foreach($r2 as $_r2) 
					{
						$childid[]=$_r2['id'];
						$r3=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r2['id']));
						if($r3)
						{
							foreach($r3 as $_r3) 
							{
								$childid[]=$_r3['id'];
								$r4=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r3['id']));
								if($r4)
								{
									foreach($r4 as $_r4) 
									{
										$childid[]=$_r4['id'];
									}
								}

							}
						}
					}
				}
			}
		}

		return array_unique($childid);
	}

	function getname($id)
	{
		$r=$this->info($id);
		return $r?$r['name']:'不限';
	}

	function parentid($id)
	{
		$r=$this->info($id);
		return $r['parentid'];
	}

	function m($id)
	{
		$r=$this->info($id);
		return $r['m'];
	}

	function opindusty($m=1,$parentid=0,$ids,$orderby,$name,$spell,$simplespell,$neworderby,$newname,$newspell,$newsimplespell)
	{
		$this->db->query("DELETE FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid AND `$this->table`.`m`=$m");
		$newindusty=array();
		if($ids)foreach($ids as $id)
		{
			if($name[$id])
			{
				$newindusty['id']=$id;
				$newindusty['parentid']=$parentid;
				$newindusty['m']=$m;
				$newindusty['orderby']=$orderby[$id];
				$newindusty['name']=$name[$id];
				$newindusty['spell']=$spell[$id];
				$newindusty['simplespell']=$simplespell[$id];
				$this->db->insert($this->table,$newindusty,true);
			}
		}
		$newindusty=array();
		if($neworderby)foreach($neworderby as $key => $val)
		{
			if($newname[$key])
			{
				$newindusty['orderby']=$val;
				$newindusty['parentid']=$parentid;
				$newindusty['m']=$m;
				$newindusty['name']=$newname[$key];
				$newindusty['spell']=$newspell[$key];
				$newindusty['simplespell']=$newsimplespell[$key];
				$this->db->insert($this->table,$newindusty);
			}
		}

		return true;
	}
}
?>
