package com.ylx.lottery.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.core.domain.model.WxLoginUser;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.SecurityUtils;
import com.ylx.lottery.domain.LotteryCountLog;
import com.ylx.lottery.domain.dto.IsLotteryDTO;
import com.ylx.lottery.domain.vo.IsLotteryVO;
import com.ylx.lottery.domain.vo.LotteryStatVO;
import com.ylx.lottery.mapper.LotteryCountLogMapper;
import com.ylx.lottery.service.LotteryCountLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LotteryCountLogServiceImpl extends ServiceImpl<LotteryCountLogMapper, LotteryCountLog> implements LotteryCountLogService {

    @Override
    public List<LotteryStatVO> selectSumByGroup(LambdaQueryWrapper<LotteryCountLog> queryWrapper) {
        return this.baseMapper.selectSumByGroup(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IsLotteryVO isLottery(IsLotteryDTO dto) {

        IsLotteryVO vo = new IsLotteryVO();

        // 1. 获取当前登录用户
        WxLoginUser loginUser = SecurityUtils.getWxLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            throw new ServiceException("用户未登录或登录已过期");
        }

        String userId = loginUser.getId();
        if (StrUtil.isBlank(userId)) {
            throw new ServiceException("用户ID不合法");
        }

        // 2. 构建查询条件 (用于查找未弹窗的记录)
        LambdaQueryWrapper<LotteryCountLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(LotteryCountLog::getId)
                .eq(LotteryCountLog::getType, dto.getType())
                .eq(LotteryCountLog::getUserId, userId)
                .eq(LotteryCountLog::getIsLottery, 0) // 只查未弹窗的
                .eq(LotteryCountLog::getIsDelete, 0);

        // 3.提取 ID 列表
        List<Long> logIds = this.baseMapper.selectList(queryWrapper).stream()
                .map(LotteryCountLog::getId)
                .collect(Collectors.toList());

        // 4. 设置返回结果
        boolean hasChance = CollUtil.isNotEmpty(logIds);
        vo.setIsLottery(hasChance ? 1 : 0);

        // 5. 如果有记录，批量更新状态为“已弹窗”
        if (CollUtil.isNotEmpty(logIds)) {
            LambdaUpdateWrapper<LotteryCountLog> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(LotteryCountLog::getId, logIds)
                    .set(LotteryCountLog::getIsLottery, 1)
                    .set(LotteryCountLog::getUpdateTime, new Date());

            this.update(updateWrapper);
        }

        return vo;
    }

}
