SELECT DISTINCT
		--resource
		MedStaffFact.MedStaffFact_id,
		MedStaffFact.Person_SurName AS Last_Name,
		MedStaffFact.Person_FirName AS First_Name,
		MedStaffFact.Person_SecName AS Middle_Name,
		MedStaffFact.Person_Snils AS SNILS,
			
		avail.MedPersonalPost_Code AS Post_Id,
		avail.Post_Code AS Inner_Id,
		avail.Post_Name AS Inner_Name,

		--date					
		cast(ttg.TimeTableGraf_begTime as date) "begTime"
	FROM (  
	 
	SELECT MedStaffFact.* 
	FROM dbo.v_MedStaffFact MedStaffFact  
     left join lateral  (
        SELECT 
            COUNT(*) as allow
        FROM DataStorage   
        WHERE DataStorage_Value='1' AND DataStorage_Name='allow_queue_auto'
          AND DataStorage.Lpu_id = MedStaffFact.Lpu_id
    ) AS AllowLpu  ON true 
	 left join lateral  (
		SELECT 
			count(q.EvnQueue_id) as countQeue
		FROM dbo.v_LpuSection LpuSection  
		INNER JOIN v_EvnQueue q   on q.Lpu_id = MedStaffFact.Lpu_id AND q.LpuSectionProfile_did = LpuSection.LpuSectionProfile_id
		INNER JOIN dbo.EvnDirection ed   on ed.Evn_id = q.EvnDirection_id  AND COALESCE(ed.evn_deleted, 1) = 1 
		  
		WHERE LpuSection.LpuUnit_id = MedStaffFact.LpuUnit_id
		  AND ed.MedStaffFact_id is null
		  AND q.QueueFailCause_id is null
		  AND q.RecMethodType_id in (1,2)
		  AND q.EvnQueueStatus_id in (1)
	) AS EvnQueueStatus  ON true 
	 left join lateral  (
		SELECT 
			count(q.EvnQueue_id) as countMed
		FROM v_EvnQueue q  
		INNER JOIN dbo.EvnDirection ed   on ed.Evn_id = q.EvnDirection_id AND ed.MedStaffFact_id = MedStaffFact.MedStaffFact_id  AND COALESCE(ed.evn_deleted, 1) = 1 
		  
		WHERE  q.Lpu_id = MedStaffFact.Lpu_id
		  AND q.QueueFailCause_id is null
		  AND q.RecMethodType_id in (1, 2)
		  AND q.EvnQueueStatus_id in (1)
	) AS EvnQueueStatusMed  ON true 
	WHERE MedStaffFact.LpuUnit_id = $1 AND COALESCE(MedStaffFactCache_isNotShown, 1) != 2
	  AND (COALESCE(AllowLpu.allow, 0) = 0 OR (EvnQueueStatus.countQeue=0 AND EvnQueueStatusMed.countMed=0 ))
  ) MedStaffFact
	INNER JOIN (SELECT dbo.tzGetDate() curDate) cdt ON 1=1
	INNER JOIN (
		 
		 
		 
		 
		 
	 
		 
		SELECT
			MedStaffFact.MedStaffFact_id,
			Post.Post_Code,
			Post.Post_Name,
			mpp.MedPersonalPost_Code
		FROM dbo.v_MedStaffFact MedStaffFact  
		INNER JOIN (SELECT dbo.tzGetDate() curDt) cdt ON curDt <= COALESCE(MedStaffFact.WorkData_endDate, curDt)
		INNER JOIN dbo.LpuUnit LpuUnit0   ON LpuUnit0.LpuUnit_id = MedStaffFact.LpuUnit_id AND LpuUnit0.LpuUnit_id = $2
		INNER JOIN nsi.v_PostLink PostLink   on PostLink.Post_lid = MedStaffFact.Post_id
		INNER JOIN nsi.v_Post Post   on Post.Post_id = PostLink.Post_id
		INNER JOIN persis.Post pp   on pp.id =  MedStaffFact.Post_id
		INNER JOIN persis.FRMPPost frp   on frp.id =  pp.frmpEntry_id
		INNER JOIN nsi.MedPersonalPost mpp   on mpp.MedPersonalPost_id =  frp.Frmr_id
	 
		 
				 
		AND (
			 
				1=1
				 
	 	)
	 	 		
		 	
		 
			AND Post.Post_Code ::bigint  NOT IN (0)
		 	
		 
			AND Post.Post_Code ::bigint  NOT IN (0)
		 	
		 	
	 
			 
	) avail ON avail.MedStaffFact_id = MedStaffFact.MedStaffFact_id
	--SPEC
	 left join lateral  (
		SELECT   fspec.FRMRNomenSpeciality_Code
		FROM v_MedSpecOms MedSpecOms   
		INNER JOIN persis.FRMRNomenSpecialityLink flink   on MedSpecOms.MedSpecClass_id=flink.MedSpecClass_fedid
		INNER JOIN persis.FRMRNomenSpeciality fspec   on fspec.FRMRNomenSpeciality_id=flink.FRMRNomenSpeciality_id
		WHERE MedStaffFact.MedSpecOms_id = MedSpecOms.MedSpecOms_id
		 LIMIT 1  
	) Spec  ON true 
	--бирки
	LEFT JOIN dbo.v_TimeTableGraf ttg   ON ttg.MedStaffFact_id = MedStaffFact.MedStaffFact_id 
		AND TimeTableType_id in (1,11,16,17 ) 
		AND ttg.Person_id IS null
		 
				AND CAST(TimeTableGraf_begTime AS date) > CAST(cdt.curDate AS date) 
			 
		AND cast(TimeTableGraf_begTime as date) >= cast( $3 as date)
		AND cast(TimeTableGraf_begTime as date) <= cast( $4 as date)	
		 	
			AND (CAST(TimeTableGraf_begTime AS date) !=  CAST(cdt.curDate AS date)+1  or CAST(cdt.curDate AS TIME) < '17:00')
		 
		AND (CAST(TimeTableGraf_begTime AS date) !=  CAST(cdt.curDate AS date)+20  or CAST(cdt.curDate AS TIME) >= '21:00')
		
	WHERE (1=1)
		  
				AND avail.MedPersonalPost_Code ::bigint  in 
				 (  
			       $5
		    	 )
