fuliqi
2024-09-03 461ec2d155d8dd35d8f73c35e7c2a33e040bf862
ycl-server/src/main/java/com/ycl/platform/service/impl/YwPointServiceImpl.java
@@ -213,6 +213,9 @@
        List<YwPoint> list = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(YwPoint::getUnitId, unitId)
                .like(YwPoint::getPointName, keyword)
                .or()
                .eq(YwPoint::getUnitId, unitId)
                .like(YwPoint::getSerialNumber, keyword)
                .list();
        List data = list.stream().map(item -> {
            Map map = new HashMap<String, Object>();
@@ -248,10 +251,10 @@
    }
    @Override
    public Result importData(MultipartFile file, Integer unitId, Date startTime, Date endTime) throws IOException {
    public Result importData(MultipartFile file, Integer unitId, Date startTime, Date endTime, Boolean needUpdateUnit) throws IOException {
        Consumer<List<PointExport>> consumer = (dataList) -> {
            try {
                this.updatePoint(dataList, unitId, startTime, endTime);
                this.updatePoint(dataList, unitId, startTime, endTime, needUpdateUnit);
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
@@ -269,7 +272,7 @@
     * @param unitId
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePoint(List<PointExport> dataList, Integer unitId, Date startTime, Date endTime) throws ExecutionException, InterruptedException {
    public void updatePoint(List<PointExport> dataList, Integer unitId, Date startTime, Date endTime, Boolean needUpdateUnit) throws ExecutionException, InterruptedException {
        if (CollectionUtils.isEmpty(dataList)) {
            throw new RuntimeException("导入数据不能为空");
        }
@@ -277,63 +280,42 @@
            YwPoint point = new YwPoint();
            point.setImportantCommandImageTag("是".equals(item.getImportantCommandImageTagString()));
            point.setProvinceTag("是".equals(item.getProvinceTagString()));
            if (Objects.nonNull(unitId)) {
                point.setUnitId(Long.valueOf(unitId));
            point.setDeptTag("是".equals(item.getDeptTag()));
            if (needUpdateUnit) {
                if (Objects.nonNull(unitId)) {
                    point.setUnitId(Long.valueOf(unitId));
                }
                point.setStartTime(startTime);
                point.setEndTime(endTime);
            } else {
                point.setUnitId(null);
                point.setStartTime(null);
                point.setEndTime(null);
            }
            point.setStartTime(startTime);
            point.setEndTime(endTime);
            point.setPointName(item.getPointName());
            point.setSerialNumber(item.getSerialNumber());
            return point;
        }).collect(Collectors.toList());
        this.waitAllFinishAndGetResult(pointList);
//        for (PointExport pointExport : dataList) {
//            YwPoint point = new YwPoint();
//            point.setImportantCommandImageTag("是".equals(pointExport.getImportantCommandImageTagString()));
//            point.setProvinceTag("是".equals(pointExport.getProvinceTagString()));
//            point.setUnitId(Long.valueOf(unitId));
//            point.setStartTime(startTime);
//            point.setEndTime(endTime);
//            point.setPointName(pointExport.getPointName());
//            point.setSerialNumber(pointExport.getSerialNumber());
//            this.baseMapper.updatePoint(point);
//        }
    }
    public void waitAllFinishAndGetResult(List<YwPoint> dataList) throws InterruptedException, ExecutionException {
        List<FutureTask<Boolean>> resultList = new ArrayList<>(512);
        List<Boolean> data = new ArrayList<>(512);
        int start = 0;
        Date startTime = new Date();
        while (true) {
            if (dataList.size() < start + 50) {
            if (dataList.size() < start + 800) {
                List<YwPoint> list = dataList.subList(start, dataList.size() - 1);
                Callable<Boolean> callable = new PointImportCallable(list, this.baseMapper);
                FutureTask<Boolean> futureTask = new FutureTask(callable);
                Thread thread = new Thread(futureTask);
                thread.start();
                // 不能直接调用Future的get方法,否则就变成串行执行了,失去多线程意义
                resultList.add(futureTask);
                this.baseMapper.updatePoint(list);
                break;
            } else {
                List<YwPoint> list = dataList.subList(start, start + 50);
                Callable<Boolean> callable = new PointImportCallable(list, this.baseMapper);
                FutureTask<Boolean> futureTask = new FutureTask(callable);
                Thread thread = new Thread(futureTask);
                thread.start();
                // 不能直接调用Future的get方法,否则就变成串行执行了,失去多线程意义
                resultList.add(futureTask);
                start += 50;
                List<YwPoint> list = dataList.subList(start, start + 800);
                this.baseMapper.updatePoint(list);
                start += 800;
            }
        }
        for (FutureTask<Boolean> futureTask : resultList) {
            data.add(futureTask.get());
        }
        Date endTime = new Date();
        log.error("总共耗时:" + (endTime.getTime() - startTime.getTime()) / 1000);
        if (data.stream().allMatch(item -> item == Boolean.TRUE)) {
            System.out.println("执行成功");
        }
        log.error("总共耗时:" + (endTime.getTime() - startTime.getTime()));
    }
}