Browse Source

:sparkles: 内河低压分析月度报表计算范围以及逻辑调整

lag 1 year ago
parent
commit
b676927fd7

+ 6 - 6
src/main/java/com/shcd/entity/SpStatementInlandLow.java

@@ -30,7 +30,7 @@ public class SpStatementInlandLow implements Serializable {
      * 使用岸电船舶艘次-当月
      */
     @TableField(value = "BOAT_USE_NUMBER_MONTH")
-    private BigDecimal boatUseNumberMonth;
+    private BigDecimal boatUseNumberMonth = BigDecimal.ZERO;
 
     /**
      * 使用岸电船舶艘次-环比
@@ -42,31 +42,31 @@ public class SpStatementInlandLow implements Serializable {
      * 使用岸电船舶艘次-当年累计
      */
     @TableField(value = "BOAT_USE_NUMBER_YEAR_TOTAL")
-    private BigDecimal boatUseNumberYearTotal;
+    private BigDecimal boatUseNumberYearTotal = BigDecimal.ZERO;
 
     /**
      * 使用岸电小时(小时)-当月
      */
     @TableField(value = "POWER_CONN_HOURS_MONTH")
-    private BigDecimal powerConnHoursMonth;
+    private BigDecimal powerConnHoursMonth = BigDecimal.ZERO;
 
     /**
      * 使用岸电小时(小时)-当年累计
      */
     @TableField(value = "POWER_CONN_HOURS_YEAR_TOTAL")
-    private BigDecimal powerConnHoursYearTotal;
+    private BigDecimal powerConnHoursYearTotal = BigDecimal.ZERO;
 
     /**
      * 使用岸电电量(kWh)-当月
      */
     @TableField(value = "ELECTRICITY_USAGE_MONTH")
-    private BigDecimal electricityUsageMonth;
+    private BigDecimal electricityUsageMonth = BigDecimal.ZERO;
 
     /**
      * 使用岸电电量(kWh)-当年累计
      */
     @TableField(value = "ELECTRICITY_USAGE_YEAR_TOTAL")
-    private BigDecimal electricityUsageYearTotal;
+    private BigDecimal electricityUsageYearTotal = BigDecimal.ZERO;
 
     /**
      * 排序字段

+ 10 - 6
src/main/java/com/shcd/mapper/SpStatementInlandLowMapper.java

@@ -12,11 +12,11 @@ import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
 
 /**
-* @author 14390
-* @description 针对表【SP_STATEMENT_INLAND_LOW(内河低压分析表)】的数据库操作Mapper
-* @createDate 2024-01-22 14:57:44
-* @Entity com.shcd.entity.SpStatementInlandLow
-*/
+ * @author 14390
+ * @description 针对表【SP_STATEMENT_INLAND_LOW(内河低压分析表)】的数据库操作Mapper
+ * @createDate 2024-01-22 14:57:44
+ * @Entity com.shcd.entity.SpStatementInlandLow
+ */
 @Mapper
 @Repository
 public interface SpStatementInlandLowMapper extends BaseMapper<SpStatementInlandLow> {
@@ -25,7 +25,8 @@ public interface SpStatementInlandLowMapper extends BaseMapper<SpStatementInland
 
   List<ShorePowerDeviceUsage> getCurrentYearOriginDataFromPage();
 
-  SpStatementInlandLow getLastMonthReportData(@Param("date") String date,@Param("type") Integer type);
+  SpStatementInlandLow getLastMonthReportData(@Param("date") String date,
+      @Param("type") Integer type);
 
   List<ShorePowerConnRecordInfoVo> selectDataFromShorePower(Map<String, Object> params);
 
@@ -34,6 +35,9 @@ public interface SpStatementInlandLowMapper extends BaseMapper<SpStatementInland
   void deleteAllCurrentMonthReport1(@Param("reportDate") String reportDate);
 
   void deleteAllCurrentMonthReport2(@Param("reportDate") String reportDate);
+
+  List<SpStatementInlandLow> getAllLastMonthReportData(@Param("date") String date,
+      @Param("type") Integer type);
 }
 
 

+ 2 - 0
src/main/java/com/shcd/service/SpStatementInlandLowService.java

@@ -37,4 +37,6 @@ public interface SpStatementInlandLowService extends IService<SpStatementInlandL
 
   void makeReport2(String shorePowerDate, String shorePowerLastMonthDate,
       String shorePowerLast2MonthDate, String shorePowerLast3MonthDate);
+
+  List<SpStatementInlandLow> getAllLastMonthReportData(String lastMonthDate, int type);
 }

