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

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 lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.adweb.enquiry.entity.AdwebPublicBlackEmail;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebPublicBlackEmailMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebPublicBlackEmailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * @Description: 询盘公共邮箱黑名单
 * @Author: jeecg-boot
 * @Date: 2024-10-18
 * @Version: V1.0
 */
@Slf4j
@Service
public class AdwebPublicBlackEmailServiceImpl extends ServiceImpl<AdwebPublicBlackEmailMapper, AdwebPublicBlackEmail> implements IAdwebPublicBlackEmailService {

//    @Resource
//    private IAdwebEnquiryService adwebEnquiryService;
//    @Resource
//    private IAdwebPublicBlackIpService adwebPublicBlackIpService;
//    @Resource
//    private RedisUtil redisUtil;

    @Value("${judge_waste_enquiry.ip.delOrdIpDate}")
    private Integer delOrdIpDate;
    @Value("${judge_waste_enquiry.email.tenMinNum}")
    private Integer emailTenMinNum;
    @Value("${judge_waste_enquiry.email.oneDayNum}")
    private Integer emailOneDayNum;
    @Value("${judge_waste_enquiry.ip.tenMinNum}")
    private Integer ipTenMinNum;
    @Value("${judge_waste_enquiry.ip.oneDayNum}")
    private Integer ipOneDayNum;
    private static final String BlackEmailKey = "BLACK_EMAIL_LIST";
    private static final String WhiteEmailListKey = "WHITE_EMAIL_LIST";
    private static final String WhiteIpListKey = "WHITE_IP_LIST";
    private static final String BlackIpKey = "BLACK_IP_LIST";

    @Resource
    AdwebPublicBlackEmailMapper adwebPublicBlackEmailMapper;

    @Override
    public IPage<AdwebPublicBlackEmail> pageList(Page<AdwebPublicBlackEmail> page, String email, Integer blackOrWhite,String column,String order) {
        return adwebPublicBlackEmailMapper.pageList(page,email,blackOrWhite,column,order);
    }

    //定时器执行
//    @Override
//    public void updateBlackListByEmailAndIp() {
//
//        List<AdwebPublicBlackEmail> emailList = this.list(new QueryWrapper<AdwebPublicBlackEmail>().eq("status", 1));
//
//        log.info("获取到的email黑名单数量为：{}", emailList.size());
//
//        List<String> blackEmailList = emailList.stream().filter(e -> e.getBlackOrWhite() == 0).map(AdwebPublicBlackEmail::getEmail).collect(Collectors.toList());
//
//        List<String> whiteEmailList = emailList.stream().filter(e -> e.getBlackOrWhite() == 1).map(AdwebPublicBlackEmail::getEmail).collect(Collectors.toList());
//
//        redisUtil.set(BlackEmailKey, blackEmailList, 60 * 60 * 24);
//        redisUtil.set(WhiteEmailListKey, whiteEmailList, 60 * 60 * 24);
//
//        log.info("更新email黑名单成功,黑名单数量为：{},白名单数量为{}", blackEmailList.size(), whiteEmailList.size());
//
//        //90天内的ip
//        List<AdwebPublicBlackIp> ipList = adwebPublicBlackIpService.list(new QueryWrapper<AdwebPublicBlackIp>().eq("status", 1).gt("create_time", DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -delOrdIpDate)));
//
//        log.info("获取到的ip黑名单数量为：{}", ipList.size());
//
//        List<String> blackIpList = ipList.stream().filter(e -> e.getBlackOrWhite() == 0).map(AdwebPublicBlackIp::getIp).collect(Collectors.toList());
//
//        List<String> whiteIpList = ipList.stream().filter(e -> e.getBlackOrWhite() == 1).map(AdwebPublicBlackIp::getIp).collect(Collectors.toList());
//
//        redisUtil.set(BlackIpKey, blackIpList, 60 * 60 * 24);
//        redisUtil.set(WhiteIpListKey, whiteIpList, 60 * 60 * 24);
//
//        log.info("更新ip黑名单成功,黑名单数量为：{},白名单数量为{}", blackIpList.size(), whiteIpList.size());
//
//        List<AdwebPublicBlackIp> ordBlackIpList = adwebPublicBlackIpService.list(new QueryWrapper<AdwebPublicBlackIp>().eq("black_or_white", 0).eq("status", 1).lt("create_time", DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -delOrdIpDate)));
//
//        if (ordBlackIpList.size() == 0) {
//            log.info("没有需要删除的ip黑名单");
//            return;
//        }
//
//        List<AdwebEnquiry> list = adwebEnquiryService.list(new QueryWrapper<AdwebEnquiry>().eq("status", 1).eq("user_effective", 0).in("from_ip", ordBlackIpList.stream().map(AdwebPublicBlackIp::getIp).collect(Collectors.toList())).select("DISTINCT id,from_ip"));
//
//        //删除90天前并且期间没有发过询盘
//        if (list.size() > 0) {
//            adwebPublicBlackIpService.update(new UpdateWrapper<AdwebPublicBlackIp>().eq("black_or_white", 0).eq("status", 1).lt("create_time", DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -delOrdIpDate)).notIn("ip", list.stream().map(AdwebEnquiry::getFromIp).collect(Collectors.toList())).set("status", 0));
//            return;
//        }
//        adwebPublicBlackIpService.update(new UpdateWrapper<AdwebPublicBlackIp>().eq("black_or_white", 0).eq("status", 1).lt("create_time", DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -delOrdIpDate)).set("status", 0).in("ip", ordBlackIpList.stream().map(AdwebPublicBlackIp::getIp).collect(Collectors.toList())));
//
//        log.info("删除90天前的ip黑名单成功");
//    }

