From 987e38327f849e1b13d8541246dde08d877db0e8 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期三, 07 一月 2026 14:10:43 +0800
Subject: [PATCH] 【市一】调整mapper获取ordid
---
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/AutoInitAspect.java | 135 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 135 insertions(+), 0 deletions(-)
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/AutoInitAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/AutoInitAspect.java
new file mode 100644
index 0000000..0608849
--- /dev/null
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/AutoInitAspect.java
@@ -0,0 +1,135 @@
+package com.ruoyi.framework.aspectj;
+
+import com.ruoyi.common.annotation.AutoInit;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
+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.stereotype.Component;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * 鑷姩濉厖orgid鍒囬潰
+ *
+ * @author smartor
+ */
+@Aspect
+@Component
+public class AutoInitAspect {
+
+ private static final Logger log = LoggerFactory.getLogger(AutoInit.class);
+
+ /**
+ * 瀹氫箟鍒囩偣 - 鎷︽埅鎵�鏈夊甫鏈堾AutoInit娉ㄨВ鐨勬柟娉�
+ */
+ @Pointcut("@annotation(com.ruoyi.common.annotation.AutoInit)")
+ public void autoInitPointCut() {
+ }
+
+ /**
+ * 鍦ㄦ柟娉曟墽琛屽墠鑷姩鍒濆鍖�
+ *
+ * @param joinPoint 鍒囩偣
+ * @param autoInit 娉ㄨВ瀵硅薄
+ */
+ @Before("autoInitPointCut() && @annotation(autoInit)")
+ public void doBefore(JoinPoint joinPoint, AutoInit autoInit) {
+ try {
+ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛鐨刼rgid
+ String orgid = SecurityUtils.getLoginUser().getUser().getOrgid();
+
+ // 濡傛灉orgid涓虹┖锛屽垯涓嶅垵濮嬪寲
+ if (StringUtils.isEmpty(orgid)) {
+ log.warn("褰撳墠鐢ㄦ埛orgid涓虹┖锛岃烦杩囪嚜鍔ㄥ垵濮嬪寲");
+ return;
+ }
+
+ // 鑾峰彇鏂规硶鍙傛暟
+ Object[] args = joinPoint.getArgs();
+ if (args == null || args.length == 0) {
+ return;
+ }
+
+ // 鑾峰彇鏂规硶绛惧悕
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+ Method method = signature.getMethod();
+ String[] parameterNames = signature.getParameterNames();
+
+ // 鑾峰彇娉ㄨВ閰嶇疆鐨勫弬鏁板悕鍒楄〃
+ String[] configuredParamNames = autoInit.paramNames();
+ List<String> targetParamNames = configuredParamNames.length > 0
+ ? Arrays.asList(configuredParamNames)
+ : null;
+
+ // 閬嶅巻鎵�鏈夊弬鏁�
+ for (int i = 0; i < args.length; i++) {
+ Object arg = args[i];
+ if (arg == null) {
+ continue;
+ }
+
+ // 濡傛灉鎸囧畾浜嗗弬鏁板悕锛屾鏌ュ綋鍓嶅弬鏁版槸鍚﹀湪鎸囧畾鍒楄〃涓�
+ if (targetParamNames != null && !targetParamNames.contains(parameterNames[i])) {
+ continue;
+ }
+
+ // 灏濊瘯濉厖orgid
+ setOrgId(arg, orgid, autoInit.override());
+ }
+
+ } catch (Exception e) {
+ log.error("鑷姩濉厖orgid澶辫触", e);
+ }
+ }
+
+ /**
+ * 濉厖orgid鍒扮洰鏍囧璞�
+ *
+ * @param target 鐩爣瀵硅薄
+ * @param orgid 瑕佸~鍏呯殑orgid鍊�
+ * @param override 鏄惁瑕嗙洊宸叉湁鍊�
+ */
+ private void setOrgId(Object target, String orgid, boolean override) {
+ try {
+ Class<?> clazz = target.getClass();
+
+ // 灏濊瘯鑾峰彇getOrgid鏂规硶
+ Method getOrgidMethod = null;
+ try {
+ getOrgidMethod = clazz.getMethod("getOrgid");
+ } catch (NoSuchMethodException e) {
+ // 瀵硅薄娌℃湁getOrgid鏂规硶锛岃烦杩�
+ return;
+ }
+
+ // 妫�鏌ユ槸鍚﹂渶瑕佽鐩�
+ if (!override) {
+ Object existingOrgid = getOrgidMethod.invoke(target);
+ if (existingOrgid != null && StringUtils.isNotEmpty(existingOrgid.toString())) {
+ log.debug("瀵硅薄宸叉湁orgid鍊�: {}锛岃烦杩囧~鍏�", existingOrgid);
+ return;
+ }
+ }
+
+ // 鑾峰彇setOrgid鏂规硶骞跺~鍏呭��
+ Method setOrgidMethod = clazz.getMethod("setOrgid", String.class);
+ setOrgidMethod.invoke(target, orgid);
+
+ log.debug("鎴愬姛涓哄璞� {} 濉厖orgid: {}", clazz.getSimpleName(), orgid);
+
+ } catch (NoSuchMethodException e) {
+ // 瀵硅薄娌℃湁setOrgid鏂规硶锛岃烦杩�
+ log.debug("瀵硅薄 {} 娌℃湁setOrgid鏂规硶锛岃烦杩囧~鍏�", target.getClass().getSimpleName());
+ } catch (Exception e) {
+ log.error("濉厖orgid鏃跺彂鐢熷紓甯�: {}", e.getMessage());
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3