+ 96 - 96
src/main/java/com/shcd/service/impl/SpStatementInlandLowServiceImpl.java

@@ -16,6 +16,7 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.ParseException;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -103,17 +104,16 @@ public class SpStatementInlandLowServiceImpl extends
       List<ShorePowerDeviceUsage> originList = this.getOriginDataFromPage(
           currentMonthDate);
       log.info("统计当月原始数据为:{}", JSONObject.toJSONString(originList));
-      // 查询上个月数据 用来计算环比
-      List<ShorePowerDeviceUsage> lastOriginList = this.getOriginDataFromPage(
-          lastMonthDate);
-      log.info("统计上月原始数据为:{}", JSONObject.toJSONString(lastOriginList));
-      // 查询全年原始数据 用来计算当年累计
-      List<ShorePowerDeviceUsage> currentYearOriginList = this.getCurrentYearOriginDataFromPage();
-      log.info("当年累计原始数据为:{}", JSONObject.toJSONString(currentYearOriginList));
+      // 查询上个月报表数据 用来计算环比
+      List<SpStatementInlandLow> lastReportDataList = this.getAllLastMonthReportData(lastMonthDate,
+          1);
+      log.info("统计上月报表数据为:{}", JSONObject.toJSONString(lastReportDataList));
+//       查询全年原始数据 用来计算当年累计
+//      List<ShorePowerDeviceUsage> currentYearOriginList = this.getCurrentYearOriginDataFromPage();
+//      log.info("当年累计原始数据为:{}", JSONObject.toJSONString(currentYearOriginList));
       List<SpStatementInlandLow> spStatementInlandLowList = new ArrayList<>(originList.size());
       // 计算各区
-      executeEveryDistrict(originList, lastOriginList, spStatementInlandLowList,
-          currentYearOriginList);
+      executeEveryDistrict(originList, lastReportDataList, spStatementInlandLowList);
       // 计算合计
       executeTotal(spStatementInlandLowList, lastMonthDate);
       log.info("最终存储的内河低压分析表1为:{}", JSONObject.toJSONString(spStatementInlandLowList));
@@ -145,29 +145,23 @@ public class SpStatementInlandLowServiceImpl extends
       List<ShorePowerConnRecordInfoVo> currentMonthData = this.selectDataFromShorePower(
           params);
       log.info("市级平台自动上传<当月>数据为:{}", JSONObject.toJSONString(currentMonthData));
-      String lastMonthParamDate = lastMonthDate + "-24 23:59:59";
-      String last2MonthParamDate = last2MonthDate + "-25 00:00:00";
-      Map<String, Object> LastMonthParams = new HashMap<>(2);
-      LastMonthParams.put("reportDateStart", last2MonthParamDate);
-      LastMonthParams.put("reportDateEnd", lastMonthParamDate);
-      params.put("lastReportDateStart", last3MonthDate + "-25 00:00:00");
-      params.put("lastReportDateEnd", last2MonthDate + "-24 23:59:59");
-      List<ShorePowerConnRecordInfoVo> lastMonthData = this.selectDataFromShorePower(
-          LastMonthParams);
-      log.info("市级平台自动上传<上月>数据为:{}", JSONObject.toJSONString(lastMonthData));
+      // 查询上个月报表数据 用来计算环比
+      List<SpStatementInlandLow> lastReportDataList = this.getAllLastMonthReportData(lastMonthDate,
+          2);
+      log.info("市级平台自动上传<上月>数据为:{}", JSONObject.toJSONString(lastReportDataList));
       // 查询全年原始数据 用来计算当年累计
