do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'MassReport';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'MassReport',
'Массовая выгрузка отчетов',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'UnloadActsReportPeriod';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'UnloadActsReportPeriod',
'Выгрузка актов за отчетный период',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'UnloadSchetsToIszl';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'UnloadSchetsToIszl',
'Выгрузка счетов в ИСЗЛ',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'UnloadSMOActsRB';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'UnloadSMOActsRB',
'Выгрузка актов МЭК, МЭЭ/ЭКМП',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'UnloadSchetsProcessClient';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'UnloadSchetsProcessClient',
'Выгрузка счетов-реестров',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'MassSchetActUnload';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'MassSchetActUnload',
'Массовая выгрузка актов',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
		
do $$
declare
export_record_id UUID;
new_export_record_id UUID := gen_random_uuid();
begin
select ExportRecordID into export_record_id
from IES.T_EXPORT_RECORD
where ProcessName = 'UnloadProvideMedicalAssistanceProcessClient';
delete from IES.T_EXPORT_RECORD_ANSWER
where ExportRecordID = export_record_id;
delete from IES.T_EXPORT_RECORD_SERVICE_ROLES
where ExportRecordService = export_record_id;
delete from IES.T_EXPORT_RECORD
where ExportRecordID = export_record_id;
insert into IES.T_EXPORT_RECORD
(ExportRecordID
,ProcessName
,ProcessDescription
,Entities
,IsSystem
,type_)
values
(new_export_record_id,
'UnloadProvideMedicalAssistanceProcessClient',
'Выгрузка сведений об оказанной медпомощи',
			
		NULL
	,
true,
(select te.EntityID from VCLib.T_ENTITIES te
where te.Schema || '.' || te.EntName = 'IES.EXPORT_RECORD_SERVICE'));
			
	insert into IES.T_EXPORT_RECORD_SERVICE_ROLES (ExportRecordServiceRolesID, ExportRecordService, Roles)
	select gen_random_uuid(), new_export_record_id, r.RoleID from VCLib.T_ROLES r
	where r.RoleName = 'Администратор';
			
end $$;
