package com.game.business.service.impl;

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.AppGameBetting;
import com.game.business.dto.FinTranRecordDTO;
import com.game.business.vo.AppGameBettingDetailsCountVO;
import com.game.business.mapper.AppGameBettingMapper;
import com.game.business.service.IAppGameBettingService;
import com.game.business.vo.AppUserGameBettingCountVO;
import com.game.business.vo.AppUserGameBettingDetailsCountVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class AppGameBettingServiceImpl extends ServiceImpl<AppGameBettingMapper, AppGameBetting> implements IAppGameBettingService {

    @Autowired
    private AppGameBettingMapper appGameBettingMapper;

    @Override
    public List<AppGameBetting> getIsWinning(Long classId, Long gameId, String gameDate, Integer isWinning) {

        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        queryWrapper.eq(AppGameBetting::getClassId, classId);
        queryWrapper.eq(AppGameBetting::getGameId, gameId);
        queryWrapper.eq(AppGameBetting::getGameDate, gameDate);
        queryWrapper.eq(AppGameBetting::getIsWinning, isWinning);
        return appGameBettingMapper.selectList(queryWrapper);

    }

    @Override
    public boolean updateById(AppGameBetting appGameBetting) {
        return appGameBettingMapper.updateById(appGameBetting) > 0;
    }

    @Override
    public boolean save(AppGameBetting appGameBetting) {
        return appGameBettingMapper.insert(appGameBetting) > 0;
    }

    @Override
    public List<AppGameBettingDetailsCountVO> getCount(String strDate, Long userId, Integer bettingType) {
        return appGameBettingMapper.getCount(strDate, userId, bettingType);
    }

    @Override
    public List<AppUserGameBettingCountVO> getUserCount(Long gameId, Long userId, Integer bettingType) {
        return appGameBettingMapper.getUserCount(gameId, userId, bettingType);
    }

    @Override
    public List<AppUserGameBettingDetailsCountVO> getUserBettingItemCount(Long gameId, String gameDate, Long userId, Integer bettingType) {
        return appGameBettingMapper.getUserBettingItemCount(gameId, gameDate, userId, bettingType);
    }

    @Override
    public List<AppGameBetting> getUserBettingItem(Long gameId, String gameDate, Long userId, Integer bettingType) {
        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        queryWrapper.eq(AppGameBetting::getGameId, gameId);
        queryWrapper.eq(AppGameBetting::getGameDate, gameDate);
        queryWrapper.eq(AppGameBetting::getUserId, userId);
        queryWrapper.eq(AppGameBetting::getBettingType, bettingType);
        queryWrapper.eq(AppGameBetting::getBettingGameType, 1);
        return appGameBettingMapper.selectList(queryWrapper);
    }

    @Override
    public List<AppGameBetting> selectListByGameId(Long gameId, Long userId, Integer isWinning, String strDate, Integer bettingType) {
        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        queryWrapper.eq(AppGameBetting::getGameId, gameId);
        queryWrapper.eq(AppGameBetting::getUserId, userId);
        if(isWinning != null && isWinning != -1){
            queryWrapper.eq(AppGameBetting::getIsWinning, isWinning);
        }
        if(bettingType != null && bettingType != -1){
            queryWrapper.eq(AppGameBetting::getBettingType, bettingType);
        }
        if(StringUtils.isNotBlank(strDate)){
            queryWrapper.between(AppGameBetting::getCreateTime, strDate + " 00:00:00", strDate + " 23:59:59");
        }
        queryWrapper.orderByDesc(AppGameBetting::getCreateTime);
        return appGameBettingMapper.selectList(queryWrapper);
    }

    @Override
    public List<AppGameBetting> getNotLottery(Integer isWinning) {
        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        queryWrapper.eq(AppGameBetting::getIsWinning, isWinning);
        return appGameBettingMapper.selectList(queryWrapper);
    }

    @Override
    public List<AppGameBetting> selectListByDate(Long userId, String beginTime, String endTime,Long gameId,String gameDate) {
        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        queryWrapper.ne(AppGameBetting::getIsWinning,0);//查询已开奖
        queryWrapper.eq(AppGameBetting::getBettingType,0);
        if(null != userId){
            queryWrapper.eq(AppGameBetting::getUserId,userId);
        }
        if(StringUtils.isNotBlank(beginTime)){
            queryWrapper.ge(AppGameBetting::getUpdateTime,beginTime);
        }
        if(null != gameId){
            queryWrapper.eq(AppGameBetting::getGameId,gameId);
        }
        if(StringUtils.isNotBlank(endTime)){
            queryWrapper.le(AppGameBetting::getUpdateTime,endTime);
        }
        if(StringUtils.isNotBlank(gameDate)){
            queryWrapper.like(AppGameBetting::getGameDate,gameDate);
        }
        return appGameBettingMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, BigDecimal> getBettingAmountSum(Long userId) {
        return appGameBettingMapper.getBettingAmountSum(userId);
    }

    @Override
    public Map<String, BigDecimal> getBettingAmountByDateSum(FinTranRecordDTO finTranRecordDTO) {
        return appGameBettingMapper.getBettingAmountByDateSum(finTranRecordDTO);
    }
}
