package org.jeecg.modules.adweb.enquiry.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.adweb.common.util.AdwebRedisUtil;
import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiryBlacklist;
import org.jeecg.modules.adweb.enquiry.entity.AdwebPublicBlackEmail;
import org.jeecg.modules.adweb.enquiry.entity.AdwebSiteBlackEmail;
import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryBlacklistService;
import org.jeecg.modules.adweb.enquiry.service.IAdwebPublicBlackEmailService;
import org.jeecg.modules.adweb.enquiry.service.IAdwebSiteBlackEmailService;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.jeecg.modules.adweb.site.service.ISiteUserPermissionService;
import org.jeecg.modules.adweb.system.service.SysAdwebApi;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: AdwebSiteBlackEmailController.java;
 * @Author: chenpeiqing
 * @Date: 2024/11/1 : 17:22
 * @Version: V1.0
 */
@Tag(name = "adweb_site_black_email")
@RestController
@RequestMapping("/adweb/enquirySiteBlackEmail")
@Slf4j
public class AdwebSiteBlackEmailController
        extends JeecgController<AdwebSiteBlackEmail, IAdwebSiteBlackEmailService> {

    @Resource private IAdwebSiteBlackEmailService enquirySiteBlackEmailService;

    @Resource private AdwebRedisUtil adwebRedisUtil;

    @Resource private SysAdwebApi sysAdwebApi;

    @Resource private IAdwebSiteService adwebSiteService;
    @Resource private ISiteUserPermissionService siteUserPermissionService;
    @Resource private IAdwebPublicBlackEmailService enquiryPublicBlackEmailService;
    @Resource private IAdwebEnquiryBlacklistService adwebEnquiryBlacklistService;

    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::";
    //	private static final String EmailTenMinKey = "JUDGE_WASTE_ENQUIRY_EMAIL_BY_10_MIN::";
    //	private static final String EmailOneDayKey = "JUDGE_WASTE_ENQUIRY_EMAIL_BY_ONE_DAY::";
    //	private static final String NotBlackEmailWasteEnquiryKey =
    // "NOT_BLACK_EMAIL_WASTE_ENQUIRY_MAP::";
    private static final String BlackEmailKey = "BLACK_EMAIL_LIST";

    /**
     * 分页列表查询
     *
     * @param enquirySiteBlackEmail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-分页列表查询")
    @Operation(summary = "adweb_site_black_email-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            AdwebSiteBlackEmail enquirySiteBlackEmail,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<AdwebSiteBlackEmail> page = new Page<AdwebSiteBlackEmail>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> codeList = null;
        List<String> uidList = null;
        if (sysAdwebApi.isAdmin()) {

        } else if (sysAdwebApi.isChannelAdmin()) {
            uidList = sysAdwebApi.getChannelGroupUids();
        } else {
            uidList = new ArrayList<>();
            uidList.add(sysUser.getId());
        }
        List<String> siteCodeList = null;
        if (CollectionUtils.isNotEmpty(uidList)) {
            siteCodeList = siteUserPermissionService.getSiteCodeListByUids(uidList);
        }

        if (enquirySiteBlackEmail.getSiteId() != null) {
            String siteCode = adwebSiteService.getSiteCodeById(enquirySiteBlackEmail.getSiteId());
            if (StringUtils.isNotBlank(siteCode)) {
                LambdaQueryWrapper<AdwebSite> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper
                        .eq(AdwebSite::getId, enquirySiteBlackEmail.getSiteId())
                        .or()
                        .eq(AdwebSite::getParentGroupCode, siteCode);
                queryWrapper.ne(AdwebSite::getStatus, 0);
                queryWrapper.in(!sysAdwebApi.isAdmin(), AdwebSite::getCode, siteCodeList);
                codeList =
                        adwebSiteService.list(queryWrapper).stream()
                                .map(AdwebSite::getCode)
                                .collect(Collectors.toList());
            }
        } else {
            codeList = siteCodeList;
        }
        String column = req.getParameter("column");
        String order = req.getParameter("order");
        IPage<AdwebSiteBlackEmail> pageList =
                enquirySiteBlackEmailService.pageList(
                        page, enquirySiteBlackEmail, codeList, column, order);
        List<AdwebSiteBlackEmail> record = pageList.getRecords();

        if (CollectionUtils.isNotEmpty(record)) {
            for (AdwebSiteBlackEmail enquirySiteBlackEmail1 : record) {
                if (enquirySiteBlackEmail1.getType().equals("0")) {
                    if (enquirySiteBlackEmail1.getWasteGeneralEnquiryNum() != null) {
                        enquirySiteBlackEmail1.setWasteEnquiryNum(
                                enquirySiteBlackEmail1.getWasteGeneralEnquiryNum());
                    }
                }
            }
        }
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param enquirySiteBlackEmail
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-添加")
    @Operation(summary = "adweb_site_black_email-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AdwebSiteBlackEmail enquirySiteBlackEmail) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (StringUtils.isBlank(enquirySiteBlackEmail.getEmail())) {
            return Result.error("邮箱不能为空");
        }

        // 将邮箱转换为小写
        enquirySiteBlackEmail.setEmail(enquirySiteBlackEmail.getEmail().trim().toLowerCase());

        int count =
                (int)
                        enquirySiteBlackEmailService.count(
                                new LambdaQueryWrapper<AdwebSiteBlackEmail>()
                                        .ne(AdwebSiteBlackEmail::getStatus, 0)
                                        .eq(
                                                AdwebSiteBlackEmail::getEmail,
                                                enquirySiteBlackEmail.getEmail())
                                        .eq(
                                                AdwebSiteBlackEmail::getSiteId,
                                                enquirySiteBlackEmail.getSiteId())
                                        .eq(
                                                AdwebSiteBlackEmail::getType,
                                                enquirySiteBlackEmail.getType()));

        if (count > 0) {
            return Result.error("邮箱已存在");
        }

        // 判断站点询盘黑名单是否存在添加进来的黑名单
        LambdaQueryWrapper<AdwebEnquiryBlacklist> adwebEnquiryBlacklistQueryWrapper =
                new LambdaQueryWrapper<>();
        adwebEnquiryBlacklistQueryWrapper.eq(
                AdwebEnquiryBlacklist::getSiteId, enquirySiteBlackEmail.getSiteId());
        adwebEnquiryBlacklistQueryWrapper.eq(AdwebEnquiryBlacklist::getStatus, 1);
        adwebEnquiryBlacklistQueryWrapper.eq(
                AdwebEnquiryBlacklist::getEmail, enquirySiteBlackEmail.getEmail());
        List<AdwebEnquiryBlacklist> enquiryBlacklists =
                adwebEnquiryBlacklistService.list(adwebEnquiryBlacklistQueryWrapper);

        if (CollectionUtils.isNotEmpty(enquiryBlacklists)) {
            return Result.error("在拒收的邮箱黑名单中已存在！");
        }

        enquirySiteBlackEmail.setStatus(1);
        enquirySiteBlackEmail.setCreateTime(new Date());
        enquirySiteBlackEmailService.save(enquirySiteBlackEmail);

        Map<String, List<String>> enquirySiteBlackEmailListMap =
                enquirySiteBlackEmailService.getSiteBlackEmailListBySiteId(
                        enquirySiteBlackEmail.getSiteId());
        List<String> emailList = enquirySiteBlackEmailListMap.get("black");
        List<String> whiteEmailList = enquirySiteBlackEmailListMap.get("white");

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

        // 处理对应站点的白名单
        if (CollectionUtils.isNotEmpty(whiteEmailList)) {
            adwebRedisUtil.set(
                    SiteWhiteEmailListKey + "::" + enquirySiteBlackEmail.getSiteId(),
                    whiteEmailList,
                    60 * 60 * 24);
        } else {
            adwebRedisUtil.del(SiteWhiteEmailListKey + "::" + enquirySiteBlackEmail.getSiteId());
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param enquirySiteBlackEmail
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-编辑")
    @Operation(summary = "adweb_site_black_email-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AdwebSiteBlackEmail enquirySiteBlackEmail) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (StringUtils.isBlank(enquirySiteBlackEmail.getEmail())) {
            return Result.error("邮箱不能为空");
        }

        // 将邮箱转换为小写
        enquirySiteBlackEmail.setEmail(enquirySiteBlackEmail.getEmail().trim().toLowerCase());

        int count =
                (int)
                        enquirySiteBlackEmailService.count(
                                new LambdaQueryWrapper<AdwebSiteBlackEmail>()
                                        .ne(AdwebSiteBlackEmail::getStatus, 0)
                                        .eq(
                                                AdwebSiteBlackEmail::getEmail,
                                                enquirySiteBlackEmail.getEmail())
                                        .eq(
                                                AdwebSiteBlackEmail::getSiteId,
                                                enquirySiteBlackEmail.getSiteId())
                                        .eq(
                                                AdwebSiteBlackEmail::getBlackOrWhite,
                                                enquirySiteBlackEmail.getBlackOrWhite())
                                        .ne(
                                                AdwebSiteBlackEmail::getId,
                                                enquirySiteBlackEmail.getId()));

        if (count > 0) {
            return Result.error("邮箱已存在");
        }

        // 判断站点询盘黑名单是否存在添加进来的黑名单
        LambdaQueryWrapper<AdwebEnquiryBlacklist> adwebEnquiryBlacklistQueryWrapper =
                new LambdaQueryWrapper<>();
        adwebEnquiryBlacklistQueryWrapper.eq(
                AdwebEnquiryBlacklist::getSiteId, enquirySiteBlackEmail.getSiteId());
        adwebEnquiryBlacklistQueryWrapper.eq(AdwebEnquiryBlacklist::getStatus, 1);
        adwebEnquiryBlacklistQueryWrapper.eq(
                AdwebEnquiryBlacklist::getEmail, enquirySiteBlackEmail.getEmail());
        List<AdwebEnquiryBlacklist> enquiryBlacklists =
                adwebEnquiryBlacklistService.list(adwebEnquiryBlacklistQueryWrapper);

        if (CollectionUtils.isNotEmpty(enquiryBlacklists)) {
            return Result.error("在拒收的邮箱黑名单中已存在！");
        }

        AdwebSiteBlackEmail oldAdwebSiteBlackEmail =
                enquirySiteBlackEmailService.getById(enquirySiteBlackEmail.getId());

        // 判断公共询盘缓存的是否包含历史的黑名单数据
        if (!oldAdwebSiteBlackEmail.getEmail().equals(enquirySiteBlackEmail.getEmail())) {
            List<AdwebPublicBlackEmail> emailList =
                    enquiryPublicBlackEmailService.list(
                            new LambdaQueryWrapper<AdwebPublicBlackEmail>()
                                    .eq(AdwebPublicBlackEmail::getStatus, 1)
                                    .eq(AdwebPublicBlackEmail::getBlackOrWhite, 0));
            List<String> pubicBlackEmailList =
                    emailList.stream()
                            .map(AdwebPublicBlackEmail::getEmail)
                            .collect(Collectors.toList());

            List<String> publicBlackList = (List<String>) adwebRedisUtil.get(BlackEmailKey);

            publicBlackList.removeAll(pubicBlackEmailList);
            if (CollectionUtils.isNotEmpty(publicBlackList)) {
                for (int i = 0; i < publicBlackList.size(); i++) {
                    String oldEmail = oldAdwebSiteBlackEmail.getEmail();
                    if (oldAdwebSiteBlackEmail.getType().equals("0")) {
                        oldEmail = "*" + oldEmail + "*";
                    }
                    if (oldEmail.equals(publicBlackList.get(i))) {
                        publicBlackList.remove(i);
                        break;
                    }
                }
                pubicBlackEmailList.addAll(publicBlackList);
                adwebRedisUtil.set(BlackEmailKey, pubicBlackEmailList, 60 * 60 * 24);
            }
        }

        enquirySiteBlackEmail.setUpdateTime(new Date());
        enquirySiteBlackEmailService.updateById(enquirySiteBlackEmail);

        LambdaQueryWrapper<AdwebSiteBlackEmail> siteAllEmailQueryWrapper =
                new LambdaQueryWrapper<>();
        siteAllEmailQueryWrapper.ne(AdwebSiteBlackEmail::getStatus, 0);
        siteAllEmailQueryWrapper.eq(
                AdwebSiteBlackEmail::getSiteId, enquirySiteBlackEmail.getSiteId());
        List<AdwebSiteBlackEmail> allEmailList =
                enquirySiteBlackEmailService.list(siteAllEmailQueryWrapper);
        if (CollectionUtils.isEmpty(allEmailList)) {
            log.info("未获取到对应站点的黑白名单数据");
        }

        Map<String, List<String>> enquirySiteBlackEmailListMap =
                enquirySiteBlackEmailService.getSiteBlackEmailListBySiteId(
                        enquirySiteBlackEmail.getSiteId());
        List<String> emailList = enquirySiteBlackEmailListMap.get("black");
        List<String> whiteEmailList = enquirySiteBlackEmailListMap.get("white");

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

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

        // 处理站点改变的数据缓存
        if (!oldAdwebSiteBlackEmail.getSiteId().equals(enquirySiteBlackEmail.getSiteId())) {
            Map<String, List<String>> oldAdwebSiteBlackEmailListMap =
                    enquirySiteBlackEmailService.getSiteBlackEmailListBySiteId(
                            oldAdwebSiteBlackEmail.getSiteId());
            List<String> oldEmailList = oldAdwebSiteBlackEmailListMap.get("black");
            List<String> oldWhiteEmailList = oldAdwebSiteBlackEmailListMap.get("white");
            // 处理对应站点的黑名单
            if (CollectionUtils.isNotEmpty(oldEmailList)) {
                adwebRedisUtil.set(
                        SiteBlackEmailKey + "::" + oldAdwebSiteBlackEmail.getSiteId(),
                        oldEmailList,
                        60 * 60 * 24);
            } else {
                adwebRedisUtil.del(SiteBlackEmailKey + "::" + oldAdwebSiteBlackEmail.getSiteId());
            }

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

        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-通过id删除")
    @Operation(summary = "adweb_site_black_email-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (StringUtils.isBlank(id)) {
            return Result.error("参数不识别！");
        }
        AdwebSiteBlackEmail enquirySiteBlackEmail = enquirySiteBlackEmailService.getById(id);
        if (enquirySiteBlackEmail == null) {
            return Result.error("未找到对应数据");
        }

        // 判断公共询盘缓存的是否包含历史的黑名单数据
        List<AdwebPublicBlackEmail> pubicEmailList =
                enquiryPublicBlackEmailService.list(
                        new LambdaQueryWrapper<AdwebPublicBlackEmail>()
                                .eq(AdwebPublicBlackEmail::getStatus, 1)
                                .eq(AdwebPublicBlackEmail::getBlackOrWhite, 0));
        List<String> pubicBlackEmailList =
                pubicEmailList.stream()
                        .map(AdwebPublicBlackEmail::getEmail)
                        .collect(Collectors.toList());

        List<String> publicBlackList = (List<String>) adwebRedisUtil.get(BlackEmailKey);

        if (CollectionUtils.isNotEmpty(publicBlackList)) {
            if (CollectionUtils.isNotEmpty(pubicBlackEmailList)) {
                publicBlackList.removeAll(pubicBlackEmailList);
            } else {
                pubicBlackEmailList = new ArrayList<>();
            }
            for (int i = 0; i < publicBlackList.size(); i++) {
                String oldEmail = enquirySiteBlackEmail.getEmail();
                if (enquirySiteBlackEmail.getType().equals("0")) {
                    oldEmail = "*" + oldEmail + "*";
                }
                if (oldEmail.equals(publicBlackList.get(i))) {
                    publicBlackList.remove(i);
                    break;
                }
            }
            pubicBlackEmailList.addAll(publicBlackList);
            adwebRedisUtil.set(BlackEmailKey, pubicBlackEmailList, 60 * 60 * 24);
        }
        enquirySiteBlackEmail.setStatus(0);
        enquirySiteBlackEmail.setUpdateTime(new Date());
        enquirySiteBlackEmailService.updateById(enquirySiteBlackEmail);

        adwebRedisUtil.del(
                SiteEmailTenMinKey
                        + enquirySiteBlackEmail.getSiteId()
                        + "::"
                        + enquirySiteBlackEmail.getEmail());
        adwebRedisUtil.del(
                SiteEmailOneDayKey
                        + enquirySiteBlackEmail.getSiteId()
                        + "::"
                        + enquirySiteBlackEmail.getEmail());
        adwebRedisUtil.del(
                NotBlackEmailWasteSiteEnquiryKey
                        + enquirySiteBlackEmail.getSiteId()
                        + "::"
                        + enquirySiteBlackEmail.getEmail());

        LambdaQueryWrapper<AdwebSiteBlackEmail> siteAllEmailQueryWrapper =
                new LambdaQueryWrapper<>();
        siteAllEmailQueryWrapper.ne(AdwebSiteBlackEmail::getStatus, 0);
        siteAllEmailQueryWrapper.eq(
                AdwebSiteBlackEmail::getSiteId, enquirySiteBlackEmail.getSiteId());
        List<AdwebSiteBlackEmail> allEmailList =
                enquirySiteBlackEmailService.list(siteAllEmailQueryWrapper);
        if (CollectionUtils.isEmpty(allEmailList)) {
            log.info("未获取到对应站点的黑白名单数据");
        }

        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());
                    }
                }
            }
        }
        // 处理对应站点的黑名单
        if (CollectionUtils.isNotEmpty(emailList)) {
            adwebRedisUtil.set(
                    SiteBlackEmailKey + "::" + enquirySiteBlackEmail.getSiteId(),
                    emailList,
                    60 * 60 * 24);
        } else {
            adwebRedisUtil.del(SiteBlackEmailKey + "::" + enquirySiteBlackEmail.getSiteId());
        }

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

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-批量删除")
    @Operation(summary = "adweb_site_black_email-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (StringUtils.isBlank(ids)) {
            return Result.error("参数不识别！");
        }

        List<String> idList = Arrays.asList(ids.split(","));

        List<AdwebSiteBlackEmail> list =
                enquirySiteBlackEmailService.list(
                        new LambdaQueryWrapper<AdwebSiteBlackEmail>()
                                .in(AdwebSiteBlackEmail::getId, idList)
                                .ne(AdwebSiteBlackEmail::getStatus, 0));

        if (list.isEmpty()) {
            return Result.error("未找到对应数据");
        }

        boolean update =
                enquirySiteBlackEmailService.update(
                        new LambdaUpdateWrapper<AdwebSiteBlackEmail>()
                                .in(AdwebSiteBlackEmail::getId, idList)
                                .set(AdwebSiteBlackEmail::getStatus, 0));

        HashSet<Integer> siteList = new HashSet<>();
        // 删除redis中的数据
        for (AdwebSiteBlackEmail enquirySiteBlackEmail : list) {

            // 判断公共询盘缓存的是否包含历史的黑名单数据
            List<AdwebPublicBlackEmail> pubicEmailList =
                    enquiryPublicBlackEmailService.list(
                            new LambdaQueryWrapper<AdwebPublicBlackEmail>()
                                    .eq(AdwebPublicBlackEmail::getStatus, 1)
                                    .eq(AdwebPublicBlackEmail::getBlackOrWhite, 0));
            List<String> pubicBlackEmailList =
                    pubicEmailList.stream()
                            .map(AdwebPublicBlackEmail::getEmail)
                            .collect(Collectors.toList());

            List<String> publicBlackList = (List<String>) adwebRedisUtil.get(BlackEmailKey);

            if (CollectionUtils.isNotEmpty(publicBlackList)) {
                if (CollectionUtils.isNotEmpty(pubicBlackEmailList)) {
                    publicBlackList.removeAll(pubicBlackEmailList);
                } else {
                    pubicBlackEmailList = new ArrayList<>();
                }
                for (int i = 0; i < publicBlackList.size(); i++) {
                    String oldEmail = enquirySiteBlackEmail.getEmail();
                    if (enquirySiteBlackEmail.getType().equals("0")) {
                        oldEmail = "*" + oldEmail + "*";
                    }
                    if (oldEmail.equals(publicBlackList.get(i))) {
                        publicBlackList.remove(i);
                        break;
                    }
                }
                pubicBlackEmailList.addAll(publicBlackList);
                adwebRedisUtil.set(BlackEmailKey, pubicBlackEmailList, 60 * 60 * 24);
            }

            adwebRedisUtil.del(
                    SiteEmailTenMinKey
                            + enquirySiteBlackEmail.getSiteId()
                            + "::"
                            + enquirySiteBlackEmail.getEmail());
            adwebRedisUtil.del(
                    SiteEmailOneDayKey
                            + enquirySiteBlackEmail.getSiteId()
                            + "::"
                            + enquirySiteBlackEmail.getEmail());
            adwebRedisUtil.del(
                    NotBlackEmailWasteSiteEnquiryKey
                            + enquirySiteBlackEmail.getSiteId()
                            + "::"
                            + enquirySiteBlackEmail.getEmail());
            siteList.add(enquirySiteBlackEmail.getSiteId());
        }

        for (Integer siteId : siteList) {
            LambdaQueryWrapper<AdwebSiteBlackEmail> siteAllEmailQueryWrapper =
                    new LambdaQueryWrapper<>();
            siteAllEmailQueryWrapper.ne(AdwebSiteBlackEmail::getStatus, 0);
            siteAllEmailQueryWrapper.eq(AdwebSiteBlackEmail::getSiteId, siteId);
            List<AdwebSiteBlackEmail> allEmailList =
                    enquirySiteBlackEmailService.list(siteAllEmailQueryWrapper);
            if (CollectionUtils.isEmpty(allEmailList)) {
                log.info("未获取到对应站点的黑白名单数据");
            }

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

            // 处理对应站点的白名单
            if (CollectionUtils.isNotEmpty(whiteEmailList)) {
                adwebRedisUtil.set(
                        SiteWhiteEmailListKey + "::" + siteId, whiteEmailList, 60 * 60 * 24);
            } else {
                adwebRedisUtil.del(SiteWhiteEmailListKey + "::" + siteId);
            }
        }
        return update ? Result.OK("批量删除成功!") : Result.error("批量删除失败!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "adweb_site_black_email-通过id查询")
    @Operation(summary = "adweb_site_black_email-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AdwebSiteBlackEmail enquirySiteBlackEmail = enquirySiteBlackEmailService.getById(id);
        if (enquirySiteBlackEmail == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(enquirySiteBlackEmail);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param enquirySiteBlackEmail
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(
            HttpServletRequest request, AdwebSiteBlackEmail enquirySiteBlackEmail) {
        return super.exportXls(
                request,
                enquirySiteBlackEmail,
                AdwebSiteBlackEmail.class,
                "adweb_site_black_email");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AdwebSiteBlackEmail.class);
    }
}
