Jelajahi Sumber

修改逆变器文件生成业务逻辑

xusl 3 tahun lalu
induk
melakukan
ca756e687a

+ 1 - 0
ipfcst-common/ipfcst-common-data/src/main/java/com/jiayue/ipfcst/common/data/repository/InverterInfoRepository.java

@@ -21,4 +21,5 @@ public interface InverterInfoRepository extends BaseRepository<InverterInfo, Int
 
     InverterInfo findByName(String name);
 
+    List<InverterInfo> findAllByStationCode(String stationCode);
 }

+ 153 - 133
ipfcst-console/src/main/java/com/jiayue/ipfcst/fileupload/service/E63UploadFileService.java

@@ -1,6 +1,7 @@
 package com.jiayue.ipfcst.fileupload.service;
 
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
 import com.jiayue.ipfcst.common.core.exception.BusinessException;
 import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
 import com.jiayue.ipfcst.common.data.abst.equipmentinfo.AbstractEquipmentInfo;
@@ -477,10 +478,10 @@ public class E63UploadFileService extends BaseUploadFileService {
         String time = qxzMap.get("time");
         long timestamp = new Date().getTime();
         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        try{
+        try {
           Date d = sf.parse(time);// 日期转换为时间戳
           timestamp = d.getTime();
-        }catch (Exception e){
+        } catch (Exception e) {
           log.info("日期转换异常");
           e.printStackTrace();
         }
@@ -609,15 +610,15 @@ public class E63UploadFileService extends BaseUploadFileService {
         String time = cftMap.get("time");
         long timestamp = new Date().getTime();
         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        try{
+        try {
           Date d = sf.parse(time);// 日期转换为时间戳
           timestamp = d.getTime();
-        }catch (Exception e){
+        } catch (Exception e) {
           log.info("日期转换异常");
           e.printStackTrace();
         }
         if (StrUtil.isNotBlank(time)) {
-          if (timestamp>= startTime && timestamp <= endTime) {
+          if (timestamp >= startTime && timestamp <= endTime) {
             // 从缓存里赋值
             ws10 = cftMap.get("wsInst10") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst10"));
             wd10 = cftMap.get("wdInst10") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst10"));
@@ -948,10 +949,10 @@ public class E63UploadFileService extends BaseUploadFileService {
           String time = fjMap.get("time");
           long timestamp = new Date().getTime();
           SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-          try{
+          try {
             Date d = sf.parse(time);// 日期转换为时间戳
             timestamp = d.getTime();
-          }catch (Exception e){
+          } catch (Exception e) {
             log.info("日期转换异常");
             e.printStackTrace();
           }
@@ -1091,151 +1092,170 @@ public class E63UploadFileService extends BaseUploadFileService {
    */
   private void generateNbqFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
     // 获取逆变器
-    List<InverterInfo> inverterInfoList = inverterInfoRepository.findAll();
+    List<InverterInfo> inverterInfoList = inverterInfoRepository.findAllByStationCode(electricFieldInfo.getStationCode());
     // 过滤上报的逆变器
     List<InverterInfo> filterInverterInfoList = inverterInfoList.stream().filter(AbstractEquipmentInfo::getReport).collect(Collectors.toList());
     if (filterInverterInfoList.size() > 0) {
-      List<Map<String, String>> dtaInverterList = new ArrayList<>();
+      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+      long timestamp = new Date().getTime();
+      List<InverterStatusData> redisList = new ArrayList<>();
       for (InverterInfo inverterInfo : filterInverterInfoList) {
-        Map<String, String> map = new HashMap<>();
-        //场站名称
-        map.put("czmc", electricFieldInfo.getName());
-        // 名称
-        map.put("name", inverterInfo.getName());
-        //集电线名称
-        map.put("CollectorCircuit", inverterInfo.getCollectorCircuit());
-        //额定容量
-        map.put("RatedCapacity", div(inverterInfo.getCapacity(), new BigDecimal("1000"), 2).toString());
-        // 开机容量
-        map.put("nbqCapacity", inverterInfo.getCapacity().toString());
-        //型号
-        map.put("modelNumber", inverterInfo.getModelNumber());
-        // 是否样板
-        map.put("sample", inverterInfo.getSample() ? "1" : "0");
-        //是否组串
-        map.put("IsString", inverterInfo.getGroupSeries() ? "1" : "0");
+        // 获取redis
         String nbqId = String.valueOf(inverterInfo.getId());
         Map<String, String> nbqMap = redisUtils.hgetall("nbq-" + electricFieldInfo.getStationCode() + "-" + nbqId);
-        DecimalFormat df = new DecimalFormat("0.00");
-        // 状态
-        String status = "1";
-        // 有功
-        BigDecimal activePower = new BigDecimal("0");
-        // 无功
-        BigDecimal reactivePower = new BigDecimal("0");
-        // 功率因数
-        BigDecimal powerFactor = new BigDecimal("0");
-        // 电压
-        BigDecimal voltage = new BigDecimal("0");
-        // 电流
-        BigDecimal electricalCurrent = new BigDecimal("0");
-        // 当日发电量
-        BigDecimal dayElectricQuantity = new BigDecimal("0");
-        // 累积发电量
-        BigDecimal cumulativeGeneratedEnergy = new BigDecimal("0");
-        boolean isUseRedis = false;
-
         if (!nbqMap.isEmpty()) {
           String time = nbqMap.get("time");
-          long timestamp = new Date().getTime();
-          SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-          try{
-            Date d = sf.parse(time);// 日期转换为时间戳
-             timestamp = d.getTime();
-          }catch (Exception e){
-            log.info("日期转换异常");
-            e.printStackTrace();
-          }
-
           if (StrUtil.isNotBlank(time)) {
+            try {
+              Date d = sf.parse(time);// 日期转换为时间戳
+              timestamp = d.getTime();
+            } catch (Exception e) {
+              log.info("日期转换异常");
+              e.printStackTrace();
+            }
             if (timestamp >= startTime && timestamp <= endTime) {
-              if (nbqMap.get("status") != null) {
-                status = nbqMap.get("status");
-              }
-              if (nbqMap.get("activePower") != null) {
-                activePower = new BigDecimal(nbqMap.get("activePower"));
-              }
-              if (nbqMap.get("reactivePower") != null) {
-                reactivePower = new BigDecimal(nbqMap.get("reactivePower"));
-              }
-              if (nbqMap.get("powerFactor") != null) {
-                powerFactor = new BigDecimal(nbqMap.get("powerFactor"));
-              }
-              if (nbqMap.get("voltage") != null) {
-                voltage = new BigDecimal(nbqMap.get("voltage"));
-              }
-              if (nbqMap.get("electricalCurrent") != null) {
-                electricalCurrent = new BigDecimal(nbqMap.get("electricalCurrent"));
-              }
-              if (nbqMap.get("dayElectricQuantity") != null) {
-                dayElectricQuantity = new BigDecimal(nbqMap.get("dayElectricQuantity"));
-              }
-              if (nbqMap.get("cumulativeGeneratedEnergy") != null) {
-                cumulativeGeneratedEnergy = new BigDecimal(nbqMap.get("cumulativeGeneratedEnergy"));
-              }
-              isUseRedis = true;
-              log.info(electricFieldInfo.getStationCode() + "生成逆变器数据用缓存");
+              // 将有效的reids内容存入list
+              InverterStatusData inverterStatusData = JSON.parseObject(JSON.toJSONString(nbqMap), InverterStatusData.class);
+              redisList.add(inverterStatusData);
             }
           }
         }
-
-        if (!isUseRedis) {
-          List<InverterStatusData> inverterStatusDataList = inverterStatusDataRepository.findByTimeBetweenAndEquipmentId(new Date(startTime), new Date(endTime), nbqId);
-          if (!inverterStatusDataList.isEmpty()) {
-            inverterStatusDataList.stream().sorted(Comparator.comparing(InverterStatusData::getTime).reversed()).collect(Collectors.toList());
-            InverterStatusData inverterStatusData = inverterStatusDataList.get(0);
-            if (inverterStatusData.getStatus() != null) {
-              status = String.valueOf(inverterStatusData.getStatus());
-            }
+      }
+      List<Map<String, String>> dtaInverterList = new ArrayList<>();
+      if (redisList.size() == filterInverterInfoList.size() && redisList.size() > 0) {
+        // redis里有全部的数据
+        for (InverterInfo inverterInfo : filterInverterInfoList) {
+          Map<String, String> map = new HashMap<>();
+          //场站名称
+          map.put("czmc", electricFieldInfo.getName());
+          // 名称
+          map.put("name", inverterInfo.getName());
+          //集电线名称
+          map.put("CollectorCircuit", inverterInfo.getCollectorCircuit());
+          //额定容量
+          map.put("RatedCapacity", div(inverterInfo.getCapacity(), new BigDecimal("1000"), 2).toString());
+          // 开机容量
+          map.put("nbqCapacity", inverterInfo.getCapacity().toString());
+          //型号
+          map.put("modelNumber", inverterInfo.getModelNumber());
+          // 是否样板
+          map.put("sample", inverterInfo.getSample() ? "1" : "0");
+          //是否组串
+          map.put("IsString", inverterInfo.getGroupSeries() ? "1" : "0");
+          List<InverterStatusData> redisFilterList = redisList.stream().filter(s -> s.getId().equals(inverterInfo.getId())).collect(Collectors.toList());
+          InverterStatusData redisValue = redisFilterList.get(0);
+          // 状态
+          String status = String.valueOf(redisValue.getStatus());
+          // 有功
+          BigDecimal activePower = redisValue.getActivePower();
+          // 无功
+          BigDecimal reactivePower = redisValue.getReactivePower();
+          // 功率因数
+          BigDecimal powerFactor = redisValue.getPowerFactor();
+          // 电压
+          BigDecimal voltage = redisValue.getVoltage();
+          // 电流
+          BigDecimal electricalCurrent = redisValue.getElectricalCurrent();
+          // 当日发电量
+          BigDecimal dayElectricQuantity = redisValue.getDayElectricQuantity();
+          // 累积发电量
+          BigDecimal cumulativeGeneratedEnergy = redisValue.getCumulativeGeneratedEnergy();
+          DecimalFormat df = new DecimalFormat("0.00");
+          map.put("status", status);
+          // 有功
+          map.put("NbqActivePower", div(activePower, new BigDecimal("1000"), 2).toString());
+          // 无功
+          map.put("NbqReActivePower", div(reactivePower, new BigDecimal("1000"), 2).toString());
+          // 电压
+          map.put("Voltage", df.format(voltage));
+          // 电流
+          map.put("GalvanicCurrent", df.format(electricalCurrent));
+          // 功率因数
+          map.put("PowerFactor", df.format(powerFactor));
+          // 日发电量
+          map.put("NbqDailyOutPut", df.format(dayElectricQuantity));
+          // 累计发电量
+          map.put("GeneratingCap", df.format(cumulativeGeneratedEnergy));
+          dtaInverterList.add(map);
+        }
+      } else {
+        // 取库里的数据
+        List<InverterStatusData> inverterStatusDataList = inverterStatusDataRepository.findByTimeBetween(new Date(startTime), new Date(endTime));
+        for (InverterInfo inverterInfo : filterInverterInfoList) {
+          // 状态
+          String status = "1";
+          // 有功
+          BigDecimal activePower = new BigDecimal("0");
+          // 无功
+          BigDecimal reactivePower = new BigDecimal("0");
+          // 功率因数
+          BigDecimal powerFactor = new BigDecimal("0");
+          // 电压
+          BigDecimal voltage = new BigDecimal("0");
+          // 电流
+          BigDecimal electricalCurrent = new BigDecimal("0");
+          // 当日发电量
+          BigDecimal dayElectricQuantity = new BigDecimal("0");
+          // 累积发电量
+          BigDecimal cumulativeGeneratedEnergy = new BigDecimal("0");
+          Map<String, String> map = new HashMap<>();
+          //场站名称
+          map.put("czmc", electricFieldInfo.getName());
+          // 名称
+          map.put("name", inverterInfo.getName());
+          //集电线名称
+          map.put("CollectorCircuit", inverterInfo.getCollectorCircuit());
+          //额定容量
+          map.put("RatedCapacity", div(inverterInfo.getCapacity(), new BigDecimal("1000"), 2).toString());
+          // 开机容量
+          map.put("nbqCapacity", inverterInfo.getCapacity().toString());
+          //型号
+          map.put("modelNumber", inverterInfo.getModelNumber());
+          // 是否样板
+          map.put("sample", inverterInfo.getSample() ? "1" : "0");
+          //是否组串
+          map.put("IsString", inverterInfo.getGroupSeries() ? "1" : "0");
+
+          List<InverterStatusData> filterList = inverterStatusDataList.stream().filter(s -> s.getId().equals(inverterInfo.getId())).collect(Collectors.toList());
+          if (filterList.size() > 0) {
+            // 用数据库
+            filterList.sort(Comparator.comparing(InverterStatusData::getTime).reversed());
+            InverterStatusData inverterStatusData = filterList.get(0);
+            // 状态
+            status = String.valueOf(inverterStatusData.getStatus());
             // 有功
-            if (inverterStatusData.getActivePower() != null) {
-              activePower = inverterStatusData.getActivePower();
-            }
+            activePower = inverterStatusData.getActivePower();
             // 无功
-            if (inverterStatusData.getReactivePower() != null) {
-              reactivePower = inverterStatusData.getReactivePower();
-            }
+            reactivePower = inverterStatusData.getReactivePower();
+            // 功率因数
+            powerFactor = inverterStatusData.getPowerFactor();
             // 电压
-            if (inverterStatusData.getVoltage() != null) {
-              voltage = inverterStatusData.getVoltage();
-            }
+            voltage = inverterStatusData.getVoltage();
             // 电流
-            if (inverterStatusData.getElectricalCurrent() != null) {
-              electricalCurrent = inverterStatusData.getElectricalCurrent();
-            }
-            // 功率因数
-            if (inverterStatusData.getPowerFactor() != null) {
-              powerFactor = inverterStatusData.getPowerFactor();
-            }
-            // 日发电量
-            if (inverterStatusData.getDayElectricQuantity() != null) {
-              dayElectricQuantity = inverterStatusData.getDayElectricQuantity();
-            }
-            // 累计发电量
-            if (inverterStatusData.getCumulativeGeneratedEnergy() != null) {
-              cumulativeGeneratedEnergy = inverterStatusData.getCumulativeGeneratedEnergy();
-            }
-            log.info(electricFieldInfo.getStationCode() + "生成逆变器数据用数据表");
-          } else {
-            log.info(electricFieldInfo.getStationCode() + "生成逆变器数据缓存和数据表都没有值,使用默认0");
+            electricalCurrent = inverterStatusData.getElectricalCurrent();
+            // 当日发电量
+            dayElectricQuantity = inverterStatusData.getDayElectricQuantity();
+            // 累积发电量
+            cumulativeGeneratedEnergy = inverterStatusData.getCumulativeGeneratedEnergy();
           }
+          DecimalFormat df = new DecimalFormat("0.00");
+          map.put("status", status);
+          // 有功
+          map.put("NbqActivePower", div(activePower, new BigDecimal("1000"), 2).toString());
+          // 无功
+          map.put("NbqReActivePower", div(reactivePower, new BigDecimal("1000"), 2).toString());
+          // 电压
+          map.put("Voltage", df.format(voltage));
+          // 电流
+          map.put("GalvanicCurrent", df.format(electricalCurrent));
+          // 功率因数
+          map.put("PowerFactor", df.format(powerFactor));
+          // 日发电量
+          map.put("NbqDailyOutPut", df.format(dayElectricQuantity));
+          // 累计发电量
+          map.put("GeneratingCap", df.format(cumulativeGeneratedEnergy));
+          dtaInverterList.add(map);
         }
-        map.put("status", status);
-        // 有功
-        map.put("NbqActivePower", div(activePower, new BigDecimal("1000"), 2).toString());
-        // 无功
-        map.put("NbqReActivePower", div(reactivePower, new BigDecimal("1000"), 2).toString());
-        // 电压
-        map.put("Voltage", df.format(voltage));
-        // 电流
-        map.put("GalvanicCurrent", df.format(electricalCurrent));
-        // 功率因数
-        map.put("PowerFactor", df.format(powerFactor));
-        // 日发电量
-        map.put("NbqDailyOutPut", df.format(dayElectricQuantity));
-        // 累计发电量
-        map.put("GeneratingCap", df.format(cumulativeGeneratedEnergy));
-        dtaInverterList.add(map);
       }
       // 创建上报文件
       File file = super.createTempFile(fileName);