首页 > 其他分享 >56、商品上架---远程上架接口

56、商品上架---远程上架接口

时间:2023-01-25 15:12:05浏览次数:46  
标签:sku map Collectors 上架 56 List --- collect esModel

 @Override
    public void up(Long spuId) {
        List<SkuEsModel> upProducts = new ArrayList<>();

        //查询所有该spu的规格属性
        List<ProductAttrValueEntity> attrValueEntities = attrValueService.baseAttrListForSpu(spuId);
        List<Long> attrIds = attrValueEntities.stream().map(attr -> {
            return attr.getAttrId();
        }).collect(Collectors.toList());//把被用来检索的规格属性的id提取出来

        //获取所有可以被检索的属性的id
        List<Long> searchAttrIds = attrService.selectSearchAttrIds(attrIds);
        Set<Long> idSet = new HashSet<>(searchAttrIds);//创建成set

        List<SkuEsModel.Attrs> attrs1 = attrValueEntities.stream().filter(item -> {
            return idSet.contains(item.getAttrId());
        }).map(item -> {
            SkuEsModel.Attrs attrs = new SkuEsModel.Attrs();
            BeanUtils.copyProperties(item, attrs);

            return attrs;
        }).collect(Collectors.toList());


        //1、查出当前spuId对应的所有的sku信息,品牌的名字
        List<SkuInfoEntity> skus = skuInfoService.getSkusBySpuId(spuId);//所有的sku信息

        List<Long> skuIds = skus.stream().map(SkuInfoEntity::getSkuId).collect(Collectors.toList());
        //远程调用查询是否有库存(写在stram流外面,只需要调用一次即可)
        //远程调用可能会失败,但是因为这是一个事务,远程调用失败下面的步骤也要执行,所以我们trycatch一下
        Map<Long, Boolean> map = null;
        try {
            R skusHasStock = wareFeignService.getSkusHasStock(skuIds);
            List<SkuHasStockVo> skuHasStockVos = (List<SkuHasStockVo>) skusHasStock.get("data");

            //转换为map,方便我们根据skuId获取是否有库存
            map = skuHasStockVos.stream().collect(Collectors.toMap(SkuHasStockVo::getSkuId, SkuHasStockVo::getHasStock));
        }catch (Exception e){
            log.error("远程调用失败,原因:{}", e);
        }


        Map<Long, Boolean> finalMap = map;
        List<SkuEsModel> collect = skus.stream().map(sku -> {
            //组装需要的数据
            SkuEsModel esModel = new SkuEsModel();
            BeanUtils.copyProperties(sku, esModel);
            esModel.setSkuPrice(sku.getPrice());
            esModel.setSkuImg(sku.getSkuDefaultImg());
            esModel.setHotScore(0L);

            if (finalMap == null){
                esModel.setHasStock(true);//远程服务有问题,默认有库存
            }else {
                esModel.setHasStock(finalMap.get(sku.getSkuId()));//设置是否有库存

            }

            Long brandId = sku.getBrandId();
            BrandEntity brandEntity = brandService.getById(brandId);
            esModel.setBrandName(brandEntity.getName());
            esModel.setBrandImg(brandEntity.getLogo());
            esModel.setCatalogName(categoryService.getNameByCatalogId(esModel.getCatalogId()));

            esModel.setAttrs(attrs1);

            return esModel;
        }).collect(Collectors.toList());

        // TODO: 2023/1/23 将数据发送给es进行保存 gulimall-search
        R r = searchFeignService.productStatusUp(collect);
        if (r.getCode() == 0){//商品上架成功
            //修改商品的状态和更新事件
            this.baseMapper.updateProductStatus(spuId, ProductConstant.UPSTATUS.SPU_UP);
        }else {
            // TODO: 2023/1/25 重复调用;接口幂等性;重复机制
        }
    }

标签:sku,map,Collectors,上架,56,List,---,collect,esModel
From: https://www.cnblogs.com/morehair/p/17066976.html

相关文章