4 changed files with 139 additions and 95 deletions
@ -0,0 +1,17 @@
|
||||
package com.logpm.report.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper; |
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.logpm.report.vo.ReportDevilerVO; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author: zqb |
||||
* @Date: 2024/6/21 |
||||
*/ |
||||
public interface ReportDataService { |
||||
|
||||
List<ReportDevilerVO> getReportDevilerVOList(IPage<ReportDevilerVO> page, Wrapper queryWrapper); |
||||
|
||||
} |
@ -0,0 +1,115 @@
|
||||
package com.logpm.report.service.impl; |
||||
|
||||
import cn.hutool.core.collection.CollUtil; |
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper; |
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.logpm.report.dto.DeliveryTrainLoadedScanDTO; |
||||
import com.logpm.report.mapper.ReportDeliverMapeer; |
||||
import com.logpm.report.service.ReportDataService; |
||||
import com.logpm.report.vo.ReportDevilerVO; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.jetbrains.annotations.NotNull; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.CompletableFuture; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @Author: zqb |
||||
* @Date: 2024/6/21 |
||||
*/ |
||||
@Service |
||||
@AllArgsConstructor |
||||
@Slf4j |
||||
public class ReportDataServiceImpl implements ReportDataService { |
||||
|
||||
private final ReportDeliverMapeer reportDeliverMapeer; |
||||
|
||||
@Override |
||||
public List<ReportDevilerVO> getReportDevilerVOList(IPage<ReportDevilerVO> page, Wrapper queryWrapper) { |
||||
// 查询数据
|
||||
List<ReportDevilerVO> deliveryTrainPage = reportDeliverMapeer.getDeliveryTrainPage(page, queryWrapper); |
||||
// 异步组装统计数据
|
||||
asyncBuildDeliveryTrainPage(deliveryTrainPage); |
||||
return deliveryTrainPage; |
||||
} |
||||
|
||||
private void asyncBuildDeliveryTrainPage(List<ReportDevilerVO> deliveryTrainPage) { |
||||
List<Long> ids = deliveryTrainPage.stream().map(ReportDevilerVO::getId).collect(Collectors.toList()); |
||||
// 异步查询扫描装车和异常扫描装车数据
|
||||
if (CollUtil.isNotEmpty(ids)) { |
||||
// 每5000个提交一次查询
|
||||
List<List<Long>> partitionedIds = CollUtil.split(ids, 5000); |
||||
List<CompletableFuture<List<DeliveryTrainLoadedScanDTO>>> futures = new ArrayList<>(); |
||||
for (List<Long> idBatch : partitionedIds) { |
||||
futures.add(getTrainLoadedScanFuture(idBatch, deliveryTrainPage)); |
||||
futures.add(getTrainLoadedScanInvnFuture(idBatch, deliveryTrainPage)); |
||||
} |
||||
try { |
||||
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); |
||||
allFutures.join(); |
||||
} catch (Exception e) { |
||||
log.error("系统异常:{}", e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private @NotNull CompletableFuture<List<DeliveryTrainLoadedScanDTO>> getTrainLoadedScanInvnFuture(List<Long> idBatch, List<ReportDevilerVO> deliveryTrainPage) { |
||||
CompletableFuture<List<DeliveryTrainLoadedScanDTO>> future = CompletableFuture.supplyAsync(() -> |
||||
reportDeliverMapeer.getDeliveryTrainLoadSacnInvnByIds(idBatch)); |
||||
future.thenAccept(list -> { |
||||
if (CollUtil.isNotEmpty(list)) { |
||||
// list 封装为map deliveryId为key
|
||||
Map<Long, DeliveryTrainLoadedScanDTO> loadedScanDTOMap = new HashMap<>(); |
||||
list.forEach(dto -> loadedScanDTOMap.put(dto.getDeliveryId(), dto)); |
||||
List<ReportDevilerVO> collect = deliveryTrainPage.stream().filter(reportDevilerVO -> idBatch.contains(reportDevilerVO.getId())).collect(Collectors.toList()); |
||||
for (ReportDevilerVO reportDevilerVO : collect) { |
||||
if (loadedScanDTOMap.containsKey(reportDevilerVO.getId())) { |
||||
DeliveryTrainLoadedScanDTO dto = loadedScanDTOMap.get(reportDevilerVO.getId()); |
||||
reportDevilerVO.setInvnLoadedNum(StrUtil.isNotEmpty(dto.getLoadedNum()) ? dto.getLoadedNum() : "0"); |
||||
reportDevilerVO.setInvnReNum(StrUtil.isNotEmpty(dto.getReNum()) ? dto.getReNum() : "0"); |
||||
} else { |
||||
reportDevilerVO.setInvnLoadedNum("0"); |
||||
reportDevilerVO.setInvnReNum("0"); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
return future; |
||||
} |
||||
|
||||
private @NotNull CompletableFuture<List<DeliveryTrainLoadedScanDTO>> getTrainLoadedScanFuture(List<Long> idBatch, List<ReportDevilerVO> deliveryTrainPage) { |
||||
CompletableFuture<List<DeliveryTrainLoadedScanDTO>> future = CompletableFuture.supplyAsync(() -> |
||||
reportDeliverMapeer.getDeliveryTrainLoadSacnByIds(idBatch)); |
||||
future.thenAccept(list -> { |
||||
if (CollUtil.isNotEmpty(list)) { |
||||
// list 封装为map deliveryId为key
|
||||
Map<Long, DeliveryTrainLoadedScanDTO> loadedScanDTOMap = new HashMap<>(); |
||||
list.forEach(dto -> loadedScanDTOMap.put(dto.getDeliveryId(), dto)); |
||||
List<ReportDevilerVO> collect = deliveryTrainPage.stream().filter(reportDevilerVO -> idBatch.contains(reportDevilerVO.getId())).collect(Collectors.toList()); |
||||
for (ReportDevilerVO reportDevilerVO : collect) { |
||||
if (loadedScanDTOMap.containsKey(reportDevilerVO.getId())) { |
||||
DeliveryTrainLoadedScanDTO dto = loadedScanDTOMap.get(reportDevilerVO.getId()); |
||||
reportDevilerVO.setLoadedNum(StrUtil.isNotEmpty(dto.getLoadedNum()) ? dto.getLoadedNum() : "0"); |
||||
reportDevilerVO.setReNum(StrUtil.isNotEmpty(dto.getReNum()) ? dto.getReNum() : "0"); |
||||
reportDevilerVO.setExLoadedNum(StrUtil.isNotEmpty(dto.getExLoadedNum()) ? dto.getExLoadedNum() : "0"); |
||||
reportDevilerVO.setExReNum(StrUtil.isNotEmpty(dto.getExReNum()) ? dto.getExReNum() : "0"); |
||||
} else { |
||||
reportDevilerVO.setLoadedNum("0"); |
||||
reportDevilerVO.setReNum("0"); |
||||
reportDevilerVO.setExLoadedNum("0"); |
||||
reportDevilerVO.setExReNum("0"); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
return future; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue