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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkcoding.http.util.StringUtil;

import jakarta.annotation.Resource;

import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiry;
import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiryBlacklist;
import org.jeecg.modules.adweb.enquiry.mapper.AdwebEnquiryBlacklistMapper;
import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryBlacklistService;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: adweb_enquiry_blacklist
 * @Author: jeecg-boot
 * @Date: 2022-08-05
 * @Version: V1.0
 */
@Service
public class AdwebEnquiryBlacklistServiceImpl
        extends ServiceImpl<AdwebEnquiryBlacklistMapper, AdwebEnquiryBlacklist>
        implements IAdwebEnquiryBlacklistService {

    @Resource private IAdwebSiteService adwebSiteService;

    /**
     * 判断询盘是否是处于黑名单中，根据手机号和邮箱进行匹配，两个都不为空且都相同才算匹配上
     *
     * @param adwebEnquiry 询盘
     * @return true表示在黑名单中，false表示不在黑名单中
     */
    @Override
    public boolean isBlackEnquiry(AdwebEnquiry adwebEnquiry) {
        String email = adwebEnquiry.getEmail();
        String phone = adwebEnquiry.getPhone();
        if (StringUtil.isEmpty(email) || StringUtil.isEmpty(phone)) {
            return false;
        }
        List<Integer> siteIds = adwebSiteService.getAllSiteIdBySiteId(adwebEnquiry.getSiteId());
        LambdaQueryWrapper<AdwebEnquiryBlacklist> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AdwebEnquiryBlacklist::getSiteId, siteIds);
        queryWrapper.eq(AdwebEnquiryBlacklist::getStatus, 1);
        queryWrapper.and(
                wrapper ->
                        wrapper.eq(AdwebEnquiryBlacklist::getEmail, email)
                                .or()
                                .eq(AdwebEnquiryBlacklist::getPhone, phone));
        return this.count(queryWrapper) > 0;
    }
}
