| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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注解çfields屿§ä¸è½ä¸ºç©º"); |
| | | } |
| | | |
| | | // æå»ºæ¥è¯¢æ¡ä»¶ |
| | | 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); |
| | | } |
| | | } |