|
@@ -3,29 +3,32 @@ package com.rongwei.zhsw.system.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.rongwe.zhsw.system.domain.SwBillManagementUnpaidDo;
|
|
|
-import com.rongwe.zhsw.system.domain.SwMeterReadingCorrectionDo;
|
|
|
-import com.rongwe.zhsw.system.domain.SwUserManagementDo;
|
|
|
-import com.rongwe.zhsw.system.domain.SwWaterUsageEntryDo;
|
|
|
+import com.rongwe.zhsw.system.domain.*;
|
|
|
import com.rongwe.zhsw.system.dto.MeterReadingCorrectionDTO;
|
|
|
+import com.rongwe.zhsw.system.dto.TransferAccountDo;
|
|
|
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
|
import com.rongwei.rwcommon.base.BaseDo;
|
|
|
import com.rongwei.rwcommon.base.R;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
import com.rongwei.zhsw.system.dao.SwBillManagementUnpaidDao;
|
|
|
-import com.rongwei.zhsw.system.service.SwBillManagementUnpaidService;
|
|
|
-import com.rongwei.zhsw.system.service.SwUserManagementService;
|
|
|
-import com.rongwei.zhsw.system.service.SwMeterReadingCorrectionService;
|
|
|
-import com.rongwei.zhsw.system.service.SwWaterUsageEntryService;
|
|
|
+import com.rongwei.zhsw.system.service.*;
|
|
|
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 java.math.BigDecimal;
|
|
|
-import java.util.Date;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
+import static com.rongwei.zhsw.system.utils.SaveConstans.billInfo.*;
|
|
|
+import static com.rongwei.zhsw.system.utils.SaveConstans.billReccord.DATASOURCEBALANCEDEDUCTION;
|
|
|
import static com.rongwei.zhsw.system.utils.SaveConstans.billReccord.HAVEREVISEDSTATUS;
|
|
|
+import static sun.security.krb5.internal.KDCOptions.with;
|
|
|
|
|
|
/**
|
|
|
* 账单管理(按年分表)(预收,未缴)(SwBillManagementUnpaid)表服务实现类
|
|
@@ -48,9 +51,19 @@ public class SwBillManagementUnpaidServiceImpl extends ServiceImpl<SwBillManagem
|
|
|
@Autowired
|
|
|
SwBillManagementUnpaidService swBillManagementUnpaidService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ SwBillManagementPaidService swBillManagementPaidService;
|
|
|
+
|
|
|
@Autowired
|
|
|
SwMeterReadingCorrectionService swMeterReadingCorrectionService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SwBillingRecordServiceImpl swBillingRecordService;
|
|
|
+
|
|
|
+
|
|
|
+ private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 抄表修正
|
|
|
* 1、将 对应抄表记录更新,本次抄表数 更新
|
|
@@ -126,5 +139,202 @@ public class SwBillManagementUnpaidServiceImpl extends ServiceImpl<SwBillManagem
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 转正式账单
|
|
|
+ * @param map
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R transferAccount(Map<String, Object> map) throws Exception {
|
|
|
+
|
|
|
+ //获取账单数据 已根据户号 分组 创建时间正序
|
|
|
+ Map<String, List<SwBillManagementUnpaidDo>> billGroups = getGroupedBills(map);
|
|
|
+
|
|
|
+ // 获取用户信息映射(按户号分组)
|
|
|
+ Map<String, List<SwUserManagementDo>> userGroups = getUserGroups(billGroups.keySet());
|
|
|
+
|
|
|
+ TransferAccountDo transferAccountDo = new TransferAccountDo();
|
|
|
+
|
|
|
+ // 处理每个户号的账单数据
|
|
|
+ billGroups.keySet().forEach(key -> processUserBills(billGroups.get(key), userGroups.get(key).get(0), transferAccountDo));
|
|
|
+
|
|
|
+ // 保存处理结果
|
|
|
+ try {
|
|
|
+ swBillManagementUnpaidService.saveProcessedData(transferAccountDo);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("转正式账单数据保存失败 {}",e.getMessage());
|
|
|
+ throw new Exception(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, List<SwUserManagementDo>> getUserGroups(Set<String> usernumbers) {
|
|
|
+ return swUserManagementService.list(new LambdaQueryWrapper<SwUserManagementDo>()
|
|
|
+ .eq(SwUserManagementDo::getDeleted, "0")
|
|
|
+ .in(SwUserManagementDo::getUsernumber, usernumbers)).stream().collect(Collectors.groupingBy(SwUserManagementDo::getUsernumber));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Map<String, List<SwBillManagementUnpaidDo>> getGroupedBills(Map<String, Object> map) {
|
|
|
+
|
|
|
+ List<SwBillManagementUnpaidDo> unpaidDoList = Collections.emptyList();
|
|
|
+
|
|
|
+ String type = map.getOrDefault("type", "").toString();
|
|
|
+ //全选
|
|
|
+ if ("all".equals(type)) {
|
|
|
+
|
|
|
+ // 全选 预收且未 删除 的账单
|
|
|
+ unpaidDoList = swBillManagementUnpaidService.list(new LambdaQueryWrapper<SwBillManagementUnpaidDo>()
|
|
|
+ .eq(SwBillManagementUnpaidDo::getDeleted, "0")
|
|
|
+ .eq(SwBillManagementUnpaidDo::getStatus, PRERECEIVEDSTATE));
|
|
|
+
|
|
|
+ }else if ("select".equals(type)) {
|
|
|
+ //根据ids 选择
|
|
|
+ List<String> ids = (List<String>) map.get("ids");
|
|
|
+ if (ids.isEmpty()){
|
|
|
+ throw new CustomException("参数 ids 异常");
|
|
|
+ }
|
|
|
+ unpaidDoList = (List<SwBillManagementUnpaidDo>) swBillManagementUnpaidService.listByIds(ids);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ throw new CustomException("无效的查询类型");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unpaidDoList.isEmpty()){
|
|
|
+ throw new CustomException("预收账单数量为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //1. 预收账单根据户号分组 创建时间正序
|
|
|
+ return unpaidDoList.stream()
|
|
|
+ .collect(Collectors.groupingBy(SwBillManagementUnpaidDo::getUsernumber,
|
|
|
+ Collectors.collectingAndThen(
|
|
|
+ Collectors.toList(), list -> list.stream()
|
|
|
+ .sorted(Comparator.comparing(SwBillManagementUnpaidDo::getCreatedate))
|
|
|
+ .collect(Collectors.toList())
|
|
|
+ )
|
|
|
+ ));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * //计算用户余额是否充足 直接生成缴费记录
|
|
|
+ *
|
|
|
+ * @param swBillManagementUnpaidDos
|
|
|
+ * @param user
|
|
|
+
|
|
|
+ */
|
|
|
+ private void processUserBills(List<SwBillManagementUnpaidDo> swBillManagementUnpaidDos, SwUserManagementDo user,
|
|
|
+ TransferAccountDo transferAccountDo ) {
|
|
|
+
|
|
|
+ BigDecimal billAmount =BigDecimal.ZERO;
|
|
|
+ List<SwBillManagementUnpaidDo> unpaidDos = new ArrayList<>();
|
|
|
+ SwBillManagementPaidDo paid;
|
|
|
+ String paymentBatchId = SecurityUtil.getUUID(); // 批次唯一ID
|
|
|
+ SysUserVo currentUser = ZHSWCommonUtils.getCurrentUser();
|
|
|
+ HashMap<String, BigDecimal> map = new HashMap<>();
|
|
|
+ //是否累加值超出余额
|
|
|
+ Boolean excessBalance =false;
|
|
|
+
|
|
|
+
|
|
|
+ for (SwBillManagementUnpaidDo unpaidDo:swBillManagementUnpaidDos){
|
|
|
+ //当累计的账单的 【原应缴(元)】-【总减免(元)】 +【滞纳金(元)】 <= 账户余额,生成计费记录
|
|
|
+ billAmount = billAmount.add(unpaidDo.getOughttohavepaid()).subtract(unpaidDo.getFeewaiver()).add(unpaidDo.getLatefees());
|
|
|
+
|
|
|
+ if (excessBalance){
|
|
|
+ transferAccountDo.getOfficeIds().add(unpaidDo.getId());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (billAmount.compareTo(user.getAccountbalance()) <= 0){
|
|
|
+ //转正式账单后 生成缴费记录,生成 已缴费账单,删除待缴费记录,更新用户余额
|
|
|
+
|
|
|
+ // 记录待删除ID
|
|
|
+ transferAccountDo.getDelUnpaidIds().add(unpaidDo.getId());
|
|
|
+
|
|
|
+ //2.生成已缴费记录
|
|
|
+ paid = new SwBillManagementPaidDo();
|
|
|
+ BeanUtils.copyProperties(unpaidDo, paid);
|
|
|
+ paid.setStatus(PAIDINSTATUS);
|
|
|
+ paid .setPaymentrecordid(paymentBatchId);
|
|
|
+ ZHSWCommonUtils.initModelGeneralParameters(paid,null);
|
|
|
+ transferAccountDo.getPaidDoList().add(paid);
|
|
|
+
|
|
|
+ //整合要生成缴费记录的预收账单
|
|
|
+ unpaidDos.add(unpaidDo);
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+ excessBalance =true;
|
|
|
+ // 超出余额部分标记为正式账单
|
|
|
+ transferAccountDo.getOfficeIds().add(unpaidDo.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!unpaidDos.isEmpty()){
|
|
|
+ //生成缴费记录
|
|
|
+ SwBillingRecordDo record = swBillingRecordService.buildBaseBillingRecord(currentUser, user);
|
|
|
+ record.setId(paymentBatchId);
|
|
|
+ record.setDatasource(DATASOURCEBALANCEDEDUCTION);
|
|
|
+ record.setPaidin(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ // 计算费用
|
|
|
+ swBillingRecordService.calculateFees(unpaidDos, record, user);
|
|
|
+ transferAccountDo.getRecordDoList().add(record);
|
|
|
+
|
|
|
+ //更新 用户余额记录
|
|
|
+ TransferAccountDo.UpdateUserBalance updateUserBalance = new TransferAccountDo.UpdateUserBalance();
|
|
|
+ updateUserBalance.setId(user.getId());
|
|
|
+ updateUserBalance.setBalanceAdd(record.getAfterpaymentbalance().subtract(user.getAccountbalance()));
|
|
|
+ transferAccountDo.getUserBalanceList().add(updateUserBalance);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ * @param transferAccountDo
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void saveProcessedData(TransferAccountDo transferAccountDo) {
|
|
|
+
|
|
|
+ //转正式账单生成
|
|
|
+ if(!transferAccountDo.getOfficeIds().isEmpty()){
|
|
|
+ swBillManagementUnpaidService.update(new LambdaUpdateWrapper<SwBillManagementUnpaidDo>()
|
|
|
+ .in(SwBillManagementUnpaidDo::getId,transferAccountDo.getOfficeIds())
|
|
|
+ .set(SwBillManagementUnpaidDo::getStatus,PENDINGSTATUS)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //删除 待收账单数据
|
|
|
+ if (!transferAccountDo.getDelUnpaidIds().isEmpty()) {
|
|
|
+ swBillingRecordService.deleteUnpaidBills(transferAccountDo.getDelUnpaidIds());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成已缴费账单
|
|
|
+ if (!transferAccountDo.getPaidDoList().isEmpty()){
|
|
|
+ swBillManagementPaidService.saveBatch(transferAccountDo.getPaidDoList(),200);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新 用户余额
|
|
|
+ if (!transferAccountDo.getUserBalanceList().isEmpty()) {
|
|
|
+ swUserManagementService.batchUpdateUserBalances(transferAccountDo.getUserBalanceList());
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成缴费
|
|
|
+ if (!transferAccountDo.getRecordDoList().isEmpty()){
|
|
|
+ swBillingRecordService.saveBatch(transferAccountDo.getRecordDoList(),200);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
}
|