from __future__ import annotations """ 福袋详情数据验证模块 ==================== 验证福袋详情数据的合理性,检测识别错误。 """ from typing import Any, Tuple import re def validate_fudai_detail(detail: Tuple, analyser: Any = None) -> Tuple[bool, str]: """验证福袋详情数据的合理性。 返回: (is_valid, reason) - is_valid=True 表示数据合理,False 表示识别出错 reason 为失败原因描述 """ if not isinstance(detail, tuple) or len(detail) < 6: return False, "detail_tuple_invalid" content = str(detail[0] or "").strip() time_text = str(detail[1] or "").strip() try: participants = int(detail[2] or 0) except Exception: participants = 0 try: copies = int(detail[3] or 0) except Exception: copies = 0 try: prize_value_yuan = float(detail[4] or 0.0) except Exception: prize_value_yuan = 0.0 countdown_tuple = detail[5] countdown_start, countdown_end = None, None if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2: try: countdown_start = float(countdown_tuple[0] or 0.0) countdown_end = float(countdown_tuple[1] or 0.0) except Exception: pass # 检查1:内容不能为空(至少要有一些文字) if not content or len(content.strip()) < 2: return False, "content_empty_or_too_short" # 检查2:价值应该大于0(福袋至少应该有价值) if prize_value_yuan <= 0: return False, "prize_value_zero_or_negative" # 检查3:价值应该合理(不能太大,比如超过100万) if prize_value_yuan > 1000000: return False, "prize_value_too_large" # 检查4:倒计时应该合理 if countdown_end is not None and countdown_start is not None: remain_seconds = countdown_end - countdown_start # 倒计时不能是负数 if remain_seconds < 0: return False, "countdown_negative" # 倒计时不能太长(超过24小时) if remain_seconds > 24 * 3600: return False, "countdown_too_long" # 倒计时不能为0(至少应该有几秒) if remain_seconds == 0 and not time_text: return False, "countdown_zero_and_no_time_text" # 倒计时合理性检查:如果 time_text 存在,验证倒计时与 time_text 是否一致 # 注意:如果analyser存在且处于强制参与模式(预约回退),跳过此检查 force_attend = False if analyser: force_attend = getattr(analyser, "_force_attend_after_reservation_back", False) if time_text and not force_attend: try: # 解析 time_text 为秒数 m = re.search(r"(\d{1,2})[::](\d{1,2})", time_text) if m: mm = int(m.group(1)) ss = int(m.group(2)) # 如果秒数>=60,尝试交换分秒 if ss >= 60 and mm < 60: mm, ss = ss, mm # 如果秒数仍然>=60,进行进位 if ss >= 60: mm += ss // 60 ss = ss % 60 text_seconds = mm * 60 + ss # 允许一定误差(±10%),但差异过大则判定为识别错误 if text_seconds > 0: diff_ratio = abs(remain_seconds - text_seconds) / max(text_seconds, 1.0) if diff_ratio > 0.15: # 差异超过15%视为不一致 return False, f"countdown_mismatch_time_text (remain={int(remain_seconds)}, text={text_seconds}, diff={round(diff_ratio*100, 1)}%)" except Exception: pass # 检查5:参与人数应该合理(不能是负数) if participants < 0: return False, "participants_negative" # 检查6:份数应该合理(不能是负数,至少应该是1) if copies < 0: return False, "copies_negative" # 检查7:如果份数为0,但参与人数大于0,可能识别错误 if copies == 0 and participants > 0: return False, "copies_zero_but_participants_positive" # 检查8:时间文本应该存在(至少应该有倒计时信息) if not time_text and (countdown_end is None or countdown_end <= 0): return False, "no_time_text_and_no_countdown" # 所有检查通过 return True, "ok"