<?php 
class CProjectMember
{
	var $user_id;
	var $real_name;	
};

class CProjectInfo
{
	var $project_id;
	var $project_name;
	var $project_state;
	var $project_code;
	var $project_desc;
	var $members;
	
	function CProjectInfo()
	{
		$this->members = array();
	}
	
	function InitMember($db, $issue_type)
	{
		$sql = "select distinct a.user_id, a.real_name from tb_user a, tb_project_worker b  where a.user_id=b.member_id and b.member_type=0 and b.issue_type_id=".$issue_type;
	$db->ExeQuerySql($sql); 
		while($db->FetchRow())
		{
			$mod = new CProjectMember();
			$mod->user_id = $db->Get("user_id");
			$mod->real_name = $db->Get("real_name");
			$this->members[sizeof($this->members)] = $mod;
		}
		$db->FreeResult();
		
		$sql = "select distinct user_id, real_name from tb_user a where user_id in ( ";
		$sql = $sql . " select user_id from tb_project_group_member a, tb_project_worker b  where a.group_id=b.member_id and b.member_type=1 and b.issue_type_id=".$issue_type.")";

		$db->ExeQuerySql($sql);
		while($db->FetchRow())
		{
			$b_find=0;
			for($i=0;$i<sizeof($this->members);$i++)
			{
				if($this->members[$i]->user_id == $db->Get("user_id"))
				{
					$b_find = 1;
					break;
				}
			}
			if($b_find) continue;
			
			$mod = new CProjectMember();
			$mod->user_id = $db->Get("user_id");
			$mod->real_name = $db->Get("real_name");
			$this->members[sizeof($this->members)] = $mod;
		}
		$db->FreeResult();
	}
	
};

class CModuleProject extends CModuleBase
{
	function Init()
	{
		$sql = "select * from tb_project order by 1";		
		$this->db->ExeQuerySql($sql);
	
		while($this->db->FetchRow())
		{			
			$mod = new CProjectInfo();
			$mod->project_id		= $this->db->Get("project_id");
			$mod->project_name	= $this->db->Get("project_name");
			$mod->project_state	= $this->db->Get("project_status");
			$mod->project_code	= $this->db->Get("project_code");
			$mod->project_desc	= $this->db->Get("project_desc");
			$this->modules[$this->GetSize()] = $mod;
		} 
		  
		$this->db->FreeResult();
	}   
	
	function InitModule($module_id)
	{
		$this->module = new CProjectInfo();
		
		$sql = "select * from tb_project where project_id=".$module_id;		
		$this->db->ExeQuerySql($sql);
	
		if($this->db->FetchRow())
		{
			$this->module->project_id		= $this->db->Get("project_id");
			$this->module->project_name	= $this->db->Get("project_name");
			$this->module->project_state	= $this->db->Get("project_status");
			$this->module->project_code	= $this->db->Get("project_code");
			$this->module->project_desc	= $this->db->Get("project_desc");
		} 
		$this->db->FreeResult();   
	}
	
	//根据用户ID获取已经加入的项目
	function InitByUserID($user_id)
	{
		$sql = "select distinct a.* from tb_project a, tb_project_issue_type b where a.project_id=b.project_id ";
		$sql = $sql . " and b.issue_type_id in ";
		$sql = $sql . "(";
		$sql = $sql . " select issue_type_id from tb_project_worker where ";
		$sql = $sql . " ( (member_type=0 and member_id=".$user_id.") or (member_type=1 and member_id in (select group_id from tb_project_group_member where user_id=".$user_id.")) )";
		$sql = $sql . " and action_id in (select action_id from tb_flow_action where src_status in (select chart_state_id from tb_flow_status where state_type=1) )";
		$sql = $sql . ") order by a.project_id";	
		
		$this->db->ExeQuerySql($sql);
	
		while($this->db->FetchRow())
		{			
			$mod = new CProjectInfo();
			$mod->project_id		= $this->db->Get("project_id");
			$mod->project_name	= $this->db->Get("project_name");
			$mod->project_state	= $this->db->Get("project_status");
			$mod->project_code	= $this->db->Get("project_code");
			$mod->project_desc	= $this->db->Get("project_desc");
			$this->modules[$this->GetSize()] = $mod;
		} 
		  
		$this->db->FreeResult();
	}
	
	function InitMember($issue_type)
	{
			$this->module->InitMember($this->db, $issue_type);
	}
	
};
?>
