From 3d7a4912e2408f1dd06a82d99f255ae15650180b Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期五, 16 十月 2020 15:20:37 +0800
Subject: [PATCH] 优化加入报表基础生成表

---
 service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java |  306 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 304 insertions(+), 2 deletions(-)

diff --git a/service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java b/service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java
index 49c44c5..19db52f 100644
--- a/service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java
+++ b/service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java
@@ -1,21 +1,32 @@
 package com.java110.report.smo.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.core.smo.IComputeFeeSMO;
 import com.java110.dto.fee.FeeDto;
+import com.java110.dto.report.ReportCarDto;
+import com.java110.dto.report.ReportFeeDetailDto;
 import com.java110.dto.report.ReportFeeDto;
 import com.java110.dto.report.ReportRoomDto;
+import com.java110.dto.reportFeeMonthStatistics.ReportFeeMonthStatisticsDto;
 import com.java110.intf.report.IGeneratorFeeMonthStatisticsInnerServiceSMO;
 import com.java110.po.reportFeeMonthStatistics.ReportFeeMonthStatisticsPo;
 import com.java110.report.dao.IReportCommunityServiceDao;
 import com.java110.report.dao.IReportFeeMonthStatisticsServiceDao;
 import com.java110.report.dao.IReportFeeServiceDao;
 import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
+import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.ListUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -42,6 +53,9 @@
     @Autowired
     private IReportFeeServiceDao reportFeeServiceDaoImpl;
 
+    @Autowired
+    private IComputeFeeSMO computeFeeSMOImpl;
+
     @Override
     public int generatorData(@RequestBody ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo) {
 
@@ -52,8 +66,47 @@
         //澶勭悊鎴垮眿璐圭敤
         dealRoomFee(reportFeeMonthStatisticsPo);
 
+        //澶勭悊杞︿綅璐圭敤
+        dealCarFee(reportFeeMonthStatisticsPo);
 
         return 0;
+    }
+
+    /**
+     * 澶勭悊杞︿綅 杞﹁締璐圭敤
+     *
+     * @param reportFeeMonthStatisticsPo
+     */
+    private void dealCarFee(ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo) {
+
+        int page = 0;
+        int max = DEFAULT_DEAL_ROOM_COUNT;
+
+        ReportCarDto reportCarDto = new ReportCarDto();
+        reportCarDto.setCommunityId(reportFeeMonthStatisticsPo.getCommunityId());
+        int count = reportCommunityServiceDaoImpl.getCarCount(reportCarDto);
+
+
+        if (count < DEFAULT_DEAL_ROOM_COUNT) {
+            page = 1;
+            max = count;
+        } else {
+            page = (int) Math.ceil((double) count / (double) DEFAULT_DEAL_ROOM_COUNT);
+            max = DEFAULT_DEAL_ROOM_COUNT;
+        }
+
+        for (int pageIndex = 0; pageIndex < page; pageIndex++) {
+            reportCarDto.setPage(pageIndex * max);
+            reportCarDto.setRow(max);
+            List<ReportCarDto> reportRoomDtos = reportCommunityServiceDaoImpl.getCarParkingSpace(reportCarDto);
+            for (ReportCarDto tmpReportCarDto : reportRoomDtos) {
+                try {
+                    doDealCarFees(tmpReportCarDto);
+                } catch (Exception e) {
+                    logger.error("鐢熸垚璐圭敤鎶ヨ〃澶辫触" + JSONObject.toJSONString(tmpReportCarDto), e);
+                }
+            }
+        }
     }
 
 
@@ -86,7 +139,7 @@
             List<ReportRoomDto> reportRoomDtos = reportCommunityServiceDaoImpl.getRoomFloorUnitAndOwner(reportRoomDto);
             for (ReportRoomDto tmpReportRoomDto : reportRoomDtos) {
                 try {
-                    doDealRoomFee(tmpReportRoomDto);
+                    doDealRoomFees(tmpReportRoomDto);
                 } catch (Exception e) {
                     logger.error("鐢熸垚璐圭敤鎶ヨ〃澶辫触" + JSONObject.toJSONString(tmpReportRoomDto), e);
                 }
@@ -94,12 +147,98 @@
         }
     }
 
