|
@@ -0,0 +1,277 @@
|
|
|
+package com.pig4cloud.pig.marketing.service.impl;
|
|
|
+
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.pig4cloud.pig.common.core.exception.BusinessException;
|
|
|
+import com.pig4cloud.pig.marketing.api.dto.rule.PageMktRuleDTO;
|
|
|
+import com.pig4cloud.pig.marketing.api.dto.rule.SaveMktRuleDTO;
|
|
|
+import com.pig4cloud.pig.marketing.api.entity.MktMgmtKeyword;
|
|
|
+import com.pig4cloud.pig.marketing.api.entity.MktMgmtRule;
|
|
|
+import com.pig4cloud.pig.marketing.api.vo.rule.PageMktRuleVO;
|
|
|
+import com.pig4cloud.pig.marketing.mapper.MktMgmtKeywordMapper;
|
|
|
+import com.pig4cloud.pig.marketing.mapper.MktMgmtRuleMapper;
|
|
|
+import com.pig4cloud.pig.marketing.service.MktMgmtRuleService;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: lwh
|
|
|
+ * @date: 2025-08-31
|
|
|
+ * @description: 营销管理规则Service实现类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
|
|
|
+
|
|
|
+ private final MktMgmtRuleMapper mktMgmtRuleMapper;
|
|
|
+
|
|
|
+ private final MktMgmtKeywordMapper mktMgmtKeywordMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存/修改营销规则
|
|
|
+ * @param reqDto 入参
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void saveMktRule(SaveMktRuleDTO reqDto) {
|
|
|
+ if (reqDto.getId() == null) {
|
|
|
+ // 新增规则
|
|
|
+ MktMgmtRule rule = new MktMgmtRule();
|
|
|
+ BeanUtils.copyProperties(reqDto, rule);
|
|
|
+ mktMgmtRuleMapper.insert(rule);
|
|
|
+
|
|
|
+ // 保存关键字
|
|
|
+ List<String> keywords = reqDto.getKeyword();
|
|
|
+
|
|
|
+ for (String keyword : keywords) {
|
|
|
+ MktMgmtKeyword mktMgmtKeyword = new MktMgmtKeyword();
|
|
|
+ mktMgmtKeyword.setKeyword(keyword);
|
|
|
+ mktMgmtKeyword.setRuleId(rule.getId());
|
|
|
+ mktMgmtKeywordMapper.insert(mktMgmtKeyword);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // 1. 更新主规则信息
|
|
|
+ MktMgmtRule mktMgmtRule = mktMgmtRuleMapper.selectById(reqDto.getId());
|
|
|
+ if (mktMgmtRule == null) {
|
|
|
+ throw new BusinessException("规则不存在");
|
|
|
+ }
|
|
|
+ MktMgmtRule rule = new MktMgmtRule();
|
|
|
+ BeanUtils.copyProperties(reqDto, rule);
|
|
|
+ mktMgmtRuleMapper.updateById(rule);
|
|
|
+
|
|
|
+ // 2. 处理关键字(先删除旧的,再添加新的)
|
|
|
+ Long ruleId = reqDto.getId();
|
|
|
+ List<String> newKeywords = reqDto.getKeyword();
|
|
|
+ newKeywords = newKeywords == null ? Collections.emptyList() : newKeywords;
|
|
|
+
|
|
|
+ List<MktMgmtKeyword> oldKeywords = mktMgmtKeywordMapper.selectList(Wrappers.<MktMgmtKeyword>lambdaQuery()
|
|
|
+ .eq(MktMgmtKeyword::getRuleId, ruleId));
|
|
|
+ Set<String> oldKeywordSet = oldKeywords.stream()
|
|
|
+ .map(MktMgmtKeyword::getKeyword)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ // 2. 提取新关键字集合
|
|
|
+ Set<String> newKeywordSet = new HashSet<>(newKeywords);
|
|
|
+
|
|
|
+ // 3. 处理需要删除的关键字
|
|
|
+ List<Long> needDeleteIds = oldKeywords.stream()
|
|
|
+ .filter(keyword -> !newKeywordSet.contains(keyword.getKeyword()))
|
|
|
+ .map(MktMgmtKeyword::getId)
|
|
|
+ .toList();
|
|
|
+ if (!needDeleteIds.isEmpty()) {
|
|
|
+ mktMgmtKeywordMapper.deleteByIds(needDeleteIds);
|
|
|
+ }
|
|
|
+ // 4. 处理需要新增的关键字(存在于新列表但不存在于旧列表)
|
|
|
+ List<String> needAddKeywords = newKeywordSet.stream()
|
|
|
+ .filter(keyword -> !oldKeywordSet.contains(keyword))
|
|
|
+ .toList();
|
|
|
+ if (!needAddKeywords.isEmpty()) {
|
|
|
+ for (String keyword : needAddKeywords) {
|
|
|
+ MktMgmtKeyword mktMgmtKeyword = new MktMgmtKeyword();
|
|
|
+ mktMgmtKeyword.setKeyword(keyword);
|
|
|
+ mktMgmtKeyword.setRuleId(ruleId);
|
|
|
+ mktMgmtKeywordMapper.insert(mktMgmtKeyword);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除营销规则
|
|
|
+ * @param ids ids
|
|
|
+ * @return Boolean
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Boolean delMktRuleByIds(List<Long> ids) {
|
|
|
+ int i = mktMgmtRuleMapper.deleteByIds(ids);
|
|
|
+ int delete = mktMgmtKeywordMapper.delete(Wrappers.<MktMgmtKeyword>lambdaQuery()
|
|
|
+ .in(MktMgmtKeyword::getRuleId, ids));
|
|
|
+
|
|
|
+ return i > 0 && delete > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询营销规则
|
|
|
+ * @param reqDto 入参
|
|
|
+ * @return Page
|
|
|
+ */
|
|
|
+// @Override
|
|
|
+ public Page<PageMktRuleVO> pageMktRule2(PageMktRuleDTO reqDto) {
|
|
|
+ Page<MktMgmtRule> page = new Page<>(reqDto.getCurrent(), reqDto.getSize());
|
|
|
+ // 1、根据入参条件查询规则
|
|
|
+ // 1.1、关键字模糊查询
|
|
|
+ // 1.2、IP模糊查询,包括IP段,如192.168.1.10,如果数据库中有192.168.1.1/10(192.168.1.1~192.168.1.10)也是符合条件
|
|
|
+ // 1.3、域名模糊查询
|
|
|
+
|
|
|
+ // 根据ruleId查询关键字
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+ public Page<PageMktRuleVO> pageMktRule3(PageMktRuleDTO reqDto) {
|
|
|
+ // 初始化分页参数
|
|
|
+ Page<MktMgmtRule> page = new Page<>(reqDto.getCurrent(), reqDto.getSize());
|
|
|
+
|
|
|
+ // 构建查询条件
|
|
|
+ QueryWrapper<MktMgmtRule> queryWrapper = new QueryWrapper<>();
|
|
|
+ // 过滤已删除的数据
|
|
|
+ queryWrapper.eq("del_flag", "0");
|
|
|
+
|
|
|
+ // 1.1 关键字模糊查询(关联关键字表)
|
|
|
+ if (StringUtils.hasText(reqDto.getKeyword())) {
|
|
|
+ // 使用子查询关联关键字表,查询包含该关键字的规则ID
|
|
|
+ queryWrapper.inSql("id",
|
|
|
+ "SELECT DISTINCT rule_id FROM mkt_mgmt_keyword WHERE keyword LIKE CONCAT('%', #{keyword}, '%') AND del_flag = '0'");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1.2 IP模糊查询(支持IP段匹配)
|
|
|
+ if (StringUtils.hasText(reqDto.getIp())) {
|
|
|
+ queryWrapper.like("ip", reqDto.getIp());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1.3 域名模糊查询
|
|
|
+ if (StringUtils.hasText(reqDto.getDomain())) {
|
|
|
+ queryWrapper.like("domain", reqDto.getDomain());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<MktMgmtRule> rulePage = mktMgmtRuleMapper.selectPage(page, queryWrapper);
|
|
|
+
|
|
|
+ // 转换为VO并补充关键字信息
|
|
|
+ IPage<PageMktRuleVO> iPage = rulePage.convert(rule -> {
|
|
|
+ PageMktRuleVO vo = new PageMktRuleVO();
|
|
|
+ BeanUtils.copyProperties(rule, vo);
|
|
|
+
|
|
|
+ // 查询当前规则关联的所有关键字
|
|
|
+ QueryWrapper<MktMgmtKeyword> keywordQuery = new QueryWrapper<>();
|
|
|
+ keywordQuery.eq("rule_id", rule.getId())
|
|
|
+ .eq("del_flag", "0");
|
|
|
+ List<MktMgmtKeyword> keywords = mktMgmtKeywordMapper.selectList(keywordQuery);
|
|
|
+ // 提取关键字字符串列表
|
|
|
+ List<String> keywordList = keywords.stream()
|
|
|
+ .map(MktMgmtKeyword::getKeyword)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ vo.setKeyword(keywordList);
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ });
|
|
|
+
|
|
|
+ Page<PageMktRuleVO> result = new Page<>(iPage.getCurrent(), iPage.getSize(), iPage.getTotal());
|
|
|
+ result.setRecords(iPage.getRecords());
|
|
|
+ result.setPages(iPage.getPages());
|
|
|
+ result.setTotal(iPage.getTotal());
|
|
|
+ result.setSize(iPage.getSize());
|
|
|
+ result.setCurrent(iPage.getCurrent());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<PageMktRuleVO> pageMktRule(PageMktRuleDTO reqDto) {
|
|
|
+ Page<MktMgmtRule> page = new Page<>(reqDto.getCurrent(), reqDto.getSize());
|
|
|
+
|
|
|
+ // 构建查询条件
|
|
|
+ QueryWrapper<MktMgmtRule> queryWrapper = new QueryWrapper<>();
|
|
|
+
|
|
|
+ // 1.1、关键字模糊查询 - 通过关联查询关键字表
|
|
|
+ if (StringUtils.hasText(reqDto.getKeyword())) {
|
|
|
+ // 先查询包含该关键字的规则ID
|
|
|
+ List<MktMgmtKeyword> keywords = mktMgmtKeywordMapper.selectList(
|
|
|
+ Wrappers.<MktMgmtKeyword>lambdaQuery()
|
|
|
+ .like(MktMgmtKeyword::getKeyword, reqDto.getKeyword())
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!keywords.isEmpty()) {
|
|
|
+ List<Long> ruleIds = keywords.stream()
|
|
|
+ .map(MktMgmtKeyword::getRuleId)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ queryWrapper.in("id", ruleIds);
|
|
|
+ } else {
|
|
|
+ // 如果没有找到匹配的关键字,返回空结果
|
|
|
+ return new Page<>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1.2、IP模糊查询,包括IP段
|
|
|
+ // 1.2、IP匹配逻辑 - 支持单IP和IP段匹配
|
|
|
+ if (StringUtils.hasText(reqDto.getIp())) {
|
|
|
+ String inputIp = reqDto.getIp();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1.3、域名模糊查询
|
|
|
+ if (StringUtils.hasText(reqDto.getDomain())) {
|
|
|
+ queryWrapper.like("domain", reqDto.getDomain());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按创建时间倒序排列
|
|
|
+ queryWrapper.orderByDesc("create_time");
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<MktMgmtRule> rulePage = mktMgmtRuleMapper.selectPage(page, queryWrapper);
|
|
|
+
|
|
|
+ // 转换为VO对象
|
|
|
+ Page<PageMktRuleVO> resultPage = new Page<>(reqDto.getCurrent(), reqDto.getSize());
|
|
|
+ resultPage.setTotal(rulePage.getTotal());
|
|
|
+ resultPage.setPages(rulePage.getPages());
|
|
|
+
|
|
|
+ List<PageMktRuleVO> voList = rulePage.getRecords().stream().map(rule -> {
|
|
|
+ PageMktRuleVO vo = new PageMktRuleVO();
|
|
|
+ BeanUtils.copyProperties(rule, vo);
|
|
|
+
|
|
|
+ // 查询该规则对应的关键字列表
|
|
|
+ List<MktMgmtKeyword> keywords = mktMgmtKeywordMapper.selectList(
|
|
|
+ Wrappers.<MktMgmtKeyword>lambdaQuery()
|
|
|
+ .eq(MktMgmtKeyword::getRuleId, rule.getId())
|
|
|
+ );
|
|
|
+
|
|
|
+ List<String> keywordList = keywords.stream()
|
|
|
+ .map(MktMgmtKeyword::getKeyword)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ vo.setKeyword(keywordList);
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ resultPage.setRecords(voList);
|
|
|
+
|
|
|
+ return resultPage;
|
|
|
+ }
|
|
|
+}
|