do $$
declare
    mapping_record RECORD;
    
    mapping_all_record RECORD;
    
    sql_text text;
begin
-- где есть фичи
    for mapping_record in (
        select * from (values 
            ('DISP_DIRECT_FILE', 'DispListLoad'),
            ('DISP_DIRECT_INFO_FILE', 'DispInfoLoad'),
            ('DISP_DIRECT_INTERVIEW_FILE', 'InterviewListLoad'),
            ('DISP_DIRECT_IDENT', 'DispIdentLoad'),
            ('DISP_DIRECT_FACT_FILE', 'DispFactListLoad'),
            ('DN_INFO_FILE', 'loadDNInfo'),
            ('DN_FACT_FILE', 'loadDN'),
            ('DN_ITEM', 'loadDN'),
            ('DN_IDENT', 'loadDNIdent')
        ) as t(PackageType, FeatureName)
    ) loop
        execute format('
            insert into ISZL.T_PACKAGE_TYPE_ROLES (PackageTypeRolesID, PackageType, Roles)
            select 
                gen_random_uuid(),
                (select PackageType from ISZL.t_Package_type where PackageType = %L),
                tr.RoleID
            from VCLib.T_BASE_FEATURE_SETTINGS tbfs
            join VCLib.T_ROLE_CLIENT_MODULES trcm ON tbfs.RoleClientModules = trcm.RoleClientModuleID
            join VCLib.T_ROLES tr ON tr.RoleID = trcm.RoleID
            join VCLib.T_CLIENT_MODULE_FEATURE tcmf ON tcmf.ClientModuleFeatureID = tbfs.ClientModuleFeature
            where tcmf.Name = %L',
            mapping_record.PackageType, 
            mapping_record.FeatureName
        );
    end loop;

    -- где нет фич
    for mapping_all_record in (
        select * from (values 
            ('SCHET_FILE', 'AutoLoadFilesClientModule'),
            ('BRANCH', 'AutoLoadFilesClientModule'),
            ('DIVISION', 'AutoLoadFilesClientModule'),
            ('AMOUNT_BED', 'AutoLoadFilesClientModule'),
            ('FREE_BED', 'AutoLoadFilesClientModule'),
            ('CANCEL_HR', 'AutoLoadFilesClientModule'),
            ('CANCEL_H', 'AutoLoadFilesClientModule'),
            ('HOSPREF', 'AutoLoadFilesClientModule'),
            ('HOSP', 'AutoLoadFilesClientModule'),
            ('MINHOSP', 'AutoLoadFilesClientModule'),
            ('RECOMENDATIONS', 'AutoLoadFilesClientModule'),
            ('SMO_EXPERTISE_ACT', 'AutoLoadFilesClientModule'),
            ('MO_ATTACHMENT', 'AutoLoadFilesClientModule'),
            ('DISP_DIRECT_GIS', 'AutoLoadFilesClientModule'),
            ('ONK_CANCREGISTER', 'AutoLoadFilesClientModule'),
            ('INSURED', 'AutoLoadFilesClientModule'),
            ('DISP_DIRECT_FACT_FILE', 'AutoLoadFilesClientModule'),
            ('APPEAL_TO_CALL_CENTER', 'CallCenterAdminClientModule')
        ) as t(PackageType, FeatureName)
    ) loop
        execute format('
            insert into ISZL.T_PACKAGE_TYPE_ROLES (PackageTypeRolesID, PackageType, Roles)
            select 
                gen_random_uuid(),
                (select PackageType from ISZL.t_Package_type where PackageType = %L),
                trcm.RoleID
            from VCLib.T_ROLE_CLIENT_MODULES trcm
            join VCLib.T_CLIENT_MODULES cm on cm.ClientModuleID = trcm.ClientModuleID
            where cm.ModuleType = %L',
            mapping_all_record.PackageType, 
            mapping_all_record.FeatureName
        );
    end loop;
    
end
$$;
