查询排版状态方法
方法用于查询当前用户排版过的所有文件及其排版结果。它的主要功能是从 Redis 中获取用户的历史排版记录,并过滤掉不需要展示的状态(如已导出或已丢弃的文件),最终返回一个排版结果列表。
queryFormat :获取当前用户排版过的所有原文件 sourceNodeIds(根据userId) -- 获取该原文件的所有排版结果 resultNodeIds(根据sourceNodeId) -- 获取每个排版结果的详情itemJson(根据resultNodeId) -- 过滤
详细:从 Redis 的 format:user:{userId} 键中获取用户排版过的原文件 sourceNodeId 列表;遍历每个原文件,通过 format:source:{userId}:{sourceNodeId} 获取其对应的排版结果 resultNodeId;从 format:item:{resultNodeId} 中读取排版结果详情,并反序列化为 FormatResultItem 对象;最终返回过滤后的排版结果列表。
分析:
java
String itemJson = stringRedisTemplate.opsForValue().get(FORMAT_ITEM_KEY_PREFIX + resultNodeId);
FormatResultItem item = objectMapper.readValue(itemJson, FormatResultItem.class);
这行代码的作用是从 Redis 中获取的 JSON 字符串 itemJson 反序列化为 FormatResultItem 对象。这个对象包含了排版任务的相关信息,最终会作为排版结果的一部分返回给前端。信息包括:
- 排版状态 (status):表示当前排版任务的状态(如排版中、已完成、失败等);
- 文件类型 (type):排版后的文件类型(如 PDF、Word 等);
- 结果文件 ID (nodeId):排版生成的文件在系统中的唯一标识;
- 源文件 ID (sourceNodeId):原始文件的唯一标识;
- 文件名 (fileName):排版后文件的名称。
这些信息会被封装到 FormatResultItem 对象中,并通过 queryFormat 接口返回给前端,供用户查看排版任务的进度和结果。
1. FormatService中:queryFormat()
java
public List<FormatResultItem> queryFormat(QueryFormatRequest request) {
Long userId = SecurityUtils.getCurrentUserId();
// 1. 获取当前用户排版过的所有原文件 nodeId
String userKey = FORMAT_USER_KEY_PREFIX + userId;
Set<String> sourceNodeIds = stringRedisTemplate.opsForSet().members(userKey);
if (CollectionUtils.isEmpty(sourceNodeIds)) {
log.info("FormatService##queryFormat-1,当前用户没有排版过的文件,userId={},userKey={}", userId,userKey);
return new ArrayList<>();
}
// 2. 遍历每个原文件,获取其排版结果
List<FormatResultItem> resultItems = new ArrayList<>();
for (String sourceNodeId : sourceNodeIds) {
// 获取该原文件的所有排版结果 nodeId
String sourceKey = FORMAT_SOURCE_KEY_PREFIX + userId + ":" + sourceNodeId;
Set<String> resultNodeIds = stringRedisTemplate.opsForSet().members(sourceKey);
if (CollectionUtils.isEmpty(resultNodeIds)) {
log.info("FormatService##queryFormat-2,当前用户没有排版过的文件,userId={},sourceNodeId={},sourceKey={}", userId, sourceNodeId, sourceKey);
continue;
}
// 获取每个排版结果的详情
for (String resultNodeId : resultNodeIds) {
String itemKey = FORMAT_ITEM_KEY_PREFIX + resultNodeId;
String itemJson = stringRedisTemplate.opsForValue().get(itemKey);
if (StringUtils.hasText(itemJson)) {
try {
FormatResultItem item = objectMapper.readValue(itemJson, FormatResultItem.class);
resultItems.add(item);
} catch (JsonProcessingException e) {
log.error("解析排版结果项失败,resultNodeId={}, itemJson={}", resultNodeId, itemJson, e);
}
}
}
}
// 3. 过滤掉状态为2(导出)和3(丢弃)的数据
return resultItems.stream()
.filter(item -> !FormatStatus.shouldFilter(item.getStatus()))
.collect(Collectors.toList());
}