package com.zanxiang.game.module.sdk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.base.pojo.enums.BanStatusEnum;
import com.zanxiang.game.module.base.pojo.enums.HttpStatusEnum;
import com.zanxiang.game.module.mybatis.entity.IpBan;
import com.zanxiang.game.module.mybatis.mapper.IpBanMapper;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.service.IIpBanService;
import com.zanxiang.game.module.sdk.service.IUserBanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

/**
 * @author : lingfeng
 * @time : 2022-06-28
 * @description : ip封禁管理
 */
@Slf4j
@Service
public class IpBanServiceImpl extends ServiceImpl<IpBanMapper, IpBan> implements IIpBanService {

    @Autowired
    private IUserBanService userBanService;

    @Override
    public Tuple2<Boolean, HttpStatusEnum> banCheck(UserData userData) {
        //IP检测
        if (super.count(new LambdaQueryWrapper<IpBan>()
                .eq(IpBan::getGameId, userData.getGameId())
                .eq(IpBan::getIp, userData.getIp())
                .eq(IpBan::getStatus, BanStatusEnum.BAN_STATUS.getStatus())
        ) > 0) {
            return Tuples.of(Boolean.TRUE, HttpStatusEnum.IP_HALT);
        }
        //设备检测
        return userBanService.banCheckByDevice(userData);
    }
}
