|
@@ -5,14 +5,12 @@ import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.ruoyi.common.annotation.Anonymous;
|
|
|
import com.ruoyi.common.constant.HttpStatus;
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.core.page.TableDataInfo;
|
|
|
-import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.core.text.Convert;
|
|
|
import com.ruoyi.common.exception.user.ProjectException;
|
|
|
import com.ruoyi.common.utils.*;
|
|
@@ -38,7 +36,6 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import java.io.File;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -73,6 +70,7 @@ public class CommunityArticleController extends BaseController {
|
|
|
|
|
|
@Autowired
|
|
|
private CommunityCircleAssociationMapper communityCircleAssociationMapper;
|
|
|
+
|
|
|
@Autowired
|
|
|
private CommunityClassCircleMapper communityClassCircleMapper;
|
|
|
|
|
@@ -121,14 +119,9 @@ public class CommunityArticleController extends BaseController {
|
|
|
@Autowired
|
|
|
private ICommunityArticleTagService communityArticleTagService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private RedisCache redisCache;
|
|
|
-
|
|
|
@Autowired
|
|
|
private CommunityReportController communityReportController;
|
|
|
|
|
|
- private static final String CACHE_PREFIX = "article:list:"; // 缓存前缀
|
|
|
- private static final Integer CACHE_EXPIRE_TIME = 30; // 缓存过期时间(分钟)
|
|
|
@Autowired
|
|
|
private CommunityUserLikeMapper communityUserLikeMapper;
|
|
|
@Autowired
|
|
@@ -149,92 +142,6 @@ public class CommunityArticleController extends BaseController {
|
|
|
@Autowired
|
|
|
private SysUserMapper sysUserMapper;
|
|
|
|
|
|
- /**
|
|
|
- * 生成唯一的缓存键(基于请求参数)
|
|
|
- */
|
|
|
- public String generateCacheKey(Long userId,
|
|
|
- CommunityArticle article,
|
|
|
- int pageNum,
|
|
|
- int pageSize,
|
|
|
- int searchType) {
|
|
|
- StringBuilder keyBuilder = new StringBuilder(CACHE_PREFIX);
|
|
|
-
|
|
|
- // 添加用户ID
|
|
|
- keyBuilder.append("userId=").append(userId != null ? userId : 0).append(":");
|
|
|
-
|
|
|
- // 添加文章查询条件(如果有)
|
|
|
- if (article != null) {
|
|
|
- if (article.getClassIds() != null && !article.getClassIds().isEmpty()) {
|
|
|
- // 直接使用逗号连接数字,避免使用String.valueOf()可能产生的"[1]"格式
|
|
|
- StringBuilder classIdsBuilder = new StringBuilder();
|
|
|
- boolean first = true;
|
|
|
- for (Long classId : article.getClassIds()) {
|
|
|
- if (!first) {
|
|
|
- classIdsBuilder.append(",");
|
|
|
- }
|
|
|
- classIdsBuilder.append(classId);
|
|
|
- first = false;
|
|
|
- }
|
|
|
- keyBuilder.append("classIds=").append(classIdsBuilder.toString()).append(":");
|
|
|
- }
|
|
|
- if (article.getId() != null) {
|
|
|
- keyBuilder.append("id=").append(article.getId()).append(":");
|
|
|
- }
|
|
|
- if (article.getCircleId() != null) {
|
|
|
- keyBuilder.append("circleId=").append(article.getCircleId()).append(":");
|
|
|
- }
|
|
|
- if (article.getTitle() != null) {
|
|
|
- keyBuilder.append("title=").append(article.getTitle()).append(":");
|
|
|
- }
|
|
|
- // 可以根据需要添加更多条件
|
|
|
- if (article.getUserId() != null) {
|
|
|
- keyBuilder.append("userId=").append(article.getUserId()).append(":");
|
|
|
- }
|
|
|
- if (article.getUserIds() != null) {
|
|
|
- keyBuilder.append("userIds=").append(article.getUserIds()).append(":");
|
|
|
- }
|
|
|
- if (article.getTags() != null) {
|
|
|
- keyBuilder.append("tags=").append(article.getTags()).append(":");
|
|
|
- }
|
|
|
- if (article.getArticleIds() != null) {
|
|
|
- keyBuilder.append("articleIds=").append(article.getArticleIds()).append(":");
|
|
|
- }
|
|
|
- if (article.getPageTagType() != null) {
|
|
|
- keyBuilder.append("pageTagType=").append(article.getPageTagType()).append(":");
|
|
|
- }
|
|
|
- if (article.getNoCollection() != null) {
|
|
|
- keyBuilder.append("noCollection=").append(article.getNoCollection()).append(":");
|
|
|
- }
|
|
|
- if (article.getIsDraft() != null) {
|
|
|
- keyBuilder.append("isDraft=").append(article.getIsDraft()).append(":");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 添加分页和搜索类型信息
|
|
|
- keyBuilder.append("page=").append(pageNum).append(":")
|
|
|
- .append("size=").append(pageSize).append(":")
|
|
|
- .append("searchType=").append(searchType);
|
|
|
-
|
|
|
- return keyBuilder.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 清除文章列表相关的所有缓存
|
|
|
- */
|
|
|
- private void clearArticleListCache() {
|
|
|
- try {
|
|
|
- // 获取所有以CACHE_PREFIX开头的缓存键
|
|
|
- Collection<String> keys = redisCache.keys(CACHE_PREFIX + "*");
|
|
|
- if (keys != null && !keys.isEmpty()) {
|
|
|
- // 删除所有匹配的缓存
|
|
|
- redisCache.deleteObject(keys);
|
|
|
- log.info("已清除{}个文章列表缓存", keys.size());
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("清除文章列表缓存失败", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取文章列表信息
|
|
|
*/
|
|
@@ -249,51 +156,14 @@ public class CommunityArticleController extends BaseController {
|
|
|
if (userId == null) {
|
|
|
userId = SecurityUtils.getUserId();
|
|
|
}
|
|
|
-
|
|
|
- // 生成缓存键
|
|
|
- String cacheKey = generateCacheKey(userId, communityArticle, pageNum, pageSize, searchType);
|
|
|
-
|
|
|
try {
|
|
|
- if (searchType != 3) {
|
|
|
-// 尝试从缓存中获取数据
|
|
|
- TableDataInfo cachedData = null;
|
|
|
- try {
|
|
|
- cachedData = redisCache.getCacheObject(cacheKey);
|
|
|
- if (cachedData != null) {
|
|
|
- log.info("从缓存中获取文章列表数据, key: {}", cacheKey);
|
|
|
- return cachedData;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- // 如果从缓存获取数据时出错,记录错误并继续从数据库获取
|
|
|
- log.error("从缓存获取数据失败: {}", e.getMessage(), e);
|
|
|
- // 清除可能损坏的缓存
|
|
|
- try {
|
|
|
- redisCache.deleteObject(cacheKey);
|
|
|
- log.info("已清除可能损坏的缓存: {}", cacheKey);
|
|
|
- } catch (Exception ex) {
|
|
|
- log.error("清除缓存失败: {}", ex.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 缓存中没有数据或获取缓存失败,从数据库查询
|
|
|
- log.info("从数据库中获取文章列表数据");
|
|
|
rspData = new TableDataInfo();
|
|
|
List<CommunityArticleVo> list = communityArticleService.selectCommunityArticleList(userId, rspData, communityArticle, pageNum, pageSize, searchType);
|
|
|
rspData.setCode(HttpStatus.SUCCESS);
|
|
|
rspData.setMsg("查询成功");
|
|
|
rspData.setRows(list);
|
|
|
-
|
|
|
- // 将查询结果存入缓存
|
|
|
- try {
|
|
|
- redisCache.setCacheObject(cacheKey, rspData, CACHE_EXPIRE_TIME, TimeUnit.MINUTES);
|
|
|
- log.info("文章列表数据已缓存, key: {}, 过期时间: {}分钟", cacheKey, CACHE_EXPIRE_TIME);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("缓存数据失败: {}", e.getMessage(), e);
|
|
|
- }
|
|
|
} catch (Exception e) {
|
|
|
- log.error("获取文章列表失败: {}", e.getMessage(), e);
|
|
|
+ log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
return rspData;
|
|
@@ -400,10 +270,6 @@ public class CommunityArticleController extends BaseController {
|
|
|
}
|
|
|
|
|
|
communityArticleService.insertCommunityArticle(communityArticle);
|
|
|
-
|
|
|
- // 清除文章列表缓存,确保新发布的文章能够立即显示
|
|
|
- clearArticleListCache();
|
|
|
- log.info("文章发布成功,已清除文章列表缓存");
|
|
|
} catch (Exception e) {
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
@@ -440,15 +306,12 @@ public class CommunityArticleController extends BaseController {
|
|
|
|
|
|
communityArticleService.editCommunityArticle(communityArticle);
|
|
|
|
|
|
- // 清除文章列表缓存,确保编辑后的文章能够立即显示
|
|
|
- clearArticleListCache();
|
|
|
- log.info("文章编辑成功,已清除文章列表缓存");
|
|
|
} catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
- return AjaxResult.success("文章编辑成功!");
|
|
|
+ return AjaxResult.success("文章发布成功!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -478,9 +341,6 @@ public class CommunityArticleController extends BaseController {
|
|
|
.set("update_by", SecurityUtils.getUserId())
|
|
|
.eq("article_id", id));
|
|
|
if (result1 && result2 >= 0) {
|
|
|
- // 清除文章列表缓存,确保删除的文章不再显示
|
|
|
- clearArticleListCache();
|
|
|
- log.info("文章删除成功,已清除文章列表缓存");
|
|
|
return success("删除成功!");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -918,6 +778,7 @@ public class CommunityArticleController extends BaseController {
|
|
|
communityCircle.setHeat(size);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
@@ -954,7 +815,7 @@ public class CommunityArticleController extends BaseController {
|
|
|
.and((wrapper) -> {
|
|
|
wrapper.ne("is_delete", 1).or().isNull("is_delete");
|
|
|
}));
|
|
|
- if (circle != null){
|
|
|
+ if (circle != null) {
|
|
|
return AjaxResult.success("圈子名称已经存在无法添加");
|
|
|
}
|
|
|
//增加圈子
|
|
@@ -1162,14 +1023,12 @@ public class CommunityArticleController extends BaseController {
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
return AjaxResult.success(communityUserInfoVos);
|
|
|
}
|
|
|
|
|
|
@ApiOperation("搜索黑名单")
|
|
|
@GetMapping("/userListBlock")
|
|
|
- public AjaxResult userListBlock(String SearchValue,Long userId,boolean isToken) {
|
|
|
+ public AjaxResult userListBlock(String SearchValue, Long userId, boolean isToken) {
|
|
|
|
|
|
int pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
|
|
|
int pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
|
|
@@ -1181,7 +1040,7 @@ public class CommunityArticleController extends BaseController {
|
|
|
|
|
|
List<CommunityUserInfoVo> communityUserInfoVos = null;
|
|
|
try {
|
|
|
- communityUserInfoVos = communityArticleService.selectBlockUserBySearchValue(SearchValue,userId,offset,pageSize);
|
|
|
+ communityUserInfoVos = communityArticleService.selectBlockUserBySearchValue(SearchValue, userId, offset, pageSize);
|
|
|
List<CommunityUserInfoVo> communityUserInfoVos_copy = new ArrayList<>(communityUserInfoVos);
|
|
|
for (CommunityUserInfoVo communityUserInfoVo : communityUserInfoVos_copy) {
|
|
|
communityUserInfoVo.setBlock(true);
|
|
@@ -1191,8 +1050,6 @@ public class CommunityArticleController extends BaseController {
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
return AjaxResult.success(communityUserInfoVos);
|
|
|
}
|
|
|
|
|
@@ -1453,7 +1310,6 @@ public class CommunityArticleController extends BaseController {
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
-
|
|
|
return AjaxResult.success("删除成功!");
|
|
|
}
|
|
|
|