+    private void doDealCarFees(ReportCarDto tmpReportCarDto) {
+        ReportFeeDto reportFeeDto = new ReportFeeDto();
+        reportFeeDto.setPayerObjId(tmpReportCarDto.getCarId());
+        reportFeeDto.setPayerObjType(FeeDto.PAYER_OBJ_TYPE_CAR);
+        reportFeeDto.setState(FeeDto.STATE_DOING);
+        List<ReportFeeDto> feeDtos = reportFeeServiceDaoImpl.getFees(reportFeeDto);
+
+        if (feeDtos == null || feeDtos.size() < 1) {
+            return;
+        }
+
+        for (ReportFeeDto tmpReportFeeDto : feeDtos) {
+            try {
+                doDealCarFee(tmpReportCarDto, tmpReportFeeDto);
+            } catch (Exception e) {
+                logger.error("澶勭悊鎴垮眿璐圭敤澶辫触" + JSONObject.toJSONString(tmpReportFeeDto), e);
+            }
+        }
+    }
+
+    private void doDealCarFee(ReportCarDto tmpReportCarDto, ReportFeeDto tmpReportFeeDto) {
+
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(tmpReportCarDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
+        reportFeeMonthStatisticsDto.setFeeYear(DateUtil.getYear() + "");
+        reportFeeMonthStatisticsDto.setFeeMonth(DateUtil.getMonth() + "");
+        List<ReportFeeMonthStatisticsDto> statistics = BeanConvertUtil.covertBeanList(
+                reportFeeMonthStatisticsServiceDaoImpl.getReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsDto)),
+                ReportFeeMonthStatisticsDto.class);
+
+        double receivedAmount = getReceivedAmount(tmpReportFeeDto); //瀹炴敹
+        //璐圭敤宸茬粡缁撴潫 骞朵笖褰撴湀瀹炴敹涓�0 閭e氨鏄� 涔嬪墠灏辩粨鏉熶簡 鏃犻渶澶勭悊  && ListUtil.isNull(statistics)
+        if (FeeDto.STATE_FINISH.equals(tmpReportFeeDto.getState())
+                && receivedAmount == 0) {
+            return;
+        }
+        double receivableAmount = getReceivableAmount(tmpReportFeeDto); //搴旀敹
+        double oweAmount = getOweAmount(tmpReportFeeDto, receivableAmount, receivedAmount); //娆犺垂
+
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        if (!ListUtil.isNull(statistics)) {
+            ReportFeeMonthStatisticsDto statistic = statistics.get(0);
+            reportFeeMonthStatisticsPo.setStatisticsId(statistic.getStatisticsId());
+            reportFeeMonthStatisticsPo.setReceivableAmount(receivableAmount + "");
+            reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+            reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+        } else {
+            reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
+            reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+            reportFeeMonthStatisticsPo.setReceivableAmount(receivableAmount + "");
+            reportFeeMonthStatisticsPo.setStatisticsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_statisticsId));
+            reportFeeMonthStatisticsPo.setCommunityId(tmpReportFeeDto.getCommunityId());
+            reportFeeMonthStatisticsPo.setConfigId(tmpReportFeeDto.getConfigId());
+            reportFeeMonthStatisticsPo.setFeeCreateTime(DateUtil.getFormatTimeString(tmpReportFeeDto.getEndTime(), DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+            reportFeeMonthStatisticsPo.setFeeMonth(DateUtil.getMonth() + "");
+            reportFeeMonthStatisticsPo.setFeeYear(DateUtil.getYear() + "");
+            reportFeeMonthStatisticsPo.setObjId(tmpReportCarDto.getCarId());
+            reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
+            reportFeeMonthStatisticsPo.setFeeName(tmpReportFeeDto.getFeeName());
+            reportFeeMonthStatisticsPo.setObjName(tmpReportCarDto.getCarNum() + "(" + tmpReportCarDto.getAreaNum() + "鍋滆溅鍦�" + tmpReportCarDto.getNum() + "杞︿綅");
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+        }
+
+
+        Date endTime = tmpReportFeeDto.getEndTime();
+
+        Calendar calender = Calendar.getInstance();
+        calender.setTime(endTime);
+        int year = calender.get(Calendar.YEAR);
+        int month = calender.get(Calendar.MONTH);
+
+        ReportFeeMonthStatisticsPo tmpReportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        tmpReportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+        tmpReportFeeMonthStatisticsPo.setFeeYear(year + "");
+        tmpReportFeeMonthStatisticsPo.setFeeMonth(month + "");
+        tmpReportFeeMonthStatisticsPo.setOweAmount("0");
+        reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsOwe(BeanConvertUtil.beanCovertMap(tmpReportFeeMonthStatisticsPo));
+    }
+
     /**
      * 澶勭悊璐圭敤
      *
      * @param reportRoomDto
      */
