From 896d14b328059863b5cc668dfc6c1d375f59de59 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 24 二月 2026 14:42:06 +0800
Subject: [PATCH] 代码提交
---
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/UniqueCheckAspect.java | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 168 insertions(+), 0 deletions(-)
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/UniqueCheckAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/UniqueCheckAspect.java
new file mode 100644
index 0000000..170a726
--- /dev/null
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/UniqueCheckAspect.java
@@ -0,0 +1,168 @@
+package com.ruoyi.framework.aspectj;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ruoyi.common.annotation.UniqueCheck;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+/**
+ * 鍞竴鎬ф牎楠屽垏闈�
+ * 鐢ㄤ簬鍦ㄦ柊澧炴搷浣滃墠杩涜瀛楁鍞竴鎬ф鏌�
+ *
+ * @author ruoyi
+ */
+@Aspect
+@Order(2)
+@Component
+public class UniqueCheckAspect
+{
+ protected Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Pointcut("@annotation(com.ruoyi.common.annotation.UniqueCheck)")
+ public void uniqueCheckPointCut()
+ {
+ }
+
+ @Before("uniqueCheckPointCut()")
+ public void doBefore(JoinPoint point) throws Throwable
+ {
+ handleUniqueCheck(point);
+ }
+
+ protected void handleUniqueCheck(JoinPoint joinPoint) throws Exception
+ {
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+ Method method = signature.getMethod();
+ UniqueCheck uniqueCheck = method.getAnnotation(UniqueCheck.class);
+
+ if (uniqueCheck == null)
+ {
+ return;
+ }
+
+ // 鑾峰彇鏂规硶鍙傛暟
+ Object[] args = joinPoint.getArgs();
+ if (args == null || args.length == 0)
+ {
+ return;
+ }
+
+ // 鑾峰彇绗竴涓弬鏁�(閫氬父鏄疄浣撳璞�)
+ Object entity = args[0];
+ if (entity == null)
+ {
+ return;
+ }
+
+ // 鏍¢獙瀹炰綋绫诲瀷
+ Class<?> entityClass = uniqueCheck.entityClass();
+ if (!entityClass.isInstance(entity))
+ {
+ logger.warn("瀹炰綋绫诲瀷涓嶅尮閰�, 鏈熸湜: {}, 瀹為檯: {}", entityClass.getName(), entity.getClass().getName());
+ return;
+ }
+
+ // 鑾峰彇Service瀹炰緥
+ Class<?> serviceClass = uniqueCheck.serviceClass();
+ IService service = (IService) SpringUtils.getBean(serviceClass);
+
+ // 鑾峰彇闇�瑕佹鏌ョ殑瀛楁
+ String[] fields = uniqueCheck.fields();
+ if (fields == null || fields.length == 0)
+ {
+ throw new ServiceException("UniqueCheck娉ㄨВ鐨刦ields灞炴�т笉鑳戒负绌�");
+ }
+
+ // 鏋勫缓鏌ヨ鏉′欢
+ QueryWrapper queryWrapper = Wrappers.query();
+ StringBuilder messageBuilder = new StringBuilder();
+ boolean hasCondition = false;
+
+ for (int i = 0; i < fields.length; i++)
+ {
+ String fieldName = fields[i];
+ Object fieldValue = getFieldValue(entity, fieldName);
+
+ // 鍙湁瀛楁鍊间笉涓虹┖鏃舵墠娣诲姞鏌ヨ鏉′欢
+ if (fieldValue != null && !"".equals(fieldValue))
+ {
+ // 浣跨敤瀛楃涓插瓧娈靛悕,MyBatis-Plus浼氳嚜鍔ㄨ浆鎹负鏁版嵁搴撳垪鍚�
+ queryWrapper.eq(StringUtils.toUnderScoreCase(fieldName), fieldValue);
+ hasCondition = true;
+
+ if (messageBuilder.length() > 0)
+ {
+ messageBuilder.append(",");
+ }
+ messageBuilder.append(fieldValue);
+ }
+ }
+
+ // 濡傛灉娌℃湁鏈夋晥鐨勬煡璇㈡潯浠�,鐩存帴杩斿洖
+ if (!hasCondition)
+ {
+ return;
+ }
+
+ // 鎵ц鏌ヨ
+ List list = service.list(queryWrapper);
+ if (list != null && !list.isEmpty())
+ {
+ String message = uniqueCheck.message();
+ // 绠�鍗曠殑娑堟伅鏍煎紡鍖�
+ if (message.contains("{value0}"))
+ {
+ message = message.replace("{value0}", messageBuilder.toString());
+ }
+
+ logger.warn("鍞竴鎬ф牎楠屽け璐�: {}", message);
+ throw new ServiceException(message);
+ }
+ }
+
+ /**
+ * 閫氳繃鍙嶅皠鑾峰彇瀹炰綋瀛楁鍊�
+ */
+ private Object getFieldValue(Object entity, String fieldName) throws Exception
+ {
+ Class<?> clazz = entity.getClass();
+
+ // 灏濊瘯鑾峰彇瀛楁
+ Field field = null;
+ while (clazz != null && field == null)
+ {
+ try
+ {
+ field = clazz.getDeclaredField(fieldName);
+ }
+ catch (NoSuchFieldException e)
+ {
+ clazz = clazz.getSuperclass();
+ }
+ }
+
+ if (field == null)
+ {
+ throw new ServiceException("瀹炰綋绫讳腑涓嶅瓨鍦ㄥ瓧娈�: " + fieldName);
+ }
+
+ field.setAccessible(true);
+ return field.get(entity);
+ }
+}
--
Gitblit v1.9.3