package com.game.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.business.domain.AppGameLottery;
import com.game.business.mapper.AppGameLotteryMapper;
import com.game.business.service.IAppGameLotteryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

import java.util.List;

@Service
public class AppGameLotteryServiceImpl extends ServiceImpl<AppGameLotteryMapper, AppGameLottery> implements IAppGameLotteryService {

    @Autowired
    private AppGameLotteryMapper appGameLotteryMapper;

    @Override
    public long selectCount(Long classId, Long gameId, String gameDate) {
        LambdaQueryWrapper<AppGameLottery> queryLotteryWrapper = Wrappers.lambdaQuery();;
        queryLotteryWrapper.eq(AppGameLottery::getClassId, classId);
        queryLotteryWrapper.eq(AppGameLottery::getGameId, gameId);
        queryLotteryWrapper.eq(AppGameLottery::getGameDate, gameDate);
        queryLotteryWrapper.eq(AppGameLottery::getIsLottery, 1);
        return appGameLotteryMapper.selectCount(queryLotteryWrapper);
    }

    @Override
    public long selectCountAuto(Long classId, Long gameId, String dateStr) {
        LambdaQueryWrapper<AppGameLottery> queryLotteryWrapper = Wrappers.lambdaQuery();;
        queryLotteryWrapper.eq(AppGameLottery::getClassId, classId);
        queryLotteryWrapper.eq(AppGameLottery::getGameId, gameId);
        queryLotteryWrapper.eq(AppGameLottery::getIsLottery, 1);
        queryLotteryWrapper.between(AppGameLottery::getGameRecordDate, dateStr + " 00:00:00", dateStr + " 23:59:59");
        return appGameLotteryMapper.selectCount(queryLotteryWrapper);
    }

    @Override
    public AppGameLottery selectLottery(Long classId, Long gameId, String gameDate) {
        LambdaQueryWrapper<AppGameLottery> queryLotteryWrapper = Wrappers.lambdaQuery();;
        queryLotteryWrapper.eq(AppGameLottery::getClassId, classId);
        queryLotteryWrapper.eq(AppGameLottery::getGameId, gameId);
        queryLotteryWrapper.eq(AppGameLottery::getGameDate, gameDate);
        return appGameLotteryMapper.selectOne(queryLotteryWrapper);
    }

    @Override
    public boolean save(AppGameLottery appGameLottery) {
        return appGameLotteryMapper.insert(appGameLottery) > 0;
    }

    /**
     * 查询游戏开奖记录
     *
     * @param id 游戏开奖记录主键
     * @return 游戏开奖记录
     */
    @Override
    public AppGameLottery selectAppGameLotteryById(Long id) {
        return appGameLotteryMapper.selectAppGameLotteryById(id);
    }

    /**
     * 查询游戏开奖记录列表
     *
     * @param appGameLottery 游戏开奖记录
     * @return 游戏开奖记录
     */
    @Override
    public List<AppGameLottery> selectAppGameLotteryList(AppGameLottery appGameLottery) {
        return appGameLotteryMapper.selectAppGameLotteryList(appGameLottery);
    }

    /**
     * 新增游戏开奖记录
     *
     * @param appGameLottery 游戏开奖记录
     * @return 结果
     */
    @Override
    public int insertAppGameLottery(AppGameLottery appGameLottery) {
        return appGameLotteryMapper.insertAppGameLottery(appGameLottery);
    }

    /**
     * 修改游戏开奖记录
     *
     * @param appGameLottery 游戏开奖记录
     * @return 结果
     */
    @Override
    public int updateAppGameLottery(AppGameLottery appGameLottery) {
        return appGameLotteryMapper.updateAppGameLottery(appGameLottery);
    }

    /**
     * 批量删除游戏开奖记录
     *
     * @param ids 需要删除的游戏开奖记录主键
     * @return 结果
     */
    @Override
    public int deleteAppGameLotteryByIds(Long[] ids) {
        return appGameLotteryMapper.deleteAppGameLotteryByIds(ids);
    }

    /**
     * 删除游戏开奖记录信息
     *
     * @param id 游戏开奖记录主键
     * @return 结果
     */
    @Override
    public int deleteAppGameLotteryById(Long id) {
        return appGameLotteryMapper.deleteAppGameLotteryById(id);
    }

    @Override
    public void leakLottery() {
        String time = DateUtil.format(DateUtil.offsetMinute(new Date(), -5),"yyyy-MM-dd HH:mm:ss");
        List<AppGameLottery> list = appGameLotteryMapper.selectLeakLottery(time);
        if(null != list && list.size() > 0) {
            list.forEach(e->{
                if(null != e.getGameId() && StringUtils.isNotBlank( e.getGameDate())){
                    LambdaQueryWrapper<AppGameLottery> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(AppGameLottery::getGameId, e.getGameId());
                    queryWrapper.eq(AppGameLottery::getGameDate, e.getGameDate());
                    if(null == appGameLotteryMapper.selectOne(queryWrapper)){
                        AppGameLottery insert = new AppGameLottery();
                        BeanUtils.copyProperties(e,insert);
                        insert.setId(null);
                        insert.setIsLottery(0);//设置为未开奖
                        insertAppGameLottery(insert);
                    }
                }
            });
        }
        List<AppGameLottery> list2 = appGameLotteryMapper.selectLeakLottery2(time);
        if(null != list2 && list2.size() > 0) {
            list2.forEach(e->{
                if(null != e.getGameId() && StringUtils.isNotBlank( e.getGameDate())){
                    LambdaQueryWrapper<AppGameLottery> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(AppGameLottery::getGameId, e.getGameId());
                    queryWrapper.eq(AppGameLottery::getGameDate, e.getGameDate());
                    if(null == appGameLotteryMapper.selectOne(queryWrapper)){
                        AppGameLottery insert = new AppGameLottery();
                        BeanUtils.copyProperties(e,insert);
                        insert.setId(null);
                        insert.setIsLottery(0);//设置为未开奖
                        insertAppGameLottery(insert);
                    }
                }
            });
        }
    }

    @Override
    public AppGameLottery leakInfo(Long id) {
        AppGameLottery reslut = null;
        AppGameLottery appGameLottery = selectAppGameLotteryById(id);
        if(null == appGameLottery){
            return reslut;
        }
        reslut = new AppGameLottery();
        BeanUtils.copyProperties(appGameLottery,reslut);
        AppGameLottery history = appGameLotteryMapper.leakInfo(reslut.getGameId(),reslut.getGameDate());
        if(null != history){
            reslut.setGameLotterySucc(history.getGameLotterySucc());
        }else{
            AppGameLottery history2 = appGameLotteryMapper.leakInfo2(reslut.getGameId(),reslut.getGameDate());
            if(null != history2){
                reslut.setGameLotterySucc(history2.getGameLotterySucc());
            }
        }
        return reslut;
    }
}
