|
@@ -0,0 +1,243 @@
|
|
|
+package com.rongwei.zhsw.sys.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.rongwe.zhsw.domain.SwUserManagement;
|
|
|
+import com.rongwe.zhsw.dto.PaymentRequestDTO;
|
|
|
+import com.rongwei.rwcommon.base.R;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.zhsw.sys.dao.SwBillManagementUnpaidDao;
|
|
|
+import com.rongwei.zhsw.sys.dao.SwBillingRecordDao;
|
|
|
+import com.rongwei.zhsw.sys.service.SwBillManagementPaidService;
|
|
|
+import com.rongwei.zhsw.sys.service.SwBillManagementUnpaidService;
|
|
|
+import com.rongwei.zhsw.sys.service.SwBillingRecordService;
|
|
|
+import com.rongwe.zhsw.domain.SwBillManagementPaidDo;
|
|
|
+import com.rongwe.zhsw.domain.SwBillManagementUnpaidDo;
|
|
|
+import com.rongwe.zhsw.domain.SwBillingRecordDo;
|
|
|
+import com.rongwei.zhsw.sys.service.SwUserManagementService;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static com.rongwei.sfcommon.utils.SaveConstans.billInfo.PAIDINSTATUS;
|
|
|
+import static com.rongwei.sfcommon.utils.SaveConstans.billInfo.PENDINGSTATUS;
|
|
|
+import static com.rongwei.sfcommon.utils.SaveConstans.billReccord.PAIDSTATUS;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 缴费记录(SwBillingRecord)表服务实现类
|
|
|
+ *
|
|
|
+ * @author makejava
|
|
|
+ * @since 2025-03-07 16:35:09
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SwBillingRecordServiceImpl extends ServiceImpl<SwBillingRecordDao, SwBillingRecordDo> implements SwBillingRecordService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwBillingRecordService swBillingRecordService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwBillManagementPaidService swBillManagementPaidService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwBillManagementUnpaidService swBillManagementUnpaidService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwBillManagementUnpaidDao swBillManagementUnpaidDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwUserManagementService swUserManagementService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缴费记录生成
|
|
|
+ * @param paymentRequestDTO
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @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);
|
|
|
+
|
|
|
+ //2.校验账单数据
|
|
|
+ checkBillingData(list,ids);
|
|
|
+
|
|
|
+ //生成缴费记录
|
|
|
+ SwBillingRecordDo swBillingRecordDo = addNewBillRecord(paidin,datasource,list);
|
|
|
+ swBillingRecordService.save(swBillingRecordDo);
|
|
|
+
|
|
|
+ //4.生成已经缴费账单
|
|
|
+// List<SwBillManagementPaidDo> paidDos = addNewPaids(list, swBillingRecordDo);
|
|
|
+// swBillManagementPaidService.saveBatch(paidDos);
|
|
|
+
|
|
|
+ //5.删除 待收账单数据
|
|
|
+ swBillManagementUnpaidDao.deleteByIds(ids);
|
|
|
+
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成缴费账单
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @param swBillingRecordDo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<SwBillManagementPaidDo> addNewPaids(List<SwBillManagementUnpaidDo> list, SwBillingRecordDo swBillingRecordDo) {
|
|
|
+ List<SwBillManagementPaidDo> paidDos =new ArrayList<>();
|
|
|
+ for (SwBillManagementUnpaidDo unpaidDo : list) {
|
|
|
+ SwBillManagementPaidDo paidDo = new SwBillManagementPaidDo();
|
|
|
+ BeanUtils.copyProperties(unpaidDo,paidDo);
|
|
|
+ paidDo.setStatus(PAIDINSTATUS); // 实缴
|
|
|
+ paidDo.setPaymentrecordid(swBillingRecordDo.getId()); //缴费记录ID
|
|
|
+ paidDos.add(paidDo);
|
|
|
+ }
|
|
|
+ return paidDos;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成新的缴费账单
|
|
|
+ *
|
|
|
+ * @param paidin
|
|
|
+ * @param datasource
|
|
|
+ * @param list
|
|
|
+ */
|
|
|
+ private SwBillingRecordDo addNewBillRecord(BigDecimal paidin, String datasource, List<SwBillManagementUnpaidDo> list) {
|
|
|
+ SwBillingRecordDo add = new SwBillingRecordDo();
|
|
|
+
|
|
|
+ add.setId(SecurityUtil.getUUID());
|
|
|
+ add.setBillingnumber(CreateBillingNumber());
|
|
|
+ add.setChargedate(new Date());
|
|
|
+ add.setUsername(list.get(0).getUsername());
|
|
|
+ add.setUsernumber(list.get(0).getUsernumber());
|
|
|
+ add.setUsertype(list.get(0).getUsertype());
|
|
|
+ add.setAddress(list.get(0).getAddress());
|
|
|
+ add.setPayfeesstatus(PAIDSTATUS); // 已缴费
|
|
|
+ add.setYear(Calendar.getInstance().get(Calendar.YEAR));
|
|
|
+ // SysUserVo currentUser = CXCommonUtils.getCurrentUser();
|
|
|
+// add.setTollcollectorid(currentUser.getId());
|
|
|
+// add.setTollcollectorname(currentUser.getName());
|
|
|
+// add.setTenantid(currentUser.getTenantid());
|
|
|
+
|
|
|
+ // list 汇总计算
|
|
|
+ BigDecimal allfeewaiver =new BigDecimal(0);
|
|
|
+ BigDecimal oughttohavepaid =new BigDecimal("0");
|
|
|
+ BigDecimal overduepaymentfine =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());
|
|
|
+ overduepaymentfine = overduepaymentfine.add(unpaidDo.getOverduepaymentfine()==null?BigDecimal.ZERO:unpaidDo.getOverduepaymentfine());
|
|
|
+
|
|
|
+ SwBillManagementPaidDo paidDo = new SwBillManagementPaidDo();
|
|
|
+ BeanUtils.copyProperties(unpaidDo,paidDo);
|
|
|
+ paidDo.setStatus(PAIDINSTATUS); // 实缴
|
|
|
+ paidDo.setPaymentrecordid(add.getId()); //缴费记录ID
|
|
|
+ paidDos.add(paidDo);
|
|
|
+
|
|
|
+ }
|
|
|
+ //生成已经缴费账单
|
|
|
+ swBillManagementPaidService.saveBatch(paidDos);
|
|
|
+
|
|
|
+ add.setAllfeewaiver(allfeewaiver); //总减免
|
|
|
+ add.setOughttohavepaid(oughttohavepaid); //原应缴
|
|
|
+ add.setOverduepaymentfine(overduepaymentfine); //滞纳金
|
|
|
+
|
|
|
+ //根据户号获取 用户记录
|
|
|
+ SwUserManagement swUserManagement = swUserManagementService.getBaseMapper().
|
|
|
+ selectOne(new LambdaQueryWrapper<SwUserManagement>().eq(SwUserManagement::getUsernumber, add.getUsernumber()));
|
|
|
+ // 用户 账户余额
|
|
|
+ BigDecimal accountbalance = swUserManagement.getAccountbalance()==null?BigDecimal.ZERO:swUserManagement.getAccountbalance();
|
|
|
+
|
|
|
+ add.setAccountbalance(accountbalance); // 账户余额
|
|
|
+ add.setOriginalbalance(accountbalance); //原余额(元) = 用户表 用户余额
|
|
|
+
|
|
|
+ add.setPaidin(paidin); //实缴
|
|
|
+
|
|
|
+ add.setActualdue(oughttohavepaid.subtract(allfeewaiver).subtract(overduepaymentfine)); //实际应缴(元) = 原应缴 - 总减免 - 滞纳金
|
|
|
+ add.setAfterpaymentbalance(paidin.subtract(add.getActualdue())); //缴费后余额 = 实缴 - 实际应缴(元)
|
|
|
+ add.setDatasource(datasource); //数据来源
|
|
|
+ //1、当 【余额】 >=【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】 时,【余额】字段=【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】
|
|
|
+ //2、当 【余额】 <【原应缴(元)】-【总减免(元)】 -【滞纳金(元)】 时,【余额】字段=【账户余额(元)】
|
|
|
+ if(add.getAccountbalance().compareTo(add.getAccountbalance())>=0){
|
|
|
+ add.setBalancededuction(add.getAccountbalance());// 余额抵扣
|
|
|
+ }else {
|
|
|
+ add.setBalancededuction(add.getAccountbalance());// 余额抵扣
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新用户表 余额字段
|
|
|
+ swUserManagementService.balanceAdd(swUserManagement.getId(),paidin.subtract(add.getActualdue()));
|
|
|
+
|
|
|
+
|
|
|
+ // add.setTotalpaidin() //总已缴
|
|
|
+ return add;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成缴费编号 年月日 + 3位数 随机数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String CreateBillingNumber() {
|
|
|
+ // 获取当前时间并格式化为 "yyyyMMddHHmmss"
|
|
|
+ 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); // 格式化为三位数,不足三位补零
|
|
|
+ return formattedDateTime + 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()) {
|
|
|
+ //去缴费账单中查询 已缴费的记录
|
|
|
+ List<SwBillManagementPaidDo> collect = swBillManagementPaidService.listByIds(ids).stream().collect(Collectors.toList());
|
|
|
+ StringBuffer errorMsg = new StringBuffer("账单编号为【");
|
|
|
+
|
|
|
+ for (int i = 0; i < collect.size(); i++) {
|
|
|
+ if (i==collect.size()-1){
|
|
|
+ errorMsg.append(collect.get(i).getBillnumber());
|
|
|
+ }else {
|
|
|
+ errorMsg.append(collect.get(i).getBillnumber()+",");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ errorMsg.append("]的账单已缴费,请重新选择户号!");
|
|
|
+ throw new Exception(errorMsg.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|