龚焕茏
2024-08-22 58c5c6e49c8b4d67cd9d1637beea4706f9b5ae83
ycl-server/src/main/java/com/ycl/platform/service/impl/YwPointServiceImpl.java
@@ -6,16 +6,13 @@
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.dataListener.CurrencyDataListener;
import com.ycl.platform.base.BaseSelect;
import com.ycl.platform.domain.entity.YwPeople;
import com.ycl.platform.domain.entity.YwPoint;
import com.ycl.platform.domain.entity.YwUnit;
import com.ycl.platform.domain.excel.PointExport;
import com.ycl.platform.domain.form.BatchEditPointForm;
import com.ycl.platform.domain.form.YwPointForm;
import com.ycl.platform.domain.query.YwPointQuery;
import com.ycl.platform.domain.vo.YwPointVO;
import com.ycl.platform.domain.excel.PointExport;
import com.ycl.platform.domain.excel.PointSelectHandler;
import com.ycl.platform.mapper.YwPeopleMapper;
import com.ycl.platform.mapper.YwPointMapper;
import com.ycl.platform.service.YwPointService;
@@ -23,6 +20,7 @@
import com.ycl.system.Result;
import com.ycl.system.mapper.SysDeptMapper;
import com.ycl.system.page.PageUtil;
import com.ycl.thread.PointImportCallable;
import com.ycl.utils.DateUtils;
import com.ycl.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;
@@ -32,12 +30,11 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -55,6 +52,14 @@
    private final YwUnitService unitService;
    private final YwPeopleMapper ywPeopleMapper;
    private static final ExecutorService executorService = new ThreadPoolExecutor(8,
            24,
            5000,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );
    /**
     * 添加
     * @param form
@@ -63,6 +68,9 @@
    @Override
    public Result add(YwPointForm form) {
        YwPoint entity = YwPointForm.getEntityByForm(form, null);
        Date now = new Date();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        if(baseMapper.insert(entity) > 0) {
            return Result.ok("添加成功");
        }
@@ -76,6 +84,9 @@
                .forEach(form -> {
                    YwPoint entity = YwPointForm.getEntityByForm(form, null);
                    entity.setStatus("未开始");
                    Date now = new Date();
                    entity.setCreateTime(now);
                    entity.setUpdateTime(now);
                    baseMapper.insert(entity);
                });
        return Result.ok("添加成功");
@@ -88,6 +99,7 @@
            new LambdaUpdateChainWrapper<>(baseMapper)
                    .in(YwPoint::getId, form.getIds())
                    .set(YwPoint::getUnitId, form.getUnitId())
                    .set(YwPoint::getUpdateTime, new Date())
                    .update();
        } else {
            new LambdaUpdateChainWrapper<>(baseMapper)
@@ -95,6 +107,7 @@
                    .set(YwPoint::getUnitId, form.getUnitId())
                    .set(YwPoint::getStartTime, null)
                    .set(YwPoint::getEndTime, null)
                    .set(YwPoint::getUpdateTime, new Date())
                    .update();
        }
        return Result.ok("修改成功");
@@ -110,7 +123,8 @@
        YwPoint entity = baseMapper.selectById(form.getId());
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        entity.setPointTag(form.getPointTag().stream().collect(Collectors.joining(",")));
        Date now = new Date();
        entity.setUpdateTime(now);
        baseMapper.updateById(entity);
        return Result.ok("修改成功");
    }
@@ -150,11 +164,6 @@
    public Result page(YwPointQuery query) {
        IPage<YwPointVO> page = PageUtil.getPage(query, YwPointVO.class);
        baseMapper.page(page, query);
        page.getRecords().stream().forEach(point -> {
            if (StringUtils.hasText(point.getPointTagString())) {
                point.setPointTags(point.getPointTagString().split(","));
            }
        });
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
@@ -168,9 +177,6 @@
        YwPoint entity = baseMapper.selectById(id);
        Assert.notNull(entity, "记录不存在");
        YwPointVO vo = YwPointVO.getVoByEntity(entity, null);
        if (StringUtils.hasText(entity.getPointTag())) {
            vo.setPointTags(entity.getPointTag().split(","));
        }
        if (Objects.nonNull(entity.getDeptId())) {
            vo.setDeptIds(sysDeptMapper.selectParents(entity.getDeptId()));
        }
@@ -208,11 +214,11 @@
                .eq(YwPoint::getUnitId, unitId)
                .like(YwPoint::getPointName, keyword)
                .list();
        List<BaseSelect> data = list.stream().map(item -> {
            BaseSelect baseSelect = new BaseSelect();
            baseSelect.setId(item.getId());
            baseSelect.setValue(item.getPointName());
            return baseSelect;
        List data = list.stream().map(item -> {
            Map map = new HashMap<String, Object>();
            map.put("id", item.getSerialNumber());
            map.put("value", item.getPointName());
            return map;
        }).collect(Collectors.toList());
        return Result.ok().data(data);
    }
@@ -222,6 +228,7 @@
        return new LambdaUpdateChainWrapper<>(baseMapper)
                .in(YwPoint::getSerialNumber, serialNumberList)
                .set(YwPoint::getRecovery, recovery)
                .set(YwPoint::getUpdateTime, new Date())
                .set(recovery == 0, YwPoint::getRecoveryTime, DateUtils.getNowDate())
                .update();
    }
@@ -235,16 +242,21 @@
    public void export(YwPointQuery query, HttpServletResponse response) throws IOException {
        // 导出数据
        List<PointExport> exportData = baseMapper.export(query);
        EasyExcel.write(response.getOutputStream(), PointExport.class)
                .sheet("点位更换运维单位")
                .doWrite(exportData);
    }
    @Override
    public Result importData(MultipartFile file, Integer unitId) throws IOException {
    public Result importData(MultipartFile file, Integer unitId, Date startTime, Date endTime) throws IOException {
        Consumer<List<PointExport>> consumer = (dataList) -> {
            this.updatePoint(dataList, unitId);
            try {
                this.updatePoint(dataList, unitId, startTime, endTime);
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        EasyExcel.read(file.getInputStream(), PointExport.class , new CurrencyDataListener(consumer)).headRowNumber(1).doReadAll();
        return Result.ok();
@@ -256,15 +268,72 @@
     * @param dataList
     * @param unitId
     */
    private void updatePoint(List<PointExport> dataList, Integer unitId) {
    @Transactional(rollbackFor = Exception.class)
    public void updatePoint(List<PointExport> dataList, Integer unitId, Date startTime, Date endTime) throws ExecutionException, InterruptedException {
        if (CollectionUtils.isEmpty(dataList)) {
            throw new RuntimeException("导入数据不能为空");
        }
        List<String> pointList = dataList.stream().map(PointExport::getSerialNumber).collect(Collectors.toList());
        new LambdaUpdateChainWrapper<>(baseMapper)
                .in(YwPoint::getSerialNumber, pointList)
                .set(YwPoint::getUnitId, unitId)
                .update();
        List<YwPoint> pointList = dataList.stream().map(item -> {
            YwPoint point = new YwPoint();
            point.setImportantCommandImageTag("是".equals(item.getImportantCommandImageTagString()));
            point.setProvinceTag("是".equals(item.getProvinceTagString()));
            if (Objects.nonNull(unitId)) {
                point.setUnitId(Long.valueOf(unitId));
            }
            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) {
                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);
                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;
            }
        }
        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("执行成功");
        }
    }
}