From 05683f2b2bdbdbe21cf17ad523c21ab338bd1c54 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期二, 19 七月 2022 21:49:55 +0800
Subject: [PATCH] 优化添加设备 功能

---
 service-report/src/main/java/com/java110/report/smo/impl/GeneratorFeeMonthStatisticsInnerServiceSMOImpl.java |  181 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 146 insertions(+), 35 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 612bad9..ab4860d 100755
--- 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
@@ -2,8 +2,10 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.core.log.LoggerFactory;
 import com.java110.core.smo.IComputeFeeSMO;
 import com.java110.dto.RoomDto;
+import com.java110.dto.fee.FeeConfigDto;
 import com.java110.dto.fee.FeeDto;
 import com.java110.dto.owner.OwnerCarDto;
 import com.java110.dto.report.ReportCarDto;
@@ -17,24 +19,15 @@
 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 com.java110.utils.util.*;
 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;
+import java.util.*;
 
 /**
  * @ClassName GeneratorFeeMonthStatisticsInnerServiceSMOImpl
@@ -79,6 +72,9 @@
 
         Assert.hasLength(communityId, "鏈寘鍚皬鍖轰俊鎭�");
 
+        //杩欓噷澶勭悊 鎶ヨ〃涓殑璐圭敤鏄惁琚汉涓� 鍙栨秷 鎴栬�呰垂鐢ㄩ」鏄惁琚垹闄わ紝杩欑鏁版嵁 鎶ヨ〃涓仛娓呯悊锛屼互闃插奖鍝� 鎶ヨ〃鐨勫噯纭害
+        feeDataFiltering(communityId);
+
         //澶勭悊鎴垮眿璐圭敤
         dealRoomFee(reportFeeMonthStatisticsPo);
 
@@ -87,6 +83,31 @@
 
         //澶勭悊缂磋垂缁撴潫鐨勬儏鍐�
         dealFinishFee(communityId);
+    }
+
+    private void feeDataFiltering(String communityId) {
+        Map reportFeeDto = new HashMap();
+        reportFeeDto.put("communityId", communityId);
+        List<Map> feeDtos = reportFeeMonthStatisticsServiceDaoImpl.queryInvalidFeeMonthStatistics(reportFeeDto);
+
+        List<String> feeIds = new ArrayList<>();
+        for (Map feeDto : feeDtos) {
+            if (!feeDto.containsKey("feeId") || StringUtil.isNullOrNone(feeDto.get("feeId"))) {
+                continue;
+            }
+
+            feeIds.add(feeDto.get("feeId").toString());
+
+            if (feeIds.size() >= 50) {
+                reportFeeDto.put("feeIds", feeIds);
+                reportFeeMonthStatisticsServiceDaoImpl.deleteInvalidFee(reportFeeDto);
+                feeIds = new ArrayList<>();
+            }
+        }
+        reportFeeDto.put("feeIds", feeIds);
+        if (feeIds.size() > 0) {
+            reportFeeMonthStatisticsServiceDaoImpl.deleteInvalidFee(reportFeeDto);
+        }
     }
 
     private void dealFinishFee(String communityId) {
@@ -223,7 +244,7 @@
         reportFeeMonthStatisticsDto.setCommunityId(tmpReportCarDto.getCommunityId());
         reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
         reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
-        //reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());//杩欓噷涓嶈兘娉ㄩ噴 濡傛灉涓�涓垂鐢ㄥ垱寤哄鏉℃椂浼氭湁bug
         reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
         reportFeeMonthStatisticsDto.setFeeYear(DateUtil.getYear() + "");
         reportFeeMonthStatisticsDto.setFeeMonth(DateUtil.getMonth() + "");
@@ -256,6 +277,10 @@
 //            reportFeeMonthStatisticsPo.setReceivedAmount("0");
             reportFeeMonthStatisticsPo.setOweAmount(oweAmount + "");
             reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
+            // 缂磋垂浜嗗氨寰楀埛
+            reportFeeMonthStatisticsPo.setHisOweAmount(getHisOweAmount(tmpReportFeeDto) + "");
+            //鏈夊彲鑳芥槸鏈堝唴鍒涘缓鐨勮垂鐢� 姣斿鐢佃垂姘磋垂
+            reportFeeMonthStatisticsPo.setCurReceivableAmount(getCurFeeReceivableAmount(tmpReportFeeDto) + "");
             reportFeeMonthStatisticsPo.setCurReceivedAmount(getReceivedAmount(tmpReportFeeDto, 1) + "");
             reportFeeMonthStatisticsPo.setHisOweReceivedAmount(getReceivedAmount(tmpReportFeeDto, 2) + "");
             reportFeeMonthStatisticsPo.setPreReceivedAmount(getReceivedAmount(tmpReportFeeDto, 3) + "");
@@ -417,7 +442,7 @@
         reportFeeMonthStatisticsDto.setCommunityId(reportRoomDto.getCommunityId());
         reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
         reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
-        //reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId()); //杩欓噷涓嶈兘娉ㄩ噴锛屽洜涓轰竴涓垂鐢ㄥ娆″垱寤烘椂浼氬瓨鍦ㄨ鐩� 瀛樺湪bug闂
         reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
         reportFeeMonthStatisticsDto.setFeeYear(DateUtil.getYear() + "");
         reportFeeMonthStatisticsDto.setFeeMonth(DateUtil.getMonth() + "");
@@ -453,6 +478,10 @@
             reportFeeMonthStatisticsPo.setFeeId(tmpReportFeeDto.getFeeId());
             reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
             reportFeeMonthStatisticsPo.setFeeName(StringUtil.isEmpty(tmpReportFeeDto.getImportFeeName()) ? tmpReportFeeDto.getFeeName() : tmpReportFeeDto.getImportFeeName());
+            // 缂磋垂浜嗗氨寰楀埛
+            reportFeeMonthStatisticsPo.setHisOweAmount(getHisOweAmount(tmpReportFeeDto) + "");
+            //鏈夊彲鑳芥槸鏈堝唴鍒涘缓鐨勮垂鐢� 姣斿鐢佃垂姘磋垂
+            reportFeeMonthStatisticsPo.setCurReceivableAmount(getCurFeeReceivableAmount(tmpReportFeeDto) + "");
             reportFeeMonthStatisticsPo.setCurReceivedAmount(getReceivedAmount(tmpReportFeeDto, 1) + "");
             reportFeeMonthStatisticsPo.setHisOweReceivedAmount(getReceivedAmount(tmpReportFeeDto, 2) + "");
             reportFeeMonthStatisticsPo.setPreReceivedAmount(getReceivedAmount(tmpReportFeeDto, 3) + "");
@@ -485,6 +514,8 @@
             reportFeeMonthStatisticsPo.setPreReceivedAmount(getReceivedAmount(tmpReportFeeDto, 3) + "");
             reportFeeMonthStatisticsPo.setUpdateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
             reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+            //澶勭悊姘寸數璐癸紝姘寸數璐规牴鎹紑濮嬫椂闂磋鍦ㄧ浉搴旀湀琛ュ厖鏁版嵁
+            dealMeteWater(reportFeeMonthStatisticsPo, tmpReportFeeDto);
         }
 
 
@@ -498,6 +529,45 @@
         reportFeeMonthStatisticsServiceDaoImpl.updateReportFeeMonthStatisticsOwe(BeanConvertUtil.beanCovertMap(tmpReportFeeMonthStatisticsPo));
     }
 
+    /**
+     * 澶勭悊姘寸數璐�
+     *
+     * @param reportFeeMonthStatisticsPo
+     * @param tmpReportFeeDto
+     */
+    private void dealMeteWater(ReportFeeMonthStatisticsPo reportFeeMonthStatisticsPo, ReportFeeDto tmpReportFeeDto) {
+
+        //濡傛灉鏄� 姘磋垂 鐢佃垂 鐓ゆ皵璐�
+        if (!FeeConfigDto.FEE_TYPE_CD_METER.equals(tmpReportFeeDto.getFeeTypeCd())
+                && !FeeConfigDto.FEE_TYPE_CD_WATER.equals(tmpReportFeeDto.getFeeTypeCd())
+                && !FeeConfigDto.FEE_TYPE_CD_GAS.equals(tmpReportFeeDto.getFeeTypeCd())
+        ) {
+            return;
+        }
+        //鏍规嵁璐圭敤寮�濮嬫椂闂� 璁$畻鏈堜唤
+        Date endTime = tmpReportFeeDto.getEndTime();
+        //鍘婚櫎 0 鍥犱负琛ㄩ噷鐨勬湀浠芥槸娌℃湁闆�
+        String curMonth = Integer.parseInt(DateUtil.getFormatTimeString(endTime, "MM"))+"";
+        String curYear = DateUtil.getFormatTimeString(endTime, "YYYY");
+        //鏌ヨ鏄惁瀛樺湪 鏁版嵁
+        ReportFeeMonthStatisticsDto reportFeeMonthStatisticsDto = new ReportFeeMonthStatisticsDto();
+        reportFeeMonthStatisticsDto.setCommunityId(tmpReportFeeDto.getCommunityId());
+        reportFeeMonthStatisticsDto.setConfigId(tmpReportFeeDto.getConfigId());
+        reportFeeMonthStatisticsDto.setObjId(tmpReportFeeDto.getPayerObjId());
+        //reportFeeMonthStatisticsDto.setFeeId(tmpReportFeeDto.getFeeId());
+        reportFeeMonthStatisticsDto.setObjType(tmpReportFeeDto.getPayerObjType());
+        reportFeeMonthStatisticsDto.setFeeYear(curYear);
+        reportFeeMonthStatisticsDto.setFeeMonth(curMonth);
+        List<ReportFeeMonthStatisticsDto> statistics = BeanConvertUtil.covertBeanList(
+                reportFeeMonthStatisticsServiceDaoImpl.getReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsDto)),
+                ReportFeeMonthStatisticsDto.class);
+        if (!ListUtil.isNull(statistics)) {
+            return;
+        }
+        reportFeeMonthStatisticsPo.setStatisticsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_statisticsId));
+        reportFeeMonthStatisticsServiceDaoImpl.saveReportFeeMonthStatisticsInfo(BeanConvertUtil.beanCovertMap(reportFeeMonthStatisticsPo));
+    }
+
 
     /**
      * @return
@@ -508,7 +578,7 @@
         paramIn.put("endTime", DateUtil.getFormatTimeString(DateUtil.getNextMonthFirstDate(), DateUtil.DATE_FORMATE_STRING_A));
         paramIn.put("feeId", tmpReportFeeDto.getFeeId());
         paramIn.put("yearMonth", DateUtil.getFormatTimeString(DateUtil.getFirstDate(), DateUtil.DATE_FORMATE_STRING_M));
-        paramIn.put("flag", 1);
+        paramIn.put("flag", flag);
         double curReceivedAmount = reportFeeMonthStatisticsServiceDaoImpl.getReceivedAmountByMonth(paramIn);
         return curReceivedAmount;
     }
@@ -523,22 +593,46 @@
         BigDecimal feePriceDec = new BigDecimal(tmpReportFeeDto.getFeePrice());
 
         Date curDate = DateUtil.getFirstDate();
+        Date nowDate = DateUtil.getCurrentDate();
         double month = 0.0;
         //宸茬粡瓒呰繃鎴鏃堕棿 鍜� 杩樻病鏈夊埌寮�濮嬫椂闂�
         if (curDate.getTime() > tmpReportFeeDto.getDeadlineTime().getTime()
-                || curDate.getTime() < tmpReportFeeDto.getEndTime().getTime()) {
+                || nowDate.getTime() < tmpReportFeeDto.getEndTime().getTime()) {
             return 0.0;
         }
-        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
-            month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), tmpReportFeeDto.getDeadlineTime());
-            month = Math.ceil(month);
-            if (month == 0) {
-                return 0.0;
-            }
-            double money = feePriceDec.divide(new BigDecimal(month), 2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
-            return money;
+
+        //濡傛灉 1鍙峰皬浜� 璁¤垂璧峰鏃堕棿 閭d箞 灏哻urDate 鏀逛负 褰撳墠鏃堕棿
+        if (curDate.getTime() < tmpReportFeeDto.getEndTime().getTime()) {
+            curDate = tmpReportFeeDto.getEndTime();
         }
-        month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), curDate);
+
+        //杩欓噷闇�瑕佸垽鏂� 缁撴潫鏃堕棿 鏄惁 澶т簬浜嗗綋鏈堟渶鍚庝竴澶╋紝褰撴湀澶╂暟 * 姣忓ぉ閲戦
+        Calendar nextDateC = Calendar.getInstance();
+        nextDateC.setTime(curDate);
+        nextDateC.add(Calendar.MONTH, 1);
+        Date nextDate = nextDateC.getTime();
+
+        if (FeeDto.FEE_FLAG_ONCE.equals(tmpReportFeeDto.getFeeFlag())) {
+            double allDays = computeFeeSMOImpl.daysBetween(tmpReportFeeDto.getEndTime(), tmpReportFeeDto.getDeadlineTime());
+            allDays = Math.ceil(allDays);
+            if (allDays == 0) { // 闃叉闄ゆ暟涓�0
+                return 0;
+            }
+            BigDecimal moneyPreDay = feePriceDec.divide(new BigDecimal(allDays), 2, BigDecimal.ROUND_HALF_EVEN);
+            if (tmpReportFeeDto.getDeadlineTime().getTime() > nextDate.getTime()) {
+                int day = DateUtil.getCurrentMonthDay();
+                return moneyPreDay.multiply(new BigDecimal(day)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+            }
+            // 缁撴潫鏃堕棿 鍦ㄥ綋鏈堝唴
+            double hisDays = computeFeeSMOImpl.daysBetween(tmpReportFeeDto.getEndTime(), curDate);
+            BigDecimal hisDayDec = moneyPreDay.multiply(new BigDecimal(hisDays)).setScale(2, BigDecimal.ROUND_HALF_UP);
+            return feePriceDec.subtract(hisDayDec).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+        }
+
+        month = computeFeeSMOImpl.dayCompare(curDate, tmpReportFeeDto.getDeadlineTime());
+        if (month < 0) {
+            return 0;
+        }
         if (month < 1) {
             return feePriceDec.multiply(new BigDecimal(month)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
         }
@@ -556,7 +650,7 @@
     private double getHisOweAmount(ReportFeeDto tmpReportFeeDto) {
 
         BigDecimal feePriceDec = new BigDecimal(tmpReportFeeDto.getFeePrice());
-        Date curDate = DateUtil.getFirstDate();
+        Date curDate = DateUtil.getFirstDate();//11鏈�1鏃�
         //璇存槑娌℃湁鍘嗗彶娆犺垂
         if (curDate.getTime() < tmpReportFeeDto.getEndTime().getTime()) {
             return 0.0;
@@ -567,10 +661,17 @@
             if (tmpReportFeeDto.getDeadlineTime().getTime() < curDate.getTime()) {
                 return tmpReportFeeDto.getFeePrice();
             }
-            double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getDeadlineTime(), tmpReportFeeDto.getEndTime());
-            month = Math.ceil(month);
-            double money = feePriceDec.divide(new BigDecimal(month), 2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
-            return money;
+            double allDays = computeFeeSMOImpl.daysBetween(tmpReportFeeDto.getEndTime(), tmpReportFeeDto.getDeadlineTime());
+            allDays = Math.ceil(allDays);
+            if (allDays == 0) { // 闃叉闄ゆ暟涓�0
+                return 0;
+            }
+            //杩欐槸姣忓ぉ鐨勯挶
+            BigDecimal moneyPreDay = feePriceDec.divide(new BigDecimal(allDays), 2, BigDecimal.ROUND_HALF_EVEN);
+
+            double hisDays = computeFeeSMOImpl.daysBetween(tmpReportFeeDto.getEndTime(), curDate);
+
+            return moneyPreDay.multiply(new BigDecimal(hisDays)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
         }
 
         double month = 0.0;
@@ -751,6 +852,11 @@
         }
         Date endTime = ownerCarDtos.get(0).getEndTime();
 
+        Date maxEndDate = tmpReportFeeDto.getDeadlineTime();
+        if (FeeDto.FEE_FLAG_CYCLE.equals(tmpReportFeeDto.getFeeFlag())) {
+            maxEndDate = tmpReportFeeDto.getConfigEndTime();
+        }
+
         //1.0 璐圭敤鍒版湡鏃堕棿鍜岃垂鐢ㄧ粨鏉熸椂闂� 閮戒笉鍦ㄥ綋鏈�
         if (!belongCurMonth(tmpReportFeeDto.getEndTime())
                 && !belongCurMonth(endTime)
@@ -770,7 +876,7 @@
         if (!belongCurMonth(tmpReportFeeDto.getEndTime())
                 && belongCurMonth(endTime)) {
             //绠楀ぉ鏁�
-            double month = computeFeeSMOImpl.dayCompare(DateUtil.getFirstDate(), tmpReportFeeDto.getConfigEndTime());
+            double month = computeFeeSMOImpl.dayCompare(DateUtil.getFirstDate(), maxEndDate);
             BigDecimal curDegree = new BigDecimal(month);
             return curDegree.multiply(feePriceDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
         }
@@ -787,7 +893,7 @@
         month = Math.ceil(month);
 
         if (month == 0) {
-            logger.debug("鐩稿樊鏈堜唤涓�0锛寋}", JSONObject.toJSONString(tmpReportFeeDto);
+            logger.debug("鐩稿樊鏈堜唤涓�0锛寋}", JSONObject.toJSONString(tmpReportFeeDto));
             return tmpReportFeeDto.getFeePrice();
         }
 
@@ -816,16 +922,21 @@
             return computeOnceFee(tmpReportFeeDto);
         }
 
+        Date maxEndDate = tmpReportFeeDto.getDeadlineTime();
+        if (FeeDto.FEE_FLAG_CYCLE.equals(tmpReportFeeDto.getFeeFlag())) {
+            maxEndDate = tmpReportFeeDto.getConfigEndTime();
+        }
+
         //1.0 璐圭敤鍒版湡鏃堕棿鍜岃垂鐢ㄧ粨鏉熸椂闂� 閮戒笉鍦ㄥ綋鏈�
         if (!belongCurMonth(tmpReportFeeDto.getEndTime())
-                && !belongCurMonth(tmpReportFeeDto.getConfigEndTime())
+                && !belongCurMonth(maxEndDate)
                 && tmpReportFeeDto.getEndTime().getTime() < DateUtil.getFirstDate().getTime()) {
             return feePrice;
         }
 
         //2.0 璐圭敤鍒版湡鏃堕棿 鍦ㄥ綋鏈堬紝璐圭敤缁撴潫鏃堕棿涓嶅湪褰撴湀
         if (belongCurMonth(tmpReportFeeDto.getEndTime())
-                && !belongCurMonth(tmpReportFeeDto.getConfigEndTime())) {
+                && !belongCurMonth(maxEndDate)) {
             //绠楀ぉ鏁�
             double month = computeFeeSMOImpl.dayCompare(tmpReportFeeDto.getEndTime(), DateUtil.getNextMonthFirstDate());
             BigDecimal curDegree = new BigDecimal(month);
@@ -833,9 +944,9 @@
         }
         //3.0 璐圭敤鍒版湡鏃堕棿 涓嶅湪褰撴湀锛岃垂鐢ㄧ粨鏉熸椂闂村湪褰撴湀
         if (!belongCurMonth(tmpReportFeeDto.getEndTime())
-                && belongCurMonth(tmpReportFeeDto.getConfigEndTime())) {
+                && belongCurMonth(maxEndDate)) {
             //绠楀ぉ鏁�
-            double month = computeFeeSMOImpl.dayCompare(DateUtil.getFirstDate(), tmpReportFeeDto.getConfigEndTime());
+            double month = computeFeeSMOImpl.dayCompare(DateUtil.getFirstDate(), maxEndDate);
             BigDecimal curDegree = new BigDecimal(month);
             return curDegree.multiply(feePriceDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
         }

--
Gitblit v1.8.0