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.DltPredictRecord;
import com.xy.xyaicpzs.service.DltPredictRecordService;
import com.xy.xyaicpzs.mapper.DltPredictRecordMapper;
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 针对表【dlt_predict_record(大乐透推测记录表)】的数据库操作Service实现
* @createDate 2025-09-08 14:01:12
*/
@Service
public class DltPredictRecordServiceImpl extends ServiceImpl<DltPredictRecordMapper, DltPredictRecord>
    implements DltPredictRecordService{

    @Override
    public DltPredictRecord createDltPredictRecord(Long userId, Long drawId, Date drawDate, List<Integer> frontBalls, List<Integer> backBalls) {
        DltPredictRecord predictRecord = new DltPredictRecord();
        predictRecord.setUserId(userId);
        predictRecord.setDrawId(drawId);
        predictRecord.setDrawDate(drawDate);
        predictRecord.setPredictTime(new Date());
        
        // 设置前区球号码（5个）
        if (frontBalls != null && frontBalls.size() >= 5) {
            predictRecord.setFrontendBall1(frontBalls.get(0));
            predictRecord.setFrontendBall2(frontBalls.get(1));
            predictRecord.setFrontendBall3(frontBalls.get(2));
            predictRecord.setFrontendBall4(frontBalls.get(3));
            predictRecord.setFrontendBall5(frontBalls.get(4));
        }
        
        // 设置后区球号码（2个）
        if (backBalls != null && backBalls.size() >= 2) {
            predictRecord.setBackendBall1(backBalls.get(0));
            predictRecord.setBackendBall2(backBalls.get(1));
        }
        
        // 默认状态为待开奖
        predictRecord.setPredictStatus("待开奖");
        
        save(predictRecord);
        return predictRecord;
    }

    @Override
    public List<DltPredictRecord> getDltPredictRecordsByUserIdWithPaging(Long userId, Integer page, Integer size) {
        QueryWrapper<DltPredictRecord> 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 getDltPredictRecordsCountByUserId(Long userId) {
        QueryWrapper<DltPredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return count(queryWrapper);
    }

    @Override
    public PageResponse<DltPredictRecord> 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<DltPredictRecord> queryWrapper = new QueryWrapper<>();
        
        // 如果提供了用户ID，添加筛选条件
        if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        
        // 如果提供了中奖等级，添加筛选条件
        if (StringUtils.isNotBlank(predictResult)) {
            queryWrapper.eq("predictResult", predictResult);
        }
        
        // 按预测时间降序排序
        queryWrapper.orderByDesc("predictTime");
        
        // 执行分页查询
        Page<DltPredictRecord> page = new Page<>(current, pageSize);
        Page<DltPredictRecord> resultPage = page(page, queryWrapper);
        
        // 构建分页响应对象
        return PageResponse.of(
                resultPage.getRecords(), 
                resultPage.getTotal(), 
                (int) resultPage.getCurrent(), 
                (int) resultPage.getSize()
        );
    }

    @Override
    public PageResponse<DltPredictRecord> 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<DltPredictRecord> queryWrapper = new QueryWrapper<>();
        
        // 排除待开奖和未中奖的记录，只保留中奖记录
        queryWrapper.ne("predictStatus", "待开奖");
        // 注意：数据库中未中奖可能存为"未中奖"或空，视具体逻辑而定，这里假设是"未中奖"
        // 同时为了保险，也可以检查bonus > 0，但有些小奖可能bonus未设置？通常会有bonus。
        // 这里使用predictResult不为"未中奖"且不为空
        queryWrapper.isNotNull("predictResult");
        
        // 如果提供了用户ID
        if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        
        // 如果提供了具体奖项等级
        if (StringUtils.isNotBlank(prizeGrade)) {
            queryWrapper.eq("predictResult", prizeGrade);
        }
        
        // 按预测时间降序排序
        queryWrapper.orderByDesc("predictTime");
        
        // 执行分页查询
        Page<DltPredictRecord> page = new Page<>(current, pageSize);
        Page<DltPredictRecord> resultPage = page(page, queryWrapper);
        
        // 构建分页响应对象
        return PageResponse.of(
                resultPage.getRecords(), 
                resultPage.getTotal(), 
                (int) resultPage.getCurrent(), 
                (int) resultPage.getSize()
        );
    }

}
