Redis-2-queryFormat()方法

查询排版状态方法

方法用于查询当前用户排版过的所有文件及其排版结果。它的主要功能是从 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 对象。这个对象包含了排版任务的相关信息,最终会作为排版结果的一部分返回给前端。信息包括:

  1. 排版状态 (status):表示当前排版任务的状态(如排版中、已完成、失败等);
  2. 文件类型 (type):排版后的文件类型(如 PDF、Word 等);
  3. 结果文件 ID (nodeId):排版生成的文件在系统中的唯一标识;
  4. 源文件 ID (sourceNodeId):原始文件的唯一标识;
  5. 文件名 (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());
    }
相关推荐
小小工匠10 小时前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
麦聪聊数据11 小时前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_11 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡11 小时前
【MySQL数据库】数据类型与表约束
数据库·mysql
ofoxcoding11 小时前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai
曹牧12 小时前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon12 小时前
SQL学习指南——视图
数据库·sql
活宝小娜12 小时前
mysql详细安装教程
数据库·mysql·adb
贤时间12 小时前
codex 助力oracle ebs 开发
数据库·oracle
秉承初心13 小时前
PostgreSQL 数据性能瓶颈突破实战
数据库·postgresql·oracle