瀏覽代碼

查询优化

zyg 1 年之前
父節點
當前提交
ef3756d86e

+ 2 - 2
hz-business/src/main/java/com/hz/business/scheduler/JobScheduler.java

@@ -153,7 +153,7 @@ public class JobScheduler {
      * 每日计算装机总量
      * 4小时一次
      */
-    //@Scheduled(cron = "0 0 */4 * * ? ")
+    @Scheduled(cron = "0 0 */4 * * ? ")
     public void calTotalElectricQuantity() {
         batteryService.calTotalElectricQuantity();
     }
@@ -162,7 +162,7 @@ public class JobScheduler {
      * 获取大屏实时数据
      * 5分钟一次
      */
-    //@Scheduled(cron = "0 0/5 * * * ?")
+    @Scheduled(cron = "0 0/5 * * * ?")
     @Async
     public void setRealTimeDataScreen(){
         log.info("当前时间{},开始同步实时数据!", DateUtils.getStringToday());

+ 50 - 31
hz-business/src/main/java/com/hz/business/service/impl/AlarmMsgServiceImpl.java

@@ -414,7 +414,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
         //标签筛选
         Set<Long> batteryIdSet = new HashSet<>();
         if (StringUtils.isNotBlank(param.getTag())) {
-            List<String> tags ;
+            List<String> tags;
             tags = Arrays.asList(param.getTag().split(","));
             Set<Long> tagIdSet = new HashSet<>();
             tags.forEach(o -> tagIdSet.add(Long.parseLong(o)));
@@ -459,8 +459,9 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
         }
         //故障名称筛选
         Set<String> codeSet = new HashSet<>();
+        if (!CollectionUtils.isEmpty(param.getAlarmName())) {
             List<Model> modelLists = modelMapper.selectList(new LambdaQueryWrapper<Model>()
-                    .in(!param.getAlarmName().isEmpty(),Model::getFaultCode, param.getAlarmName())
+                    .in(!param.getAlarmName().isEmpty(), Model::getFaultCode, param.getAlarmName())
                     .select(Model::getFaultCode)
                     .eq(Model::getIsDelete, 0));
             if (!CollectionUtils.isEmpty(modelLists)) {
@@ -469,11 +470,25 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
             if (codeSet.size() <= 0) {
                 return new PageInfo<>();
             }
+        }
+        List<String> snList = new ArrayList<>();
+        if (StringUtils.isNotBlank(param.getSnOrVin())) {
+            List<Battery> batteryList = batteryMapper.selectList(new LambdaQueryWrapper<Battery>()
+                    .and(StringUtils.isNotBlank(param.getSnOrVin()), p -> p.like(Battery::getSn, param.getSnOrVin())
+                            .or().like(Battery::getVin, param.getSnOrVin()))
+                    .eq(Battery::getIsDelete, 0)
+                    .select(Battery::getSn));
+            if (!CollectionUtils.isEmpty(batteryList)) {
+                snList = batteryList.stream().map(Battery::getSn).distinct().collect(Collectors.toList());
+            }
+            if (snList.size() <= 0) {
+                return new PageInfo<>();
+            }
+        }
 
         PageHelper.startPage(param.getPageIndex(), param.getPageSize());
         QueryWrapper<AlarmMsg> wrapper = new QueryWrapper<>();
-        wrapper.and(StringUtils.isNotBlank(param.getSnOrVin()), p -> p.like("sn", param.getSnOrVin())
-                .or().like("vin", param.getSnOrVin()));
+        wrapper.in(StringUtils.isNotBlank(param.getSnOrVin()), "sn", snList);
         wrapper.in(!param.getFaultLevel().isEmpty(), "alarm_level", param.getFaultLevel());
         wrapper.in(null != param.getDealStatus() && param.getDealStatus() == 2 && statusSet.size() > 0, "id", statusSet);
         wrapper.in(null != param.getDealStatus() && param.getDealStatus() == 1 && statusSet.size() > 0, "id", statusSet);
@@ -525,7 +540,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
             LambdaQueryWrapper<Battery> wp = new LambdaQueryWrapper<>();
             wp.in(Battery::getSn, snSet);
             wp.eq(Battery::getIsDelete, 0);
-            wp.select(Battery::getSn, Battery::getId);
+            wp.select(Battery::getSn, Battery::getId, Battery::getVin);
             batteryList = batteryMapper.selectList(wp);
         }
         if (!CollectionUtils.isEmpty(modelCodeSet)) {
@@ -648,6 +663,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
                 finalBatteryList.forEach(m -> {
                     if (o.getSn() != null && m.getSn().equals(o.getSn())) {
                         vo.setBatteryId(m.getId());
+                        vo.setVin(m.getVin());
                     }
                 });
             }
@@ -755,7 +771,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
     public List<MyBatteryFolderVo> getMyCollectDataAll(BatteryFolderParam param) {
         List<MyBatteryFolderVo> myBatteryFolderVoList = new ArrayList<>();
         LoginUser loginUser = SecurityUtils.getLoginUser();
-        List<Folder> folderList ;
+        List<Folder> folderList;
 
         folderList = folderMapper.selectList(new LambdaQueryWrapper<Folder>()
                 .eq(Folder::getFolderType, param.getType())
@@ -814,7 +830,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
 
         /*创建树*/
         MyBatteryFolderTree myBatteryFolderTree = new MyBatteryFolderTree(myBatteryFolderVoList);
-        List<MyBatteryFolderVo> myBatteryFolderVos ;
+        List<MyBatteryFolderVo> myBatteryFolderVos;
         myBatteryFolderVos = myBatteryFolderTree.buildTree();
 
         return myBatteryFolderVos;
@@ -940,14 +956,14 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
 
         Battery battery = batteryMapper.selectOne(new LambdaQueryWrapper<Battery>()
                 .eq(Battery::getIsDelete, 0)
-                .select(Battery::getPackModelCode, Battery::getCellModelCode)
+                .select(Battery::getPackModelCode, Battery::getCellModelCode, Battery::getVin)
                 .last(" limit 1 ")
                 .eq(Battery::getSn, alarmMsg.getSn()));
 
         String model = battery.getPackModelCode() + battery.getCellModelCode();
         Set<String> batteryModelSet = new HashSet<>();
         batteryModelSet.add(model);
-        List<BatteryModel> batteryModelList ;
+        List<BatteryModel> batteryModelList;
         if (!batteryModelSet.isEmpty()) {
             batteryModelList = batteryModelMapper.getModel(model);
             model = batteryModelList.get(0).getBatteryModel();
@@ -990,6 +1006,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
 
                 });
             }
+            vo.setVin(battery.getVin());
             vo.setBatteryModel(model);
         }
         return vo;
@@ -1299,8 +1316,8 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
      * @return com.hz.business.vo.FaultOverviewVo
      */
     @Override
-    @Cacheable(cacheNames = "query::faultOverviewCache",key = "#faultOverviewParam")
-    public FaultOverviewVo getFaultOverview(FaultOverviewParam faultOverviewParam){
+    @Cacheable(cacheNames = "query::faultOverviewCache", key = "#faultOverviewParam")
+    public FaultOverviewVo getFaultOverview(FaultOverviewParam faultOverviewParam) {
         FaultOverviewVo vo = new FaultOverviewVo();
         //改为合众车型查询
         List<String> carModelList1;
@@ -1331,13 +1348,13 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
         return vo;
     }
 
-    private void setFaultOverviewParam(FaultOverviewParam faultOverviewParam){
+    private void setFaultOverviewParam(FaultOverviewParam faultOverviewParam) {
         List<Integer> ids = faultOverviewParam.getBatteryModel();
-        if(!ids.isEmpty()){
+        if (!ids.isEmpty()) {
             List<BatteryModel> batteryModelList = batteryModelMapper.getBatteryModelListByIds(ids);
             List<String> cellList = new ArrayList<>();
             List<String> packList = new ArrayList<>();
-            batteryModelList.forEach(o->{
+            batteryModelList.forEach(o -> {
                 cellList.add(o.getCellModel());
                 packList.add(o.getPackModel());
             });
@@ -1348,6 +1365,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
 
     /**
      * 故障趋势图
+     *
      * @param faultOverviewParam
      * @return
      */
@@ -1434,7 +1452,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
 
         BigDecimal count = BigDecimal.ZERO;
         if (total > 0) {
-            BigDecimal result = BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(total),5,RoundingMode.UP);
+            BigDecimal result = BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(total), 5, RoundingMode.UP);
             count = result.multiply(new BigDecimal(10000));
         }
 
@@ -1453,7 +1471,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
      * @param target 参照
      * @return java.math.BigDecimal
      */
-    private BigDecimal calculatePercent(BigDecimal source, BigDecimal  target) {
+    private BigDecimal calculatePercent(BigDecimal source, BigDecimal target) {
         if (source == null || target == null) {
             return null;
         }
@@ -1546,7 +1564,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
     private void setSelectFaultCountVo(FaultOverviewVo vo, FaultOverviewParam param, List<String> carModelList) {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
         String carModel = !carModelList.isEmpty() ? "'" + String.join("','", carModelList) + "'" : null;
-        int count = alarmMsgMapper.faultCount(carModel, param.getPackModel(),param.getCellModel(), param.getFaultType(), sdf.format(new Date()));
+        int count = alarmMsgMapper.faultCount(carModel, param.getPackModel(), param.getCellModel(), param.getFaultType(), sdf.format(new Date()));
 
         BaseWindowDataVo baseWindowDataVo = new BaseWindowDataVo();
         baseWindowDataVo.setName("今日报警总数(条)");
@@ -1559,8 +1577,8 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
     /**
      * 故障趋势图
      *
-     * @param baseLineChartVo    目标vo
-     * @param param 查询参数
+     * @param baseLineChartVo 目标vo
+     * @param param           查询参数
      */
     private void setBaseLineChartVo(BaseLineChartVo baseLineChartVo, FaultOverviewParam param, List<String> carModelList) throws ParseException {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@@ -1652,7 +1670,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
      * @return com.hz.business.vo.HistoryFaultOverviewVo
      */
     @Override
-    @Cacheable(cacheNames = "query::historyFaultCache",key = "#root.methodName")
+    @Cacheable(cacheNames = "query::historyFaultCache", key = "#root.methodName")
     public HistoryFaultOverviewVo getHistoryFaultOverview() {
         HistoryFaultOverviewVo vo = new HistoryFaultOverviewVo();
         //历史故障总数(台)
@@ -1858,7 +1876,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
             }
             return listDate;
         } catch (Exception e) {
-            log.error("",e);
+            log.error("", e);
         }
         return listDate;
     }
@@ -1870,7 +1888,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
      * @return SingleHistoryOverviewVo
      */
     @Override
-    public SingleHistoryOverviewVo getSingleHistory(String sn,Integer pageIndex,Integer pageSize) {
+    public SingleHistoryOverviewVo getSingleHistory(String sn, Integer pageIndex, Integer pageSize) {
         SingleHistoryOverviewVo vo = new SingleHistoryOverviewVo();
         List<AlarmMsg> list = this.list(new LambdaQueryWrapper<AlarmMsg>()
                 .eq(AlarmMsg::getSn, sn)
@@ -1884,7 +1902,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
             //单个电池--故障类型分布图
             setSingleFaultTypeChart(vo, list);
             //单个电池--故障历史记录
-            setFaultHistoryList(vo, list,pageIndex,pageSize);
+            setFaultHistoryList(vo, list, pageIndex, pageSize);
         }
         return vo;
     }
@@ -1896,7 +1914,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
      * @param list 故障列表
      * @return void
      */
-    private void setFaultHistoryList(SingleHistoryOverviewVo vo, List<AlarmMsg> list,Integer pageIndex,Integer pageSize) {
+    private void setFaultHistoryList(SingleHistoryOverviewVo vo, List<AlarmMsg> list, Integer pageIndex, Integer pageSize) {
         List<AlarmMsgHistoryVo> faultHistoryList = new ArrayList<>();
         List<SysDictData> alarmTypeList = getDictDataByType(BUSINESS_ALARM_TYPE);
         List<SysDictData> faultStatusList = getDictDataByType("business_fault_status");
@@ -1925,26 +1943,27 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
             faultHistoryList.add(alarmMsgHistoryVo);
         });
         List<AlarmMsgHistoryVo> collect = faultHistoryList.stream().sorted(Comparator.comparing(AlarmMsgHistoryVo::getAlarmTime).reversed()).collect(Collectors.toList());
-        if(pageIndex!=null && pageSize!=null){
+        if (pageIndex != null && pageSize != null) {
             Page pages = getPages(pageIndex, pageSize, collect);
             vo.setFaultHistoryList(pages);
         }
     }
+
     private Page getPages(Integer currentPage, Integer pageSize, List list) {
         Page page = new Page();
-        if(list==null){
-            return  null;
+        if (list == null) {
+            return null;
         }
         int size = list.size();
 
-        if(pageSize > size) {
+        if (pageSize > size) {
             pageSize = size;
         }
-        if (pageSize!=0){
+        if (pageSize != 0) {
             // 求出最大页数,防止currentPage越界
             int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
 
-            if(currentPage > maxPage) {
+            if (currentPage > maxPage) {
                 currentPage = maxPage;
             }
         }
@@ -1954,7 +1973,7 @@ public class AlarmMsgServiceImpl extends ServiceImpl<AlarmMsgMapper, AlarmMsg> i
         List pageList = new ArrayList();
 
         // 将当前页的数据放进pageList
-        for(int i = 0; i < pageSize && curIdx + i < size; i++) {
+        for (int i = 0; i < pageSize && curIdx + i < size; i++) {
             pageList.add(list.get(curIdx + i));
         }