LateFeeByDayRule.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2017-2020 吴学文 and java110 team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.java110.fee.discount.impl;
  17. import com.java110.core.smo.IComputeFeeSMO;
  18. import com.java110.dto.fee.FeeDto;
  19. import com.java110.dto.fee.ComputeDiscountDto;
  20. import com.java110.dto.fee.FeeDiscountDto;
  21. import com.java110.dto.fee.FeeDiscountSpecDto;
  22. import com.java110.fee.discount.IComputeDiscount;
  23. import com.java110.intf.fee.IFeeInnerServiceSMO;
  24. import com.java110.utils.util.DateUtil;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Component;
  27. import java.math.BigDecimal;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * @desc add by 吴学文 12:43
  34. */
  35. @Component(value = "lateFeeByDayRule")
  36. public class LateFeeByDayRule implements IComputeDiscount {
  37. /**
  38. * 89002020980001 102020001 月份
  39. * 89002020980002 102020001 打折率
  40. */
  41. private static final String SPEC_RATE = "89002020980005"; // 打折率
  42. private static final String SPEC_DAY = "89002020980018"; // 延迟天数
  43. @Autowired
  44. private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
  45. @Autowired
  46. private IComputeFeeSMO computeFeeSMOImpl;
  47. @Override
  48. public ComputeDiscountDto compute(FeeDiscountDto feeDiscountDto) {
  49. List<FeeDiscountSpecDto> feeDiscountSpecDtos = feeDiscountDto.getFeeDiscountSpecs();
  50. if (feeDiscountSpecDtos.isEmpty()) {
  51. return null;
  52. }
  53. double rate = 0.0;
  54. int delayDay = 1;
  55. for (FeeDiscountSpecDto feeDiscountSpecDto : feeDiscountSpecDtos) {
  56. if (SPEC_RATE.equals(feeDiscountSpecDto.getSpecId())) {
  57. rate = Double.parseDouble(feeDiscountSpecDto.getSpecValue());
  58. }
  59. if (SPEC_DAY.equals(feeDiscountSpecDto.getSpecId())) {
  60. delayDay = Integer.parseInt(feeDiscountSpecDto.getSpecValue());
  61. }
  62. }
  63. FeeDto feeDto = new FeeDto();
  64. feeDto.setCommunityId(feeDiscountDto.getCommunityId());
  65. feeDto.setFeeId(feeDiscountDto.getFeeId());
  66. List<FeeDto> feeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
  67. // Date curTime = DateUtil.getCurrentDate();
  68. Calendar calendar = Calendar.getInstance();
  69. calendar.add(Calendar.DAY_OF_MONTH, delayDay * -1);
  70. Date curTime = calendar.getTime();
  71. Date endTime = feeDtos.get(0).getEndTime();
  72. if (endTime.getTime() > curTime.getTime()) {
  73. ComputeDiscountDto computeDiscountDto = new ComputeDiscountDto();
  74. computeDiscountDto.setDiscountId(feeDiscountDto.getDiscountId());
  75. computeDiscountDto.setDiscountType(FeeDiscountDto.DISCOUNT_TYPE_V);
  76. computeDiscountDto.setRuleId(feeDiscountDto.getRuleId());
  77. computeDiscountDto.setRuleName(feeDiscountDto.getRuleName());
  78. computeDiscountDto.setDiscountName(feeDiscountDto.getDiscountName());
  79. computeDiscountDto.setDiscountPrice(0.0);
  80. computeDiscountDto.setFeeDiscountSpecs(feeDiscountSpecDtos);
  81. return computeDiscountDto;
  82. }
  83. //查询费用
  84. int day = DateUtil.daysBetween(curTime, endTime);
  85. if (day < 1) {
  86. ComputeDiscountDto computeDiscountDto = new ComputeDiscountDto();
  87. computeDiscountDto.setDiscountId(feeDiscountDto.getDiscountId());
  88. computeDiscountDto.setDiscountType(FeeDiscountDto.DISCOUNT_TYPE_V);
  89. computeDiscountDto.setRuleId(feeDiscountDto.getRuleId());
  90. computeDiscountDto.setRuleName(feeDiscountDto.getRuleName());
  91. computeDiscountDto.setDiscountName(feeDiscountDto.getDiscountName());
  92. computeDiscountDto.setDiscountPrice(0.0);
  93. computeDiscountDto.setFeeDiscountSpecs(feeDiscountSpecDtos);
  94. return computeDiscountDto;
  95. }
  96. Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDtos.get(0));
  97. BigDecimal priceDec = new BigDecimal(feePriceAll.get("feePrice").toString());
  98. BigDecimal dayDec = new BigDecimal(day);
  99. double discountPrice = 0.0;
  100. //todo 一次性计算滞纳金
  101. if (FeeDto.FEE_FLAG_ONCE.equals(feeDtos.get(0).getFeeFlag())) {
  102. discountPrice = priceDec.multiply(new BigDecimal(rate)).multiply(dayDec).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
  103. } else {//todo 计算 滞纳金,
  104. discountPrice = computeLateDiscountPrice(priceDec, feeDtos.get(0), curTime,rate);
  105. }
  106. ComputeDiscountDto computeDiscountDto = new ComputeDiscountDto();
  107. computeDiscountDto.setDiscountId(feeDiscountDto.getDiscountId());
  108. computeDiscountDto.setDiscountType(FeeDiscountDto.DISCOUNT_TYPE_V);
  109. computeDiscountDto.setRuleId(feeDiscountDto.getRuleId());
  110. computeDiscountDto.setRuleName(feeDiscountDto.getRuleName());
  111. computeDiscountDto.setDiscountName(feeDiscountDto.getDiscountName());
  112. computeDiscountDto.setDiscountPrice(discountPrice * -1);
  113. computeDiscountDto.setFeeDiscountSpecs(feeDiscountSpecDtos);
  114. return computeDiscountDto;
  115. }
  116. private double computeLateDiscountPrice(BigDecimal priceDec, FeeDto feeDto,Date curTime,double rate) {
  117. //todo 欠费最大结束时间
  118. Date targetEndDate = computeFeeSMOImpl.getDeadlineTime(feeDto);
  119. //Date targetEndDate = DateUtil.getDateFromStringB("2023-11-30");
  120. //todo 周期开始时间
  121. Date targetStartDate = getTargetStartDate(feeDto);
  122. double allMonth = DateUtil.dayCompare(feeDto.getEndTime(), targetEndDate);
  123. int paymentCycle = Integer.parseInt(feeDto.getPaymentCycle());
  124. double maxCycle = Math.ceil(allMonth / paymentCycle);
  125. int lateDay = 0;
  126. BigDecimal discountPriceDec = new BigDecimal("0.00");
  127. BigDecimal curCycleDiscountPriceDec = null;
  128. for (int cycleIndex = 0; cycleIndex < maxCycle; cycleIndex++) {
  129. //todo 计算 违约天数
  130. lateDay = DateUtil.daysBetween(curTime, targetStartDate);
  131. curCycleDiscountPriceDec = priceDec.multiply(new BigDecimal(paymentCycle)).multiply(new BigDecimal(rate)).multiply(new BigDecimal(lateDay)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
  132. System.out.println(DateUtil.getFormatTimeStringB(targetStartDate)+">"+lateDay+":"+curCycleDiscountPriceDec.doubleValue());
  133. discountPriceDec = discountPriceDec.add(curCycleDiscountPriceDec);
  134. targetStartDate = DateUtil.stepMonth(targetStartDate,paymentCycle);
  135. }
  136. return discountPriceDec.doubleValue();
  137. }
  138. private static Date getTargetStartDate(FeeDto feeDto) {
  139. Calendar startCal = Calendar.getInstance();
  140. startCal.setTime(feeDto.getStartTime());
  141. Date targetStartTime = feeDto.getStartTime();
  142. while (startCal.getTime().before(feeDto.getEndTime())) {
  143. targetStartTime = startCal.getTime();
  144. startCal.add(Calendar.MONTH, Integer.parseInt(feeDto.getPaymentCycle()));
  145. }
  146. return targetStartTime;
  147. }
  148. public static void main(String[] args) throws Exception {
  149. BigDecimal priceDec = new BigDecimal("89.43");
  150. FeeDto feeDto = new FeeDto();
  151. feeDto.setStartTime(DateUtil.getDateFromStringB("2022-01-01"));
  152. feeDto.setEndTime(DateUtil.getDateFromStringB("2022-01-01"));
  153. feeDto.setPaymentCycle("1");
  154. // double discountPrice = computeLateDiscountPrice(priceDec, feeDto, DateUtil.getDateFromStringB("2023-11-07"),0.0005);
  155. // System.out.println("discountPrice = "+discountPrice);
  156. }
  157. }