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 com.game.business.vo.AppUserGameBettingWebCountVO;
import com.game.common.utils.DateUtils;
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.Date;
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<AppUserGameBettingWebCountVO> getUserWebCount(Long gameId, Long userId, Integer bettingType,String beginTime,String endTime) {
        return appGameBettingMapper.getUserWebCount(gameId, userId, bettingType,beginTime,endTime);
    }

    @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,Integer isWinner,Integer bettingType){
        LambdaQueryWrapper<AppGameBetting> queryWrapper = Wrappers.lambdaQuery();;
        if(null != isWinner){
            queryWrapper.eq(AppGameBetting::getIsWinning, isWinner);
        }
        if(null != bettingType) {
            queryWrapper.eq(AppGameBetting::getBettingType, bettingType);
        }
        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 List<AppGameBetting> selectListIsWinByDate(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, Double> getBettingAmountByDate(Long gameId, String dateStr) {
        return appGameBettingMapper.getBettingAmountByDate(gameId, dateStr);
    }

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

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

    @Override
    public Integer getBettingCount(Long userId, Long gameId, String gameDate, String bettingItem) {
        return appGameBettingMapper.getBettingCount(userId, gameId, gameDate, bettingItem);
    }

    @Override
    public Integer getBettingItemCount(Long userId, Long gameId, String gameDate, String bettingItem) {
        return appGameBettingMapper.getBettingItemCount(userId, gameId, gameDate, bettingItem);
    }

    @Override
    public BigDecimal getBettingAmount(Long userId, Long gameId, String gameDate, String bettingItem) {
        return appGameBettingMapper.getBettingAmount(userId, gameId, gameDate, bettingItem);
    }

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

    /**
     * 查询用户下注记录
     *
     * @param id 用户下注记录主键
     * @return 用户下注记录
     */
    @Override
    public AppGameBetting selectAppGameBettingById(Long id) {
        return appGameBettingMapper.selectAppGameBettingById(id);
    }

    /**
     * 查询用户下注记录列表
     *
     * @param appGameBetting 用户下注记录
     * @return 用户下注记录
     */
    @Override
    public List<AppGameBetting> selectAppGameBettingList(AppGameBetting appGameBetting) {
        return appGameBettingMapper.selectAppGameBettingList(appGameBetting);
    }

    /**
     * 新增用户下注记录
     *
     * @param appGameBetting 用户下注记录
     * @return 结果
     */
    @Override
    public int insertAppGameBetting(AppGameBetting appGameBetting) {
        appGameBetting.setCreateTime(DateUtils.getNowDate());
        return appGameBettingMapper.insertAppGameBetting(appGameBetting);
    }

    /**
     * 修改用户下注记录
     *
     * @param appGameBetting 用户下注记录
     * @return 结果
     */
    @Override
    public int updateAppGameBetting(AppGameBetting appGameBetting) {
        appGameBetting.setUpdateTime(DateUtils.getNowDate());
        return appGameBettingMapper.updateAppGameBetting(appGameBetting);
    }

    /**
     * 批量删除用户下注记录
     *
     * @param ids 需要删除的用户下注记录主键
     * @return 结果
     */
    @Override
    public int deleteAppGameBettingByIds(Long[] ids) {
        return appGameBettingMapper.deleteAppGameBettingByIds(ids);
    }

    /**
     * 删除用户下注记录信息
     *
     * @param id 用户下注记录主键
     * @return 结果
     */
    @Override
    public int deleteAppGameBettingById(Long id) {
        return appGameBettingMapper.deleteAppGameBettingById(id);
    }

    @Override
    public Double sumBettingAmount(Long userId,Integer bettingType,String beginTime,String endTime,Long gameId) {
        return appGameBettingMapper.sumBettingAmount(userId,bettingType,beginTime,endTime,gameId);
    }

    @Override
    public int selectBettingCount(Long userId, Date beginTime, Date endTime) {
        //查询时间段内投注且时间断外已开奖数量
        LambdaQueryWrapper<AppGameBetting> queryKjWrapper = new LambdaQueryWrapper<>();
        queryKjWrapper.eq(AppGameBetting::getUserId,userId);
        queryKjWrapper.ne(AppGameBetting::getIsWinning,0);
        queryKjWrapper.between(AppGameBetting::getCreateTime,beginTime,endTime);
        queryKjWrapper.gt(AppGameBetting::getUpdateTime,endTime);//开奖时间大于结束时间
        Long count = appGameBettingMapper.selectCount(queryKjWrapper);
        if(null == count){
            count = 0L;
        }
        //查询时间段内投注且时间断外未开奖数量
        LambdaQueryWrapper<AppGameBetting> queryWKjWrapper = new LambdaQueryWrapper<>();
        queryWKjWrapper.eq(AppGameBetting::getUserId,userId);
        queryWKjWrapper.eq(AppGameBetting::getIsWinning,0);
        queryWKjWrapper.between(AppGameBetting::getCreateTime,beginTime,endTime);
        Long wkjCount = appGameBettingMapper.selectCount(queryWKjWrapper);
        if(null != wkjCount){
            count += wkjCount;
        }
        return count.intValue();
    }


}
