package com.xy.xyaicpzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.xyaicpzs.domain.entity.PredictRecord;
import com.xy.xyaicpzs.mapper.PredictRecordMapper;
import com.xy.xyaicpzs.service.PredictRecordService;
import com.xy.xyaicpzs.common.response.PageResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author XY003
* @description 针对表【predict_record(彩票开奖信息表)】的数据库操作Service实现
* @createDate 2025-06-16 13:17:53
*/
@Service
public class PredictRecordServiceImpl extends ServiceImpl<PredictRecordMapper, PredictRecord>
    implements PredictRecordService{

    @Override
    public PredictRecord createPredictRecord(Long userId, Long drawId, Date drawDate, List<Integer> redBalls, Integer blueBall) {
        PredictRecord predictRecord = new PredictRecord();
        predictRecord.setUserId(userId);
        predictRecord.setDrawId(drawId);
        predictRecord.setDrawDate(drawDate);
        predictRecord.setPredictTime(new Date());
        
        // 设置红球号码
        if (redBalls != null && redBalls.size() >= 6) {
            predictRecord.setRedBall1(redBalls.get(0));
            predictRecord.setRedBall2(redBalls.get(1));
            predictRecord.setRedBall3(redBalls.get(2));
            predictRecord.setRedBall4(redBalls.get(3));
            predictRecord.setRedBall5(redBalls.get(4));
            predictRecord.setRedBall6(redBalls.get(5));
        }
        
        // 设置蓝球号码
        if (blueBall != null) {
            predictRecord.setBlueBall(blueBall);
        }
        
        // 默认状态为待开奖
        predictRecord.setPredictStatus("待开奖");
        predictRecord.setType("ssq");
        
        save(predictRecord);
        return predictRecord;
    }

    @Override
    public List<PredictRecord> getPredictRecordsByUserId(Long userId) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).orderByDesc("predictTime");
        return list(queryWrapper);
    }

    @Override
    public List<PredictRecord> getPredictRecordsByUserIdWithPaging(Long userId, Integer page, Integer size) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).orderByDesc("predictTime");
        
        // 计算偏移量
        int offset = (page - 1) * size;
        queryWrapper.last("LIMIT " + offset + ", " + size);
        
        return list(queryWrapper);
    }

    @Override
    public Long getPredictRecordsCountByUserId(Long userId) {
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return count(queryWrapper);
    }

    @Override
    public PageResponse<PredictRecord> getAllRecordsForAdmin(Long userId, String predictResult, Integer current, Integer pageSize) {
        // 参数校验
        if (current < 1) {
            current = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (pageSize > 100) {
            pageSize = 100; // 限制最大每页100条
        }
        
        // 构建查询条件
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        
        // 如果提供了用户ID，添加筛选条件
        if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        
        // 如果提供了中奖等级，添加筛选条件
        if (StringUtils.isNotBlank(predictResult)) {
            queryWrapper.eq("predictResult", predictResult);
        }
        
        // 按预测时间降序排序
        queryWrapper.orderByDesc("predictTime");
        
        // 执行分页查询
        Page<PredictRecord> page = new Page<>(current, pageSize);
        Page<PredictRecord> resultPage = page(page, queryWrapper);
        
        // 构建分页响应对象
        return PageResponse.of(
                resultPage.getRecords(), 
                resultPage.getTotal(), 
                (int) resultPage.getCurrent(), 
                (int) resultPage.getSize()
        );
    }

    @Override
    public PageResponse<PredictRecord> getWinningRecordsForAdmin(Long userId, String prizeGrade, Integer current, Integer pageSize) {
        // 参数校验
        if (current < 1) {
            current = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (pageSize > 100) {
            pageSize = 100;
        }
        
        // 构建查询条件
        QueryWrapper<PredictRecord> queryWrapper = new QueryWrapper<>();
        
        // 排除待开奖和未中奖的记录
        queryWrapper.ne("predictStatus", "待开奖");
        queryWrapper.isNotNull("predictResult");
        
        // 如果提供了用户ID
        if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        
        // 如果提供了具体奖项等级
        if (StringUtils.isNotBlank(prizeGrade)) {
            queryWrapper.eq("predictResult", prizeGrade);
        }
        
        // 按预测时间降序排序
        queryWrapper.orderByDesc("predictTime");
        
        // 执行分页查询
        Page<PredictRecord> page = new Page<>(current, pageSize);
        Page<PredictRecord> resultPage = page(page, queryWrapper);
        
        // 构建分页响应对象
        return PageResponse.of(
                resultPage.getRecords(), 
                resultPage.getTotal(), 
                (int) resultPage.getCurrent(), 
                (int) resultPage.getSize()
        );
    }
}