<?php 
class CActionConfigRole 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()
	{
		$role_name = $_POST["role_name"];
		$role_desc = $_POST["role_desc"];
		$role_grade = $_POST["role_grade"];
		
		$nCount = $this->db->GetCount('tb_role', 'role_name', $role_name);
		if($nCount > 0) return 2; //部门名称相同
		
		$role_id = $this->db->GetMaxField('tb_role', 'role_id')+1;
		$sql = "insert into tb_role (role_id, role_grade, role_name, role_desc) values (".$role_id.", ".$role_grade.", '".$role_name."', '".$role_desc."')";
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Mod()
	{
		$role_id = $_POST["role_id"];
		$role_name = $_POST["role_name"];
		$role_desc = $_POST["role_desc"];
		$role_grade = $_POST["role_grade"];
		
		$sql = "select count(*) nCount from tb_role where role_name = '".$role_name."' and role_id <>".$role_id;
		$this->db->ExeQuerySql($sql);
		if($this->db->FetchRow())
		{
				if($this->db->Get('nCount')>0) return 2; //角色名称相同
		}
		
		$sql = "update tb_role set role_grade=".$role_grade.", role_name='".$role_name."', role_desc='".$role_desc."' where role_id=".$role_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
	function Del()
	{
		$role_id = $_POST["role_id"];
		
		$nCount = $this->db->GetCount('tb_user', 'role_id', $role_id);
		if($nCount > 0) return 1; //角色成员不为空
		
		$sql = "delete from tb_role where role_id=".$role_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
};
?>
