Quellcode durchsuchen

update: 营销规则\推送记录模块

lwh vor 2 Wochen
Ursprung
Commit
0b053e6103

+ 9 - 0
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/controller/MktMgmtRuleController.java

@@ -14,7 +14,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.validation.Valid;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotEmpty;
+import jakarta.validation.constraints.NotNull;
 import lombok.AllArgsConstructor;
+import org.springdoc.core.annotations.ParameterObject;
 import org.springframework.http.HttpHeaders;
 import org.springframework.web.bind.annotation.*;
 
@@ -54,4 +56,11 @@ public class MktMgmtRuleController {
 		Page<PageMktRuleVO> result = mktMgmtRuleService.pageMktRule(reqDto);
 		return R.ok(result);
 	}
+
+	@GetMapping("/get")
+	@Operation(summary = "查询营销规则详情")
+	public R<PageMktRuleVO> pageRule(@NotNull(message = "id不能为空") @ParameterObject Long id) {
+		PageMktRuleVO result = mktMgmtRuleService.getMktRuleById(id);
+		return R.ok(result);
+	}
 }

+ 8 - 0
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/MktMgmtRuleService.java

@@ -6,6 +6,7 @@ import com.pig4cloud.pig.marketing.api.dto.rule.PageMktRuleDTO;
 import com.pig4cloud.pig.marketing.api.dto.rule.SaveMktRuleDTO;
 import com.pig4cloud.pig.marketing.api.vo.rule.PageMktRuleVO;
 import jakarta.validation.Valid;
+import jakarta.validation.constraints.NotNull;
 
 import java.util.List;
 
@@ -36,4 +37,11 @@ public interface MktMgmtRuleService {
 	 * @return Page
 	 */
 	Page<PageMktRuleVO> pageMktRule(@Valid PageMktRuleDTO reqDto);
+
+	/**
+	 * 根据id查询营销规则
+	 * @param id id
+	 * @return PageMktRuleVO
+	 */
+	PageMktRuleVO getMktRuleById(@NotNull(message = "id不能为空") Long id);
 }

+ 104 - 75
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/impl/MktMgmtRuleServiceImpl.java

@@ -2,20 +2,22 @@ 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.common.core.util.ip.IPUtils;
 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.util.DomainValidationUtil;
 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.springdoc.core.annotations.ParameterObject;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -48,10 +50,71 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 	@Override
 	@Transactional
 	public void saveMktRule(SaveMktRuleDTO reqDto) {
+		// 处理IP格式解析
+		String ip = reqDto.getIp();
+		int ipMode = 0;
+		String startIp = null;
+		String endIp = null;
+
+		// 处理IP格式
+		if (StringUtils.hasText(ip)) {
+			// 检查是否为IP段格式(包含"/")
+			if (ip.contains("/")) {
+				// IP段格式:192.168.1.1/10
+				String[] parts = ip.split("/");
+				if (parts.length == 2) {
+					String startIpPart = parts[0];
+					String endIpSuffix = parts[1];
+					// 验证起始IP格式
+					if (!IPUtils.isValidIp(startIpPart)) {
+						throw new BusinessException("起始IP格式不正确");
+					}
+					// 计算结束IP
+					try {
+						// 解析起始IP
+						String[] startParts = startIpPart.split("\\.");
+						if (startParts.length == 4) {
+							int endSuffix = Integer.parseInt(endIpSuffix);
+							// 构建结束IP,保持前三段不变
+							endIp = String.format("%s.%s.%s.%d",
+									startParts[0], startParts[1], startParts[2], endSuffix);
+							startIp = startIpPart;
+							ipMode = 2; // IP段模式
+						}
+					} catch (NumberFormatException e) {
+						throw new BusinessException("IP段格式不正确");
+					}
+					if (endIp == null) {
+						throw new BusinessException("IP段格式不正确");
+					}
+				} else {
+					throw new BusinessException("IP段格式不正确,应为:起始IP/结束IP后缀");
+				}
+			} else {
+				// 单IP格式
+				if (!IPUtils.isValidIp(ip)) {
+					throw new BusinessException("IP格式不正确");
+				}
+				// 设置IP相关字段
+				ipMode = 1;
+				startIp = ip;
+			}
+		}
+		// 验证域名
+		if (StringUtils.hasText(reqDto.getDomain())){
+			if (!DomainValidationUtil.isValidDomain(reqDto.getDomain())){
+				throw new BusinessException("域名格式错误");
+			}
+		}
+
 		if (reqDto.getId() == null) {
 			// 新增规则
 			MktMgmtRule rule = new MktMgmtRule();
 			BeanUtils.copyProperties(reqDto, rule);
+			// 设置IP相关字段
+			rule.setIpMode(ipMode);
+			rule.setStartIp(startIp == null ? "" : startIp);
+			rule.setEndIp(endIp == null ? "" : endIp);
 			mktMgmtRuleMapper.insert(rule);
 
 			// 保存关键字
@@ -71,6 +134,10 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 			}
 			MktMgmtRule rule = new MktMgmtRule();
 			BeanUtils.copyProperties(reqDto, rule);
+			// 设置IP相关字段
+			rule.setIpMode(ipMode);
+			rule.setStartIp(startIp == null ? "" : startIp);
+			rule.setEndIp(endIp == null ? "" : endIp);
 			mktMgmtRuleMapper.updateById(rule);
 
 			// 2. 处理关键字(先删除旧的,再添加新的)
@@ -118,7 +185,7 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 	 */
 	@Override
 	@Transactional
-	public Boolean delMktRuleByIds(List<Long> ids) {
+	public Boolean delMktRuleByIds(@ParameterObject List<Long> ids) {
 		int i = mktMgmtRuleMapper.deleteByIds(ids);
 		int delete = mktMgmtKeywordMapper.delete(Wrappers.<MktMgmtKeyword>lambdaQuery()
 				.in(MktMgmtKeyword::getRuleId, ids));
@@ -131,78 +198,6 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 	 * @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());
@@ -230,10 +225,21 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 			}
 		}
 
