Browse Source

报告数据计算修改为decimal计算

lixing 3 years ago
parent
commit
c551f04c9f

+ 47 - 4
src/main/java/com/persagy/apm/energy/report/common/utils/DataUtils.java

@@ -90,15 +90,16 @@ public class DataUtils {
                 if (CollectionUtils.isEmpty(notNullList)) {
                     declaredField.set(result, null);
                 } else {
-                    declaredField.set(result, notNullList.stream().mapToDouble(
+                    declaredField.set(result, notNullList.stream().map(
                             obj -> {
                                 try {
-                                    return (Double) declaredField.get(obj);
+                                    return new BigDecimal(declaredField.get(obj).toString()) ;
                                 } catch (IllegalAccessException e) {
                                     e.printStackTrace();
-                                    return 0d;
+                                    return new BigDecimal("0");
                                 }
-                            }).sum());
+                            }).reduce(BigDecimal::add).
+                            map(BigDecimal::doubleValue).orElse(0d));
                 }
 
             }
@@ -108,4 +109,46 @@ public class DataUtils {
             return null;
         }
     }
+
+    /**
+     * 两个double做除法(结果保留4位小数)
+     *
+     * @param d1 第一个double值
+     * @param d2 第二个double值
+     * @return 结果
+     * @author lixing
+     * @version V1.0 2021/6/2 11:02 上午
+     */
+    public static Double doubleDivide(Double d1, Double d2) {
+        // 结果保留的位数
+        int scale = 4;
+        if (d1 == null || d2 == null) {
+            throw new IllegalArgumentException("两个double做除法,其中一个为空或两个都为空");
+        }
+        if (d2.equals(0d)) {
+            throw new IllegalArgumentException("double做除法,被除数不能为0");
+        }
+        BigDecimal bigDecimal1 = new BigDecimal(d1);
+        BigDecimal bigDecimal2 = new BigDecimal(d2);
+        return bigDecimal1.divide(
+                bigDecimal2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
+    }
+
+    /**
+     * 两个double做减法
+     *
+     * @param d1 第一个double值
+     * @param d2 第二个double值
+     * @return 结果
+     * @author lixing
+     * @version V1.0 2021/6/2 11:02 上午
+     */
+    public static Double doubleSubtract(Double d1, Double d2) {
+        if (d1 == null || d2 == null) {
+            throw new IllegalArgumentException("两个double做减法,其中一个为空或两个都为空");
+        }
+        BigDecimal bigDecimal1 = new BigDecimal(d1);
+        BigDecimal bigDecimal2 = new BigDecimal(d2);
+        return bigDecimal1.subtract(bigDecimal2).doubleValue();
+    }
 }

+ 1 - 1
src/main/java/com/persagy/apm/energy/report/monthly/config/type/constant/enums/BuildingTypeEnum.java

