|
|
@@ -1,11 +1,20 @@
|
|
|
package com.lc.ibps.common.system.repository.impl;
|
|
|
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DatabaseMetaData;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
|
+import com.lc.ibps.base.framework.table.ICommonDao;
|
|
|
+import com.lc.ibps.base.web.context.ContextUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import com.lc.ibps.api.base.constants.CacheKeyConstants;
|
|
|
@@ -18,11 +27,12 @@ import com.lc.ibps.common.system.domain.News;
|
|
|
import com.lc.ibps.common.system.persistence.dao.NewsQueryDao;
|
|
|
import com.lc.ibps.common.system.persistence.entity.NewsPo;
|
|
|
import com.lc.ibps.common.system.repository.NewsRepository;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
/**
|
|
|
* ibps_news 仓库的实现类
|
|
|
*
|
|
|
- *<pre>
|
|
|
+ *<pre>
|
|
|
* 开发公司:广州流辰信息技术有限公司
|
|
|
* 开发人员:chunyan huang
|
|
|
* 邮箱地址:370653110@qq.com
|
|
|
@@ -31,10 +41,16 @@ import com.lc.ibps.common.system.repository.NewsRepository;
|
|
|
*/
|
|
|
@Repository
|
|
|
public class NewsRepositoryImpl extends AbstractRepository<String, NewsPo,News> implements NewsRepository{
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private NewsQueryDao newsQueryDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ICommonDao<?> commonDao;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DataSource dataSource;
|
|
|
+
|
|
|
public News newInstance() {
|
|
|
NewsPo po = new NewsPo();
|
|
|
News news = AppUtil.getBean(News.class);
|
|
|
@@ -46,23 +62,23 @@ public class NewsRepositoryImpl extends AbstractRepository<String, NewsPo,News>
|
|
|
News news = AppUtil.getBean(News.class);
|
|
|
news.setData(po);
|
|
|
return news;
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected IQueryDao<String, NewsPo> getQueryDao() {
|
|
|
return newsQueryDao;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public String getInternalCacheName() {
|
|
|
return CacheKeyConstants.Region.REGION_IBPS_SYS;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+ @Override
|
|
|
public List<NewsPo> getListByOrg(String depId) {
|
|
|
- List<NewsPo> newsPos = findByKey("getListByOrg", "getIdListByOrg", b().a("depId", depId).p());
|
|
|
- return newsPos;
|
|
|
- }
|
|
|
+ List<NewsPo> newsPos = findByKey("getListByOrg", "getIdListByOrg", b().a("depId", depId).p());
|
|
|
+ return newsPos;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public List<NewsPo> findByIdAndPublic(Map<String, String> groupMap, Page page) {
|
|
|
@@ -71,22 +87,126 @@ public class NewsRepositoryImpl extends AbstractRepository<String, NewsPo,News>
|
|
|
queryFilter.addParamsFilter(entry.getKey(), entry.getValue());
|
|
|
}
|
|
|
queryFilter.setPage(page);
|
|
|
- List<NewsPo> newsPos = queryByKey("findByIdAndPublic", "findIdsByIdAndPublic", queryFilter);
|
|
|
- return newsPos;
|
|
|
- }
|
|
|
+ List<NewsPo> newsPos = queryByKey("findByIdAndPublic", "findIdsByIdAndPublic", queryFilter);
|
|
|
+ return newsPos;
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
+ @Override
|
|
|
public List<NewsPo> findNotPublicNewsOutOfUser(String currentUserId) {
|
|
|
- List<NewsPo> newsPos = findByKey("findNotPublicNewsOutOfUser", "findIdsNotPublicNewsOutOfUser",
|
|
|
- b().a("userId", currentUserId).p());
|
|
|
- return newsPos;
|
|
|
+ List<NewsPo> newsPos = findByKey("findNotPublicNewsOutOfUser", "findIdsNotPublicNewsOutOfUser",
|
|
|
+ b().a("userId", currentUserId).p());
|
|
|
+ return newsPos;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<NewsPo> findByExpired(Date date) {
|
|
|
- List<NewsPo> newsPos = findByKey("findIdsByExpired", "findIdsByExpired",
|
|
|
+ List<NewsPo> newsPos = findByKey("findIdsByExpired", "findIdsByExpired",
|
|
|
b().a("curDate", date).p());
|
|
|
- return newsPos;
|
|
|
+ return newsPos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 t_ggfwb 表范围过滤公告列表
|
|
|
+ * 兼容旧系统:如果表不存在,直接返回原列表
|
|
|
+ */
|
|
|
+ public List<NewsPo> getScopeNews(List<NewsPo> originalList) {
|
|
|
+ // 1. 空值保护
|
|
|
+ if (CollectionUtils.isEmpty(originalList)) {
|
|
|
+ return originalList;
|
|
|
+ }
|
|
|
+ if (!isTableExists("t_ggfwb")) {
|
|
|
+ // 表不存在,直接返回,不执行后续逻辑
|
|
|
+ return originalList;
|
|
|
+ }
|
|
|
+ String currentUserId = ContextUtil.getCurrentUserId();
|
|
|
+ String safeUserId = "'" + currentUserId + "'";
|
|
|
+ List<String> allowedNewsIds = new ArrayList<>();
|
|
|
+ // 2. 尝试从 t_ggfwb 表获取范围数据
|
|
|
+ // 使用 try-catch 来兼容旧系统(表不存在的情况)
|
|
|
+ try {
|
|
|
+ // SQL:查询当前用户可见,或者全员可见('ALL')的公告ID
|
|
|
+ String sql = "SELECT DISTINCT gong_gao_id_ FROM t_ggfwb " +
|
|
|
+ "WHERE gong_gao_mu_biao_ = 'ALL' " +
|
|
|
+ "OR gong_gao_mu_biao_ = " + safeUserId;
|
|
|
+ // commonDao.query 返回 List<Map<String, Object>>
|
|
|
+ List<Map<String, Object>> dbList = (List<Map<String, Object>>) commonDao.query(sql, new Object[]{currentUserId});
|
|
|
+ if (!CollectionUtils.isEmpty(dbList)) {
|
|
|
+ for (Map<String, Object> map : dbList) {
|
|
|
+ Object val = map.get("gong_gao_id_");
|
|
|
+ if (val != null) {
|
|
|
+ allowedNewsIds.add(val.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 捕获异常,判断是否因为“表不存在”引起的
|
|
|
+ String errorMsg = e.getMessage();
|
|
|
+ return originalList;
|
|
|
+ }
|
|
|
+ // 3. 执行过滤
|
|
|
+ // “表存在但为空”时显示所有公告(后面需要屏蔽)
|
|
|
+ if (allowedNewsIds.isEmpty()) return originalList;
|
|
|
+ return originalList.stream()
|
|
|
+ .filter(news -> {
|
|
|
+ String newsId = news.getId(); // 假设 NewsPo 有 getId()
|
|
|
+ return newsId != null && allowedNewsIds.contains(newsId);
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isTableExists(String tableName) {
|
|
|
+ Connection connection = null;
|
|
|
+ try {
|
|
|
+ // 1. 从注入的 DataSource 获取连接
|
|
|
+ connection = dataSource.getConnection();
|
|
|
+
|
|
|
+ // 2. 获取数据库元数据
|
|
|
+ DatabaseMetaData metaData = connection.getMetaData();
|
|
|
+
|
|
|
+ // 3. 查询表信息
|
|
|
+ String[] types = {"TABLE"};
|
|
|
+ ResultSet rs = metaData.getTables(null, null, tableName, types);
|
|
|
+
|
|
|
+ // 4. 如果结果集有下一行,说明表存在
|
|
|
+ return rs.next();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果发生异常(如连接失败),默认认为表不存在
|
|
|
+ // log.warn("检查表是否存在时发生异常: " + e.getMessage());
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ // 5. 关闭连接,避免连接泄漏
|
|
|
+ if (connection != null) {
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略关闭连接的异常
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getScopeUserIdsByNewsId(String newsId) {
|
|
|
+ List<String> userIds = new ArrayList<>();
|
|
|
+ String safeNewsID = "'" + newsId + "'";
|
|
|
+ try {
|
|
|
+ String sql = "SELECT gong_gao_mu_biao_ FROM t_ggfwb WHERE gong_gao_id_ = " + safeNewsID;
|
|
|
+
|
|
|
+ List<Map<String, Object>> resultList = (List<Map<String, Object>>) commonDao.query(sql, new Object[]{newsId});
|
|
|
+
|
|
|
+ if (resultList != null && !resultList.isEmpty()) {
|
|
|
+ for (Map<String, Object> row : resultList) {
|
|
|
+ // 从 Map 中获取 "gong_gao_mu_biao_" 字段的值
|
|
|
+ Object val = row.get("gong_gao_mu_biao_");
|
|
|
+ if (val != null) {
|
|
|
+ userIds.add(String.valueOf(val));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return userIds;
|
|
|
}
|
|
|
-
|
|
|
}
|