From 7f5fbd9d2cc0751e748dca86ceadc6fcf54516d0 Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Tue, 27 Aug 2024 18:46:59 +0800 Subject: [PATCH] =?UTF-8?q?#=E6=9B=B2=E7=BA=BF=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/HistoryDeviceServiceImpl.java | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/hzims-service/equipment/src/main/java/com/hnac/hzims/history/service/impl/HistoryDeviceServiceImpl.java b/hzims-service/equipment/src/main/java/com/hnac/hzims/history/service/impl/HistoryDeviceServiceImpl.java index 0bada0e..0693b23 100644 --- a/hzims-service/equipment/src/main/java/com/hnac/hzims/history/service/impl/HistoryDeviceServiceImpl.java +++ b/hzims-service/equipment/src/main/java/com/hnac/hzims/history/service/impl/HistoryDeviceServiceImpl.java @@ -2,6 +2,7 @@ package com.hnac.hzims.history.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.hnac.hzims.common.logs.utils.StringUtils; import com.hnac.hzims.history.entity.HistoryPointRecordEntity; import com.hnac.hzims.history.service.IHistoryDeviceService; @@ -24,12 +25,14 @@ import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.DateUtil; import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.StringUtil; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.*; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.*; import java.util.stream.Collectors; /** @@ -39,6 +42,7 @@ import java.util.stream.Collectors; @AllArgsConstructor public class HistoryDeviceServiceImpl implements IHistoryDeviceService { + private final IHistoryPointRecordService pointRecordService; private final HzFacClient hzFacClient; @@ -47,6 +51,8 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { private final DeviceDataClient deviceDataClient; + private static final ExecutorService pool = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10), new ThreadFactoryBuilder().setNameFormat("history-point-save-%d").build() , new ThreadPoolExecutor.CallerRunsPolicy()); + /** * 项目设备列表 * @param projectId @@ -199,7 +205,7 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { return new ArrayList<>(); } // 异步线程保存常用查询 - CompletableFuture.runAsync(() -> this.commonUserSave(param)); + this.commonUserSave(param); // 返回结构 return result.getData().entrySet().stream().map(entry->{ @@ -310,6 +316,8 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { * 异步保存常用记录 * @param param */ + @Async + @Transactional private void commonUserSave(HistoryDataParamVo param) { // 当前用户Id Long userId = AuthUtil.getUserId(); @@ -320,16 +328,16 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { points.add(param.getFirstAttrId()); } if(StringUtils.isNotEmpty(param.getSecondAttrId())){ - points.add(param.getFirstAttrId()); + points.add(param.getSecondAttrId()); } if(StringUtils.isNotEmpty(param.getThirdlyAttrId())){ - points.add(param.getFirstAttrId()); + points.add(param.getThirdlyAttrId()); } if(StringUtils.isNotEmpty(param.getFourthAttrId())){ - points.add(param.getFirstAttrId()); + points.add(param.getFourthAttrId()); } if(StringUtils.isNotEmpty(param.getFifthAttrId())){ - points.add(param.getFirstAttrId()); + points.add(param.getFifthAttrId()); } // 查询历史保存数据 List records = pointRecordService.list(Wrappers.lambdaQuery() @@ -343,13 +351,13 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { entity.setStationId(param.getStationId()); entity.setStationName(param.getStationName()); entity.setHistoryPoint(point); - if("firstAttr".equals(point)){ + if(param.getFirstAttrId().equals(point)){ entity.setHistoryPointName(param.getFirstAttrName()); - }else if("secondAttr".equals(point)){ + }else if(param.getSecondAttrId().equals(point)){ entity.setHistoryPointName(param.getSecondAttrName()); - }else if("thirdlyAttr".equals(point)){ + }else if(param.getThirdlyAttrId().equals(point)){ entity.setHistoryPointName(param.getThirdlyAttrName()); - }else if("fourthAttr".equals(point)){ + }else if(param.getFourthAttrId().equals(point)){ entity.setHistoryPointName(param.getFourthAttrName()); }else{ entity.setHistoryPointName(param.getFifthAttrName()); @@ -361,6 +369,7 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService { if(records.stream().map(HistoryPointRecordEntity::getHistoryPoint).collect(Collectors.toList()).contains(point)){ Optional optional = records.stream().filter(o->o.getHistoryPoint().equals(point)).findFirst(); if(optional.isPresent()){ + entity.setId(optional.get().getId()); entity.setNumber(optional.get().getNumber() + 1); }else{ entity.setNumber(1);