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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.adweb.enquiry.dto.param.EffectiveEnquiryParamDto;
import org.jeecg.modules.adweb.enquiry.entity.AdwebSiteEnquiryRule;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebSiteEnquiryRuleMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebSiteEnquiryRuleService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: adweb_site_enquiry_rule
 * @Author: jeecg-boot
 * @Date: 2023-08-31
 * @Version: V1.0
 */
@Service
public class AdwebSiteEnquiryRuleServiceImpl extends ServiceImpl<AdwebSiteEnquiryRuleMapper, AdwebSiteEnquiryRule> implements IAdwebSiteEnquiryRuleService {

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

                AdwebSiteEnquiryRule newAdwebSiteEnquiryRule = new AdwebSiteEnquiryRule();
                newAdwebSiteEnquiryRule.setWord(effectiveEnquiryParamDto.getWasteEnquirySeason());
                newAdwebSiteEnquiryRule.setBlackOrWhite(0);
                newAdwebSiteEnquiryRule.setSiteId(effectiveEnquiryParamDto.getSiteId());
                newAdwebSiteEnquiryRule.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
                newAdwebSiteEnquiryRule.setStatus(1);
                newAdwebSiteEnquiryRule.setCreateTime(new Date());
                save(newAdwebSiteEnquiryRule);
            }
        } else {
            AdwebSiteEnquiryRule newAdwebSiteEnquiryRule = new AdwebSiteEnquiryRule();
            newAdwebSiteEnquiryRule.setWord(effectiveEnquiryParamDto.getWasteEnquirySeason());
            newAdwebSiteEnquiryRule.setBlackOrWhite(0);
            newAdwebSiteEnquiryRule.setSiteId(effectiveEnquiryParamDto.getSiteId());
            newAdwebSiteEnquiryRule.setSiteCode(effectiveEnquiryParamDto.getSiteCode());
            newAdwebSiteEnquiryRule.setStatus(1);
            newAdwebSiteEnquiryRule.setCreateTime(new Date());
            save(newAdwebSiteEnquiryRule);
        }
    }
}