-		// 1.2、IP模糊查询,包括IP段
 		// 1.2、IP匹配逻辑 - 支持单IP和IP段匹配
 		if (StringUtils.hasText(reqDto.getIp())) {
 			String inputIp = reqDto.getIp();
+			// 验证输入IP格式
+			if (!IPUtils.isValidIp(inputIp)) {
+				throw new BusinessException("输入的IP格式不正确");
+			}
+
+			queryWrapper.and(wrapper -> wrapper
+					// 匹配单IP模式:ipMode=1 且 startIp等于输入IP
+					.and(w -> w.eq("ip_mode", 1).eq("start_ip", inputIp))
+					// 匹配IP段模式:ipMode=2 且 输入IP在startIp和endIp范围内
+					.or(w -> w.eq("ip_mode", 2)
+							.apply("INET_ATON({0}) BETWEEN INET_ATON(start_ip) AND INET_ATON(end_ip)", inputIp))
+			);
 		}
 
 		// 1.3、域名模糊查询
@@ -274,4 +280,27 @@ public class MktMgmtRuleServiceImpl implements MktMgmtRuleService {
 
 		return resultPage;
 	}
+
+	/**
+	 * 根据ID查询营销规则
+	 * @param id id
+	 * @return PageMktRuleVO
+	 */
+	@Override
+	public PageMktRuleVO getMktRuleById(Long id) {
+		MktMgmtRule mktMgmtRule = mktMgmtRuleMapper.selectById(id);
+		if (mktMgmtRule == null){
+			throw new BusinessException("规则不存在");
+		}
+		PageMktRuleVO result = new PageMktRuleVO();
+		BeanUtils.copyProperties(mktMgmtRule, result);
+		// 查询该规则对应关键字列表
+		List<MktMgmtKeyword> keywords = mktMgmtKeywordMapper.selectList(Wrappers.<MktMgmtKeyword>lambdaQuery()
+				.eq(MktMgmtKeyword::getRuleId, id));
+		List<String> keywordList = keywords.stream()
+				.map(MktMgmtKeyword::getKeyword)
+				.collect(Collectors.toList());
+		result.setKeyword(keywordList);
+		return result;
+	}
 }