个人代码,自创循环套循环后输出Map数据结构的写法

@Override
public HttpCommandResultWithData getSalesDataByUsersAndTransId(SalesQueryLogDataByUserCommand command) {
    HttpCommandResultWithData result = new HttpCommandResultWithData().fillResult(ReturnCode.OK);
    // 入参校验
    List<String> userIds = command.getUserIds();
    if (CollectionUtils.isEmpty(userIds)) {
        throw new SellAppCustomFailException(ErrorMsgEnum.SELL_APP_SERVICE_PARAMS_ERROR_USERS_NOT_FIND);
    }
    List<String> transIds = command.getTransIds();
    if (CollectionUtils.isEmpty(transIds)) {
        throw new SellAppCustomFailException(ErrorMsgEnum.SELL_APP_SERVICE_PARAMS_ERROR_TRANS_ID_NOT_FIND);
    }
    // 获取redis数据方法
    Function<String, Map<String, String>> getDataByRedis = userId -> {
        Map<String, String> beanMap = new HashMap<>();
        transIds.forEach(transId -> {
            String jsonLogArrays = Optional.ofNullable(
                    (String) redisTemplate.opsForHash().get(RedisStaticConst.QINGQI_LOG_REDIS_KEY_PREX + transId, userId))
                    .orElseGet(String::new);
            List<RedisLogSimplePojo> list = new ArrayList();
            try {
                list = JsonUtil.toList(jsonLogArrays, RedisLogSimplePojo.class);
            } catch (IOException e) {
                logger.warn("=== getSalesDataByUsers 查询云端redis数据 云端redis hkey : {} key: {} 转换json数组异常", RedisStaticConst.QINGQI_LOG_REDIS_KEY_PREX + transId, userId);
            }
            beanMap.put(transId, String.valueOf(list.size()));
        });
        return beanMap;
    };
    // 返回结果
    AtomicInteger index = new AtomicInteger();
    Map<String, Map<String, String>> retMap =
            userIds.stream().collect(Collectors.toMap(key -> {
                String userId = userIds.get(index.get());
                index.incrementAndGet();
                return userId;
            }, getDataByRedis));
    result.setData(retMap);
    return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值