-      String yearStartDate =
-          (Integer.parseInt(currentDate.split("-")[0]) - 1) + "-12-25  00:00:00";
-      String yearEndDate = currentDate + "-24 23:59:59";
-      Map<String, Object> yearParams = new HashMap<>(2);
-      yearParams.put("reportDateStart", yearStartDate);
-      yearParams.put("reportDateEnd", yearEndDate);
-      List<ShorePowerConnRecordInfoVo> currentYearDataList = this.selectDataFromShorePower(
-          yearParams);
-      log.info("市级平台自动上传<全年>数据为:{}", JSONObject.toJSONString(currentYearDataList));
+//      String yearStartDate =
+//          (Integer.parseInt(currentDate.split("-")[0]) - 1) + "-12-25  00:00:00";
+//      String yearEndDate = currentDate + "-24 23:59:59";
+//      Map<String, Object> yearParams = new HashMap<>(2);
+//      yearParams.put("reportDateStart", yearStartDate);
+//      yearParams.put("reportDateEnd", yearEndDate);
+//      List<ShorePowerConnRecordInfoVo> currentYearDataList = this.selectDataFromShorePower(
+//          yearParams);
+//      log.info("市级平台自动上传<全年>数据为:{}", JSONObject.toJSONString(currentYearDataList));
       // 计算各区
       List<SpStatementInlandLow> spStatementInlandLowList = executeEveryDistrict2(currentMonthData,
-          lastMonthData, currentYearDataList, currentDate);
+          lastReportDataList, currentDate);
       // 计算合计
       executeTotal2(spStatementInlandLowList, lastMonthDate);
       List<SpStatementInlandLow> currentList = this.selectCurrentMonthReport2Count(
@@ -184,13 +178,17 @@ public class SpStatementInlandLowServiceImpl extends
     }
   }
 
