Browse Source

优化获取设备名称性能

wukai 11 months ago
parent
commit
bb789b4807

+ 8 - 6
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/ApiController.java

@@ -383,7 +383,7 @@ public class ApiController extends BaseController {
         Date sd = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
         localDate = LocalDate.parse(end);
         Date ed = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
-        List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(sd,ed);
+        List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(sd, ed);
         try (FileInputStream inputStream = new FileInputStream(totalExcelTemplate); Workbook wb = new XSSFWorkbook(inputStream); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream())) {
             CreationHelper creationHelper = wb.getCreationHelper();
             CellStyle percentStyle = wb.createCellStyle();
@@ -442,6 +442,7 @@ public class ApiController extends BaseController {
         Long[] stopTime = new Long[7];
         Arrays.fill(stopTime, 0L);
         AtomicReference<Long> totalTime = new AtomicReference<>(0L);
+        Map<Long, TwinDevice> deviceMap = deviceService.deviceMap();
         for (Map.Entry<Integer, List<TwinCalcStop>> entry : stopDeviceGroup.entrySet()) {
             Integer stopType = entry.getKey();
             int pos = stopType - 1;
@@ -495,7 +496,7 @@ public class ApiController extends BaseController {
                     cells[j] = row.createCell(j);
                 }
 
-                cells[0].setCellValue(deviceService.deviceName(stop.getDeviceId()));
+                cells[0].setCellValue(deviceMap.get(stop.getDeviceId()).getDeviceName());
                 cells[1].setCellValue(stopStr[stop.getStopType() - 1]);
                 cells[2].setCellValue(stop.getStartTime());
                 cells[2].setCellStyle(timeStyle);
@@ -542,7 +543,7 @@ public class ApiController extends BaseController {
         } while (!s.isAfter(e));
 
         List<TwinCalcStop> stopList = list.stream().filter(stop -> stop.getStopType().equals(type)).collect(Collectors.toList());
-
+        Map<Long, TwinDevice> deviceMap = deviceService.deviceMap();
         Map<Date, List<TwinCalcStop>> stopDateGroup = stopList.stream().collect(Collectors.groupingBy(TwinCalcStop::getDataDate, LinkedHashMap::new, Collectors.toList()));
         //1停经片停机,2-CCD停机(相机号+断纱/故障),3-人工停机,4-断电停机,5-设备故障停机,6-落布米数达到停机,7-盘头剩余圈数达到停机
         String[] stopStr = {"断纱", "断纱", "人工停机", "断电停机", "设备故障停机", "下卷", "叫料"};
@@ -595,7 +596,7 @@ public class ApiController extends BaseController {
                     cs[0].setCellValue(entry.getKey());
                     cs[0].setCellStyle(dateStyle);
                     cs[1].setCellValue(st + "-" + et);
-                    cs[2].setCellValue(deviceService.deviceName(stop.getDeviceId()));
+                    cs[2].setCellValue(deviceMap.get(stop.getDeviceId()).getDeviceName());
                     cs[3].setCellValue(stopStr[stop.getStopType() - 1]);
                     cs[4].setCellValue(stop.getStartTime());
                     cs[4].setCellStyle(timeStyle);
@@ -636,6 +637,7 @@ public class ApiController extends BaseController {
         Date eTime = DateUtils.parseDate(end, DateUtils.YYYY_MM_DD_HH_MM_SS);
         List<TwinCalcAlarms> list = alarmsService.selectTwinCalcAlarmsListByDate(sTime, eTime);
         list.sort((Comparator.comparing(TwinCalcAlarms::getDeviceId)));
+        Map<Long, TwinDevice> deviceMap = deviceService.deviceMap();
         Map<Long, List<TwinCalcAlarms>> alarmsGroup = list.stream().collect(Collectors.groupingBy(TwinCalcAlarms::getDeviceId, LinkedHashMap::new, Collectors.toList()));
         String[] alarmStr = {"GB1伺服通讯故障", "GB2伺服通讯故障", "GB3伺服通讯故障", "GB4伺服通讯故障", "GB5伺服通讯故障", "牵引伺服通讯故障", "卷曲伺服通讯故障", "断纱故障", "电源故障", "联动未完全啮合", "米长到达", "GB1剩余圈数报警", "GB2剩余圈数报警", "GB3剩余圈数报警", "GB4剩余圈数报警", "GB5剩余圈数报警", "主轴变频掉线", "主轴变频故障", "GB1伺服故障", "GB2伺服故障", "GB3伺服故障", "GB4伺服故障", "GB5伺服故障", "牵引伺服故障", "卷曲伺服故障", "拍照自停", "CCD位置报警信息显示"};
         try (FileInputStream inputStream = new FileInputStream(alarmsExcelTemplate); Workbook wb = new XSSFWorkbook(inputStream); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream())) {
@@ -653,7 +655,7 @@ public class ApiController extends BaseController {
                 for (int j = 0; j < cells.length; j++) {
                     cells[j] = row.createCell(j);
                 }
-                cells[0].setCellValue(deviceService.deviceName(entry.getKey()));
+                cells[0].setCellValue(deviceMap.get(entry.getKey()).getDeviceName());
                 cells[1].setCellValue(alarms.size());
                 Map<Integer, Long> temp = alarms.stream().collect(Collectors.groupingBy(TwinCalcAlarms::getAlarmType, Collectors.counting()));
                 for (Integer v : temp.keySet()) {
@@ -671,7 +673,7 @@ public class ApiController extends BaseController {
                     cells[j] = row.createCell(j);
                 }
 
-                cells[0].setCellValue(deviceService.deviceName(alarms.getDeviceId()));
+                cells[0].setCellValue(deviceMap.get(alarms.getDeviceId()).getDeviceName());
                 cells[1].setCellValue(alarmStr[alarms.getAlarmType() - 1]);
                 cells[2].setCellValue(alarms.getStartTime());
                 cells[2].setCellStyle(timeStyle);

+ 9 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITwinDeviceService.java

@@ -3,6 +3,7 @@ package com.ruoyi.biz.service;
 import com.ruoyi.biz.domain.TwinDevice;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 设备管理Service接口
@@ -18,6 +19,7 @@ public interface ITwinDeviceService {
      * @return 设备管理
      */
     public TwinDevice selectTwinDeviceById(Long id);
+
     /**
      * 根据设备ID查询名称
      *
@@ -27,6 +29,13 @@ public interface ITwinDeviceService {
     String deviceName(Long id);
 
     /**
+     * 获取deviceMap
+     *
+     * @return map
+     */
+    Map<Long, TwinDevice> deviceMap();
+
+    /**
      * 查询设备管理列表
      *
      * @param twinDevice 设备管理

+ 12 - 3
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinDeviceServiceImpl.java

@@ -9,7 +9,6 @@ import com.ruoyi.common.utils.CacheUtils;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -44,8 +43,18 @@ public class TwinDeviceServiceImpl implements ITwinDeviceService {
      */
     @Override
     public String deviceName(Long id) {
+        return deviceMap().get(id).getDeviceName();
+    }
+
+    /**
+     * 获取deviceMap
+     *
+     * @return map
+     */
+    @Override
+    public Map<Long, TwinDevice> deviceMap() {
         Object d = CacheUtils.get(Constants.IOT_TOKEN, Constants.DEVICE_ALL);
-        Map<Long, TwinDevice> deviceMap = new HashMap<>(16);
+        Map<Long, TwinDevice> deviceMap;
         if (d == null) {
             List<TwinDevice> list = selectTwinDeviceList(new TwinDevice());
             deviceMap = list.stream().collect(Collectors.toMap(TwinDevice::getDeviceId, o -> o));
@@ -53,7 +62,7 @@ public class TwinDeviceServiceImpl implements ITwinDeviceService {
         } else {
             deviceMap = (Map<Long, TwinDevice>) d;
         }
-        return deviceMap.get(id).getDeviceName();
+        return deviceMap;
     }
 
     /**