package com.xy.xyaicpzs.jt.jtssq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xy.xyaicpzs.domain.entity.*;
import com.xy.xyaicpzs.domain.vo.SSQFirstStepResultVO;
import com.xy.xyaicpzs.mapper.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 精推双色球第一步算法分析
 */
@Slf4j
@Component
public class FirstBallAnalysis {
    
    @Autowired
    private T3Mapper t3Mapper;
    
    @Autowired
    private T4Mapper t4Mapper;
    
    @Autowired
    private HistoryTopMapper historyTopMapper;
    
    @Autowired
    private HistoryTop100Mapper historyTop100Mapper;
    
    /**
     * 球号和系数的关联类
     */
    @Data
    public static class BallWithCoefficient {
        private Integer ballNumber;
        private Double coefficient;
        private Integer masterBallNumber; // 主球号，用于标识来源

        public BallWithCoefficient(Integer ballNumber, Double coefficient, Integer masterBallNumber) {
            this.ballNumber = ballNumber;
            this.coefficient = coefficient;
            this.masterBallNumber = masterBallNumber;
        }
    }
    
    /**
     * 精推双色球第一步算法主方法
     * @param level 位置级别（H-高位/M-中位/L-低位）
     * @param redBalls 前6个红球号码
     * @param blueBall 蓝球号码
     * @return 分析结果
     */
    public SSQFirstStepResultVO analyze(String level, List<Integer> redBalls, Integer blueBall) {
        // 参数验证
        validateInputParams(level, redBalls, blueBall);
        
        log.info("开始精推双色球第一步分析，策略：{}，红球：{}，蓝球：{}", level, redBalls, blueBall);
        
        // 用于存储所有候选球号和对应的系数
        Map<Integer, List<BallWithCoefficient>> ballCoefficientMap = new HashMap<>();
        List<Integer> allCandidateBalls = new ArrayList<>();
        
        // Step 1: 根据6个红球号码获取候选球
        for (Integer redBall : redBalls) {
            List<BallWithCoefficient> ballsWithCoefficients = getT3BallsByLevel(redBall, level);
            for (BallWithCoefficient ball : ballsWithCoefficients) {
                ballCoefficientMap.computeIfAbsent(ball.getBallNumber(), k -> new ArrayList<>()).add(ball);
                allCandidateBalls.add(ball.getBallNumber());
            }
        }
        
        // Step 2: 从history_top获取前3个球号
        List<Integer> top3HistoryTop = getTop3FromHistoryTop();
        allCandidateBalls.addAll(top3HistoryTop);
        
        // Step 3: 根据蓝球号码获取候选球
        List<BallWithCoefficient> blueBallsWithCoefficients = getT4BallsByLevel(blueBall, level);
        for (BallWithCoefficient ball : blueBallsWithCoefficients) {
            ballCoefficientMap.computeIfAbsent(ball.getBallNumber(), k -> new ArrayList<>()).add(ball);
            allCandidateBalls.add(ball.getBallNumber());
        }
        
        // Step 4: 统计分析并生成结果
        List<SSQFirstStepResultVO.BallAnalysisResult> results = analyzeResults(allCandidateBalls, ballCoefficientMap);
        
        return SSQFirstStepResultVO.builder()
                .results(results)
                .strategy(level)
                .redBalls(redBalls)
                .blueBall(blueBall)
                .build();
    }
    
    /**
     * 根据级别从T3表获取候选球号和系数
     */
    private List<BallWithCoefficient> getT3BallsByLevel(Integer masterBallNumber, String level) {
        List<T3> t3List = t3Mapper.selectList(
            new QueryWrapper<T3>()
                .eq("masterBallNumber", masterBallNumber)
                .orderByDesc("lineCoefficient"));
        
        if (CollectionUtils.isEmpty(t3List)) {
            log.warn("T3表中主球{}没有数据", masterBallNumber);
            return new ArrayList<>();
        }
        
        if ("H".equalsIgnoreCase(level)) {
            return getHighLevelBallsFromT3(t3List, masterBallNumber);
        } else if ("M".equalsIgnoreCase(level)) {
            return getMiddleLevelBallsFromT3(t3List, masterBallNumber);
        } else { // L
            return getLowLevelBallsFromT3(t3List, masterBallNumber);
        }
    }
    
