|
@@ -0,0 +1,237 @@
|
|
|
+package com.rongwei.zhsw.system.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.rongwe.zhsw.system.domain.SwBillManagementUnpaidDo;
|
|
|
+import com.rongwe.zhsw.system.domain.SwUserManagementDo;
|
|
|
+import com.rongwe.zhsw.system.domain.SwWaterUsageEntryDo;
|
|
|
+import com.rongwei.rwcommon.base.BaseDo;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.zhsw.system.utils.ZHSWCommonUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static org.springframework.transaction.annotation.Propagation.REQUIRED;
|
|
|
+
|
|
|
+/**
|
|
|
+ * BillGenerationServiceImpl class
|
|
|
+ *
|
|
|
+ * @author XH
|
|
|
+ * @date 2025/03/18
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class BillGenerationServiceImpl {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(BillGenerationServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SwWaterUsageEntryServiceImpl swWaterUsageEntryService;
|
|
|
+ @Autowired
|
|
|
+ private SwUserManagementServiceImpl swUserManagementService;
|
|
|
+ @Autowired
|
|
|
+ private SwBillManagementUnpaidServiceImpl swBillManagementUnpaidService;
|
|
|
+
|
|
|
+ private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ public static String formatDate(Date date) {
|
|
|
+ synchronized (formatter) {
|
|
|
+ return formatter.format(date);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void generateBill(List<SwWaterUsageEntryDo> swWaterUsageEntryDoList) {
|
|
|
+ log.info("开始生成账单信息");
|
|
|
+ if (swWaterUsageEntryDoList == null || swWaterUsageEntryDoList.isEmpty()) {
|
|
|
+ log.error("暂无需要生成账单的信息");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 对数据按照抄表日期排序
|
|
|
+ List<SwWaterUsageEntryDo> collect = swWaterUsageEntryDoList.stream()
|
|
|
+ .sorted(Comparator.comparing(SwWaterUsageEntryDo::getCurrentreadingdate))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<SwWaterUsageEntryDo> saveUsageEntryList = new ArrayList<>();
|
|
|
+ List<SwBillManagementUnpaidDo> saveList = new ArrayList<>();
|
|
|
+ List<SwUserManagementDo> userSaveList = new ArrayList<>();
|
|
|
+ collect.forEach(swWaterUsageEntry -> {
|
|
|
+ String usernumber = swWaterUsageEntry.getUsernumber();
|
|
|
+ // 本次抄表日期
|
|
|
+ Date currentreadingdate = swWaterUsageEntry.getCurrentreadingdate();
|
|
|
+ // 校验 是否已存在抄表记录
|
|
|
+ if (swWaterUsageEntry.getState() != 0) {
|
|
|
+ log.error("当前用户:{},时间:{}的抄表记录已生成账单信息", usernumber, currentreadingdate);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SwUserManagementDo swUserManagementDo = swUserManagementService.getOne(new LambdaQueryWrapper<SwUserManagementDo>()
|
|
|
+ .select(SwUserManagementDo::getId,
|
|
|
+ SwUserManagementDo::getUsernumber,
|
|
|
+ SwUserManagementDo::getMetermaxvalue,
|
|
|
+ SwUserManagementDo::getWaterprice,
|
|
|
+ SwUserManagementDo::getVillagename,
|
|
|
+ SwUserManagementDo::getUsernumber,
|
|
|
+ SwUserManagementDo::getUsername,
|
|
|
+ SwUserManagementDo::getUsertype,
|
|
|
+ SwUserManagementDo::getAddress,
|
|
|
+ SwUserManagementDo::getExemptionamount,
|
|
|
+ SwUserManagementDo::getExemptionwater,
|
|
|
+ SwUserManagementDo::getWatertype,
|
|
|
+ SwUserManagementDo::getLastmeterreaddate,
|
|
|
+ SwUserManagementDo::getLastmeterreading
|
|
|
+ )
|
|
|
+ .eq(BaseDo::getDeleted, "0")
|
|
|
+ .eq(SwUserManagementDo::getUsernumber, usernumber));
|
|
|
+ SwWaterUsageEntryDo usageEntryDo = new SwWaterUsageEntryDo();
|
|
|
+ usageEntryDo.setId(swWaterUsageEntry.getId());
|
|
|
+ if (swUserManagementDo == null) {
|
|
|
+ log.error("当前户号不存在");
|
|
|
+ usageEntryDo.setState(2);
|
|
|
+ saveUsageEntryList.add(usageEntryDo);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验是否可以生成账单
|
|
|
+ if (swUserManagementDo.getLastmeterreaddate() != null &&
|
|
|
+ swUserManagementDo.getLastmeterreaddate().compareTo(swWaterUsageEntry.getCurrentreadingdate()) > 0) {
|
|
|
+ log.error("本次抄表日期:{}小于用户表最后一次抄表日期:{}", swUserManagementDo.getLastmeterreaddate(), swWaterUsageEntry.getCurrentreadingdate());
|
|
|
+ usageEntryDo.setState(3);
|
|
|
+ saveUsageEntryList.add(usageEntryDo);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SwBillManagementUnpaidDo swBillManagementUnpaidDo=null;
|
|
|
+ try {
|
|
|
+ // 生成代缴费账单
|
|
|
+ swBillManagementUnpaidDo= produceBill(swUserManagementDo, swWaterUsageEntry);
|
|
|
+ }catch (Exception e) {
|
|
|
+ log.error("缴费记录生成失败原因:{}",e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(swBillManagementUnpaidDo==null){
|
|
|
+ usageEntryDo.setState(4);
|
|
|
+ }else{
|
|
|
+ usageEntryDo.setState(1);
|
|
|
+ saveList.add(swBillManagementUnpaidDo);
|
|
|
+ // 设置本次的抄表日期
|
|
|
+ swUserManagementDo.setLastmeterreaddate(swBillManagementUnpaidDo.getThismeterreadingdate());
|
|
|
+ // 设置本次的抄表用量
|
|
|
+ swUserManagementDo.setLastmeterreading(swBillManagementUnpaidDo.getLastmeterreading());
|
|
|
+ }
|
|
|
+ userSaveList.add(swUserManagementDo);
|
|
|
+ saveUsageEntryList.add(usageEntryDo);
|
|
|
+ });
|
|
|
+ dataSave(saveList, userSaveList, saveUsageEntryList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void generateBill(String id) {
|
|
|
+ SwWaterUsageEntryDo swWaterUsageEntryDo = swWaterUsageEntryService.getById(id);
|
|
|
+ this.generateBill(Collections.singletonList(swWaterUsageEntryDo));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void generateBill(List<String> ids, boolean a) {
|
|
|
+ if (ids.isEmpty()) {
|
|
|
+ log.error("抄表记录ID为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.generateBill(swWaterUsageEntryService.getBaseMapper().selectBatchIds(ids));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void generateBill() {
|
|
|
+ List<SwWaterUsageEntryDo> list = swWaterUsageEntryService.list(new LambdaQueryWrapper<SwWaterUsageEntryDo>()
|
|
|
+ .eq(SwWaterUsageEntryDo::getDeleted, "0")
|
|
|
+ .eq(SwWaterUsageEntryDo::getState, 0));
|
|
|
+ this.generateBill(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据保存
|
|
|
+ * @param swBillManagementUnpaidDos 账单信息
|
|
|
+ * @param userSaveList 用户信息
|
|
|
+ * @param saveUsageEntryList 抄表记录
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class, propagation = REQUIRED)
|
|
|
+ public void dataSave(List<SwBillManagementUnpaidDo> swBillManagementUnpaidDos, List<SwUserManagementDo> userSaveList,
|
|
|
+ List<SwWaterUsageEntryDo> saveUsageEntryList) {
|
|
|
+ swWaterUsageEntryService.updateBatchById(saveUsageEntryList,4000);
|
|
|
+ // 账单保存
|
|
|
+ swBillManagementUnpaidService.saveBatch(swBillManagementUnpaidDos, 1000);
|
|
|
+ // 更新用户表的本次抄表时间和本次抄表度数lll
|
|
|
+ swUserManagementService.updateBatchById(userSaveList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成账单
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public SwBillManagementUnpaidDo produceBill(SwUserManagementDo swUserManagementDo, SwWaterUsageEntryDo swWaterUsageEntryDo) {
|
|
|
+ // 上次抄表数
|
|
|
+ BigDecimal lastreading = swWaterUsageEntryDo.getLastreading();
|
|
|
+ // 本次抄表数
|
|
|
+ BigDecimal thisreading = swWaterUsageEntryDo.getThisreading();
|
|
|
+
|
|
|
+ //当前用水
|
|
|
+ BigDecimal waterConsumption;
|
|
|
+ // 本次大于上次
|
|
|
+ if (thisreading.compareTo(lastreading) >= 0) {
|
|
|
+ waterConsumption = thisreading.subtract(lastreading);
|
|
|
+ } else {
|
|
|
+ // 本次抄表数小于上次抄表数 用表具的最大值-上次抄表数+本次抄表数
|
|
|
+ waterConsumption = swUserManagementDo.getMetermaxvalue().subtract(lastreading).add(thisreading);
|
|
|
+ }
|
|
|
+ // 获取水价
|
|
|
+ BigDecimal waterprice = swUserManagementDo.getWaterprice();
|
|
|
+ Date currentreadingdate = swWaterUsageEntryDo.getCurrentreadingdate();
|
|
|
+ // 账单实例
|
|
|
+ SwBillManagementUnpaidDo swBillManagementUnpaidDo = new SwBillManagementUnpaidDo();
|
|
|
+ swBillManagementUnpaidDo.setId(SecurityUtil.getUUID());
|
|
|
+
|
|
|
+ String dateStr = formatDate(currentreadingdate);
|
|
|
+ // 抄表日期+-+户号
|
|
|
+ swBillManagementUnpaidDo.setBillnumber(dateStr + "-" + swWaterUsageEntryDo.getUsernumber());
|
|
|
+ swBillManagementUnpaidDo.setYear(Integer.parseInt(dateStr.substring(0, 4)));
|
|
|
+ swBillManagementUnpaidDo.setMonth(Integer.parseInt(dateStr.substring(5, 7)));
|
|
|
+ swBillManagementUnpaidDo.setVillagename(swUserManagementDo.getVillagename());
|
|
|
+ swBillManagementUnpaidDo.setUsernumber(swUserManagementDo.getUsernumber());
|
|
|
+ swBillManagementUnpaidDo.setUsername(swUserManagementDo.getUsername());
|
|
|
+ swBillManagementUnpaidDo.setUsertype(swUserManagementDo.getUsertype());
|
|
|
+ swBillManagementUnpaidDo.setAddress(swUserManagementDo.getAddress());
|
|
|
+ swBillManagementUnpaidDo.setUnitprice(waterprice);
|
|
|
+ swBillManagementUnpaidDo.setCurrentwateruse(waterConsumption);
|
|
|
+
|
|
|
+ //减免计算方式减免金额
|
|
|
+ BigDecimal exemptionAmount = swUserManagementDo.getExemptionamount() == null ? BigDecimal.ZERO : swUserManagementDo.getExemptionamount();
|
|
|
+ // 减免用水
|
|
|
+ BigDecimal exemptionWater = swUserManagementDo.getExemptionwater() == null ? BigDecimal.ZERO : swUserManagementDo.getExemptionwater();
|
|
|
+ // 优先金额减免
|
|
|
+ if (exemptionAmount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ // 计算减免用水
|
|
|
+ exemptionWater = exemptionAmount.divide(waterprice, 2, RoundingMode.HALF_UP);
|
|
|
+ } else {
|
|
|
+ exemptionAmount = exemptionWater.multiply(waterprice);
|
|
|
+ }
|
|
|
+ swBillManagementUnpaidDo.setReducedwateruse(exemptionWater);
|
|
|
+ swBillManagementUnpaidDo.setFeewaiver(exemptionAmount);
|
|
|
+ swBillManagementUnpaidDo.setOughttohavepaid(waterprice.multiply(waterConsumption));
|
|
|
+ swBillManagementUnpaidDo.setWatertype(swUserManagementDo.getWatertype());
|
|
|
+ // 实际应缴=原应缴-减免
|
|
|
+ swBillManagementUnpaidDo.setActualdue(swBillManagementUnpaidDo.getOughttohavepaid().subtract(swBillManagementUnpaidDo.getFeewaiver()));
|
|
|
+ swBillManagementUnpaidDo.setLastmeterreadingdate(swWaterUsageEntryDo.getLastreadingdate());
|
|
|
+ swBillManagementUnpaidDo.setLastmeterreading(swWaterUsageEntryDo.getLastreading());
|
|
|
+ swBillManagementUnpaidDo.setThismeterreadingdate(swWaterUsageEntryDo.getCurrentreadingdate());
|
|
|
+ swBillManagementUnpaidDo.setThismeterreading(swWaterUsageEntryDo.getThisreading());
|
|
|
+ swBillManagementUnpaidDo.setStatus(1 + "");
|
|
|
+ ZHSWCommonUtils.initModelGeneralParameters(swBillManagementUnpaidDo, null);
|
|
|
+ swBillManagementUnpaidDo.setWaterusageid(swWaterUsageEntryDo.getId());
|
|
|
+ return swBillManagementUnpaidDo;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|