|
|
@ -2,6 +2,7 @@ package com.techsor.datacenter.business.common.config; |
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
@ -12,10 +13,7 @@ import com.techsor.datacenter.business.common.Constants; |
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.*; |
|
|
import java.util.HashSet; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong; |
|
|
import java.util.concurrent.atomic.AtomicLong; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -41,25 +39,46 @@ public class DataSourceInterceptor implements HandlerInterceptor { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public long getTopCompanyId(String companyId) { |
|
|
public long getTopCompanyId(String companyId) { |
|
|
|
|
|
|
|
|
String sql="SELECT " |
|
|
if (StringUtils.isBlank(companyId)) { |
|
|
+ " bcom.id, bcom.parent_id parentId" |
|
|
throw new IllegalArgumentException("companyId不能为空"); |
|
|
+ " FROM data_center_aeon_admin.basic_company bcom " |
|
|
} |
|
|
+ " WHERE bcom.flag != 1 and bcom.id = " + companyId; |
|
|
|
|
|
|
|
|
return getTopCompanyIdInternal(Long.parseLong(companyId), new HashSet<>()); |
|
|
AtomicLong parentId = new AtomicLong(0); |
|
|
} |
|
|
AtomicLong id = new AtomicLong(0); |
|
|
|
|
|
jdbcTemplate.query(sql,rs -> { |
|
|
private long getTopCompanyIdInternal(Long companyId, Set<Long> visited) { |
|
|
parentId.set(rs.getLong("parentId")); |
|
|
|
|
|
id.set(rs.getLong("id")); |
|
|
// 防止循环引用
|
|
|
}); |
|
|
if (!visited.add(companyId)) { |
|
|
//Recursive logic
|
|
|
throw new IllegalStateException("检测到公司父子结构循环,companyId=" + companyId); |
|
|
if (1 == parentId.get() || -1 == parentId.get()) { |
|
|
} |
|
|
return id.get(); |
|
|
|
|
|
} else { |
|
|
String sql = """ |
|
|
return getTopCompanyId(parentId.get()+""); |
|
|
SELECT id, parent_id |
|
|
} |
|
|
FROM data_center_aeon_admin.basic_company |
|
|
} |
|
|
WHERE flag != 1 AND id = ? |
|
|
|
|
|
"""; |
|
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, companyId); |
|
|
|
|
|
|
|
|
|
|
|
if (result.isEmpty()) { |
|
|
|
|
|
throw new IllegalStateException("公司不存在,companyId=" + companyId); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> row = result.get(0); |
|
|
|
|
|
|
|
|
|
|
|
Long parentId = row.get("parent_id") == null |
|
|
|
|
|
? null |
|
|
|
|
|
: ((Number) row.get("parent_id")).longValue(); |
|
|
|
|
|
|
|
|
|
|
|
// 顶级企业
|
|
|
|
|
|
if (parentId == null || parentId == 1 || parentId == -1) { |
|
|
|
|
|
return companyId; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return getTopCompanyIdInternal(parentId, visited); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取所有的一级企业 |
|
|
* 获取所有的一级企业 |
|
|
|