Browse Source

Merge remote-tracking branch 'origin/master'

wangbo 4 months ago
parent
commit
24822ee999

+ 1 - 0
zhsw-common/src/main/java/com/rongwei/zhsw/system/config/ThreadPoolConfig.java

@@ -31,6 +31,7 @@ public class ThreadPoolConfig implements AsyncConfigurer {
         executor.setCorePoolSize(8);
         executor.setMaxPoolSize(16);
         executor.setQueueCapacity(30);
+        executor.setThreadNamePrefix(NAME_PRE);
         executor.setTaskDecorator(new RequestContextTaskDecorator());
         executor.initialize();
         return executor;

+ 5 - 1
zhsw-common/src/main/java/com/rongwei/zhsw/system/service/impl/SwBillManagementUnpaidServiceImpl.java

@@ -115,13 +115,17 @@ public class SwBillManagementUnpaidServiceImpl extends ServiceImpl<SwBillManagem
             BigDecimal currentUsing = thisMeterReading.subtract(unpaidDo.getLastmeterreading() == null ? BigDecimal.ZERO : unpaidDo.getLastmeterreading());
             // 原应缴
             BigDecimal oughttohavepaid = unpaidDo.getUnitprice().multiply(currentUsing);
+            BigDecimal actualDue = oughttohavepaid.subtract(unpaidDo.getFeewaiver()).add(unpaidDo.getLatefees());
+            if(actualDue.compareTo(BigDecimal.ZERO)<=0){
+                actualDue=BigDecimal.ZERO;
+            }
             swBillManagementUnpaidService.update(new LambdaUpdateWrapper<SwBillManagementUnpaidDo>()
                     .eq(SwBillManagementUnpaidDo::getId, billId)
                     .set(SwBillManagementUnpaidDo::getThismeterreading, thisMeterReading)
                     .set(SwBillManagementUnpaidDo::getCurrentwateruse, currentUsing)
                     .set(SwBillManagementUnpaidDo::getOughttohavepaid, oughttohavepaid)
                     .set(SwBillManagementUnpaidDo::getMeterreadingcorrectionstatus, HAVEREVISEDSTATUS)
-                    .set(SwBillManagementUnpaidDo::getActualdue, oughttohavepaid.subtract(unpaidDo.getFeewaiver()).add(unpaidDo.getLatefees()))
+                    .set(SwBillManagementUnpaidDo::getActualdue, actualDue)
                     .set(BaseDo::getModifyuserid, currentUser.getId())
                     .set(BaseDo::getModifyusername, currentUser.getName())
                     .set(BaseDo::getModifydate, new Date()));

+ 92 - 57
zhsw-common/src/main/java/com/rongwei/zhsw/system/service/impl/SwBillingRecordServiceImpl.java

@@ -7,6 +7,7 @@ import com.rongwe.zhsw.system.domain.SwBillManagementUnpaidDo;
 import com.rongwe.zhsw.system.domain.SwBillingRecordDo;
 import com.rongwe.zhsw.system.domain.SwUserManagementDo;
 import com.rongwe.zhsw.system.dto.PaymentRequestDTO;
+import com.rongwei.commonservice.service.impl.RedisServiceImpl;
 import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.base.exception.CustomException;
@@ -30,6 +31,7 @@ import java.security.SecureRandom;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.stream.Collectors;
 
 import static com.rongwei.zhsw.system.utils.SaveConstans.billInfo.PAIDINSTATUS;
 import static com.rongwei.zhsw.system.utils.SaveConstans.billInfo.PENDINGSTATUS;
@@ -57,6 +59,9 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
     @Autowired
     private SwUserManagementService swUserManagementService;
 
+    @Autowired
+    private RedisServiceImpl redisService;
+
 
     private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
 
@@ -71,7 +76,6 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
     @Override
     @Transactional
     public R windowPayment(PaymentRequestDTO paymentRequestDTO)  {
-
         try {
             log.info("窗口缴费开始");
             if (paymentRequestDTO.getIds() != null && !paymentRequestDTO.getIds().isEmpty()) {
@@ -172,23 +176,22 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
             throw new CustomException("户号不存在");
         }
 
-        String id = SecurityUtil.getUUID(); // 批次唯一ID
-
-        SwBillingRecordDo add = buildBaseBillingRecord(currentUser, user);
-        add.setId(id);
-        add.setDatasource(paymentRequestDTO.getDatasource());
-        add.setPaidin(paymentRequestDTO.getPaidin());
+        // 缴费记录
+        SwBillingRecordDo billingRecordDo = buildBaseBillingRecord(currentUser, user);
+        billingRecordDo.setId(SecurityUtil.getUUID());
+        billingRecordDo.setDatasource(paymentRequestDTO.getDatasource());
+        billingRecordDo.setPaidin(paymentRequestDTO.getPaidin());
 
         // 计算费用
-        calculateFees(list, add, user);
+        calculateFees(list, billingRecordDo, user);
 
         if (list != null && !list.isEmpty()) {
             //生成已缴费账单
-            bills = createBills(list, add);
+            bills = createBills(list, billingRecordDo);
         }
 
         // 保存处理结果
-        saveProcessedData(add, user, bills, paymentRequestDTO.getIds());
+        saveProcessedData(billingRecordDo, user, bills, paymentRequestDTO.getIds());
 
 
     }
@@ -196,15 +199,15 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
     /**
      * 存储,修改数据
      *
-     * @param add
+     * @param billingRecordDo
      * @param user
      * @param bills
      * @param ids
      */
 
-    public void saveProcessedData(SwBillingRecordDo add, SwUserManagementDo user, List<SwBillManagementPaidDo> bills, List<String> ids) {
+    public void saveProcessedData(SwBillingRecordDo billingRecordDo, SwUserManagementDo user, List<SwBillManagementPaidDo> bills, List<String> ids) {
         //账单缴费记录生成
-        this.baseMapper.insert(add);
+        this.baseMapper.insert(billingRecordDo);
         //已缴费账单生成
         if (!bills.isEmpty()) {
             swBillManagementPaidService.saveBatch(bills);
@@ -215,7 +218,7 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
         }
 
         //更新用户余额
-        updateUserBalance(add, user);
+        updateUserBalance(billingRecordDo, user);
 
     }
 
@@ -255,66 +258,98 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
      * 设置减免金额 原应缴 滞纳金 账户余额 原余额 余额抵扣 实际应缴 缴费后余额
      *
      * @param list
-     * @param add
+     * @param billingRecordDo
      * @param user
      */
-    public void calculateFees(List<SwBillManagementUnpaidDo> list, SwBillingRecordDo add, SwUserManagementDo user) {
-
+    public void calculateFees(List<SwBillManagementUnpaidDo> list, SwBillingRecordDo billingRecordDo, SwUserManagementDo user) {
+        // 对数据按照抄表日期排序
+        list = list.stream().sorted(Comparator.comparing(SwBillManagementUnpaidDo::getThismeterreadingdate)).collect(Collectors.toList());
 
         // 用户 账户余额
         BigDecimal accountbalance = user.getAccountbalance() == null ? BigDecimal.ZERO : user.getAccountbalance();
+        // 用户的实缴金额
+        BigDecimal paidin = billingRecordDo.getPaidin();
+        // 实际的总减免
         BigDecimal totalWaivers = BigDecimal.ZERO;
+        // 实际的原应缴
         BigDecimal totalOught = BigDecimal.ZERO;
+        // 实际的滞纳金
         BigDecimal totalLateFees = BigDecimal.ZERO;
-
-
-        if (list != null) {
-            // 使用Stream API进行汇总计算
-            totalWaivers = list.stream()
-                    .map(SwBillManagementUnpaidDo::getFeewaiver)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-
-            totalOught = list.stream()
-                    .map(SwBillManagementUnpaidDo::getOughttohavepaid)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-
-            totalLateFees = list.stream()
-                    .map(SwBillManagementUnpaidDo::getLatefees)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal actualAmount=BigDecimal.ZERO;
+        /**
+         * 开始进行账单的余额划扣逻辑
+         * 按照抄表日期对账单从
+         */
+        for (SwBillManagementUnpaidDo swBillManagementUnpaidDo : list) {
+            // // 当前账单的减免金额
+            BigDecimal currentReductionAmount = swBillManagementUnpaidDo.getFeewaiver();
+            // // 当前账单的原应缴
+            BigDecimal currentPayableAmount = swBillManagementUnpaidDo.getOughttohavepaid();
+            // // 当前账单的滞纳金
+            BigDecimal currentLateFeeAmount = swBillManagementUnpaidDo.getLatefees();
+            // 实际等于=原应缴+滞纳金-减免金额
+            BigDecimal currentActualAmount = swBillManagementUnpaidDo.getActualdue();
+            actualAmount=actualAmount.add(currentActualAmount);
+            // 求和实际减免
+            totalWaivers= totalWaivers.add(currentReductionAmount.compareTo(currentPayableAmount.add(currentLateFeeAmount))>0?
+                    currentPayableAmount.add(currentLateFeeAmount):currentReductionAmount);
+            // 滞纳金求和
+            totalLateFees=totalLateFees.add(currentLateFeeAmount);
+            // 原应缴
+            totalOught= totalOught.add(currentPayableAmount);
+
+            // 实缴小于等于0
+            if(currentActualAmount.compareTo(BigDecimal.ZERO)<=0){
+                swBillManagementUnpaidDo.setBalancedebitamount(BigDecimal.ZERO);
+            }else {
+                // 优先使用余额划扣
+                if(accountbalance.compareTo(BigDecimal.ZERO)>0){
+                    if(accountbalance.compareTo(currentActualAmount)>=0){
+                        // 账户余额大于等于本次实缴
+                        swBillManagementUnpaidDo.setBalancedebitamount(currentActualAmount);
+                        accountbalance=accountbalance.subtract(currentActualAmount);
+                        currentActualAmount=BigDecimal.ZERO;
+                    }else{
+                        // 账户余额小于本次实缴
+                        swBillManagementUnpaidDo.setBalancedebitamount(accountbalance);
+                        // 修改本次实缴
+                        currentActualAmount=currentActualAmount.subtract(accountbalance);
+                        accountbalance=BigDecimal.ZERO;
+                    }
+                }
+                // 余额划扣结束且本次实际应缴大于0时 使用缴费金额 扣除
+                if(currentActualAmount.compareTo(BigDecimal.ZERO)>0){
+                    paidin=paidin.subtract(currentActualAmount);
+                }
+            }
         }
+        // 如果本次账单划扣结束后 实缴金额小于0 则用户缴费金额小于实际划扣金额
+        if(paidin.compareTo(BigDecimal.ZERO)<0){
+            log.error("用户缴费金额小于等于实缴金额");
+            throw new CustomException("户号:"+user.getUsernumber()+"的缴费金额小于实际应缴金额");
+        }
+
 
 
         //总减免
-        add.setAllfeewaiver(totalWaivers);
+        billingRecordDo.setAllfeewaiver(totalWaivers);
         //原应缴
-        add.setOughttohavepaid(totalOught);
+        billingRecordDo.setOughttohavepaid(totalOught);
         //滞纳金
-        add.setLatefees(totalLateFees);
-        // 账户余额
-        add.setAccountbalance(accountbalance);
-        //原余额(元)   = 用户表 用户余额
-        add.setOriginalbalance(accountbalance);
-        //1、当 【余额】 >=【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】  时,【余额抵扣】字段=【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】
-        //2、当 【余额】 <【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】  时,【余额抵扣】字段=【账户余额(元)】
-        if (totalOught.subtract(totalWaivers).add(totalLateFees).compareTo(accountbalance) < 0) {
-            add.setBalancededuction(totalOught.add(totalWaivers).subtract(totalLateFees));
-        } else {
-            add.setBalancededuction(accountbalance);
-        }
-        // 【实际应缴(元)】  【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 >0 = 【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 else  0
-        BigDecimal actualdue = BigDecimal.ZERO;
-        if (totalOught.subtract(totalWaivers).add(totalLateFees).compareTo(BigDecimal.ZERO) > -1) {
-            actualdue = totalOught.subtract(totalWaivers).add(totalLateFees);
-        }
-        add.setActualdue(actualdue);
+        billingRecordDo.setLatefees(totalLateFees);
+        // 账户余额= 划扣之后的账户余额=剩余的余额+扣款之后的剩余缴费金额
+        billingRecordDo.setAccountbalance(accountbalance.add(paidin));
+        //原余额(元) =划扣之前的账户余额
+        billingRecordDo.setOriginalbalance(user.getAccountbalance());
+        // 实际划扣的金额
+        billingRecordDo.setBalancededuction(billingRecordDo.getOriginalbalance().subtract(accountbalance));
 
-        //【实缴后余额(元)】=【账户余额】+ 【本次实缴(元-整)】- 【实际应缴(元)】
-        add.setAfterpaymentbalance(add.getPaidin().add(accountbalance).subtract(add.getActualdue()));
 
+        // 【实际应缴(元)】
+        billingRecordDo.setActualdue(actualAmount);
 
+        //【实缴后余额(元)】=【账户余额】+ 【本次实缴(元-整)】- 【实际应缴(元)】
+        billingRecordDo.setAfterpaymentbalance(accountbalance.add(paidin));
     }
 
     /**