<?php 
class CActionConfigDept extends CActionBase
{
	function doAction()
	{
		$ret = 0;
		$job = $_POST["job"];
		switch($job)
		{
			case "add":
				$ret = $this->Add();
			break;
			case "mod":
				$ret = $this->Mod();
			break;
			case "del":
				$ret = $this->Del();
			break;
		}
		
		return $ret;
	}	
	
	function Add()
	{
		$dept_name = $_POST["dept_name"];
		$dept_desc = $_POST["dept_desc"];
		
		$nCount = $this->db->GetCount('tb_dept', 'dept_name', $dept_name);
		if($nCount > 0) return 2; //部门名称相同
		
		$dept_id = $this->db->GetMaxField('tb_dept', 'dept_id')+1;
		$sql = "insert into tb_dept (dept_id, dept_name, dept_desc) values (".$dept_id.", '".$dept_name."', '".$dept_desc."')";
		$this->db->ExeSql($sql);
		return 1;
	}
	
	function Mod()
	{
		$dept_id = $_POST["dept_id"];
		$dept_name = $_POST["dept_name"];
		$dept_desc = $_POST["dept_desc"];
		
		$sql = "select count(*) nCount from tb_dept where dept_name = '".$dept_name."' and dept_id <>".$dept_id;
		$this->db->ExeQuerySql($sql);
		if($this->db->FetchRow())
		{
				if($this->db->Get('nCount')>0) return 2; //部门名称相同
		}
		
		$sql = "update tb_dept set dept_name='".$dept_name."', dept_desc='".$dept_desc."' where dept_id=".$dept_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Del()
	{
		$dept_id = $_POST["dept_id"];
		//判断部门成员是否为空
		$nCount = $this->db->GetCount('tb_user', 'dept_id', $dept_id);
		if($nCount > 0) return 1; //部门成员不为空
		
		$sql="delete from tb_dept where dept_id=".$dept_id;
		$this->db->ExeSql($sql);
		return 0;	
	}
	
};
?>