    /**
    * 定时器执行
    */
//    @Override
//    public void updateEnquiryBlackList() {
//        List<AdwebEnquiry> enquiryListByTenMin = adwebEnquiryService.list(new QueryWrapper<AdwebEnquiry>().eq("user_effective", 0).eq("status", 1).select("DISTINCT email").groupBy("email,UNIX_TIMESTAMP( record_ctime ) DIV 600 ").having("count(id) > "+emailTenMinNum));
//
//        List<AdwebEnquiry> enquiryListByOneDay = adwebEnquiryService.list(new QueryWrapper<AdwebEnquiry>().eq("user_effective", 0).eq("status", 1).select("DISTINCT email").groupBy("email,UNIX_TIMESTAMP( record_ctime ) DIV 3600 ").having("count(id) > "+emailOneDayNum));
//
//        List<AdwebEnquiry> enquiryListByIpTenMin = adwebEnquiryService.list(new QueryWrapper<AdwebEnquiry>().eq("user_effective", 0).eq("status", 1).select("DISTINCT from_ip").groupBy("from_ip,UNIX_TIMESTAMP( record_ctime ) DIV 600 ").having("count(id) > "+ipTenMinNum));
//
//        List<AdwebEnquiry> enquiryListByIpOneDay = adwebEnquiryService.list(new QueryWrapper<AdwebEnquiry>().eq("user_effective", 0).eq("status", 1).select("DISTINCT from_ip").groupBy("from_ip,UNIX_TIMESTAMP( record_ctime ) DIV 3600 ").having("count(id) > "+ipOneDayNum));
//
//        Set<String> emailSet = new HashSet<>();
//        emailSet.addAll(enquiryListByTenMin.stream().map(AdwebEnquiry::getEmail).collect(Collectors.toList()));
//        emailSet.addAll(enquiryListByOneDay.stream().map(AdwebEnquiry::getEmail).collect(Collectors.toList()));
//
//        Set<String> ipSet = new HashSet<>();
//        ipSet.addAll(enquiryListByIpTenMin.stream().map(AdwebEnquiry::getFromIp).collect(Collectors.toList()));
//        ipSet.addAll(enquiryListByIpOneDay.stream().map(AdwebEnquiry::getFromIp).collect(Collectors.toList()));
//
//        List<AdwebPublicBlackEmail> emailList = this.list(new QueryWrapper<AdwebPublicBlackEmail>().eq("status", 1));
//
//        List<AdwebPublicBlackIp> ipList = adwebPublicBlackIpService.list(new QueryWrapper<AdwebPublicBlackIp>().eq("status", 1));
//
//
//        List<AdwebPublicBlackEmail> blackEmailList = new ArrayList<>();
//        List<AdwebPublicBlackIp> blackIpList = new ArrayList<>();
//
//        //判断是否在存在
//        for (String email : emailSet) {
//            if (emailList.stream().noneMatch(e -> e.getEmail().equals(email))) {
//                AdwebPublicBlackEmail blackEmail = new AdwebPublicBlackEmail();
//                blackEmail.setEmail(email);
//                blackEmail.setBlackOrWhite(0);
//                blackEmail.setStatus(1);
//                blackEmail.setCreateTime(new Date());
//                blackEmailList.add(blackEmail);
//            }
//        }
//
//        //判断是否在存在
//        for (String ip : ipSet) {
//            if (ipList.stream().noneMatch(e -> e.getIp().equals(ip))) {
//                AdwebPublicBlackIp blackIp = new AdwebPublicBlackIp();
//                blackIp.setIp(ip);
//                blackIp.setBlackOrWhite(0);
//                blackIp.setStatus(1);
//                blackIp.setCreateTime(new Date());
//                blackIpList.add(blackIp);
//            }
//        }
//
//        if (blackEmailList.size() > 0) {
//            this.saveBatch(blackEmailList);
//        }
//        if (blackIpList.size() > 0) {
//            adwebPublicBlackIpService.saveBatch(blackIpList);
//        }
//        log.info("从询盘更新Email和IP黑名单成功,新增Email黑名单数量为：" + blackEmailList.size() + ",新增IP黑名单数量为" + blackIpList.size());
//    }
}
