|
@@ -2,10 +2,7 @@ package com.rongwei.zhsw.system.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.rongwe.zhsw.system.domain.SwBillManagementPaidDo;
|
|
|
-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.domain.*;
|
|
|
import com.rongwe.zhsw.system.dto.PaymentRequestDTO;
|
|
|
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
|
import com.rongwei.rwcommon.base.R;
|
|
@@ -17,12 +14,16 @@ import com.rongwei.zhsw.system.service.SwBillManagementUnpaidService;
|
|
|
import com.rongwei.zhsw.system.service.SwBillingRecordService;
|
|
|
import com.rongwei.zhsw.system.service.SwUserManagementService;
|
|
|
import com.rongwei.zhsw.system.utils.ZHSWCommonUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.security.SecureRandom;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
@@ -56,6 +57,9 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
|
|
|
@Autowired
|
|
|
private SwUserManagementService swUserManagementService;
|
|
|
|
|
|
+ private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 缴费记录生成
|
|
@@ -66,161 +70,262 @@ public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao,
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public R windowPayment(PaymentRequestDTO paymentRequestDTO) throws Exception {
|
|
|
- List<String> ids = paymentRequestDTO.getIds();
|
|
|
- BigDecimal paidIn = paymentRequestDTO.getPaidin();
|
|
|
- String datasource = paymentRequestDTO.getDatasource();
|
|
|
- //1. 根据户号查询 待缴费 状态 无缴费记录的 的待缴费账单
|
|
|
- LambdaQueryWrapper<SwBillManagementUnpaidDo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- lambdaQueryWrapper.in(SwBillManagementUnpaidDo::getId, ids)
|
|
|
- .eq(SwBillManagementUnpaidDo::getStatus,PENDINGSTATUS)
|
|
|
- .eq(SwBillManagementUnpaidDo::getDeleted,"0");
|
|
|
|
|
|
- List<SwBillManagementUnpaidDo> list = swBillManagementUnpaidService.list(lambdaQueryWrapper);
|
|
|
+ try {
|
|
|
|
|
|
- //2.校验账单数据
|
|
|
- checkBillingData(list,ids);
|
|
|
+ log.info("窗口缴费开始");
|
|
|
|
|
|
- //生成缴费记录
|
|
|
- addNewBillRecord(paidIn,datasource,list);
|
|
|
+ if (paymentRequestDTO.getIds()==null){
|
|
|
|
|
|
- //5.删除 待收账单数据
|
|
|
- swBillManagementUnpaidDao.deleteByIds(ids);
|
|
|
+ log.info("无账单缴费");
|
|
|
+ windowPaymentNoBill(paymentRequestDTO);
|
|
|
+ }else {
|
|
|
+
|
|
|
+ log.info("有账单缴费");
|
|
|
+ windowPaymentHasBill(paymentRequestDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("窗口缴费结束");
|
|
|
+
|
|
|
+ }catch (Exception e ){
|
|
|
+ log.error("缴费失败 {}",e.getMessage());
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return R.error(e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 有账单生成 缴费记录
|
|
|
+ *
|
|
|
+ * @param paymentRequestDTO
|
|
|
+ */
|
|
|
+ private void windowPaymentHasBill(PaymentRequestDTO paymentRequestDTO) {
|
|
|
+
|
|
|
+ List<String> ids = paymentRequestDTO.getIds();
|
|
|
+ // 查询待缴费账单
|
|
|
+ List<SwBillManagementUnpaidDo> unpaidBills = queryUnpaidBills(paymentRequestDTO.getIds());
|
|
|
+
|
|
|
+ //生成缴费记录
|
|
|
+ addNewBillRecord(paymentRequestDTO,unpaidBills);
|
|
|
|
|
|
+ //删除 待收账单数据
|
|
|
+ deleteUnpaidBills(ids);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * 生成新的缴费账单
|
|
|
- *
|
|
|
- * @param paidin
|
|
|
- * @param datasource
|
|
|
- * @param list
|
|
|
+ * 无账单生成缴费记录
|
|
|
+
|
|
|
*/
|
|
|
- private void addNewBillRecord(BigDecimal paidin, String datasource, List<SwBillManagementUnpaidDo> list) {
|
|
|
- SwBillingRecordDo add = new SwBillingRecordDo();
|
|
|
+ private void windowPaymentNoBill(PaymentRequestDTO paymentRequestDTO) {
|
|
|
+
|
|
|
+
|
|
|
+ //生成缴费记录
|
|
|
+ addNewBillRecord(paymentRequestDTO,null);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- add.setId(SecurityUtil.getUUID())
|
|
|
+ /**
|
|
|
+ * 删除 待收账单数据
|
|
|
+ * @param ids
|
|
|
+ */
|
|
|
+ private void deleteUnpaidBills(List<String> ids) {
|
|
|
+ swBillManagementUnpaidDao.deleteByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据户号查询 待缴费 状态 无缴费记录的 的待缴费账单
|
|
|
+ * @param ids
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<SwBillManagementUnpaidDo> queryUnpaidBills(List<String> ids) {
|
|
|
+ return swBillManagementUnpaidService.list(
|
|
|
+ new LambdaQueryWrapper<SwBillManagementUnpaidDo>()
|
|
|
+ .in(SwBillManagementUnpaidDo::getId, ids)
|
|
|
+ .eq(SwBillManagementUnpaidDo::getStatus, PENDINGSTATUS)
|
|
|
+ .eq(SwBillManagementUnpaidDo::getDeleted, "0")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取公共字段设置
|
|
|
+ private SwBillingRecordDo buildBaseBillingRecord( PaymentRequestDTO paymentRequestDTO, SysUserVo currentUser, SwUserManagementDo user) {
|
|
|
+ SwBillingRecordDo record = new SwBillingRecordDo();
|
|
|
+
|
|
|
+
|
|
|
+ return record.setId(SecurityUtil.getUUID())
|
|
|
.setChargedate(new Date())
|
|
|
.setPayfeesstatus(PAIDSTATUS)
|
|
|
.setYear(Calendar.getInstance().get(Calendar.YEAR))
|
|
|
- .setUsername(list.get(0).getUsername())
|
|
|
- .setUsernumber(list.get(0).getUsernumber())
|
|
|
- .setUsertype(list.get(0).getUsertype())
|
|
|
- .setWatertype(list.get(0).getWatertype())
|
|
|
- .setAddress(list.get(0).getAddress());
|
|
|
-
|
|
|
- //生成缴费编号 年月日 + 3位数 随机数
|
|
|
- CreateBillingNumber(add);
|
|
|
-
|
|
|
- //当前用户
|
|
|
- SysUserVo currentUser = ZHSWCommonUtils.getCurrentUser();
|
|
|
- add.setTollcollectorid(currentUser.getId());
|
|
|
- add.setTollcollectorname(currentUser.getName());
|
|
|
- add.setTenantid(currentUser.getTenantid())
|
|
|
+ .setUsername(user.getUsername())
|
|
|
+ .setUsernumber(user.getUsernumber())
|
|
|
+ .setUsertype(user.getUsertype())
|
|
|
+ .setWatertype(user.getWatertype())
|
|
|
+ .setAddress(user.getAddress())
|
|
|
+ .setTollcollectorid(currentUser.getId())
|
|
|
+ .setTollcollectorname(currentUser.getName())
|
|
|
+ .setTenantid(currentUser.getTenantid())
|
|
|
.setCreateuserid(currentUser.getId())
|
|
|
.setCreateusername(currentUser.getName())
|
|
|
.setCreatedate(new Date())
|
|
|
.setModifydate(new Date())
|
|
|
.setModifyuserid(currentUser.getId())
|
|
|
+ .setTenantid(currentUser.getTenantid())
|
|
|
+ .setDatasource(paymentRequestDTO.getDatasource())
|
|
|
+ .setPaidin(paymentRequestDTO.getPaidin())
|
|
|
.setModifyusername(currentUser.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成新的缴费账单
|
|
|
+ *
|
|
|
+ * @param paidin
|
|
|
+ * @param datasource
|
|
|
+ * @param paymentRequestDTO
|
|
|
+ * @param unpaidBills
|
|
|
+ */
|
|
|
+ private void addNewBillRecord(PaymentRequestDTO paymentRequestDTO, List<SwBillManagementUnpaidDo> list) {
|
|
|
+
|
|
|
+ SysUserVo currentUser = ZHSWCommonUtils.getCurrentUser();
|
|
|
+
|
|
|
+ //根据户号获取 用户记录
|
|
|
+ SwUserManagementDo user = swUserManagementService.getBaseMapper().
|
|
|
+ selectOne(new LambdaQueryWrapper<SwUserManagementDo>().eq(SwUserManagementDo::getUsernumber, paymentRequestDTO.getUsernumber()));
|
|
|
+
|
|
|
+
|
|
|
+ SwBillingRecordDo add = buildBaseBillingRecord(paymentRequestDTO,currentUser,user);
|
|
|
+
|
|
|
+
|
|
|
+ // 生成缴费编号
|
|
|
+ generateBillingNumber(add);
|
|
|
+
|
|
|
+ // 计算费用
|
|
|
+ calculateFees(list, add,user);
|
|
|
+
|
|
|
+
|
|
|
+ if(list!=null){
|
|
|
+ //生成已缴费账单
|
|
|
+ createBills(list,add,currentUser);
|
|
|
+ }
|
|
|
|
|
|
- // list 汇总计算
|
|
|
- BigDecimal allfeewaiver =new BigDecimal(0);
|
|
|
- BigDecimal oughttohavepaid =new BigDecimal("0");
|
|
|
- BigDecimal latefees =new BigDecimal(0);
|
|
|
- List<SwBillManagementPaidDo> paidDos =new ArrayList<>();
|
|
|
- for (SwBillManagementUnpaidDo unpaidDo : list) {
|
|
|
- allfeewaiver = allfeewaiver.add(unpaidDo.getFeewaiver()==null?BigDecimal.ZERO:unpaidDo.getFeewaiver());
|
|
|
- oughttohavepaid =oughttohavepaid.add(unpaidDo.getOughttohavepaid()==null?BigDecimal.ZERO:unpaidDo.getOughttohavepaid());
|
|
|
- latefees = latefees.add(unpaidDo.getLatefees()==null?BigDecimal.ZERO:unpaidDo.getLatefees());
|
|
|
-
|
|
|
- SwBillManagementPaidDo paidDo = new SwBillManagementPaidDo();
|
|
|
- BeanUtils.copyProperties(unpaidDo,paidDo);
|
|
|
- // 新生成的 已缴费账单
|
|
|
- paidDo.setStatus(PAIDINSTATUS);
|
|
|
- //缴费记录ID
|
|
|
- paidDo.setPaymentrecordid(add.getId());
|
|
|
|
|
|
- paidDo.setCreateuserid(currentUser.getId())
|
|
|
+
|
|
|
+ //更新用户余额
|
|
|
+ updateUserBalance(add,user);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createBills(List<SwBillManagementUnpaidDo> list, SwBillingRecordDo add, SysUserVo currentUser) {
|
|
|
+ List<SwBillManagementPaidDo> paidList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (SwBillManagementUnpaidDo unpaid : list) {
|
|
|
+
|
|
|
+
|
|
|
+ SwBillManagementPaidDo paid = new SwBillManagementPaidDo();
|
|
|
+ BeanUtils.copyProperties(unpaid, paid);
|
|
|
+ // 新生成的 已缴费账单
|
|
|
+ paid.setStatus(PAIDINSTATUS)
|
|
|
+ //缴费记录ID
|
|
|
+ .setPaymentrecordid(add.getId())
|
|
|
+ .setCreateuserid(currentUser.getId())
|
|
|
.setCreateusername(currentUser.getName())
|
|
|
.setCreatedate(new Date())
|
|
|
.setModifydate(new Date())
|
|
|
.setModifyuserid(currentUser.getId())
|
|
|
.setModifyusername(currentUser.getName());
|
|
|
- paidDos.add(paidDo);
|
|
|
+ paidList.add(paid);
|
|
|
|
|
|
}
|
|
|
- //生成已经缴费账单
|
|
|
- swBillManagementPaidService.saveBatch(paidDos);
|
|
|
+ // 保存已缴费账单
|
|
|
+ swBillManagementPaidService.saveBatch(paidList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户余额
|
|
|
+ *
|
|
|
+ * @param add
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ private void updateUserBalance(SwBillingRecordDo add, SwUserManagementDo user) {
|
|
|
+ BigDecimal balanceChange = add.getAfterpaymentbalance().subtract(user.getAccountbalance());
|
|
|
+ swUserManagementService.balanceAdd(user.getId(), balanceChange);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void calculateFees(List<SwBillManagementUnpaidDo> list, SwBillingRecordDo add, SwUserManagementDo user) {
|
|
|
+
|
|
|
+
|
|
|
+ // 用户 账户余额
|
|
|
+ BigDecimal accountbalance = user.getAccountbalance()==null?BigDecimal.ZERO:user.getAccountbalance();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//总减免
|
|
|
- add.setAllfeewaiver(allfeewaiver);
|
|
|
+ add.setAllfeewaiver(totalWaivers);
|
|
|
//原应缴
|
|
|
- add.setOughttohavepaid(oughttohavepaid);
|
|
|
+ add.setOughttohavepaid(totalOught);
|
|
|
//滞纳金
|
|
|
- add.setLatefees(latefees);
|
|
|
- //根据户号获取 用户记录
|
|
|
- SwUserManagementDo swUserManagement = swUserManagementService.getBaseMapper().
|
|
|
- selectOne(new LambdaQueryWrapper<SwUserManagementDo>().eq(SwUserManagementDo::getUsernumber, add.getUsernumber()));
|
|
|
-
|
|
|
- // 用户 账户余额
|
|
|
- BigDecimal accountbalance = swUserManagement.getAccountbalance()==null?BigDecimal.ZERO:swUserManagement.getAccountbalance();
|
|
|
+ add.setLatefees(totalLateFees);
|
|
|
// 账户余额
|
|
|
add.setAccountbalance(accountbalance);
|
|
|
//原余额(元) = 用户表 用户余额
|
|
|
add.setOriginalbalance(accountbalance);
|
|
|
- //实缴
|
|
|
- add.setPaidin(paidin);
|
|
|
- //1、当 【余额】 >=【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】 时,【余额抵扣】字段=【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】
|
|
|
- //2、当 【余额】 <【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】 时,【余额抵扣】字段=【账户余额(元)】
|
|
|
- if(oughttohavepaid.subtract(allfeewaiver).subtract(latefees).compareTo(accountbalance)<0){
|
|
|
- add.setBalancededuction(oughttohavepaid.subtract(allfeewaiver).subtract(latefees));
|
|
|
+ //1、当 【余额】 >=【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 时,【余额抵扣】字段=【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】
|
|
|
+ //2、当 【余额】 <【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 时,【余额抵扣】字段=【账户余额(元)】
|
|
|
+ if(totalOught.subtract(totalWaivers).add(totalLateFees).compareTo(accountbalance)<0){
|
|
|
+ add.setBalancededuction(totalOught.add(totalWaivers).subtract(totalLateFees));
|
|
|
}else {
|
|
|
add.setBalancededuction(accountbalance);
|
|
|
}
|
|
|
- // 【实际应缴(元)】=【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】-【余额抵扣(元)】
|
|
|
- add.setActualdue(oughttohavepaid.subtract(allfeewaiver).add(latefees).subtract(add.getBalancededuction()));
|
|
|
+ // 【实际应缴(元)】 【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 >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);
|
|
|
+
|
|
|
//【实缴后余额(元)】=【账户余额】+【本次实缴(元-整)】- 原应缴 + 总减免 - 滞纳金
|
|
|
- add.setAfterpaymentbalance(paidin.add(accountbalance).subtract(oughttohavepaid).add(allfeewaiver).subtract(latefees));
|
|
|
- //数据来源
|
|
|
- add.setDatasource(datasource);
|
|
|
- //更新用户表 余额字段
|
|
|
- swUserManagementService.balanceAdd(swUserManagement.getId(), add.getAfterpaymentbalance().subtract(accountbalance));
|
|
|
+ add.setAfterpaymentbalance(add.getPaidin().add(accountbalance).subtract(totalOught).add(totalWaivers).subtract(totalLateFees));
|
|
|
+
|
|
|
swBillingRecordService.save(add);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 生成缴费编号 年月日 + 3位数 随机数
|
|
|
- * @return
|
|
|
+ * 年月日 +
|
|
|
+ * @param add 3位数 随机数
|
|
|
*/
|
|
|
- private void CreateBillingNumber(SwBillingRecordDo add) {
|
|
|
- // 获取当前时间并格式化为 "yyyyMMddHHmmss"
|
|
|
+ private void generateBillingNumber(SwBillingRecordDo add) {
|
|
|
+ SecureRandom random = new SecureRandom();
|
|
|
+ int randomNumber = random.nextInt(1000);
|
|
|
+ String randomStr = String.format("%03d", randomNumber);
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
|
- String formattedDateTime = now.format(formatter);
|
|
|
-
|
|
|
- // 生成一个三位数的随机数
|
|
|
- Random random = new Random();
|
|
|
- int randomNumber = random.nextInt(1000); // 生成0到999之间的随机数
|
|
|
- String randomStr = String.format("%03d", randomNumber); // 格式化为三位数,不足三位补零
|
|
|
- add.setBillingnumber(formattedDateTime + randomStr);
|
|
|
-
|
|
|
-
|
|
|
+ add.setBillingnumber(now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + randomStr);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 校验账单数据
|
|
|
- *
|
|
|
- * @param list
|
|
|
- * @param ids
|
|
|
- */
|
|
|
- private void checkBillingData(List<SwBillManagementUnpaidDo> list, List<String> ids) throws Exception {
|
|
|
- //判空
|
|
|
- if (list.isEmpty()) throw new Exception("该户号下待缴账单已全部缴费,请重新选择户号!");
|
|
|
- //个别 被缴费
|
|
|
- if (list.size() < ids.size()) {
|
|
|
- throw new Exception("当前账单中存在已缴费的,请重新查询用户最新账单在确认缴费");
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
}
|