+  @Override
+  public List<SpStatementInlandLow> getAllLastMonthReportData(String lastMonthDate, int type) {
+    return mapper.getAllLastMonthReportData(lastMonthDate, type);
+  }
+
 
   private void executeEveryDistrict(List<ShorePowerDeviceUsage> originList,
-      List<ShorePowerDeviceUsage> lastOriginList,
-      List<SpStatementInlandLow> spStatementInlandLowList,
-      List<ShorePowerDeviceUsage> currentYearOriginList) {
+      List<SpStatementInlandLow> lastReportDataList,
+      List<SpStatementInlandLow> spStatementInlandLowList) {
     for (ShorePowerDeviceUsage thisMonthData : originList) {
-      // 计算各区
+      // 计算各区  当月数据
       SpStatementInlandLow spStatementInlandLow = new SpStatementInlandLow();
       String id = String.valueOf(snowflakeMakeConf.snowflakeId());
       spStatementInlandLow.setId(id);
@@ -203,40 +201,51 @@ public class SpStatementInlandLowServiceImpl extends
           thisMonthData.getElectricityConsumption());
       spStatementInlandLow.setReportDate(thisMonthData.getReportDate());
       spStatementInlandLow.setType(1);
-      if (CollectionUtils.isEmpty(lastOriginList)) {
-        spStatementInlandLow.setBoatUseNumberChain(null);
+      // 计算当年累计 以及 环比
+      Calendar thisCalendar = Calendar.getInstance();
+      thisCalendar.setTime(thisMonthData.getReportDate());
+      if (thisCalendar.get(Calendar.MONTH) == Calendar.JANUARY) {
+        spStatementInlandLow.setBoatUseNumberYearTotal(
+            spStatementInlandLow.getBoatUseNumberMonth());
+        spStatementInlandLow.setPowerConnHoursYearTotal(
+            spStatementInlandLow.getPowerConnHoursMonth());
+        spStatementInlandLow.setElectricityUsageYearTotal(
+            spStatementInlandLow.getElectricityUsageMonth());
       } else {
-        for (ShorePowerDeviceUsage lastMonthData : lastOriginList) {
-          if (lastMonthData.getDistrict().equals(thisMonthData.getDistrict())) {
-            BigDecimal rate = getRate(thisMonthData, lastMonthData);
-            spStatementInlandLow.setBoatUseNumberChain(rate);
-          } else {
-            spStatementInlandLow.setBoatUseNumberChain(null);
+        // 2月才开始计算环比
+        for (SpStatementInlandLow lastReportData : lastReportDataList) {
+          if (lastReportData.getDistrict().equals(spStatementInlandLow.getDistrict())) {
+            spStatementInlandLow.setBoatUseNumberChain(
+                getRate(spStatementInlandLow.getBoatUseNumberMonth(),
+                    lastReportData.getBoatUseNumberMonth()));
+            spStatementInlandLow.setBoatUseNumberYearTotal(
+                spStatementInlandLow.getBoatUseNumberMonth()
+                    .add(lastReportData.getBoatUseNumberYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
+            spStatementInlandLow.setPowerConnHoursYearTotal(
+                spStatementInlandLow.getPowerConnHoursMonth()
+                    .add(lastReportData.getPowerConnHoursYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
+            spStatementInlandLow.setElectricityUsageYearTotal(
+                spStatementInlandLow.getElectricityUsageMonth()
+                    .add(lastReportData.getElectricityUsageYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
           }
+
         }
       }
       spStatementInlandLowList.add(spStatementInlandLow);
     }
-    for (SpStatementInlandLow statementInlandLow : spStatementInlandLowList) {
-      for (ShorePowerDeviceUsage yearData : currentYearOriginList) {
-        if (statementInlandLow.getDistrict().equals(yearData.getDistrict())) {
-          statementInlandLow.setBoatUseNumberYearTotal(yearData.getShorePowerBoatUseNumber());
-          statementInlandLow.setPowerConnHoursYearTotal(yearData.getPowerConnHours());
-          statementInlandLow.setElectricityUsageYearTotal(yearData.getElectricityConsumption());
-        }
-      }
-    }
   }
 
-  private static BigDecimal getRate(ShorePowerDeviceUsage thisMonthData,
-      ShorePowerDeviceUsage lastMonthData) {
-    if (Objects.isNull(lastMonthData) || Objects.isNull(lastMonthData.getShorePowerBoatUseNumber())
-        || lastMonthData.getShorePowerBoatUseNumber().equals(BigDecimal.ZERO)) {
+  private static BigDecimal getRate(BigDecimal thisMonthData,
+      BigDecimal lastMonthData) {
+    if (Objects.isNull(lastMonthData) || lastMonthData.equals(BigDecimal.ZERO)) {
       return null;
     }
-    return (thisMonthData.getShorePowerBoatUseNumber()
-        .subtract(lastMonthData.getShorePowerBoatUseNumber())).multiply(BigDecimal.valueOf(100))
-        .divide(lastMonthData.getShorePowerBoatUseNumber(), 2, RoundingMode.HALF_UP);
+    BigDecimal result = (thisMonthData.subtract(lastMonthData)).multiply(BigDecimal.valueOf(100))
+        .divide(lastMonthData, 2, RoundingMode.HALF_UP);
+    return result.setScale(2, RoundingMode.HALF_UP);
   }
 
   private void executeTotal(List<SpStatementInlandLow> spStatementInlandLowList,
@@ -335,8 +344,7 @@ public class SpStatementInlandLowServiceImpl extends
 
   private List<SpStatementInlandLow> executeEveryDistrict2(
       List<ShorePowerConnRecordInfoVo> currentMonthData,
-      List<ShorePowerConnRecordInfoVo> lastMonthData,
-      List<ShorePowerConnRecordInfoVo> currentYearDataList, String currentDate)
+      List<SpStatementInlandLow> lastReportDataList, String currentDate)
       throws ParseException {
     List<SpStatementInlandLow> spStatementInlandLowList = new ArrayList<>(
         currentMonthData.size());
@@ -353,53 +361,45 @@ public class SpStatementInlandLowServiceImpl extends
       spStatementInlandLow.setType(2);
       spStatementInlandLow.setBoatUseNumberMonth(currentMonth.getTotal());
       // 使用岸电小时在查询数据库时已经转换为小时计算
-//      spStatementInlandLow.setPowerConnHoursMonth(
-//          currentMonth.getDuration().divide(BigDecimal.valueOf(60), 2,
-//              RoundingMode.HALF_UP));
       spStatementInlandLow.setPowerConnHoursMonth(currentMonth.getDuration());
       spStatementInlandLow.setElectricityUsageMonth(currentMonth.getElect());
-      if (CollectionUtils.isEmpty(lastMonthData)) {
-        spStatementInlandLow.setBoatUseNumberChain(null);
+      // 计算当年累计 以及 环比
+      Calendar thisCalendar = Calendar.getInstance();
+      thisCalendar.setTime(DateUtils.stringToDate(currentDate, DateUtils.DATE_FORMAT_12));
+      if (thisCalendar.get(Calendar.MONTH) == Calendar.JANUARY) {
+        spStatementInlandLow.setBoatUseNumberYearTotal(
+            spStatementInlandLow.getBoatUseNumberMonth());
+        spStatementInlandLow.setPowerConnHoursYearTotal(
+            spStatementInlandLow.getPowerConnHoursMonth());
+        spStatementInlandLow.setElectricityUsageYearTotal(
+            spStatementInlandLow.getElectricityUsageMonth());
       } else {
-        for (ShorePowerConnRecordInfoVo lastMonth : lastMonthData) {
-          if (currentMonth.getArea().equals(lastMonth.getArea())) {
-            // 计算使用岸电船舶(艘次)环比
-            BigDecimal rate = getRate2(currentMonth, lastMonth);
-            spStatementInlandLow.setBoatUseNumberChain(rate);
+        // 2月才开始计算环比
+        for (SpStatementInlandLow lastReportData : lastReportDataList) {
+          if (lastReportData.getDistrict().equals(spStatementInlandLow.getDistrict())) {
+            spStatementInlandLow.setBoatUseNumberChain(
+                getRate(spStatementInlandLow.getBoatUseNumberMonth(),
+                    lastReportData.getBoatUseNumberMonth()));
+            spStatementInlandLow.setBoatUseNumberYearTotal(
+                spStatementInlandLow.getBoatUseNumberMonth()
+                    .add(lastReportData.getBoatUseNumberYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
+            spStatementInlandLow.setPowerConnHoursYearTotal(
+                spStatementInlandLow.getPowerConnHoursMonth()
+                    .add(lastReportData.getPowerConnHoursYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
+            spStatementInlandLow.setElectricityUsageYearTotal(
+                spStatementInlandLow.getElectricityUsageMonth()
+                    .add(lastReportData.getElectricityUsageYearTotal())
+                    .setScale(2, RoundingMode.HALF_UP));
           }
+
         }
       }
       spStatementInlandLowList.add(spStatementInlandLow);
     }
-    // 计算各区的当年累计
-    for (SpStatementInlandLow statementInlandLow : spStatementInlandLowList) {
-      for (ShorePowerConnRecordInfoVo yearData : currentYearDataList) {
-        if (statementInlandLow.getDistrict().equals(
-            ShorePowerConnRecordInfoEnum.getByArea(Integer.parseInt(yearData.getArea()))
-                .getArea())) {
-          statementInlandLow.setBoatUseNumberYearTotal(yearData.getTotal());
-//            statementInlandLow.setPowerConnHoursYearTotal(
-//                yearData.getDuration().divide(BigDecimal.valueOf(60), 2,
-//                    RoundingMode.HALF_UP));
-          statementInlandLow.setPowerConnHoursYearTotal(yearData.getDuration());
-          statementInlandLow.setElectricityUsageYearTotal(yearData.getElect());
-        }
-      }
-    }
     return spStatementInlandLowList;
   }
-
-  private BigDecimal getRate2(ShorePowerConnRecordInfoVo currentMonth,
-      ShorePowerConnRecordInfoVo lastMonth) {
-    if (Objects.isNull(lastMonth) || Objects.isNull(lastMonth.getTotal())
-        || lastMonth.getTotal().equals(BigDecimal.ZERO)) {
-      return null;
-    } else {
-      return (currentMonth.getTotal()
-          .subtract(lastMonth.getTotal())).multiply(BigDecimal.valueOf(100))
-          .divide(lastMonth.getTotal(), 2, RoundingMode.HALF_UP);
-    }
-  }
 }
 
 

+ 15 - 0
src/main/resources/mapper/SpStatementInlandLowMapper.xml

@@ -129,4 +129,19 @@
     GROUP BY PORT.AREA
   </select>
 
+
+    <select id="getAllLastMonthReportData" parameterType="String"
+    resultType="com.shcd.entity.SpStatementInlandLow">
+      SELECT T.*
+      FROM GHJG_PORT_MANAGE.SP_STATEMENT_INLAND_LOW T
+      WHERE T.DELETE_FLAG = 0
+      <if test="date != '' and date != null">
+        and T.Report_Date &gt;= to_date(#{date},'yyyy-MM') and T.Report_Date &lt;=
+        to_Date(#{date},'yyyy-MM')
+      </if>
+      <if test="type != '' and type != null">
+        AND T.TYPE = ${type}
+      </if>
+    </select>
+
 </mapper>