From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期五, 01 十一月 2024 15:11:19 +0800
Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成"

---
 Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 118 insertions(+), 0 deletions(-)

diff --git a/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java b/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java
new file mode 100644
index 0000000..74c11bd
--- /dev/null
+++ b/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java
@@ -0,0 +1,118 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: Chill 搴勯獮 (smallchill@163.com)
+ */
+package org.springblade.core.tenant.dynamic;
+
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
+import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
+import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
+import lombok.AllArgsConstructor;
+import org.springblade.core.cache.utils.CacheUtil;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springframework.beans.BeanUtils;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.sql.DataSource;
+import java.util.Set;
+
+import static org.springblade.core.tenant.constant.TenantBaseConstant.*;
+
+/**
+ * 绉熸埛鏁版嵁婧愭牳蹇冨鐞嗙被
+ *
+ * @author Chill
+ */
+@AllArgsConstructor
+public class TenantDataSourceHolder {
+
+	private final DataSource dataSource;
+	private final DataSourceCreator dataSourceCreator;
+	private final JdbcTemplate jdbcTemplate;
+
+	/**
+	 * 鏁版嵁婧愮紦瀛樺鐞�
+	 *
+	 * @param tenantId 绉熸埛ID
+	 */
+	public void handleDataSource(String tenantId) {
+		// 鑾峰彇鍌ㄥ瓨鐨勬暟鎹簮闆嗗悎
+		DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
+		Set<String> keys = ds.getCurrentDataSources().keySet();
+		// 閰嶇疆涓嶅瓨鍦ㄥ垯鍔ㄦ�佹坊鍔犳暟鎹簮锛屼互鎳掑姞杞界殑妯″紡瑙e喅鍒嗗竷寮忓満鏅殑閰嶇疆鍚屾
+		// 涓轰簡淇濊瘉鏁版嵁瀹屾暣鎬э紝閰嶇疆鍚庣敓鎴愭暟鎹簮缂撳瓨锛屽悗鍙颁究鏃犳硶淇敼鏇存崲鏁版嵁婧愶紝鑻ヤ竴瀹氳淇敼璇疯縼绉绘暟鎹悗閲嶅惎鏈嶅姟鎴栬嚜琛屼慨鏀瑰簳灞傞�昏緫
+		if (!keys.contains(tenantId)) {
+			TenantDataSource tenantDataSource = getDataSource(tenantId);
+			if (tenantDataSource != null) {
+				// 鍒涘缓鏁版嵁婧愰厤缃�
+				DataSourceProperty dataSourceProperty = new DataSourceProperty();
+				// 鎷疯礉鏁版嵁婧愰厤缃�
+				BeanUtils.copyProperties(tenantDataSource, dataSourceProperty);
+				// 鍏抽棴鎳掑姞杞�
+				dataSourceProperty.setLazy(Boolean.FALSE);
+				// 鍒涘缓鍔ㄦ�佹暟鎹簮
+				DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
+				// 娣诲姞鏈�鏂版暟鎹簮
+				ds.addDataSource(tenantId, dataSource);
+			}
+		}
+	}
+
+	/**
+	 * 鍒ゆ柇绉熸埛鏄惁鏈夋暟鎹簮閰嶇疆
+	 *
+	 * @param tenantId 绉熸埛ID
+	 */
+	private Boolean existDataSource(String tenantId) {
+		// 灏嗙鎴锋槸鍚﹂厤缃暟鎹簮杩涜缂撳瓨锛岃嫢閲嶆柊閰嶇疆浼氬皢姝ょ紦瀛樻竻绌哄苟鍦ㄤ笅娆¤姹傜殑鏃跺�欐噿鍔犺浇
+		// 鑻ョ鎴锋病鏈夐厤缃暟鎹簮鍒欎細鑷姩浣跨敤master鏁版嵁婧愶紝姝や妇鏄负浜嗛伩鍏嶅湪娌℃湁鏁版嵁搴撶殑鏃跺�欓绻佹煡璇㈠鑷寸紦瀛樺嚮绌�
+		Boolean exist = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, Boolean.class, Boolean.FALSE);
+		if (exist == null) {
+			TenantDataSource tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_EXIST_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
+			if (tenantDataSource != null && StringUtil.isNotBlank(tenantDataSource.getDatasourceId())) {
+				exist = Boolean.TRUE;
+			} else {
+				exist = Boolean.FALSE;
+			}
+			CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, exist, Boolean.FALSE);
+		}
+		return exist;
+	}
+
+	/**
+	 * 鑾峰彇瀵瑰簲鐨勬暟鎹簮閰嶇疆
+	 *
+	 * @param tenantId 绉熸埛ID
+	 */
+	private TenantDataSource getDataSource(String tenantId) {
+		// 涓嶅瓨鍦ㄧ鎴锋暟鎹簮鍒欒繑鍥炵┖锛岄槻姝㈢紦瀛樺嚮绌�
+		if (!existDataSource(tenantId)) {
+			return null;
+		}
+		// 鑾峰彇绉熸埛鏁版嵁婧愪俊鎭�
+		TenantDataSource tenantDataSource = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, TenantDataSource.class, Boolean.FALSE);
+		if (tenantDataSource == null) {
+			tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_SINGLE_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
+			if (tenantDataSource != null && StringUtil.isNoneBlank(tenantDataSource.getTenantId(), tenantDataSource.getDriverClass(), tenantDataSource.getUrl(), tenantDataSource.getUsername(), tenantDataSource.getPassword())) {
+				CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, tenantDataSource, Boolean.FALSE);
+			} else {
+				tenantDataSource = null;
+			}
+		}
+		return tenantDataSource;
+	}
+
+}

--
Gitblit v1.9.3