<?php 
class CActionConfigWorkGroup 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()
	{
		$project_id	 = $_POST["project_id"];
		$group_name = $_POST["group_name"];
		$group_desc = $_POST["group_desc"];
		$group_grade = $_POST["group_grade"];
		$group_id = $this->db->GetMaxField('tb_project_group', 'group_id')+1;
		
		$sql = "insert into tb_project_group (group_id, project_id, group_name, group_grade, group_desc) values (";
		$sql = $sql . $group_id . ",";
		$sql = $sql . $project_id . ",";
		$sql = $sql . "'".$group_name."',";
		$sql = $sql . $group_grade . ",";
		$sql = $sql . "'".$group_desc."'";
		$sql = $sql . ")";
		//return $sql;
		$this->db->ExeSql($sql);
		
		//添加项目组成员
		$members = json_decode($_POST["members"]);
		for($i=0;$i<sizeof($members);$i++)
		{
			$sql = "insert into tb_project_group_member (group_id, user_id) values (".$group_id.", ".$members[$i].")";
			$this->db->ExeSql($sql);	
		}
		
		return 0;
	}
	
	function Mod()
	{
		$group_name = $_POST["group_name"];
		$group_desc = $_POST["group_desc"];
		$group_grade = $_POST["group_grade"];
		$group_id = $_POST["group_id"];
		
		$sql = "update tb_project_group set ";
		$sql = $sql . "group_name ='".$group_name."',";
		$sql = $sql . "group_desc ='".$group_desc."',";
		$sql = $sql . "group_grade = ".$group_grade;		
		$sql = $sql . " where group_id=".$group_id;
		$this->db->ExeSql($sql);
		
		$sql = "delete from tb_project_group_member where group_id=".$group_id;
		$this->db->ExeSql($sql);	
		
		$members = json_decode($_POST["members"]);
		for($i=0;$i<sizeof($members);$i++)
		{
			$sql = "insert into tb_project_group_member (group_id, user_id) values (".$group_id.", ".$members[$i].")";
			$this->db->ExeSql($sql);	
		}
		
		return 0;
	}
	
	function Del()
	{
		$group_id = $_POST["group_id"];
		
		$sql = "delete from tb_project_group_member where group_id=".$group_id;
		$this->db->ExeSql($sql);	
		
		$sql = "delete from tb_project_group where group_id=".$group_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
};
?>
