SELECT 
		'prod-conc-50301-rmis'/*из конфига*/ AS Rmis_Id,
		concat('m.', t.TimeTableMedServiceHist_id) AS Book_Id_Rmis,
		t.TimetableMedService_begTime AS Visit_Time,
		t.Status_id,
		'1' as paymentSourceId,
		LpuUnit.LpuUnit_id AS MO_Id,
		COALESCE(LpuUnit_FRMOUnitID, FRMOUnit.FRMOUnit_OID, FRMOSection.FRMOUnit_OID, Token.PassportToken_tid) AS MO_OID,		
		CASE 
			WHEN LpuUnit_FRMOid IS NOT NULL THEN CONCAT(lpuunit.LpuUnit_Name, ', ', Org.Org_Nick)
			ELSE CONCAT(COALESCE(FRMOUnit.FRMOUnit_Name, FRMOSection.FRMOUnit_Name, PassportToken_ShortName), ', ', Org.Org_Nick) 
		END AS MO_Name,
		concat('vac.', t.UslugaComplexMedService_id) roomId,
		COALESCE(LpuUnit_FRMOUnitID, FRMOUnit.FRMOUnit_OID, FRMOSection.FRMOUnit_OID, Token.PassportToken_tid) roomOID,
		'Кабинет вакцинации Covid-19' roomName,
		1 specialtyId,
		'B04.014.004' ServiceSpec_Code,
		'Кабинет вакцинации Covid-19' ServiceSpec_Name,
		concat('vac.', t.UslugaComplexMedService_id) ServiceSpec_Id, 
		patient.lastName patient_lastName,
		patient.firstName patient_firstName,
		patient.middleName patient_middleName,
		patient.birthDate patient_birthDate,
		patient.omsNumber patient_omsNumber,
		patient.snils patient_snils, 
		patient.sex patient_sex,
		patient.Age patient_Age,
		patient.isVillager patient_isVillager,
		'PRIMARY' as appointmentType_id,
		'PROPHYLACTIC' as appointmentReason_id,
		CASE 
			WHEN t.UserId = 999900 THEN 'EPGU'
			WHEN RecMethodType.RecMethodType_FedCode = 'OTHER' THEN 'OTHER_WEB'
			ELSE COALESCE(RecMethodType.RecMethodType_FedCode,'REG') 
		END AS Code_id
	FROM (
		SELECT 
		   ttms.TimeTableMedService_id, 
		   ttms.TimetableMedService_begTime,
		   ttms.MedService_id,
		   ttms.UslugaComplexMedService_id, 
		   ttms.RecMethodType_id, 
		   hist.TimeTableMedServiceHist_id,
		   hist.TimeTableMedServiceHist_insDT, 
		   hist.TimeTableActionType_id, 
		   hist.pmUser_updID UserId,
		   ttms.Person_id,
		   'RECORDED' status_id,
			vacServices.LpuUnit_id
		FROM dbo.TimeTableMedService ttms   
		INNER JOIN dbo.TimeTableMedServiceHist hist   ON hist.TimeTableMedService_id = ttms.TimeTableMedService_id
		INNER JOIN  ( 
	SELECT DISTINCT
		ms.LpuUnit_id,
		uslms.UslugaComplexMedService_id
	from dbo.v_MedService ms  
	inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 31
	inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
	inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id and usl.UslugaComplex_Code='B04.014.004.007'
 ) vacServices ON vacServices.UslugaComplexMedService_id = ttms.UslugaComplexMedService_id
		WHERE hist.TimeTableActionType_id = 2
		  AND ttms.TimetableMedService_deleted = 1
		  AND hist.TimeTableMedServiceHist_deleted = 1
		  AND hist.pmUser_updID <> 999900
		  AND hist.TimeTableMedServiceHist_insDT >= $1 
		UNION ALL
		SELECT 
		   ttms.TimeTableMedService_id, 
		   ttms.TimetableMedService_begTime,
		   ttms.MedService_id,
		   ttms.UslugaComplexMedService_id, 
		   ttms.RecMethodType_id, 
		   hist1.TimeTableMedServiceHist_id,
		   hist.TimeTableMedServiceHist_insDT, 
		   hist.TimeTableActionType_id, 
		   hist1.pmUser_updID UserId,
		   ttms.Person_id,
		   CASE WHEN hist.TimeTableMedServiceHist_insDT < ttms.TimetableMedService_begTime then 'CANCELED' ELSE 'PATIENT_NOT_ARRIVED' END status_id,
		   vacServices.LpuUnit_id
		FROM dbo.TimeTableMedService ttms  
		INNER JOIN dbo.TimeTableMedServiceHist hist   ON hist.TimeTableMedService_id = ttms.TimeTableMedService_id
		INNER JOIN  ( 
	SELECT DISTINCT
		ms.LpuUnit_id,
		uslms.UslugaComplexMedService_id
	from dbo.v_MedService ms  
	inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 31
	inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
	inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id and usl.UslugaComplex_Code='B04.014.004.007'
 ) vacServices ON vacServices.UslugaComplexMedService_id = ttms.UslugaComplexMedService_id
		 left join lateral  (
			select   h.* from dbo.TimeTableMedServiceHist h  
			where h.TimeTableActionType_id = 2
			  and h.TimeTableMedServiceHist_deleted = 1
			  and h.TimeTableMedServiceHist_insDT <= hist.TimeTableMedServiceHist_insDT
			  and h.TimeTableMedService_id = hist.TimeTableMedService_id
		    order by hist.TimeTableMedServiceHist_insDT desc
		     limit 1 
		) hist1  ON true 
		WHERE hist.TimeTableActionType_id = 3
		  AND ttms.TimetableMedService_deleted = 1
		  AND hist.TimeTableMedServiceHist_deleted = 1
		  AND hist1.pmUser_updID <> 999900
		  AND hist.TimeTableMedServiceHist_insDT >= $2 
		UNION ALL
		SELECT 
			ttms.TimeTableMedService_id, 
			ttms.TimetableMedService_begTime,
			ttms.MedService_id,
			ttms.UslugaComplexMedService_id, 
			ttms.RecMethodType_id, 
			hist.TimeTableMedServiceHist_id,
			hist.TimeTableMedServiceHist_insDT, 
			hist.TimeTableActionType_id, 
			hist.pmUser_updID UserId,
			ttms.Person_id,
			'SUCCESS' status_id,
			vacServices.LpuUnit_id
		FROM dbo.TimeTableMedService ttms  
		INNER JOIN dbo.v_EvnDirection_all ed   on ttms.EvnDirection_id = ed.EvnDirection_id and ed.EvnStatus_id = 15 and ed.EvnDirection_statusDate >= $3
		INNER JOIN  ( 
	SELECT DISTINCT
		ms.LpuUnit_id,
		uslms.UslugaComplexMedService_id
	from dbo.v_MedService ms  
	inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 31
	inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
	inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id and usl.UslugaComplex_Code='B04.014.004.007'
 ) vacServices ON vacServices.UslugaComplexMedService_id = ttms.UslugaComplexMedService_id
		 left join lateral  (
			select   hist.* from dbo.TimeTableMedServiceHist hist  
			where hist.TimeTableActionType_id in (2,3)
			  and hist.TimeTableMedServiceHist_deleted = 1
			  and hist.TimeTableMedService_id = ttms.TimeTableMedService_id
		    order by hist.TimeTableMedServiceHist_insDT desc
		     limit 1 
		) hist  ON true 
		WHERE hist.TimeTableActionType_id = 2
		  AND ttms.TimetableMedService_deleted = 1
		UNION ALL
		SELECT 
		   ttms.TimeTableMedService_id, 
		   ttms.TimetableMedService_begTime,
		   ttms.MedService_id,
		   ttms.UslugaComplexMedService_id, 
		   ttms.RecMethodType_id, 
		   hist.TimeTableMedServiceHist_id,
		   hist.TimeTableMedServiceHist_insDT, 
		   hist.TimeTableActionType_id, 
		   hist.pmUser_updID UserId,
		   ttms.Person_id,
		   'PATIENT_NOT_ARRIVED' status_id,
		   vacServices.LpuUnit_id
		FROM dbo.TimeTableMedService ttms  
		INNER JOIN dbo.v_EvnDirection_all ed   on ttms.EvnDirection_id = ed.EvnDirection_id and not COALESCE(ed.EvnStatus_id,0) = 15
		INNER JOIN  ( 
	SELECT DISTINCT
		ms.LpuUnit_id,
		uslms.UslugaComplexMedService_id
	from dbo.v_MedService ms  
	inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 31
	inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
	inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id and usl.UslugaComplex_Code='B04.014.004.007'
 ) vacServices ON vacServices.UslugaComplexMedService_id = ttms.UslugaComplexMedService_id
		 left join lateral  (
			select   hist.* from dbo.TimeTableMedServiceHist hist
			where hist.TimeTableActionType_id in (2,3)
			  and hist.TimeTableMedServiceHist_deleted = 1
			  and hist.TimeTableMedService_id = ttms.TimeTableMedService_id
		    order by hist.TimeTableMedServiceHist_insDT desc
		     limit 1 
		) hist  ON true 
		 
		INNER JOIN (
			select
				cast(dbo.tzGetDate() as date) + case when EXTRACT(HOUR FROM dbo.tzGetDate()) > 4 then -5+1 else -5-1 end endDate,
				cast($4 as date) + case when EXTRACT(HOUR FROM $5::timestamp) < 5 then -5 else 1-5 end begDate
		) fail ON true
		 
		WHERE hist.TimeTableActionType_id = 2
		  AND ttms.TimetableMedService_deleted = 1
		  AND cast(ttms.TimetableMedService_begTime as date) BETWEEN fail.begDate and fail.endDate
	) t
	LEFT JOIN dbo.RecMethodType   ON RecMethodType.RecMethodType_id = t.RecMethodType_id
	LEFT JOIN dbo.v_LpuUnit LpuUnit   ON LpuUnit.LpuUnit_id = t.LpuUnit_id 
	LEFT JOIN nsi.FRMOUnit   ON FRMOUnit.FRMOUnit_id = LpuUnit.FRMOUnit_id
	 left join lateral  (
			SELECT   FRMOSection.FRMOUnit_OID, FRMOUnit_Name 
			FROM dbo.v_LpuSection  
			INNER JOIN nsi.FRMOSection   ON FRMOSection.FRMOSection_id = v_LpuSection.FRMOSection_id
			INNER JOIN nsi.FRMOUnit   ON FRMOUnit.FRMOUnit_OID = FRMOSection.FRMOUnit_OID
			WHERE FRMOSection.FRMOUnit_OID IS NOT NULL
			  AND v_LpuSection.LpuUnit_id=LpuUnit.LpuUnit_id
			 LIMIT 1 
		) AS FRMOSection  ON true 
	LEFT JOIN fed.v_PassportToken Token   ON Token.Lpu_id = LpuUnit.Lpu_id 
	LEFT JOIN dbo.v_Org Org   ON Org.Org_id = LpuUnit.Lpu_id
	 left join lateral  (
		 
		 
	SELECT   
		ps.Person_SurName lastName,
		ps.Person_FirName firstName,
		ps.Person_SecName middleName,
		ps.Person_BirthDay birthDate,
		ps.Polis_Num omsNumber,
		ps.Person_Snils snils, 
		case when ps.Sex_id is null then null when ps.Sex_id = 1 then 'M' else 'F' end sex,
		dbo.Age(Person_BirthDay,GETDATE()) AS Age,
		CASE WHEN addr.KLAreaType_id=1 THEN 0 ELSE 1 END AS isVillager
	FROM dbo.v_PersonState ps  
	INNER JOIN dbo.v_Address addr   ON addr.Address_id=COALESCE(ps.UAddress_id, ps.PAddress_id)
	WHERE ps.Person_id=t.Person_id
	 LIMIT 1 
 
	) patient  ON true 
	WHERE COALESCE(LpuUnit_FRMOUnitID, FRMOUnit.FRMOUnit_OID, FRMOSection.FRMOUnit_OID) IS NOT NULL
       AND t.UserId != 999900  
	order by t.TimetableMedService_begTime, t.TimeTableMedServiceHist_id, t.TimeTableMedServiceHist_insDT
