Загрузка данных



def _normalize_report_text_for_match(text: str) -> str:
    return text.lower().replace("ё", "е")




def find_matching_exported_item(
    items: List[Any],
    expected_data_type: Any,
    name_substring: str,
    tu_name_substring: str,
    period_start: datetime,
    period_end: datetime,
    period_tolerance_minutes: int = ReportConst.REPORT_PERIOD_TOLERANCE_MINUTES,
) -> Optional[Any]:
    """
    Ищет элемент списка по типу, подстрокам в имени (отчёт + ТУ) и периоду start/end с допуском.
    """
    name_substring_normalized = _normalize_report_text_for_match(name_substring)
    tu_name_normalized = _normalize_report_text_for_match(tu_name_substring)

    matched_items = []
    for item in items:
        if item.exportedDataType != expected_data_type:
            continue
        item_name_normalized = _normalize_report_text_for_match(item.name or "")
        if name_substring_normalized not in item_name_normalized:
            continue
        if tu_name_normalized not in item_name_normalized:
            continue
        if item.start is None or item.end is None:
            continue
        if not _exported_item_period_matches(item.start, item.end, period_start, period_end, period_tolerance_minutes):
            continue
        matched_items.append(item)

    if not matched_items:
        return None
    return max(matched_items, key=lambda exported_item: exported_item.id)