@@ -15,7 +15,7 @@ public enum BuildingTypeEnum {
     /**
      * 业态
      */
-    BUSINESS("0B0", "商业"),
+    BUSINESS("B00", "商业"),
     HOTEL("A00", "酒店");
 
     @Setter

+ 1 - 1
src/main/java/com/persagy/apm/energy/report/monthly/outline/service/IBusinessReportBuilder.java

@@ -1,4 +1,4 @@
-package com.persagy.apm.energy.report.monthly.outline.service;
+package com.persagy.apm.energy.report.monthly.outline.service.builder;
 
 import com.persagy.apm.energy.report.monthly.config.function.model.Function;
 import com.persagy.apm.energy.report.monthly.detail.business.model.Platform;

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

@@ -1,4 +1,4 @@
-package com.persagy.apm.energy.report.monthly.outline.service.impl;
+package com.persagy.apm.energy.report.monthly.outline.service.builder.impl;
 
 import com.persagy.apm.energy.report.monthly.detail.business.model.Platform;
 import com.persagy.apm.energy.report.monthly.detail.business.model.dto.AddReportBusinessDetailDTO;
@@ -9,6 +9,7 @@ import com.persagy.apm.energy.report.monthly.outline.constants.BusinessReportPar
 import com.persagy.apm.energy.report.monthly.outline.constants.enums.ReportStateEnum;
 import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
 import com.persagy.apm.energy.report.monthly.outline.service.*;
+import com.persagy.apm.energy.report.monthly.outline.service.builder.IBusinessReportBuilder;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;

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

@@ -1,24 +1,8 @@
 package com.persagy.apm.energy.report.monthly.outline.service.impl;
 
-import com.persagy.apm.energy.report.monthly.detail.business.model.*;
-import com.persagy.apm.energy.report.monthly.detail.business.model.dto.AddReportBusinessDetailDTO;
-import com.persagy.apm.energy.report.monthly.detail.business.model.vo.CostVO;
-import com.persagy.apm.energy.report.monthly.detail.business.model.vo.PowerVO;
-import com.persagy.apm.energy.report.monthly.detail.business.service.IReportBusinessDetailService;
-import com.persagy.apm.energy.report.monthly.outline.constants.BusinessReportParagraphs;
-import com.persagy.apm.energy.report.monthly.outline.constants.enums.ReportStateEnum;
-import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportCostInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPlatformInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPowerInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IReportOutlineService;
+import com.persagy.apm.energy.report.monthly.outline.service.builder.impl.BusinessReportBuilder;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.springframework.transaction.annotation.Transactional;
-import org.springframework.transaction.interceptor.TransactionAspectSupport;
-
-import java.util.List;
 
 /**
  * 商业项目报告生成类
@@ -28,6 +12,6 @@ import java.util.List;
  **/
 @Component
 @Slf4j
-public class BusinessAreaReportGenerator extends BusinessReportBuilder{
+public class BusinessAreaReportGenerator extends BusinessReportBuilder {
 
 }

+ 1 - 13
src/main/java/com/persagy/apm/energy/report/monthly/outline/service/impl/BusinessProjectReportGenerator.java

@@ -1,23 +1,11 @@
 package com.persagy.apm.energy.report.monthly.outline.service.impl;
 
-import com.persagy.apm.energy.report.monthly.detail.business.model.Platform;
-import com.persagy.apm.energy.report.monthly.detail.business.model.dto.AddReportBusinessDetailDTO;
 import com.persagy.apm.energy.report.monthly.detail.business.model.vo.CostVO;
 import com.persagy.apm.energy.report.monthly.detail.business.model.vo.PowerVO;
-import com.persagy.apm.energy.report.monthly.detail.business.service.IReportBusinessDetailService;
-import com.persagy.apm.energy.report.monthly.outline.constants.BusinessReportParagraphs;
-import com.persagy.apm.energy.report.monthly.outline.constants.enums.ReportStateEnum;
 import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportCostInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPlatformInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPowerInfoService;
-import com.persagy.apm.energy.report.monthly.outline.service.IReportOutlineService;
+import com.persagy.apm.energy.report.monthly.outline.service.builder.impl.BusinessReportBuilder;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
 
 /**
  * 商业项目报告生成类

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

@@ -11,6 +11,7 @@ import com.persagy.apm.energy.report.monthly.detail.business.model.vo.CostVO;
 import com.persagy.apm.energy.report.monthly.detail.business.model.vo.GroupInfo;
 import com.persagy.apm.energy.report.monthly.functionvalue.service.IFunctionValueService;
 import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
+import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportCostInfoService;
 import com.persagy.apm.energy.report.monthly.outline.service.IReportOutlineService;
 import com.persagy.apm.energy.report.saasweb.model.vo.ReportProjectVO;
 import com.persagy.apm.energy.report.saasweb.service.ISaasWebService;
@@ -32,7 +33,7 @@ import java.util.stream.Collectors;
  * @version V1.0 2021/5/30 7:30 下午
  **/
 @Service
-public class BusinessReportCostInfoServiceImpl implements com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportCostInfoService {
+public class BusinessReportCostInfoServiceImpl implements IBusinessReportCostInfoService {
     @Autowired
     private IFunctionGroupService functionGroupService;
     @Autowired
@@ -179,7 +180,7 @@ public class BusinessReportCostInfoServiceImpl implements com.persagy.apm.energy
             lastYearCount = lastYearSumItem.getLastYearCount();
             costItemVO.setLastYearCount(lastYearCount);
             if (totalCommercialArea != null && !totalCommercialArea.equals(zero) && lastYearCount != null) {
-                costItemVO.setLastYearPerCount(lastYearCount / totalCommercialArea);
+                costItemVO.setLastYearPerCount(DataUtils.doubleDivide(lastYearCount, totalCommercialArea));
             }
         }
 
@@ -203,21 +204,21 @@ public class BusinessReportCostInfoServiceImpl implements com.persagy.apm.energy
             yearCount = yearSumItem.getYearCount();
             costItemVO.setYearCount(yearCount);
             if (totalCommercialArea != null && !totalCommercialArea.equals(zero) && yearCount != null) {
-                costItemVO.setYearPerCount(yearCount / totalCommercialArea);
+                costItemVO.setYearPerCount(DataUtils.doubleDivide(yearCount, totalCommercialArea));
             }
         }
 
         if (currentMonth != null && lastMonth != null) {
-            Double linkCount = currentMonth - lastMonth;
-            Double linkRange = linkCount / lastMonth;
+            Double linkCount = DataUtils.doubleSubtract(currentMonth, lastMonth);
+            Double linkRange = DataUtils.doubleDivide(linkCount, lastMonth);
             costItemVO.setLinkCount(linkCount);
             costItemVO.setLinkRange(linkRange);
         }
 
         if (lastYearCount != null && yearCount != null) {
-            double sameCount = yearCount - lastYearCount;
+            double sameCount = DataUtils.doubleSubtract(yearCount, lastYearCount);
             costItemVO.setSameCount(sameCount);
-            costItemVO.setSameRange(sameCount / lastYearCount);
+            costItemVO.setSameRange(DataUtils.doubleDivide(sameCount, lastYearCount));
         }
         return costItemVO;
     }

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

@@ -13,6 +13,7 @@ import com.persagy.apm.energy.report.monthly.config.type.model.ReportType;
 import com.persagy.apm.energy.report.monthly.config.type.service.IReportTypeService;
 import com.persagy.apm.energy.report.monthly.detail.business.model.Platform;
 import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
+import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPlatformInfoService;
 import com.persagy.apm.energy.report.monthly.outline.service.IReportOutlineService;
 import org.assertj.core.util.Lists;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +32,7 @@ import java.util.stream.Collectors;
  * @version V1.0 2021/5/30 10:22 下午
  **/
 @Service
-public class BusinessReportPlatformInfoServiceImpl implements com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPlatformInfoService {
+public class BusinessReportPlatformInfoServiceImpl implements IBusinessReportPlatformInfoService {
     @Autowired
     private IReportTypeStatisticsItemRelService reportTypeStatisticsItemRelService;
     @Autowired

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

@@ -13,6 +13,7 @@ import com.persagy.apm.energy.report.monthly.detail.business.model.vo.GroupInfo;
 import com.persagy.apm.energy.report.monthly.detail.business.model.vo.PowerItemVO;
 import com.persagy.apm.energy.report.monthly.detail.business.model.vo.PowerVO;
 import com.persagy.apm.energy.report.monthly.outline.model.ReportOutline;
+import com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPowerInfoService;
 import com.persagy.apm.energy.report.monthly.outline.service.IReportOutlineService;
 import com.persagy.apm.energy.report.saasweb.model.vo.ReportProjectVO;
 import com.persagy.apm.energy.report.saasweb.service.ISaasWebService;
@@ -31,7 +32,7 @@ import java.util.stream.Collectors;
  * @version V1.0 2021/5/30 7:30 下午
  **/
 @Service
-public class BusinessReportPowerInfoServiceImpl implements com.persagy.apm.energy.report.monthly.outline.service.IBusinessReportPowerInfoService {
+public class BusinessReportPowerInfoServiceImpl implements IBusinessReportPowerInfoService {
     @Autowired
     private IReportOutlineService reportOutlineService;
     @Autowired
@@ -168,7 +169,7 @@ public class BusinessReportPowerInfoServiceImpl implements com.persagy.apm.energ
             lastYearCount = lastYearSumItem.getLastYearCount();
             powerItemVO.setLastYearCount(lastYearCount);
             if (totalCommercialArea != null && !totalCommercialArea.equals(zero) && lastYearCount != null) {
-                powerItemVO.setLastYearPerCount(lastYearCount / totalCommercialArea);
+                powerItemVO.setLastYearPerCount(DataUtils.doubleDivide(lastYearCount, totalCommercialArea));
             }
         }
 
@@ -189,21 +190,21 @@ public class BusinessReportPowerInfoServiceImpl implements com.persagy.apm.energ
             yearCount = yearSumItem.getYearCount();
             powerItemVO.setYearCount(yearCount);
             if (totalCommercialArea != null && !totalCommercialArea.equals(zero) && yearCount != null) {
-                powerItemVO.setYearPerCount(yearCount / totalCommercialArea);
+                powerItemVO.setYearPerCount(DataUtils.doubleDivide(yearCount, totalCommercialArea));
             }
         }
 
         if (currentMonth != null && lastMonth != null) {
-            Double linkCount = currentMonth - lastMonth;
-            Double linkRange = linkCount / lastMonth;
+            Double linkCount = DataUtils.doubleSubtract(currentMonth, lastMonth);
+            Double linkRange = DataUtils.doubleDivide(linkCount, lastMonth);
             powerItemVO.setLinkCount(linkCount);
             powerItemVO.setLinkRange(linkRange);
         }
 
         if (lastYearCount != null && yearCount != null) {
-            double sameCount = yearCount - lastYearCount;
+            double sameCount = DataUtils.doubleSubtract(yearCount, lastYearCount);
             powerItemVO.setSameCount(sameCount);
-            powerItemVO.setSameRange(sameCount / lastYearCount);
+            powerItemVO.setSameRange(DataUtils.doubleDivide(sameCount, lastYearCount));
         }
         return powerItemVO;
     }

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

@@ -62,9 +62,13 @@ public class GenerateReportThreadPool {
                 ReportType reportType = reportTypeService.queryReportTypeDetail(reportTypeId);
                 if (BuildingTypeEnum.BUSINESS.getType().equals(reportType.getBuildingType())) {
                     if (BelongTypeEnum.PROJECT.getType().equals(reportType.getBelongType())) {
-                        businessProjectReportGenerator.generateReport(reportOutline);
+                        synchronized (reportOutline.getId().intern()) {
+                            businessProjectReportGenerator.generateReport(reportOutline);
+                        }
                     } else {
-                        businessAreaReportGenerator.generateReport(reportOutline);
+                        synchronized (reportOutline.getId().intern()) {
+                            businessAreaReportGenerator.generateReport(reportOutline);
+                        }
                     }
                 }
             } catch (Exception e) {

+ 10 - 0
src/main/java/com/persagy/apm/energy/report/saasweb/service/ISaasWebService.java

@@ -87,6 +87,16 @@ public interface ISaasWebService {
     List<SimpleProjectVO> getAvailableProjects(String buildingType);
 
     /**
+     * 级联获取所有关联的业态
+     *
+     * @param buildingType 查询条件
+     * @return 所有关联的业态(包括自己)
+     * @author lixing
+     * @version V1.0 2021/5/24 6:04 下午
+     */
+    List<String> getRelatedBuildingTypes(String buildingType);
+
+    /**
      * 根据业态获取用户可选的分区树
      *
      * @param buildingType 项目业态

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

@@ -236,14 +236,8 @@ public class SaasWebServiceImpl implements ISaasWebService {
         return result;
     }
 
-    /**
-     * 级联获取所有关联的业态
-     *
-     * @return 所有关联的业态(包括自己)
-     * @author lixing
-     * @version V1.0 2021/5/24 6:04 下午
-     */
-    private List<String> getRelatedBuildingTypes(String buildingType) {
+    @Override
+    public List<String> getRelatedBuildingTypes(String buildingType) {
         if (StringUtils.isBlank(buildingType)) {
             return Lists.newArrayList();
         }