聊聊Spring中事务传播机制

125 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情


@EqualsAndHashCode(callSuper = true)
@Slf4j
@Data
public class ReportUploadListener extends AnalysisEventListener<StoreReportLoadDTO> {

    private StoreReportLoadService storeReportLoadService;

    //记录导入失败的数据信息
    private List<String> errorDataList = new ArrayList<>();
    private List<StoreReportLoadDTO> storeReportLoadDTOS = new ArrayList<>();
    private List<ChannelStoreDTO> channelStoreDTOS = new ArrayList<>();

    public ReportUploadListener(StoreReportLoadService storeReportLoadService) {
        this.storeReportLoadService = storeReportLoadService;
    }

    @Override
    public void invoke(StoreReportLoadDTO storeReportLoadDTO, AnalysisContext analysisContext) {
        try {
            if (StringUtils.isNotBlank(storeReportLoadDTO.getType())) {
                storeReportLoadDTOS.add(storeReportLoadDTO);
            }
        } catch (Exception e) {
            errorDataList.add(e.getMessage());
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        Map<Long, ProductInfoPO> infoPOMap = storeReportLoadService.getAllProduct().stream().collect(Collectors.toMap(ProductInfoPO::getId, Function.identity()));
        storeReportLoadDTOS.forEach(storeReportLoadDTO -> {
            // 获取店铺id
            Long shopId = storeReportLoadService.getShopId(storeReportLoadDTO.getShopName(),storeReportLoadDTO.getChannel());
            if (shopId == null){
                shopId = storeReportLoadService.addShop(storeReportLoadDTO);
            }

            // 店铺产品关联
            ProductInfoPO productInfoPO = storeReportLoadService.getProductBySkuCode(storeReportLoadDTO.getSkuCode());

            storeReportLoadService.addStoreProduct(shopId,productInfoPO,infoPOMap.get(productInfoPO.getProductRoot()));
            String reportMonth = "2022-0%s";
            String replace = storeReportLoadDTO.getReportMonth().replace("月", "");
            reportMonth = String.format(reportMonth, replace);

            int sale = Integer.parseInt(storeReportLoadDTO.getSale());
            int fourSale = sale - (int) (sale * 0.25)- (int) (sale * 0.25)- (int) (sale * 0.25);
            StoreReportPO reportPO = StoreReportPO.builder()
                    .reportMonth(reportMonth)
                    .shopId(shopId)
                    .productId(productInfoPO.getId())
                    .productRootId(productInfoPO.getProductRoot())
                    .shopName(storeReportLoadDTO.getShopName())
                    .productCategory(infoPOMap.get(productInfoPO.getProductRoot()).getProductName())
                    .skuName(storeReportLoadDTO.getSkuName())
                    .skuCode(storeReportLoadDTO.getSkuCode())
                    .unitPrice(productInfoPO.getUnitPrice().toString())
                    .boxSpec(productInfoPO.getProductSpec())
                    .baseSales(sale)
                    .activitySales(0)
                    .totalSales(sale)
                    .oneWeight(25)
                    .oneSales((int) (sale * 0.25))
                    .twoWeight(25)
                    .twoSales((int) (sale * 0.25))
                    .threeWeight(25)
                    .threeSales((int) (sale * 0.25))
                    .fourWeight(25)
                    .fourSales(fourSale)
                    .build();
            storeReportLoadService.addReport(reportPO);
        });
    }


}