From c8195a8a1c34b9fa8db9aa4dc340d4344044ce97 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期二, 29 六月 2021 20:52:03 +0800
Subject: [PATCH] Merge branch 'master' of http://git.homecommunity.cn/supervip/MicroCommunity

---
 service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java |  648 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 637 insertions(+), 11 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
old mode 100644
new mode 100755
index 49c44c5..28076bb
--- 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,22 +1,41 @@
 package com.java110.report.smo.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.core.smo.IComputeFeeSMO;
+import com.java110.dto.RoomDto;
 import com.java110.dto.fee.FeeDto;
+import com.java110.dto.owner.OwnerCarDto;
+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.intf.user.IOwnerCarInnerServiceSMO;
 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 com.java110.utils.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 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.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @ClassName GeneratorFeeMonthStatisticsInnerServiceSMOImpl
@@ -42,9 +61,21 @@
     @Autowired
     private IReportFeeServiceDao reportFeeServiceDaoImpl;
 
+    @Autowired
+    private IComputeFeeSMO computeFeeSMOImpl;
+
+    @Autowired
+    private IOwnerCarInnerServiceSMO ownerCarInnerServiceSMOImpl;
+
     @Override
     public int generatorData(@RequestBody ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo) {
 
+        doGeneratorData(reportFeeMonthStatisticsPo);
+        return 0;
+    }
+
+    @Async
+    private void doGeneratorData(ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo) {
         String communityId = reportFeeMonthStatisticsPo.getCommunityId();
 
         Assert.hasLength(communityId, "鏈寘鍚皬鍖轰俊鎭�");
@@ -52,8 +83,76 @@
         //澶勭悊鎴垮眿璐圭敤
         dealRoomFee(reportFeeMonthStatisticsPo);
 
+        //澶勭悊杞︿綅璐圭敤
+        dealCarFee(reportFeeMonthStatisticsPo);
 
-        return 0;
+        //澶勭悊缂磋垂缁撴潫鐨勬儏鍐�
+        dealFinishFee(communityId);
+    }
+
+    private void dealFinishFee(String communityId) {
+        Map reportFeeDto = new HashMap();
+        reportFeeDto.put("communityId", communityId);
+        List<Map> feeDtos = reportFeeMonthStatisticsServiceDaoImpl.queryFinishOweFee(reportFeeDto);
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = null;
+        for (Map info : feeDtos) {
+            try {
+                Date tmpTime = DateUtil.getDateFromString(info.get("feeYear").toString() + "-" + info.get("feeMonth").toString() + "-01", DateUtil.DATE_FORMATE_STRING_B);
+                Calendar c = Calendar.getInstance();
+                c.setTime(tmpTime);
+                c.add(Calendar.MONTH, 1);
+                ReportFeeDetailDto feeDetailDto = new ReportFeeDetailDto();
+                feeDetailDto.setStartTime(DateUtil.getFormatTimeString(tmpTime, DateUtil.DATE_FORMATE_STRING_A));
+                feeDetailDto.setEndTime(DateUtil.getFormatTimeString(c.getTime(), DateUtil.DATE_FORMATE_STRING_A));
+                feeDetailDto.setFeeId(info.get("feeId").toString());
+                double receivedAmount = reportFeeServiceDaoImpl.getFeeReceivedAmount(feeDetailDto);
+                reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+                reportFeeMonthStatisticsPo.setStatisticsId(info.get("statisticsId").toString());
+                reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+                reportFeeMonthStatisticsPo.setOweAmount("0");
+                reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+                reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+            } catch (Exception e) {
+                logger.error("澶勭悊 缂磋垂缁撴潫鎶ヨ〃澶辫触", e);
+            }
+        }
+    }
+
+    /**
+     * 澶勭悊杞︿綅 杞﹁締璐圭敤
+     *
+     * @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 +185,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,22 +193,549 @@
         }
     }
 
-    /**
-     * 澶勭悊璐圭敤
-     *
-     * @param reportRoomDto
-     */
-    private void doDealRoomFee(ReportRoomDto reportRoomDto) {
+    private void doDealCarFees(ReportCarDto tmpReportCarDto) {
         ReportFeeDto reportFeeDto = new ReportFeeDto();
-        reportFeeDto.setPayerObjId(reportRoomDto.getRoomId());
-        reportFeeDto.setPayerObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
-        reportFeeDto.setState(FeeDto.STATE_DOING);
+        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) {
+
+        //璐圭敤宸茬粡缁撴潫 骞朵笖褰撴湀瀹炴敹涓�0 閭e氨鏄� 涔嬪墠灏辩粨鏉熶簡 鏃犻渶澶勭悊  && ListUtil.isNull(statistics)
+        if (FeeDto.STATE_FINISH.equals(tmpReportFeeDto.getState())
+                && getCurFeeReceivedAmount(tmpReportFeeDto) == 0) {
+            return;
+        }
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(tmpReportCarDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        //reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        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); //瀹炴敹
+
+        FeeDto feeDto = BeanConvertUtil.covertBean(tmpReportFeeDto, FeeDto.class);
+        OwnerCarDto ownerCarDto = BeanConvertUtil.covertBean(tmpReportCarDto, OwnerCarDto.class);
+        Map<String, Object> targetEndDateAndOweMonth = computeFeeSMOImpl.getTargetEndDateAndOweMonth(feeDto, ownerCarDto);
+
+        Date targetEndDate = (Date) targetEndDateAndOweMonth.get("targetEndDate");
+        tmpReportFeeDto.setDeadlineTime(targetEndDate);
+        double oweAmount = getOweAmountByCar(tmpReportFeeDto, null, tmpReportCarDto); //搴旀敹
+
+        dealBeforeUploadCarFee(tmpReportFeeDto, tmpReportCarDto);
+        double receivableAmount = getReceivableAmount(tmpReportFeeDto,receivedAmount); //娆犺垂
+
+
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        reportFeeMonthStatisticsPo.setDeadlineTime(DateUtil.getFormatTimeString(targetEndDate, DateUtil.DATE_FORMATE_STRING_A));
+        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.setCurMaxTime(DateUtil.getNextMonthFirstDay(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsPo.setObjId(tmpReportCarDto.getCarId());
+            reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_CAR);
+            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.setCurMaxTime(DateUtil.getFormatTimeString(endTime,DateUtil.DATE_FORMATE_STRING_A));
+        tmpReportFeeMonthStatisticsPo.setOweAmount("0");
+        reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsOwe(BeanConvertUtil.beanCovertMap(tmpReportFeeMonthStatisticsPo));
+    }
+
+    /**
+     * 瑙e喅涓婄嚎鍓� 娆犺垂鏁版嵁
+     *
+     * @param tmpReportCarDto
+     * @param tmpReportFeeDto
+     */
+    private void dealBeforeUploadCarFee(ReportFeeDto tmpReportFeeDto, ReportCarDto tmpReportCarDto) {
+
+
+        Calendar preMonthDate = Calendar.getInstance();
+        preMonthDate.set(Calendar.DAY_OF_MONTH, 1);
+        preMonthDate.add(Calendar.DAY_OF_MONTH, -1);
+
+        //褰撴湀涓�鏃�
+        Calendar curMonthDate = Calendar.getInstance();
+        curMonthDate.set(Calendar.DAY_OF_MONTH, 1);
+        curMonthDate.set(Calendar.HOUR_OF_DAY, 0);
+        curMonthDate.set(Calendar.MINUTE, 0);
+        curMonthDate.set(Calendar.SECOND, 0);
+        if (tmpReportFeeDto.getEndTime().getTime() > curMonthDate.getTime().getTime()) { //璇存槑娌℃湁娆犺垂
+            return;
+        }
+
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(tmpReportCarDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
+        reportFeeMonthStatisticsDto.setFeeYear(preMonthDate.get(Calendar.YEAR) + "");
+        reportFeeMonthStatisticsDto.setFeeMonth((preMonthDate.get(Calendar.MONTH) + 1) + "");
+        List<ReportFeeMonthStatisticsDto> statistics = BeanConvertUtil.covertBeanList(
+                reportFeeMonthStatisticsServiceDaoImpl.getReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsDto)),
+                ReportFeeMonthStatisticsDto.class);
+        //涓婁釜鏈堟湁鏁版嵁 涓嶅鐞�
+        if (statistics != null && statistics.size() > 0) {
+            return;
+        }
+
+        if (tmpReportFeeDto.getDeadlineTime().getTime() < curMonthDate.getTime().getTime()) {
+            curMonthDate.setTime(tmpReportFeeDto.getDeadlineTime());
+        }
+
+        double receivableAmount = 0.0;
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            receivableAmount = tmpReportFeeDto.getFeePrice();
+        } else {
+            double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), curMonthDate.getTime());
+            BigDecimal curDegree = new BigDecimal(month);
+            receivableAmount = curDegree.multiply(new BigDecimal(tmpReportFeeDto.getFeePrice())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        }
+
+
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        reportFeeMonthStatisticsPo.setDeadlineTime(DateUtil.getFormatTimeString(curMonthDate.getTime(), DateUtil.DATE_FORMATE_STRING_A));
+
+        reportFeeMonthStatisticsPo.setOweAmount(receivableAmount + "");
+        reportFeeMonthStatisticsPo.setReceivedAmount("0");
+        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((preMonthDate.get(Calendar.MONTH) + 1) + "");
+        reportFeeMonthStatisticsPo.setFeeYear(preMonthDate.get(Calendar.YEAR) + "");
+        reportFeeMonthStatisticsPo.setCurMaxTime(DateUtil.getFormatTimeString(DateUtil.getFirstDate(),DateUtil.DATE_FORMATE_STRING_A));
+        reportFeeMonthStatisticsPo.setObjId(tmpReportCarDto.getCarId());
+        reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_CAR);
+        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));
+
+
+    }
+
+    /**
+     * 澶勭悊璐圭敤
+     *
+     * @param reportRoomDto
+     */
+    private void doDealRoomFees(ReportRoomDto reportRoomDto) {
+        ReportFeeDto reportFeeDto = new ReportFeeDto();
+        reportFeeDto.setPayerObjId(reportRoomDto.getRoomId());
+        reportFeeDto.setPayerObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
+        //reportFeeDto.setState(FeeDto.STATE_DOING);
+        List<ReportFeeDto> feeDtos = reportFeeServiceDaoImpl.getFees(reportFeeDto);
+
+        if (feeDtos == null || feeDtos.size() < 1) {
+            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) {
+
+        //璐圭敤宸茬粡缁撴潫 骞朵笖褰撴湀瀹炴敹涓�0 閭e氨鏄� 涔嬪墠灏辩粨鏉熶簡 鏃犻渶澶勭悊  && ListUtil.isNull(statistics)
+        if (FeeDto.STATE_FINISH.equals(tmpReportFeeDto.getState())
+                && getCurFeeReceivedAmount(tmpReportFeeDto) == 0) {
+            return;
+        }
+
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(reportRoomDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        //reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        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); //瀹炴敹
+
+        FeeDto feeDto = BeanConvertUtil.covertBean(tmpReportFeeDto, FeeDto.class);
+        Map<String, Object> targetEndDateAndOweMonth = computeFeeSMOImpl.getTargetEndDateAndOweMonth(feeDto, null);
+
+        Date targetEndDate = (Date) targetEndDateAndOweMonth.get("targetEndDate");
+        tmpReportFeeDto.setDeadlineTime(targetEndDate);
+        double oweAmount = getOweAmount(tmpReportFeeDto, reportRoomDto, null); //娆犺垂
+
+        double receivableAmount = getReceivableAmount(tmpReportFeeDto, receivedAmount); //搴旀敹
+        //瑙e喅涓婄嚎鏃� 涔嬪墠娆犺垂娌℃湁鍒峰叆瀵艰嚧璐圭敤閲戦瀵逛笉涓婇棶棰樺鐞�
+        dealBeforeUploadRoomFee(reportRoomDto, tmpReportFeeDto, receivableAmount);
+
+
+
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        reportFeeMonthStatisticsPo.setDeadlineTime(DateUtil.getFormatTimeString(targetEndDate, DateUtil.DATE_FORMATE_STRING_A));
+        if (!ListUtil.isNull(statistics)) {
+            ReportFeeMonthStatisticsDto statistic = statistics.get(0);
+            reportFeeMonthStatisticsPo.setStatisticsId(statistic.getStatisticsId());
+            //reportFeeMonthStatisticsPo.setReceivableAmount(receivableAmount + "");
+            reportFeeMonthStatisticsPo.setReceivedAmount(receivedAmount + "");
+            reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
+            reportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsPo.setFeeName(StringUtil.isEmpty(tmpReportFeeDto.getImportFeeName()) ? tmpReportFeeDto.getFeeName() : tmpReportFeeDto.getImportFeeName());
+            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.setCurMaxTime(DateUtil.getNextMonthFirstDay(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsPo.setObjId(reportRoomDto.getRoomId());
+            reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
+            reportFeeMonthStatisticsPo.setFeeName(StringUtil.isEmpty(tmpReportFeeDto.getImportFeeName()) ? tmpReportFeeDto.getFeeName() : tmpReportFeeDto.getImportFeeName());
+            if (RoomDto.ROOM_TYPE_ROOM.equals(reportRoomDto.getRoomType())) {
+                reportFeeMonthStatisticsPo.setObjName(reportRoomDto.getFloorNum() + "鏍�" + reportRoomDto.getUnitNum() + "鍗曞厓" + reportRoomDto.getRoomNum() + "瀹�");
+            } else {
+                reportFeeMonthStatisticsPo.setObjName(reportRoomDto.getFloorNum() + "鏍�" + reportRoomDto.getRoomNum() + "瀹�");
+            }
+            reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+        }
+
+
+        //灏嗙即璐� 鍒版湡鏃堕棿涔嬪墠寰楁瑺璐瑰埛涓�0
+        Date endTime = tmpReportFeeDto.getEndTime();
+
+        ReportFeeMonthStatisticsPo tmpReportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        tmpReportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
+        tmpReportFeeMonthStatisticsPo.setCurMaxTime(DateUtil.getFormatTimeString(endTime,DateUtil.DATE_FORMATE_STRING_A));
+        tmpReportFeeMonthStatisticsPo.setOweAmount("0");
+        reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsOwe(BeanConvertUtil.beanCovertMap(tmpReportFeeMonthStatisticsPo));
+    }
+
+    public static void main(String[] args) {
+        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));
+
+        System.out.println(JSONObject.toJSONString(feeDetailDto));
+    }
+
+    /**
+     * 瑙e喅涓婄嚎鍓� 娆犺垂鏁版嵁
+     *
+     * @param reportRoomDto
+     * @param tmpReportFeeDto
+     */
+    private void dealBeforeUploadRoomFee(ReportRoomDto reportRoomDto, ReportFeeDto tmpReportFeeDto, double curMonthReceivableAmount) {
+
+
+        Calendar preMonthDate = Calendar.getInstance();
+        preMonthDate.set(Calendar.DAY_OF_MONTH, 1);
+        preMonthDate.add(Calendar.DAY_OF_MONTH, -1);
+
+        //褰撴湀涓�鏃�
+        Calendar curMonthDate = Calendar.getInstance();
+        curMonthDate.set(Calendar.DAY_OF_MONTH, 1);
+        curMonthDate.set(Calendar.HOUR_OF_DAY, 0);
+        curMonthDate.set(Calendar.MINUTE, 0);
+        curMonthDate.set(Calendar.SECOND, 0);
+        if (tmpReportFeeDto.getEndTime().getTime() > curMonthDate.getTime().getTime()) { //璇存槑娌℃湁娆犺垂
+            return;
+        }
+
+        if (tmpReportFeeDto.getDeadlineTime().getTime() < curMonthDate.getTime().getTime()) {
+            curMonthDate.setTime(tmpReportFeeDto.getDeadlineTime());
+        }
+
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(reportRoomDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
+        reportFeeMonthStatisticsDto.setFeeYear(preMonthDate.get(Calendar.YEAR) + "");
+        reportFeeMonthStatisticsDto.setFeeMonth((preMonthDate.get(Calendar.MONTH) + 1) + "");
+        List<ReportFeeMonthStatisticsDto> statistics = BeanConvertUtil.covertBeanList(
+                reportFeeMonthStatisticsServiceDaoImpl.getReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsDto)),
+                ReportFeeMonthStatisticsDto.class);
+        //涓婁釜鏈堟湁鏁版嵁 涓嶅鐞�
+        if (statistics != null && statistics.size() > 0) {
+            return;
+        }
+
+        double receivableAmount = 0.0;
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            receivableAmount = tmpReportFeeDto.getFeePrice();
+        } else {
+            double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), curMonthDate.getTime());
+            BigDecimal curDegree = new BigDecimal(month);
+            receivableAmount = curDegree.multiply(new BigDecimal(tmpReportFeeDto.getFeePrice())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        }
+        ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo = new ReportFeeMonthStatisticsPo();
+        reportFeeMonthStatisticsPo.setDeadlineTime(DateUtil.getFormatTimeString(curMonthDate.getTime(), DateUtil.DATE_FORMATE_STRING_A));
+
+        reportFeeMonthStatisticsPo.setOweAmount(receivableAmount + "");
+        reportFeeMonthStatisticsPo.setReceivedAmount("0");
+        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((preMonthDate.get(Calendar.MONTH) + 1) + "");
+        reportFeeMonthStatisticsPo.setFeeYear(preMonthDate.get(Calendar.YEAR) + "");
+        reportFeeMonthStatisticsPo.setCurMaxTime(DateUtil.getFormatTimeString(DateUtil.getFirstDate(),DateUtil.DATE_FORMATE_STRING_A));
+
+        reportFeeMonthStatisticsPo.setObjId(reportRoomDto.getRoomId());
+        reportFeeMonthStatisticsPo.setObjType(FeeDto.PAYER_OBJ_TYPE_ROOM);
+        reportFeeMonthStatisticsPo.setFeeName(StringUtil.isEmpty(tmpReportFeeDto.getImportFeeName()) ? tmpReportFeeDto.getFeeName() : tmpReportFeeDto.getImportFeeName());
+        if (RoomDto.ROOM_TYPE_ROOM.equals(reportRoomDto.getRoomType())) {
+            reportFeeMonthStatisticsPo.setObjName(reportRoomDto.getFloorNum() + "鏍�" + reportRoomDto.getUnitNum() + "鍗曞厓" + reportRoomDto.getRoomNum() + "瀹�");
+        } else {
+            reportFeeMonthStatisticsPo.setObjName(reportRoomDto.getFloorNum() + "鏍�" + reportRoomDto.getRoomNum() + "瀹�");
+        }
+        reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+        reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+
+
+    }
+
+    /**
+     * 褰撴湀娆犺垂
+     *
+     * @param tmpReportFeeDto
+     * @param receivedAmount
+     * @return
+     */
+    private double getReceivableAmount(ReportFeeDto tmpReportFeeDto, double receivedAmount) {
+
+        //涓�娆℃�ц垂鐢� 闄や互鏈堜唤 骞冲潎
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            return computeOnceFee(tmpReportFeeDto);
+        }
+        return tmpReportFeeDto.getFeePrice();
+
+    }
+
+    /**
+     * 鑾峰彇褰撴湀瀹炴敹
+     *
+     * @param tmpReportFeeDto
+     * @return
+     */
+    private double getCurFeeReceivedAmount(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 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.setConfigId(tmpReportFeeDto.getConfigId());
+        feeDetailDto.setPayerObjId(tmpReportFeeDto.getPayerObjId());
+
+        double receivedAmount = reportFeeServiceDaoImpl.getFeeReceivedAmount(feeDetailDto);
+
+        return receivedAmount;
+    }
+
+    /**
+     * 鑾峰彇褰撴湀搴旀敹
+     *
+     * @param tmpReportFeeDto
+     * @return
+     */
+    private double getOweAmountByCar(ReportFeeDto tmpReportFeeDto, ReportRoomDto reportRoomDto, ReportCarDto reportCarDto) {
+
+        double feePrice = computeFeeSMOImpl.getReportFeePrice(tmpReportFeeDto, reportRoomDto, reportCarDto);
+        tmpReportFeeDto.setFeePrice(feePrice);
+        BigDecimal feePriceDec = new BigDecimal(feePrice);
+
+        if (DateUtil.getCurrentDate().getTime() < tmpReportFeeDto.getStartTime().getTime()) {
+            return 0.0;
+        }
+
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            return computeOnceFee(tmpReportFeeDto);
+        }
+        OwnerCarDto ownerCarDto = new OwnerCarDto();
+        ownerCarDto.setCommunityId(tmpReportFeeDto.getCommunityId());
+        ownerCarDto.setCarId(tmpReportFeeDto.getCarId());
+        List<OwnerCarDto> ownerCarDtos = ownerCarInnerServiceSMOImpl.queryOwnerCars(ownerCarDto);
+        if (ownerCarDtos == null || ownerCarDtos.size() < 1) {
+            return 0.0;
+        }
+        Date endTime = ownerCarDtos.get(0).getEndTime();
+
+        //1.0 璐圭敤鍒版湡鏃堕棿鍜岃垂鐢ㄧ粨鏉熸椂闂� 閮戒笉鍦ㄥ綋鏈�
+        if (!belongCurMonth(tmpReportFeeDto.getEndTime())
+                && !belongCurMonth(endTime)
+                && tmpReportFeeDto.getEndTime().getTime() < DateUtil.getFirstDate().getTime()) {
+            return feePrice;
+        }
+
+        //2.0 璐圭敤鍒版湡鏃堕棿 鍦ㄥ綋鏈堬紝璐圭敤缁撴潫鏃堕棿涓嶅湪褰撴湀
+        if (belongCurMonth(tmpReportFeeDto.getEndTime())
+                && !belongCurMonth(endTime)) {
+            //绠楀ぉ鏁�
+            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(endTime)) {
+            //绠楀ぉ鏁�
+            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 double computeOnceFee(ReportFeeDto tmpReportFeeDto) {
+        Date nowTime = DateUtil.getCurrentDate();
+        if (tmpReportFeeDto.getEndTime().getTime() > nowTime.getTime()
+                || tmpReportFeeDto.getDeadlineTime().getTime() < nowTime.getTime()) {
+            return 0.0;
+        }
+        double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getDeadlineTime(), tmpReportFeeDto.getEndTime());
+        month = Math.ceil(month);
+
+        BigDecimal feePriceDec = new BigDecimal(tmpReportFeeDto.getFeePrice());
+        double money = feePriceDec.divide(new BigDecimal(month),2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        return money;
+    }
+
+    /**
+     * 鑾峰彇褰撴湀搴旀敹
+     *
+     * @param tmpReportFeeDto
+     * @return
+     */
+    private double getOweAmount(ReportFeeDto tmpReportFeeDto, ReportRoomDto reportRoomDto, ReportCarDto reportCarDto) {
+
+        double feePrice = computeFeeSMOImpl.getReportFeePrice(tmpReportFeeDto, reportRoomDto, reportCarDto);
+        tmpReportFeeDto.setFeePrice(feePrice);
+        BigDecimal feePriceDec = new BigDecimal(feePrice);
+
+        if (DateUtil.getCurrentDate().getTime() < tmpReportFeeDto.getStartTime().getTime()) {
+            return 0.0;
+        }
+
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            return computeOnceFee(tmpReportFeeDto);
+        }
+
+        //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