package org.jeecg.modules.adweb.enquiry.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.adweb.enquiry.dto.param.EffectiveEnquiryParamDto;
import org.jeecg.modules.adweb.enquiry.entity.AdwebSiteBlackIp;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebSiteBlackIpMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebSiteBlackIpService;
import org.springframework.stereotype.Service;

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

/**
 * @Description: adweb_site_black_ip
 * @Author: jeecg-boot
 * @Date:   2023-08-31
 * @Version: V1.0
 */
@Service
public class AdwebSiteBlackIpServiceImpl extends ServiceImpl<AdwebSiteBlackIpMapper, AdwebSiteBlackIp> implements IAdwebSiteBlackIpService {

    @Resource
    private AdwebSiteBlackIpMapper adwebSiteBlackIpMapper;
    @Resource
    private RedisUtil redisUtil;

    private static final String SiteIpTenMinKey = "JUDGE_WASTE_ENQUIRY_SITE_IP_BY_10_MIN::";
    private static final String SiteIpOneDayKey = "JUDGE_WASTE_ENQUIRY_SITE_IP_BY_ONE_DAY::";
    private static final String NotSiteBlackIpWasteEnquiryKey = "NOT_SITE_BLACK_IP_WASTE_ENQUIRY_MAP::";
    private static final String SiteBlackIpKey = "SITE_BLACK_IP_LIST";
    private static final String SiteWhiteIpListKey = "SITE_WHITE_IP_LIST";

    /**
     * 更改为垃圾询盘处理ip黑名单
     * @param effectiveEnquiryParamDto
     */
    @Override
    public void addBlackIpByContent(EffectiveEnquiryParamDto effectiveEnquiryParamDto){
        //排查添加的ip是否已有
        QueryWrapper<AdwebSiteBlackIp> adwebSiteBlackIpQueryWrapper = new QueryWrapper<>();
        adwebSiteBlackIpQueryWrapper.eq("ip", effectiveEnquiryParamDto.getWasteEnquirySeason());
        adwebSiteBlackIpQueryWrapper.eq("site_id", effectiveEnquiryParamDto.getSiteId());
        adwebSiteBlackIpQueryWrapper.eq("site_code", effectiveEnquiryParamDto.getSiteCode());
        adwebSiteBlackIpQueryWrapper.eq("status", 1);
        List<AdwebSiteBlackIp> adwebSiteBlackIpList = list(adwebSiteBlackIpQueryWrapper);
        if(!adwebSiteBlackIpList.isEmpty()){
            //如果有白名单 先删除原数据，后添加新的黑名单数据
            AdwebSiteBlackIp adwebSiteBlackIp = adwebSiteBlackIpList.get(0);
            if(adwebSiteBlackIp.getBlackOrWhite() == 1){
                adwebSiteBlackIp.setStatus(0);
                updateById(adwebSiteBlackIp);

                AdwebSiteBlackIp newSiteBlackIp = new AdwebSiteBlackIp();
                newSiteBlackIp.setIp(effectiveEnquiryParamDto.getWasteEnquirySeason());
                newSiteBlackIp.setBlackOrWhite(0);
                newSiteBlackIp.setSiteId(effectiveEnquiryParamDto.getSiteId());
                newSiteBlackIp.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
                newSiteBlackIp.setStatus(1);
                newSiteBlackIp.setCreateTime(new Date());
                save(newSiteBlackIp);
            }
        }else{
            AdwebSiteBlackIp newSiteBlackIp = new AdwebSiteBlackIp();
            newSiteBlackIp.setIp(effectiveEnquiryParamDto.getWasteEnquirySeason());
            newSiteBlackIp.setBlackOrWhite(0);
            newSiteBlackIp.setSiteId(effectiveEnquiryParamDto.getSiteId());
            newSiteBlackIp.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
            newSiteBlackIp.setStatus(1);
            newSiteBlackIp.setCreateTime(new Date());
            save(newSiteBlackIp);
        }

        //重新调整对应站点redis缓存
        QueryWrapper<AdwebSiteBlackIp> redisWrapper = new QueryWrapper<>();
        redisWrapper.eq("site_id", effectiveEnquiryParamDto.getSiteId());
        redisWrapper.eq("site_code", effectiveEnquiryParamDto.getSiteCode());
        redisWrapper.eq("status", 1);
        List<AdwebSiteBlackIp> redisList = list(redisWrapper);

        if (!redisList.isEmpty()) {
            redisUtil.del(SiteBlackIpKey + "::" + effectiveEnquiryParamDto.getSiteId());
            redisUtil.del(SiteWhiteIpListKey + "::" + effectiveEnquiryParamDto.getSiteId());
        }
        if(CollectionUtils.isNotEmpty(redisList)){
            List<String> blackIpList = redisList.stream().filter(siteBlackIp -> siteBlackIp.getBlackOrWhite().equals(0)).map(AdwebSiteBlackIp::getIp).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(blackIpList)){
                redisUtil.set(SiteBlackIpKey + "::" + effectiveEnquiryParamDto.getSiteId(), blackIpList, 60 * 60 * 24);
            } else{
                redisUtil.del(SiteBlackIpKey + "::" + effectiveEnquiryParamDto.getSiteId());
            }

            List<String> whiteIpList = redisList.stream().filter(siteBlackIp -> siteBlackIp.getBlackOrWhite().equals(1)).map(AdwebSiteBlackIp::getIp).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(whiteIpList)){
                redisUtil.set(SiteWhiteIpListKey + "::" + effectiveEnquiryParamDto.getSiteId(), whiteIpList, 60 * 60 * 24);
            } else{
                redisUtil.del(SiteWhiteIpListKey + "::" + effectiveEnquiryParamDto.getSiteId());
            }
        }

    }

    /**
     * 获取分页列表数据
     * @param page
     * @param adwebSiteBlackIp
     * @param codeList
     * @return
     */
    @Override
    public IPage<AdwebSiteBlackIp> pageList(Page<AdwebSiteBlackIp> page, AdwebSiteBlackIp adwebSiteBlackIp, List<String> codeList, String column, String order){
        return adwebSiteBlackIpMapper.getPageList(page, adwebSiteBlackIp, codeList, column, order);
    }
}