-    private void doDealRoomFee(ReportRoomDto reportRoomDto) {
+    private void doDealRoomFees(ReportRoomDto reportRoomDto) {
         ReportFeeDto reportFeeDto = new ReportFeeDto();
         reportFeeDto.setPayerObjId(reportRoomDto.getRoomId());
         reportFeeDto.setPayerObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
@@ -110,6 +249,169 @@
             return;
         }
 
+        for (ReportFeeDto tmpReportFeeDto : feeDtos) {
+            try {
+                doDealRoomFee(reportRoomDto, tmpReportFeeDto);
+            } catch (Exception e) {
+                logger.error("澶勭悊鎴垮眿璐圭敤澶辫触" + JSONObject.toJSONString(tmpReportFeeDto), e);
+            }
+        }
 
+
+    }
+
+    private void doDealRoomFee(ReportRoomDto reportRoomDto, ReportFeeDto tmpReportFeeDto) {
+
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(reportRoomDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
+        reportFeeMonthStatisticsDto.setFeeYear(DateUtil.getYear() + "");
+        reportFeeMonthStatisticsDto.setFeeMonth(DateUtil.getMonth() + "");
+        List<ReportFeeMonthStatisticsDto> statistics = BeanConvertUtil.covertBeanList(
+                reportFeeMonthStatisticsServiceDaoImpl.getReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsDto)),
+                ReportFeeMonthStatisticsDto.class);
+
+        double receivedAmount = getReceivedAmount(tmpReportFeeDto); //瀹炴敹
+        //璐圭敤宸茬粡缁撴潫 骞朵笖褰撴湀瀹炴敹涓�0 閭e氨鏄� 涔嬪墠灏辩粨鏉熶簡 鏃犻渶澶勭悊  && ListUtil.isNull(statistics)
+        if (FeeDto.STATE_FINISH.equals(tmpReportFeeDto.getState())
+                && receivedAmount == 0) {
+            return;
+        }
+        double receivableAmount = getReceivableAmount(tmpReportFeeDto); //搴旀敹
+        double oweAmount = getOweAmount(tmpReportFeeDto, receivableAmount, receivedAmount); //娆犺垂
+
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        if (!ListUtil.isNull(statistics)) {
+            ReportFeeMonthStatisticsDto statistic = statistics.get(0);
+            reportFeeMonthStatisticsPo.setStatisticsId(statistic.getStatisticsId());
+            reportFeeMonthStatisticsPo.setReceivableAmount(receivableAmount + "");
+            reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+            reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+        } else {
+            reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
+            reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+            reportFeeMonthStatisticsPo.setReceivableAmount(receivableAmount + "");
+            reportFeeMonthStatisticsPo.setStatisticsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_statisticsId));
+            reportFeeMonthStatisticsPo.setCommunityId(tmpReportFeeDto.getCommunityId());
+            reportFeeMonthStatisticsPo.setConfigId(tmpReportFeeDto.getConfigId());
+            reportFeeMonthStatisticsPo.setFeeCreateTime(DateUtil.getFormatTimeString(tmpReportFeeDto.getEndTime(), DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+            reportFeeMonthStatisticsPo.setFeeMonth(DateUtil.getMonth() + "");
+            reportFeeMonthStatisticsPo.setFeeYear(DateUtil.getYear() + "");
+            reportFeeMonthStatisticsPo.setObjId(reportRoomDto.getRoomId());
+            reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
+            reportFeeMonthStatisticsPo.setFeeName(tmpReportFeeDto.getFeeName());
+            reportFeeMonthStatisticsPo.setObjName(reportRoomDto.getFloorNum() + "鏍�" + reportRoomDto.getUnitNum() + "鍗曞厓" + reportRoomDto.getRoomNum() + "瀹�");
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+        }
+
+
+        Date endTime = tmpReportFeeDto.getEndTime();
+
+        Calendar calender = Calendar.getInstance();
+        calender.setTime(endTime);
+        int year = calender.get(Calendar.YEAR);
+        int month = calender.get(Calendar.MONTH);
+
+        ReportFeeMonthStatisticsPo tmpReportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        tmpReportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+        tmpReportFeeMonthStatisticsPo.setFeeYear(year + "");
+        tmpReportFeeMonthStatisticsPo.setFeeMonth(month + "");
+        tmpReportFeeMonthStatisticsPo.setOweAmount("0");
+        reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsOwe(BeanConvertUtil.beanCovertMap(tmpReportFeeMonthStatisticsPo));
+    }
+
+    /**
+     * 褰撴湀娆犺垂
+     *
+     * @param tmpReportFeeDto
+     * @param receivableAmount
+     * @param receivedAmount
+     * @return
+     */
+    private double getOweAmount(ReportFeeDto tmpReportFeeDto, double receivableAmount, double receivedAmount) {
+
+        if (receivableAmount == 0) {
+            return 0;
+        }
+
+        if (receivedAmount > receivableAmount) {
+            return 0;
+        }
+
+        BigDecimal receivedAmountDec = new BigDecimal(receivedAmount);
+        BigDecimal receivableAmountDec = new BigDecimal(receivedAmount);
+
+        return receivableAmountDec.subtract(receivedAmountDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+    }
+
+    /**
+     * 鑾峰彇褰撴湀瀹炴敹
+     *
+     * @param tmpReportFeeDto
+     * @return
+     */
+    private double getReceivedAmount(ReportFeeDto tmpReportFeeDto) {
+        ReportFeeDetailDto feeDetailDto = new ReportFeeDetailDto();
+        feeDetailDto.setStartTime(DateUtil.getFormatTimeString(DateUtil.getFirstDate(), DateUtil.DATE_FORMATE_STRING_A));
+        feeDetailDto.setEndTime(DateUtil.getFormatTimeString(DateUtil.getNextMonthFirstDate(), DateUtil.DATE_FORMATE_STRING_A));
+        feeDetailDto.setFeeId(tmpReportFeeDto.getFeeId());
+        double receivedAmount = reportFeeServiceDaoImpl.getFeeReceivedAmount(feeDetailDto);
+
+        return receivedAmount;
+    }
+
+    /**
+     * 鑾峰彇褰撴湀搴旀敹
+     *
+     * @param tmpReportFeeDto
+     * @return
+     */
+    private double getReceivableAmount(ReportFeeDto tmpReportFeeDto) {
+        FeeDto feeDto = BeanConvertUtil.covertBean(tmpReportFeeDto, FeeDto.class);
+
+        double feePrice = computeFeeSMOImpl.getFeePrice(feeDto);
+        BigDecimal feePriceDec = new BigDecimal(feePrice);
+
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeTypeCd())) {
+            return feePrice;
+        }
+
+        //1.0 璐圭敤鍒版湡鏃堕棿鍜岃垂鐢ㄧ粨鏉熸椂闂� 閮戒笉鍦ㄥ綋鏈�
+        if (!belongCurMonth(tmpReportFeeDto.getEndTime())
+                && !belongCurMonth(tmpReportFeeDto.getConfigEndTime())
+                && tmpReportFeeDto.getEndTime().getTime() < DateUtil.getFirstDate().getTime()) {
+            return feePrice;
+        }
+
+        //2.0 璐圭敤鍒版湡鏃堕棿 鍦ㄥ綋鏈堬紝璐圭敤缁撴潫鏃堕棿涓嶅湪褰撴湀
+        if (belongCurMonth(tmpReportFeeDto.getEndTime())
+                && !belongCurMonth(tmpReportFeeDto.getConfigEndTime())) {
+            //绠楀ぉ鏁�
+            double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), DateUtil.getNextMonthFirstDate());
+            BigDecimal curDegree = new BigDecimal(month);
+            return curDegree.multiply(feePriceDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        }
+        //3.0 璐圭敤鍒版湡鏃堕棿 涓嶅湪褰撴湀锛岃垂鐢ㄧ粨鏉熸椂闂村湪褰撴湀
+        if (!belongCurMonth(tmpReportFeeDto.getEndTime())
+                && belongCurMonth(tmpReportFeeDto.getConfigEndTime())) {
+            //绠楀ぉ鏁�
+            double month = computeFeeSMOImpl.dayCompare(DateUtil.getFirstDate(), tmpReportFeeDto.getConfigEndTime());
+            BigDecimal curDegree = new BigDecimal(month);
+            return curDegree.multiply(feePriceDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        }
+        return 0.0;
+    }
+
+    private boolean belongCurMonth(Date date) {
+        if (DateUtil.belongCalendar(date, DateUtil.getFirstDate(), DateUtil.getNextMonthFirstDate())) {
+            return true;
+        }
+        return false;
     }
 }

--
Gitblit v1.8.0