    /**
     * 根据级别从T4表获取候选球号和系数
     */
    private List<BallWithCoefficient> getT4BallsByLevel(Integer masterBallNumber, String level) {
        List<T4> t4List = t4Mapper.selectList(
            new QueryWrapper<T4>()
                .eq("masterBallNumber", masterBallNumber)
                .orderByDesc("lineCoefficient"));
        
        if (CollectionUtils.isEmpty(t4List)) {
            log.warn("T4表中主球{}没有数据", masterBallNumber);
            return new ArrayList<>();
        }
        
        if ("H".equalsIgnoreCase(level)) {
            return getHighLevelBallsFromT4(t4List, masterBallNumber);
        } else if ("M".equalsIgnoreCase(level)) {
            return getMiddleLevelBallsFromT4(t4List, masterBallNumber);
        } else { // L
            return getLowLevelBallsFromT4(t4List, masterBallNumber);
        }
    }
    
    /**
     * 高位策略：从T3表获取系数最大的前17个球号（如果第17个系数相同则一并加入）
     */
    private List<BallWithCoefficient> getHighLevelBallsFromT3(List<T3> t3List, Integer masterBallNumber) {
        List<BallWithCoefficient> result = new ArrayList<>();
        
        if (t3List.size() <= 17) {
            // 如果总数不超过17个，全部加入
            for (T3 t3 : t3List) {
                result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 先加入前17个
        for (int i = 0; i < 17; i++) {
            T3 t3 = t3List.get(i);
            result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
        }
        
        // 获取第17个球号的系数
        Double boundaryCoefficient = t3List.get(16).getLineCoefficient();
        
        // 继续查找后面系数相同的球号
        for (int i = 17; i < t3List.size(); i++) {
            T3 t3 = t3List.get(i);
            if (t3.getLineCoefficient().equals(boundaryCoefficient)) {
                result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
            } else {
                break; // 系数不同，停止查找
            }
        }
        
        return result;
    }
    
    /**
     * 高位策略：从T4表获取系数最大的前17个球号（如果第17个系数相同则一并加入）
     */
    private List<BallWithCoefficient> getHighLevelBallsFromT4(List<T4> t4List, Integer masterBallNumber) {
        List<BallWithCoefficient> result = new ArrayList<>();
        
        if (t4List.size() <= 17) {
            // 如果总数不超过17个，全部加入
            for (T4 t4 : t4List) {
                result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 先加入前17个
        for (int i = 0; i < 17; i++) {
            T4 t4 = t4List.get(i);
            result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
        }
        
        // 获取第17个球号的系数
        Double boundaryCoefficient = t4List.get(16).getLineCoefficient();
        
        // 继续查找后面系数相同的球号
        for (int i = 17; i < t4List.size(); i++) {
            T4 t4 = t4List.get(i);
            if (t4.getLineCoefficient().equals(boundaryCoefficient)) {
                result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
            } else {
                break; // 系数不同，停止查找
            }
        }
        
        return result;
    }
    
    /**
     * 中位策略：从T3表获取平均值附近的17个球号（向上8个，向下8个，边界系数相同则一并加入）
     */
    private List<BallWithCoefficient> getMiddleLevelBallsFromT3(List<T3> t3List, Integer masterBallNumber) {
        if (t3List.size() < 17) {
            log.warn("T3表数据不足17条，实际{}条", t3List.size());
            List<BallWithCoefficient> result = new ArrayList<>();
            for (T3 t3 : t3List) {
                result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 计算平均系数（保留两位小数，截断方式）
        double avgCoefficient = t3List.stream()
                .mapToDouble(T3::getLineCoefficient)
                .average()
                .orElse(0.0);
        // 截断到两位小数
        avgCoefficient = Math.floor(avgCoefficient * 100) / 100;
        
        // 找到第一个比平均值大的位置（从下到上遍历）
        int avgPosition = -1;
        for (int i = t3List.size() - 1; i >= 0; i--) {
            if (t3List.get(i).getLineCoefficient() >= avgCoefficient) {
                avgPosition = i;
                break;
            }
        }
        
        if (avgPosition == -1) {
            avgPosition = 0; // 如果没找到，取第一个
        }
        
        List<BallWithCoefficient> result = new ArrayList<>();
        
        // 先加入平均值位置的球号
        result.add(new BallWithCoefficient(t3List.get(avgPosition).getSlaveBallNumber(), 
                t3List.get(avgPosition).getLineCoefficient(), masterBallNumber));
        
        // 向上取8个球号，处理边界系数相同情况
        int upCount = 0;
        Double upBoundaryCoeff = null;
        for (int i = avgPosition - 1; i >= 0 && upCount < 8; i--) {
            result.add(new BallWithCoefficient(t3List.get(i).getSlaveBallNumber(), 
                    t3List.get(i).getLineCoefficient(), masterBallNumber));
            upCount++;
            if (upCount == 8) {
                upBoundaryCoeff = t3List.get(i).getLineCoefficient();
            }
        }
        
        // 向上边界处理：继续添加与第8个球号系数相同的球号
        if (upBoundaryCoeff != null) {
            for (int i = avgPosition - 9; i >= 0; i--) {
                if (t3List.get(i).getLineCoefficient().equals(upBoundaryCoeff)) {
                    result.add(new BallWithCoefficient(t3List.get(i).getSlaveBallNumber(), 
                            t3List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break;
                }
            }
        }
        
        // 向下取8个球号，处理边界系数相同情况
        int downCount = 0;
        Double downBoundaryCoeff = null;
        for (int i = avgPosition + 1; i < t3List.size() && downCount < 8; i++) {
            result.add(new BallWithCoefficient(t3List.get(i).getSlaveBallNumber(), 
                    t3List.get(i).getLineCoefficient(), masterBallNumber));
            downCount++;
            if (downCount == 8) {
                downBoundaryCoeff = t3List.get(i).getLineCoefficient();
            }
        }
        
        // 向下边界处理：继续添加与第8个球号系数相同的球号
        if (downBoundaryCoeff != null) {
            for (int i = avgPosition + 9; i < t3List.size(); i++) {
                if (t3List.get(i).getLineCoefficient().equals(downBoundaryCoeff)) {
                    result.add(new BallWithCoefficient(t3List.get(i).getSlaveBallNumber(), 
                            t3List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break;
                }
            }
        }
        
        return result;
    }
    
    /**
     * 中位策略：从T4表获取平均值附近的17个球号（向上8个，向下8个，边界系数相同则一并加入）
     */
    private List<BallWithCoefficient> getMiddleLevelBallsFromT4(List<T4> t4List, Integer masterBallNumber) {
        if (t4List.size() < 17) {
            log.warn("T4表数据不足17条，实际{}条", t4List.size());
            List<BallWithCoefficient> result = new ArrayList<>();
            for (T4 t4 : t4List) {
                result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 计算平均系数（保留两位小数，截断方式）
        double avgCoefficient = t4List.stream()
                .mapToDouble(T4::getLineCoefficient)
                .average()
                .orElse(0.0);
        // 截断到两位小数
        avgCoefficient = Math.floor(avgCoefficient * 100) / 100;
        
        // 找到第一个比平均值大的位置（从下到上遍历）
        int avgPosition = -1;
        for (int i = t4List.size() - 1; i >= 0; i--) {
            if (t4List.get(i).getLineCoefficient() >= avgCoefficient) {
                avgPosition = i;
                break;
            }
        }
        
        if (avgPosition == -1) {
            avgPosition = 0; // 如果没找到，取第一个
        }
        
        List<BallWithCoefficient> result = new ArrayList<>();
        
        // 先加入平均值位置的球号
        result.add(new BallWithCoefficient(t4List.get(avgPosition).getSlaveBallNumber(), 
                t4List.get(avgPosition).getLineCoefficient(), masterBallNumber));
        
        // 向上取8个球号，处理边界系数相同情况
        int upCount = 0;
        Double upBoundaryCoeff = null;
        for (int i = avgPosition - 1; i >= 0 && upCount < 8; i--) {
            result.add(new BallWithCoefficient(t4List.get(i).getSlaveBallNumber(), 
                    t4List.get(i).getLineCoefficient(), masterBallNumber));
            upCount++;
            if (upCount == 8) {
                upBoundaryCoeff = t4List.get(i).getLineCoefficient();
            }
        }
        
        // 向上边界处理：继续添加与第8个球号系数相同的球号
        if (upBoundaryCoeff != null) {
            for (int i = avgPosition - 9; i >= 0; i--) {
                if (t4List.get(i).getLineCoefficient().equals(upBoundaryCoeff)) {
                    result.add(new BallWithCoefficient(t4List.get(i).getSlaveBallNumber(), 
                            t4List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break;
                }
            }
        }
        
        // 向下取8个球号，处理边界系数相同情况
        int downCount = 0;
        Double downBoundaryCoeff = null;
        for (int i = avgPosition + 1; i < t4List.size() && downCount < 8; i++) {
            result.add(new BallWithCoefficient(t4List.get(i).getSlaveBallNumber(), 
                    t4List.get(i).getLineCoefficient(), masterBallNumber));
            downCount++;
            if (downCount == 8) {
                downBoundaryCoeff = t4List.get(i).getLineCoefficient();
            }
        }
        
        // 向下边界处理：继续添加与第8个球号系数相同的球号
        if (downBoundaryCoeff != null) {
            for (int i = avgPosition + 9; i < t4List.size(); i++) {
                if (t4List.get(i).getLineCoefficient().equals(downBoundaryCoeff)) {
                    result.add(new BallWithCoefficient(t4List.get(i).getSlaveBallNumber(), 
                            t4List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break;
                }
            }
        }
        
        return result;
    }
    
    /**
     * 低位策略：从T3表获取最小值向上17个球号（含最小值，第1个系数相同则一并加入）
     */
    private List<BallWithCoefficient> getLowLevelBallsFromT3(List<T3> t3List, Integer masterBallNumber) {
        if (t3List.size() < 17) {
            log.warn("T3表数据不足17条，实际{}条", t3List.size());
            List<BallWithCoefficient> result = new ArrayList<>();
            for (T3 t3 : t3List) {
                result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 从最小值开始向上取17个（t3List已按系数降序排列，最后17个就是最小的）
        List<BallWithCoefficient> result = new ArrayList<>();
        int startIndex = Math.max(0, t3List.size() - 17);
        
        // 先加入基本的17个球号
        for (int i = startIndex; i < t3List.size(); i++) {
            T3 t3 = t3List.get(i);
            result.add(new BallWithCoefficient(t3.getSlaveBallNumber(), t3.getLineCoefficient(), masterBallNumber));
        }
        
        // 处理第1个球号（系数最大的）边界相同情况
        if (startIndex > 0) {
            Double firstBallCoeff = t3List.get(startIndex).getLineCoefficient();
            
            // 继续向前查找系数相同的球号
            for (int i = startIndex - 1; i >= 0; i--) {
                if (t3List.get(i).getLineCoefficient().equals(firstBallCoeff)) {
                    result.add(new BallWithCoefficient(t3List.get(i).getSlaveBallNumber(), 
                            t3List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break; // 系数不同，停止查找
                }
            }
        }
        
        return result;
    }
    
    /**
     * 低位策略：从T4表获取最小值向上17个球号（含最小值，第1个系数相同则一并加入）
     */
    private List<BallWithCoefficient> getLowLevelBallsFromT4(List<T4> t4List, Integer masterBallNumber) {
        if (t4List.size() < 17) {
            log.warn("T4表数据不足17条，实际{}条", t4List.size());
            List<BallWithCoefficient> result = new ArrayList<>();
            for (T4 t4 : t4List) {
                result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
            }
            return result;
        }
        
        // 从最小值开始向上取17个（t4List已按系数降序排列，最后17个就是最小的）
        List<BallWithCoefficient> result = new ArrayList<>();
        int startIndex = Math.max(0, t4List.size() - 17);
        
        // 先加入基本的17个球号
        for (int i = startIndex; i < t4List.size(); i++) {
            T4 t4 = t4List.get(i);
            result.add(new BallWithCoefficient(t4.getSlaveBallNumber(), t4.getLineCoefficient(), masterBallNumber));
        }
        
        // 处理第1个球号（系数最大的）边界相同情况
        if (startIndex > 0) {
            Double firstBallCoeff = t4List.get(startIndex).getLineCoefficient();
            
            // 继续向前查找系数相同的球号
            for (int i = startIndex - 1; i >= 0; i--) {
                if (t4List.get(i).getLineCoefficient().equals(firstBallCoeff)) {
                    result.add(new BallWithCoefficient(t4List.get(i).getSlaveBallNumber(), 
                            t4List.get(i).getLineCoefficient(), masterBallNumber));
                } else {
                    break; // 系数不同，停止查找
                }
            }
        }
        
        return result;
    }
    
    /**
     * 从历史排行表获取前3个球号（按点系数排行）
     */
    private List<Integer> getTop3FromHistoryTop() {
        List<HistoryTop> historyTopList = historyTopMapper.selectList(
            new QueryWrapper<HistoryTop>()
                .orderByDesc("pointCoefficient")
                .last("LIMIT 3"));
        
        if (CollectionUtils.isEmpty(historyTopList)) {
            log.warn("历史排行表数据不足3条，实际{}条", historyTopList.size());
            return new ArrayList<>();
        }
        
        return historyTopList.stream()
                .map(HistoryTop::getBallNumber)
                .collect(Collectors.toList());
    }
    
    /**
     * 统计分析并生成结果
     */
    private List<SSQFirstStepResultVO.BallAnalysisResult> analyzeResults(
            List<Integer> allCandidateBalls, 
            Map<Integer, List<BallWithCoefficient>> ballCoefficientMap) {
        
        // 统计球号出现次数
        Map<Integer, Integer> ballFrequencyMap = new HashMap<>();
        for (Integer ball : allCandidateBalls) {
            ballFrequencyMap.put(ball, ballFrequencyMap.getOrDefault(ball, 0) + 1);
        }
        
        // 计算系数和
        Map<Integer, Double> ballCoefficientSumMap = new HashMap<>();
        for (Map.Entry<Integer, List<BallWithCoefficient>> entry : ballCoefficientMap.entrySet()) {
            Integer ballNumber = entry.getKey();
            List<BallWithCoefficient> coefficients = entry.getValue();
            double sum = coefficients.stream()
                    .mapToDouble(BallWithCoefficient::getCoefficient)
                    .sum();
            ballCoefficientSumMap.put(ballNumber, sum);
        }
        
        // 获取所有有排行数据的球号（包括频次为0的球号）
        Set<Integer> allBallsWithRanking = getAllBallsWithRanking();
        
        // 获取百期排位和历史排位
        Map<Integer, Integer> top100RankingMap = getTop100Rankings(allBallsWithRanking);
        Map<Integer, Integer> historyRankingMap = getHistoryRankings(allBallsWithRanking);
        
        // 组装结果
        List<SSQFirstStepResultVO.BallAnalysisResult> results = new ArrayList<>();
        for (Integer ballNumber : allBallsWithRanking) {
            SSQFirstStepResultVO.BallAnalysisResult result = SSQFirstStepResultVO.BallAnalysisResult.builder()
                    .ballNumber(ballNumber)
                    .frequency(ballFrequencyMap.getOrDefault(ballNumber, 0))
                    .coefficientSum(ballCoefficientSumMap.getOrDefault(ballNumber, 0.0))
                    .top100Ranking(top100RankingMap.get(ballNumber))
                    .historyRanking(historyRankingMap.get(ballNumber))
                    .build();
            results.add(result);
        }
        
        // 按出现次数降序排列，次数相同的按球号升序排列
        results.sort((a, b) -> {
            int frequencyCompare = b.getFrequency().compareTo(a.getFrequency());
            if (frequencyCompare != 0) {
                return frequencyCompare;
            }
            return a.getBallNumber().compareTo(b.getBallNumber());
        });
        
        return results;
    }
    
    /**
     * 获取所有有排行数据的球号（包括频次为0的球号）
     */
    private Set<Integer> getAllBallsWithRanking() {
        Set<Integer> allBalls = new HashSet<>();
        
        // 从百期排行表获取所有球号
        List<HistoryTop100> top100List = historyTop100Mapper.selectList(
            new QueryWrapper<HistoryTop100>());
        for (HistoryTop100 item : top100List) {
            allBalls.add(item.getBallNumber());
        }
        
        // 从历史排行表获取所有球号
        List<HistoryTop> historyTopList = historyTopMapper.selectList(
            new QueryWrapper<HistoryTop>());
        for (HistoryTop item : historyTopList) {
            allBalls.add(item.getBallNumber());
        }
        
        return allBalls;
    }
    
    /**
     * 获取球号在百期排行表中的排位
     */
    private Map<Integer, Integer> getTop100Rankings(Set<Integer> ballNumbers) {
        Map<Integer, Integer> result = new HashMap<>();
        
        // 直接查询指定球号的排行数据，使用表中的no字段作为排名
        List<HistoryTop100> top100List = historyTop100Mapper.selectList(
            new QueryWrapper<HistoryTop100>()
                .in("ballNumber", ballNumbers));
        
        // 使用表中的no字段作为排名
        for (HistoryTop100 item : top100List) {
            result.put(item.getBallNumber(), item.getNo());
        }
        
        // 对于没有在百期排行表中的球号，设置为null
        for (Integer ballNumber : ballNumbers) {
            if (!result.containsKey(ballNumber)) {
                result.put(ballNumber, null);
            }
        }
        
        return result;
    }
    
    /**
     * 获取球号在历史排行表中的排位
     */
    private Map<Integer, Integer> getHistoryRankings(Set<Integer> ballNumbers) {
        Map<Integer, Integer> result = new HashMap<>();
        
        // 直接查询指定球号的排行数据，使用表中的no字段作为排名
        List<HistoryTop> historyTopList = historyTopMapper.selectList(
            new QueryWrapper<HistoryTop>()
                .in("ballNumber", ballNumbers));
        
        // 使用表中的no字段作为排名
        for (HistoryTop item : historyTopList) {
            result.put(item.getBallNumber(), item.getNo());
        }
        
        // 对于没有在历史排行表中的球号，设置为null
        for (Integer ballNumber : ballNumbers) {
            if (!result.containsKey(ballNumber)) {
                result.put(ballNumber, null);
            }
        }
        
        return result;
    }
    
    /**
     * 参数验证
     */
    private void validateInputParams(String level, List<Integer> redBalls, Integer blueBall) {
        if (!"H".equalsIgnoreCase(level) && !"M".equalsIgnoreCase(level) && !"L".equalsIgnoreCase(level)) {
            throw new IllegalArgumentException("位置级别必须是H/M/L之一");
        }
        
        if (CollectionUtils.isEmpty(redBalls) || redBalls.size() != 6) {
            throw new IllegalArgumentException("红球号码必须为6个");
        }
        
        if (blueBall == null || blueBall < 1 || blueBall > 16) {
            throw new IllegalArgumentException("蓝球号码范围应为1-16");
        }
        
        for (Integer redBall : redBalls) {
            if (redBall < 1 || redBall > 33) {
                throw new IllegalArgumentException("红球号码范围应为1-33");
            }
        }
    }
}
