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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.modules.adweb.common.util.AdwebRedisUtil;
import org.jeecg.modules.adweb.enquiry.dto.param.EffectiveEnquiryParamDto;
import org.jeecg.modules.adweb.enquiry.entity.AdwebSiteBlackEmail;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebSiteBlackEmailMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebSiteBlackEmailService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: adweb_site_black_email
 * @Author: jeecg-boot
 * @Date: 2023-06-28
 * @Version: V1.0
 */
@Service
public class AdwebSiteBlackEmailServiceImpl
        extends ServiceImpl<AdwebSiteBlackEmailMapper, AdwebSiteBlackEmail>
        implements IAdwebSiteBlackEmailService {

    @Resource private AdwebRedisUtil adwebRedisUtil;
    @Resource private AdwebSiteBlackEmailMapper enquirySiteBlackEmailMapper;

    private static final String SiteBlackEmailKey = "SITE_BLACK_EMAIL_LIST";
    private static final String SiteWhiteEmailListKey = "SITE_WHITE_EMAIL_LIST";
    private static final String SiteEmailTenMinKey = "JUDGE_WASTE_SITE_ENQUIRY_EMAIL_BY_10_MIN::";
    private static final String SiteEmailOneDayKey = "JUDGE_WASTE_SITE_ENQUIRY_EMAIL_BY_ONE_DAY::";
    private static final String NotBlackEmailWasteSiteEnquiryKey =
            "NOT_BLACK_EMAIL_WASTE_SITE_ENQUIRY_MAP::";

    /** 根据站点id获取并存取黑名单数据到redis */
    @Override
    public void saveRedisSiteBlackEmailBySiteId(Integer siteId, Integer blackOrWhite) {
        LambdaQueryWrapper<AdwebSiteBlackEmail> siteAllEmailQueryWrapper =
                new LambdaQueryWrapper<>();
        siteAllEmailQueryWrapper.ne(AdwebSiteBlackEmail::getStatus, 0);
        siteAllEmailQueryWrapper.eq(AdwebSiteBlackEmail::getSiteId, siteId);
        siteAllEmailQueryWrapper.eq(AdwebSiteBlackEmail::getBlackOrWhite, blackOrWhite);
        List<AdwebSiteBlackEmail> allEmailList = list(siteAllEmailQueryWrapper);
        if (CollectionUtils.isEmpty(allEmailList)) {
            log.warn("未获取到对应站点的名单数据");
        }

        List<String> emailList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allEmailList)) {
            for (AdwebSiteBlackEmail enquirySiteBlackEmail1 : allEmailList) {
                if (enquirySiteBlackEmail1.getType().equals("0")) {
                    emailList.add("*" + enquirySiteBlackEmail1.getEmail() + "*");
                } else {
                    emailList.add(enquirySiteBlackEmail1.getEmail());
                }
            }
        }
        // 处理对应站点的黑名单
        if (blackOrWhite == 0) {
            if (CollectionUtils.isNotEmpty(emailList)) {
                adwebRedisUtil.set(SiteBlackEmailKey + "::" + siteId, emailList, 60 * 60 * 24);
            } else {
                adwebRedisUtil.del(SiteBlackEmailKey + "::" + siteId);
            }
        } else {
            if (CollectionUtils.isNotEmpty(emailList)) {
                adwebRedisUtil.set(SiteWhiteEmailListKey + "::" + siteId, emailList, 60 * 60 * 24);
            } else {
                adwebRedisUtil.del(SiteWhiteEmailListKey + "::" + siteId);
            }
        }
    }

    /** 根据站点id获取对应所有的黑白名单数据 */
    @Override
    public Map<String, List<String>> getSiteBlackEmailListBySiteId(Integer siteId) {
        LambdaQueryWrapper<AdwebSiteBlackEmail> siteAllEmailQueryWrapper =
                new LambdaQueryWrapper<>();
        siteAllEmailQueryWrapper.ne(AdwebSiteBlackEmail::getStatus, 0);
        siteAllEmailQueryWrapper.eq(AdwebSiteBlackEmail::getSiteId, siteId);
        List<AdwebSiteBlackEmail> allEmailList = list(siteAllEmailQueryWrapper);
        if (CollectionUtils.isEmpty(allEmailList)) {
            log.warn("未获取到对应站点的黑白名单数据");
        }

        List<String> emailList = new ArrayList<>();
        List<String> whiteEmailList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allEmailList)) {
            for (AdwebSiteBlackEmail enquirySiteBlackEmail1 : allEmailList) {
                if (enquirySiteBlackEmail1.getBlackOrWhite() == 0) {
                    if (enquirySiteBlackEmail1.getType().equals("0")) {
                        emailList.add("*" + enquirySiteBlackEmail1.getEmail() + "*");
                    } else {
                        emailList.add(enquirySiteBlackEmail1.getEmail());
                    }
                } else {
                    if (enquirySiteBlackEmail1.getType().equals("0")) {
                        whiteEmailList.add("*" + enquirySiteBlackEmail1.getEmail() + "*");
                    } else {
                        whiteEmailList.add(enquirySiteBlackEmail1.getEmail());
                    }
                }
            }
        }

        Map<String, List<String>> enquirySiteBlackEmailMap = new HashMap<>();
        enquirySiteBlackEmailMap.put("black", emailList);
        enquirySiteBlackEmailMap.put("white", whiteEmailList);
        return enquirySiteBlackEmailMap;
    }

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

    /**
     * 更改为垃圾询盘处理邮箱黑名单
     *
     * @param effectiveEnquiryParamDto
     */
    @Override
    public void addBlackEmailByContent(EffectiveEnquiryParamDto effectiveEnquiryParamDto) {
        // 排查添加的邮箱是否已有
        LambdaQueryWrapper<AdwebSiteBlackEmail> enquirySiteBlackEmailQueryWrapper =
                new LambdaQueryWrapper<>();
        enquirySiteBlackEmailQueryWrapper.eq(
                AdwebSiteBlackEmail::getEmail, effectiveEnquiryParamDto.getWasteEnquirySeason());
        enquirySiteBlackEmailQueryWrapper.eq(
                AdwebSiteBlackEmail::getSiteId, effectiveEnquiryParamDto.getSiteId());
        enquirySiteBlackEmailQueryWrapper.eq(
                AdwebSiteBlackEmail::getSiteCode, effectiveEnquiryParamDto.getSiteCode());
        enquirySiteBlackEmailQueryWrapper.eq(AdwebSiteBlackEmail::getStatus, 1);
        List<AdwebSiteBlackEmail> enquirySiteBlackEmailList =
                list(enquirySiteBlackEmailQueryWrapper);
        if (CollectionUtils.isNotEmpty(enquirySiteBlackEmailList)) {
            // 如果有白名单 先删除原数据，后添加新的黑名单数据
            for (AdwebSiteBlackEmail enquirySiteBlackEmail : enquirySiteBlackEmailList) {
                if (enquirySiteBlackEmail.getBlackOrWhite() == 1) {
                    if (enquirySiteBlackEmail.getType().equals("1")) {
                        enquirySiteBlackEmail.setStatus(0);
                        enquirySiteBlackEmail.setUpdateTime(new Date());
                        updateById(enquirySiteBlackEmail);
                    }

                    AdwebSiteBlackEmail newEnquirySiteBlackEmail = new AdwebSiteBlackEmail();
                    newEnquirySiteBlackEmail.setEmail(
                            effectiveEnquiryParamDto.getWasteEnquirySeason());
                    newEnquirySiteBlackEmail.setBlackOrWhite(0);
                    newEnquirySiteBlackEmail.setType("1");
                    newEnquirySiteBlackEmail.setSiteId(effectiveEnquiryParamDto.getSiteId());
                    newEnquirySiteBlackEmail.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
                    newEnquirySiteBlackEmail.setStatus(1);
                    newEnquirySiteBlackEmail.setCreateTime(new Date());
                    save(newEnquirySiteBlackEmail);
                }
            }
        } else {
            AdwebSiteBlackEmail enquirySiteBlackEmail = new AdwebSiteBlackEmail();
            enquirySiteBlackEmail.setEmail(effectiveEnquiryParamDto.getWasteEnquirySeason());
            enquirySiteBlackEmail.setType("1");
            enquirySiteBlackEmail.setBlackOrWhite(0);
            enquirySiteBlackEmail.setSiteId(effectiveEnquiryParamDto.getSiteId());
            enquirySiteBlackEmail.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
            enquirySiteBlackEmail.setStatus(1);
            enquirySiteBlackEmail.setCreateTime(new Date());
            save(enquirySiteBlackEmail);
        }

        // 重新处理站点的黑名单redis缓存
        Map<String, List<String>> enquirySiteBlackEmailListMap =
                getSiteBlackEmailListBySiteId(effectiveEnquiryParamDto.getSiteId());
        List<String> emailList = enquirySiteBlackEmailListMap.get("black");
        List<String> whiteEmailList = enquirySiteBlackEmailListMap.get("white");

        // 处理对应站点的黑名单
        if (CollectionUtils.isNotEmpty(emailList)) {
            adwebRedisUtil.set(
                    SiteBlackEmailKey + "::" + effectiveEnquiryParamDto.getSiteId(),
                    emailList,
                    60 * 60 * 24);
        } else {
            adwebRedisUtil.del(SiteBlackEmailKey + "::" + effectiveEnquiryParamDto.getSiteId());
        }

        // 处理对应站点的白名单
        if (CollectionUtils.isNotEmpty(whiteEmailList)) {
            adwebRedisUtil.set(
                    SiteWhiteEmailListKey + "::" + effectiveEnquiryParamDto.getSiteId(),
                    whiteEmailList,
                    60 * 60 * 24);
        } else {
            adwebRedisUtil.del(SiteWhiteEmailListKey + "::" + effectiveEnquiryParamDto.getSiteId());
        }
    }
}
