浏览代码

修改报告名称月份错误的问题

lixing 3 年之前
父节点
当前提交
26bd498c98

+ 51 - 1
src/main/java/com/persagy/apm/energy/report/common/utils/DateUtils.java

@@ -15,6 +15,7 @@ import java.util.List;
 
 public class DateUtils {
     public static final String SDFSECOND = "yyyyMMddHHmmss";
+    public static final String SDFDAY = "yyyyMMdd";
     public static final String SDF_SECOND = "yyyy-MM-dd HH:mm:ss";
 
     /**
@@ -193,6 +194,9 @@ public class DateUtils {
      * @version V1.0 2021/5/30 11:43 上午
      */
     public static Date getFirstDayOfLastYear(Date date) {
+        if (date == null) {
+            return null;
+        }
         Instant instant = date.toInstant();
         ZoneId zoneId = ZoneId.systemDefault();
 
@@ -203,6 +207,24 @@ public class DateUtils {
     }
 
     /**
+     * date转换为localDate
+     *
+     * @param date Date对象
+     * @return LocalDate对象
+     * @author lixing
+     * @version V1.0 2021/6/3 11:36 上午
+     */
+    public static LocalDate convert2LocalDate(Date date) {
+        if (date == null) {
+            return null;
+        }
+        Instant instant = date.toInstant();
+        ZoneId zoneId = ZoneId.systemDefault();
+
+        return instant.atZone(zoneId).toLocalDate();
+    }
+
+    /**
      * 去年十二月第一天
      *
      * @param date 参照的日期
@@ -211,6 +233,9 @@ public class DateUtils {
      * @version V1.0 2021/5/30 11:43 上午
      */
     public static Date getDecemberFirstDayOfLastYear(Date date) {
+        if (date == null) {
+            return null;
+        }
         Instant instant = date.toInstant();
         ZoneId zoneId = ZoneId.systemDefault();
 
@@ -221,6 +246,28 @@ public class DateUtils {
     }
 
     /**
+     * 去年同期月份第一天
+     *
+     * @param date 参照的日期
+     * @return 去年同期月份第一天
+     * @author lixing
+     * @version V1.0 2021/5/30 11:43 上午
+     */
+    public static Date getSameMonthFirstDayOfLastYear(Date date) {
+        if (date == null) {
+            return null;
+        }
+        Instant instant = date.toInstant();
+        ZoneId zoneId = ZoneId.systemDefault();
+
+        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
+        LocalDate lastYear = localDate.minusYears(1);
+        LocalDate lastYearFirstDay = LocalDate.of(lastYear.getYear(), lastYear.getMonth(), 1);
+        return Date.from(lastYearFirstDay.atStartOfDay(zoneId).toInstant());
+    }
+
+
+    /**
      * 获取阶段内的每个月的第一天
      *
      * @param startDate 开始日期
@@ -279,6 +326,9 @@ public class DateUtils {
      * @version V1.0 2021/5/30 11:44 上午
      */
     public static Date getFirstDayOfLastMonth(Date date) {
+        if (date == null) {
+            return null;
+        }
         Instant instant = date.toInstant();
         ZoneId zoneId = ZoneId.systemDefault();
 
@@ -316,7 +366,7 @@ public class DateUtils {
         // 2020-02-01
         Date startDate = new Date(1580486400000L);
         Date endDate = new Date();
-        System.out.println(getFirstDayOfEveryMonth(startDate, endDate));
+        System.out.println(getSameMonthFirstDayOfLastYear(startDate));
     }
 
 }

+ 2 - 2
src/main/java/com/persagy/apm/energy/report/monthly/outline/service/impl/BusinessReportCostInfoServiceImpl.java

@@ -160,11 +160,11 @@ public class BusinessReportCostInfoServiceImpl implements IBusinessReportCostInf
             costItemVO.setLastMonth(lastMonth);
         }
 
-        // 统计去年的费用
+        // 统计去年同期的费用
         Double lastYearCount = null;
         List<Date> lastYearMonths = DateUtils.getFirstDayOfEveryMonth(
                 DateUtils.getFirstDayOfLastYear(reportMonth),
-                DateUtils.getDecemberFirstDayOfLastYear(reportMonth));
+                DateUtils.getSameMonthFirstDayOfLastYear(reportMonth));
         List<CostItemVO> lastYearItems = new ArrayList<>();
         for (Date lastYearMonth : lastYearMonths) {
             CostItemVO tmp = new CostItemVO();

+ 2 - 2
src/main/java/com/persagy/apm/energy/report/monthly/outline/service/impl/BusinessReportPowerInfoServiceImpl.java

@@ -152,11 +152,11 @@ public class BusinessReportPowerInfoServiceImpl implements IBusinessReportPowerI
         Double lastMonth = dateValueMap.get(DateUtils.getFirstDayOfLastMonth(reportMonth));
         powerItemVO.setLastMonth(lastMonth);
 
-        // 统计去年的用电量
+        // 统计去年同期的用电量
         Double lastYearCount = null;
         List<Date> lastYearMonths = DateUtils.getFirstDayOfEveryMonth(
                 DateUtils.getFirstDayOfLastYear(reportMonth),
-                DateUtils.getDecemberFirstDayOfLastYear(reportMonth));
+                DateUtils.getSameMonthFirstDayOfLastYear(reportMonth));
         List<PowerItemVO> lastYearItems = new ArrayList<>();
         for (Date lastYearMonth : lastYearMonths) {
             PowerItemVO tmp = new PowerItemVO();

+ 5 - 5
src/main/java/com/persagy/apm/energy/report/monthly/outline/service/impl/ReportOutlineServiceImpl.java

@@ -8,6 +8,7 @@ import com.google.common.base.CaseFormat;
 import com.persagy.apm.common.constant.enums.ValidEnum;
 import com.persagy.apm.common.context.poems.PoemsContext;
 import com.persagy.apm.common.model.dto.Sort;
+import com.persagy.apm.energy.report.common.utils.DateUtils;
 import com.persagy.apm.energy.report.monthly.config.rel.detailstatisticsitem.model.ConvertReportDetailStatisticsItemRelTool;
 import com.persagy.apm.energy.report.monthly.config.rel.detailstatisticsitem.model.ReportDetailStatisticsItemRel;
 import com.persagy.apm.energy.report.monthly.config.rel.detailstatisticsitem.model.dto.QueryReportDetailStatisticsItemRelDTO;
@@ -36,6 +37,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
+import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.List;
@@ -118,11 +120,9 @@ public class ReportOutlineServiceImpl extends ServiceImpl<ReportOutlineMapper, R
             belongName = reportAreaInfo.getAreaName();
         }
 
-
-        Calendar calendar = Calendar.getInstance();
-        calendar.setTime(reportOutline.getReportMonth());
-        reportOutline.setName(calendar.get(Calendar.YEAR) + "年" +
-                calendar.get(Calendar.MONTH) + "月" +
+        LocalDate reportMonthLocalDate = DateUtils.convert2LocalDate(reportOutline.getReportMonth());
+        reportOutline.setName(reportMonthLocalDate.getYear() + "年" +
+                reportMonthLocalDate.getMonthValue() + "月" +
                 belongName + reportType.getName());
     }
 

+ 21 - 7
src/main/java/com/persagy/apm/energy/report/saasweb/service/impl/SaasWebServiceImpl.java

@@ -566,10 +566,8 @@ public class SaasWebServiceImpl implements ISaasWebService {
             SimpleProjectVO simpleProjectInfo = getSimpleProjectInfo(projectId);
             String businessOpeningTime = simpleProjectInfo.getBusinessOpeningTime();
             try {
-                Date openDate = DateUtils.str2Date(businessOpeningTime, DateUtils.SDFSECOND);
-                Date firstDayOfLastYear = DateUtils.getFirstDayOfLastYear(month);
                 // 如果开业时间早于去年,添加到结果集
-                if (DateUtils.compareDate(openDate, firstDayOfLastYear)) {
+                if (isComparable(month, businessOpeningTime)) {
                     result.add(projectId);
                 }
             } catch (Exception e) {
@@ -589,10 +587,7 @@ public class SaasWebServiceImpl implements ISaasWebService {
         List<PoemsProjectVO> comparableProjects = projectsByAreaAndBuildingType.stream().filter(poemsProjectVO -> {
             String businessOpeningTime = poemsProjectVO.getBusinessOpeningTime();
             try {
-                Date openDate = DateUtils.str2Date(businessOpeningTime, DateUtils.SDFSECOND);
-                Date firstDayOfLastYear = DateUtils.getFirstDayOfLastYear(reportMonth);
-                // 如果开业时间早于去年,添加到结果集
-                return DateUtils.compareDate(openDate, firstDayOfLastYear);
+                return isComparable(reportMonth, businessOpeningTime);
             } catch (Exception e) {
                 log.error("时间转换异常", e);
                 return false;
@@ -603,4 +598,23 @@ public class SaasWebServiceImpl implements ISaasWebService {
         reportAreaVO.setCommercialArea(reportAreaAreaInfo.getCommercialArea());
         return reportAreaVO;
     }
+
+    /**
+     * 如果开业时间早于报告月份上一年一月一日,项目为可同比项目
+     *
+     * @param reportMonth 报告月份
+     * @param businessOpeningTime 开业时间
+     * @return 是否是可同比项目
+     * @author lixing
+     * @version V1.0 2021/6/3 11:44 上午
+     */
+    private boolean isComparable(Date reportMonth, String businessOpeningTime) throws Exception {
+        if (StringUtils.isBlank(businessOpeningTime)) {
+            return false;
+        }
+        Date openDate = DateUtils.str2Date(businessOpeningTime, DateUtils.SDFDAY);
+        Date firstDayOfLastYear = DateUtils.getFirstDayOfLastYear(reportMonth);
+        // 如果开业时间早于去年,添加到结果集
+        return DateUtils.compareDate(openDate, firstDayOfLastYear);
+    }
 }