--关键事项

CREATE PROCEDURE [dbo].[Sp_GJSX_getlist] 
	-- Add the parameters for the stored procedure here
	 @ProjectId nvarchar(max),
	 @sql_where varchar(max)	
AS
BEGIN
		IF EXISTS(select * from tempdb..sysobjects where id=object_id('tempdb..#GJSX_List'))
		BEGIN
			DROP TABLE #GJSX_List
		END
		DECLARE @Sql NVARCHAR(max)
		select * into  #GJSX_List  
		from(
		select  
		a.GJSXID  
		,a.ProjectId 
		,a.detail
		,b.username
		,a.createDate 
		,User_ReceiveID=STUFF(( SELECT ',' + p2.UserName  FROM dbo.Sys_User  as p2  where  PATINDEX('%,' + RTRIM(p2.UserId) + ',%',',' +a.User_ReceiveID  + ',') >0  FOR XML PATH('')), 1, 1,'')   
		, CNProfessionalId=STUFF(( SELECT ',' + Base_CNProfessional.ProfessionalName  FROM dbo.Base_CNProfessional     where  PATINDEX('%,' + RTRIM(Base_CNProfessional.CNProfessionalId) + ',%',',' +a.CNProfessional_ID  + ',') >0  FOR XML PATH('')), 1, 1,'')
		,Base_Project.ProjectName
		,Base_Unit.unitname 
		,a.CloseDate
		--,a.state
		--,case a.state when 2 then '正在进行' when 3 then '正在进行' when 0 then '已关闭'  when  1 then '编辑中' end  as stateStr 
		,case a.state when 2 then (case when GETDATE()>a.CompleteDate then 4 else a.state end) when 3 then (case when GETDATE()>a.CompleteDate then 4 else a.state end) when 0 then a.state  when  1 then a.state end  as state 
		,case a.state when 2 then (case when GETDATE()>a.CompleteDate then '已超期' else '正在进行' end) when 3 then (case when GETDATE()>a.CompleteDate then '已超期' else '正在进行' end) when 0 then '已关闭'  when  1 then '编辑中' end  as stateStr 
		,QuestionTypeName=STUFF(( SELECT ',' + Base_QuestionType.QuestionTypeName  FROM dbo.Base_QuestionType     where  PATINDEX('%,' + RTRIM(Base_QuestionType.QuestionTypeID) + ',%',',' +a.QuestionTypeID  + ',') >0  FOR XML PATH('')), 1, 1,'')
		,GJSXTypeName = STUFF((SELECT ',' + Base_GJSXType.GJSXTypeName FROM dbo.Base_GJSXType where PATINDEX('%,' + RTRIM(Base_GJSXType.GJSXTypeID) + ',%', ',' + a.GJSXTypeID + ',') > 0 FOR XML PATH('')), 1, 1,'')
		,a.IsManypeople
		,a.CompleteDate
		,a.AttachUrl 
		,user_Acceptance =STUFF(( SELECT ',' + p2.UserName  FROM dbo.Sys_User  as p2  where  PATINDEX('%,' + RTRIM(p2.UserId) + ',%',',' +a.user_Acceptance  + ',') >0  FOR XML PATH('')), 1, 1,'')    
		,(select top 1 ProgressStatus+'：'+Progress_detail from GJSX_detail where GJSXID=a.GJSXID order by Date desc) as LastProgress 
    from GJSX  as   a  left join   Sys_User  as  b   on   a.UserId=b.UserId
		left join [dbo].[Base_Project]  on a.ProjectId =Base_Project.ProjectId
		left join [dbo].[Base_Unit] on  a.UnitId=Base_Unit.UnitId
		--left join [dbo].[Base_QuestionType] on a.QuestionTypeID=Base_QuestionType.QuestionTypeID 
		--left join[dbo].[Base_GJSXType] on a.GJSXTypeID = Base_GJSXType.GJSXTypeID
		where a.ProjectId=@ProjectId   
		) as a

		set @Sql ='select * from  #GJSX_List  where  1=1  ' +@sql_where  +' order by state desc,GJSXID asc'

		exec(@Sql) 


		--select  
		--a.GJSXID  
		--,a.ProjectId 
		--,a.detail
		--,b.username
		--,a.createDate 
		--,User_ReceiveID=STUFF(( SELECT ',' + p2.UserName  FROM dbo.Sys_User  as p2  where  PATINDEX('%,' + RTRIM(p2.UserId) + ',%',',' +a.User_ReceiveID  + ',') >0  FOR XML PATH('')), 1, 1,'')   
		--, CNProfessionalId=STUFF(( SELECT ',' + Base_CNProfessional.ProfessionalName  FROM dbo.Base_CNProfessional     where  PATINDEX('%,' + RTRIM(Base_CNProfessional.CNProfessionalId) + ',%',',' +a.CNProfessional_ID  + ',') >0  FOR XML PATH('')), 1, 1,'')
		--,Base_Project.ProjectName
		--,Base_Unit.unitname 
		--,a.CloseDate
		--,case a.state when  2 then '正在进行' when  3 then '待办' when 0 then '已关闭'  when  1 then '开放' end  as state 
		--,QuestionTypeName=STUFF(( SELECT ',' + Base_QuestionType.QuestionTypeName  FROM dbo.Base_QuestionType     where  PATINDEX('%,' + RTRIM(Base_QuestionType.QuestionTypeID) + ',%',',' +a.QuestionTypeID  + ',') >0  FOR XML PATH('')), 1, 1,'')
		--,GJSXTypeName = STUFF((SELECT ',' + Base_GJSXType.GJSXTypeName FROM dbo.Base_GJSXType where PATINDEX('%,' + RTRIM(Base_GJSXType.GJSXTypeID) + ',%', ',' + a.GJSXTypeID + ',') > 0 FOR XML PATH('')), 1, 1,'')
		--,a.IsManypeople
		--,a.CompleteDate
		--,a.AttachUrl 
		--,user_Acceptance =STUFF(( SELECT ',' + p2.UserName  FROM dbo.Sys_User  as p2  where  PATINDEX('%,' + RTRIM(p2.UserId) + ',%',',' +a.user_Acceptance  + ',') >0  FOR XML PATH('')), 1, 1,'')    
		--from GJSX  as   a  left join   Sys_User  as  b   on   a.UserId=b.UserId
		--left join [dbo].[Base_Project]  on a.ProjectId =Base_Project.ProjectId
		--left join [dbo].[Base_Unit] on  a.UnitId=Base_Unit.UnitId
		----left join [dbo].[Base_QuestionType] on a.QuestionTypeID=Base_QuestionType.QuestionTypeID 
		----left join[dbo].[Base_GJSXType] on a.GJSXTypeID = Base_GJSXType.GJSXTypeID
		--where a.ProjectId=@ProjectId    
END