无关风月
2025-04-10 099ea14bba367fd86f0dde37d908f07cc04c3d39
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TBillServiceImpl.java
@@ -1,24 +1,43 @@
package com.ruoyi.system.service.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.config.SmsProperties;
import com.ruoyi.common.constant.AmountConstant;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.DisabledEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.dto.*;
import com.ruoyi.system.dto.BillStatisticsDto;
import com.ruoyi.system.dto.CachPayDto;
import com.ruoyi.system.dto.OfflinePayCheckDto;
import com.ruoyi.system.dto.SmsByBillDto;
import com.ruoyi.system.dto.TBillDto;
import com.ruoyi.system.dto.TbillSaveDto;
import com.ruoyi.system.mapper.TBillMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.model.TBankFlow;
import com.ruoyi.system.model.TBill;
import com.ruoyi.system.model.TBillDetail;
import com.ruoyi.system.model.TFlowManagement;
import com.ruoyi.system.model.TInvoiceToBill;
import com.ruoyi.system.model.TOrderBill;
import com.ruoyi.system.model.TPayOrder;
import com.ruoyi.system.query.TBillQuery;
import com.ruoyi.system.query.TInvoiceToBillQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.TBankFlowService;
import com.ruoyi.system.service.TBillConfirmService;
import com.ruoyi.system.service.TBillDetailService;
import com.ruoyi.system.service.TBillService;
import com.ruoyi.system.service.TFlowManagementService;
import com.ruoyi.system.service.TInvoiceToBillService;
import com.ruoyi.system.service.TOrderBillService;
import com.ruoyi.system.service.TPayOrderService;
import com.ruoyi.system.vo.ScreenRentRankVO;
import com.taxi591.bankapi.dto.ChargeBillRequest;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -28,7 +47,6 @@
import javax.validation.constraints.NotEmpty;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
@@ -88,6 +106,12 @@
        return info;
    }
    public PageInfo<TBillDto> queryPageForApplet(TBillQuery query){
        PageInfo<TBill> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        PageInfo<TBillDto> info = tBillMapper.pageForApplet(pageInfo, query);
        return info;
    }
    @Override
    public List<String> getBillIds(TBillQuery query) {
        List<TBillDto> billDtos = tBillMapper.getBillList(query);
@@ -124,11 +148,13 @@
        if (isok){
            try {
                TBill save = new TBill();
                save.setId(tBill.getId());
                TBill presist = getById(tBill.getId());
                //如果账单是已缴费状态,本方法不再进行更新账单
                if (presist.getPayFeesStatus().equals("3")){
                    throw new ServiceException("该账单已缴费完成");
                }
                save.setBusinessDeptId(presist.getBusinessDeptId());
                switch (type){
                    // 仅更新除金额字段外的属性
                    case 1:
@@ -161,6 +187,7 @@
                            BigDecimal outstand = presist.getPayableFeesMoney()
                                    .add(save.getPayableFeesPenalty())
                                    .subtract(presist.getPayFeesMoney());
                            save.setOutstandingMoney(outstand);
                        }
                        //处理缴费金额
@@ -226,6 +253,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBill(TbillSaveDto bill) {
        bill.setManualAddition(DisabledEnum.YES.getCode());
        save(bill);
        if (bill.getBillType().equals("3")){
            if (bill.getDetails()==null || bill.getDetails().size()==0){
@@ -280,6 +308,7 @@
            TFlowManagement save = new TFlowManagement();
            save.setPayType(3);
            save.setPayer(dto.getPayer());
            save.setBusinessDeptId(bill.getBusinessDeptId());
            save.setPayTime(bankflow.getPayTime());
            save.setSysSerialNumber(OrderNos.getDid(30));
            save.setBankSerialNumber(bankflow.getBankSerialNumber());
@@ -305,6 +334,7 @@
        save.setPayType(3);
        save.setPayer(dto.getPayer());
        save.setPayTime(billSave.getPayFeesTime());
        save.setBusinessDeptId(bill.getBusinessDeptId());
        save.setSysSerialNumber(OrderNos.getDid(30));
        save.setFlowType(1);
        save.setPaymentBillId(back.getId());
@@ -323,6 +353,7 @@
        String uuid = UUID.fastUUID().toString();
        boolean lock = redisCache.trylockLoop(CacheConstants.COMPLETE_PAY_LOCK_KEY + orderNo, uuid, 60);
        if (lock){
            try {
            TPayOrder order = tPayOrderService.getById(orderNo);
            if (order==null){
                throw new ServiceException("订单不存在");
@@ -360,6 +391,7 @@
                TFlowManagement saveFlow = new TFlowManagement();
                saveFlow.setPayType(1);
                saveFlow.setPayer(order.getUserId());
                    saveFlow.setBusinessDeptId(bill.getBusinessDeptId());
                saveFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime()));
                saveFlow.setSysSerialNumber(OrderNos.getDid(30));
                saveFlow.setBankSerialNumber(save.getPayNo());
@@ -371,14 +403,17 @@
                saveFlow.setPreOutstand(bill.getPreOutstand());
                tFlowManagementService.save(saveFlow);
            });
            TBankFlow bankFlow = new TBankFlow();
            bankFlow.setPayType(1);
            bankFlow.setPayer(order.getUserId());
            bankFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime()));
            bankFlow.setBankSerialNumber(save.getPayNo());
            bankFlow.setFlowMoney(payAmount);
            bankFlow.setFlowStatus(1);
            tBankFlowService.save(bankFlow);
//                TBankFlow bankFlow = new TBankFlow();
//                bankFlow.setPayType(1);
//                bankFlow.setPayer(order.getUserId());
//                bankFlow.setPayTime(DateUtils.dateToLocalDateTime(save.getPayTime()));
//                bankFlow.setBankSerialNumber(save.getPayNo());
//                bankFlow.setFlowMoney(payAmount);
//                bankFlow.setFlowStatus(1);
//                tBankFlowService.save(bankFlow);
            }finally {
                redisCache.unlock(CacheConstants.COMPLETE_PAY_LOCK_KEY + orderNo,uuid);
            }
        }
@@ -524,6 +559,7 @@
        TFlowManagement save = new TFlowManagement();
        save.setPayType(3);
        save.setPayer(dto.getPayer());
        save.setBusinessDeptId(bill.getBusinessDeptId());
        save.setPayTime(bankflow!=null?bankflow.getPayTime():DateUtils.dateToLocalDateTime(new Date()));
        save.setSysSerialNumber(OrderNos.getDid());
        save.setBankSerialNumber(bankflow!=null?bankflow.getBankSerialNumber():null);
@@ -538,12 +574,12 @@
    }
    @Override
    public BillStatisticsDto statistics() {
    public BillStatisticsDto statistics(TBillQuery query) {
        BillStatisticsDto dto = new BillStatisticsDto();
        dto.setRent(getBaseMapper().statisticsAllRent());
        dto.setNopay(getBaseMapper().statisticsNoPay());
        dto.setPayed(getBaseMapper().statisticsPayed());
        dto.setOverdue(getBaseMapper().statisticsOverdue());
        dto.setRent(getBaseMapper().statisticsAllRent(query));
        dto.setNopay(getBaseMapper().statisticsNoPay(query));
        dto.setPayed(getBaseMapper().statisticsPayed(query));
        dto.setOverdue(getBaseMapper().statisticsOverdue(query));
        return dto;
    }
@@ -552,5 +588,84 @@
        return this.baseMapper.batchBillCount(userId,billIds);
    }
    /**
     * 街道租金排行
     * @return
     */
    @Override
    public List<ScreenRentRankVO> getStreetRentRank(String businessDeptId) {
        return baseMapper.getStreetRentRank(businessDeptId);
    }
    /**
     * 查询季付账单
     * @param businessDeptId
     * @return
     */
    @Override
    public List<TBill> getJiFuBillList(String businessDeptId) {
        return baseMapper.getJiFuBillList(businessDeptId,null,null);
    }
    /**
     * 查询当前季度的季付账单
     * @param businessDeptId
     * @param first
     * @param last
     * @return
     */
    @Override
    public List<TBill> getJiFuBillListByTime(String businessDeptId, Date first, Date last) {
        return baseMapper.getJiFuBillList(businessDeptId,first,last);
    }
    @Override
    public void editAmount(TbillSaveDto bill) {
        String requestId = UUID.fastUUID().toString();
        String lockkey = CacheConstants.BILL_UPDATE_LOCK_KEY + bill.getId();
        boolean isok = redisCache.trylockLoop(lockkey, requestId, 60);
        if (isok){
            try {
                TBill presist = getById(bill.getId());
                TBill save = new TBill();
                save.setId(bill.getId());
                BigDecimal preOutstand = presist.getOutstandingMoney();
                // 如果传入的金额小于0,则是扣减,如果大于0,是增加金额,增加金额只加入欠费金额中
                if (bill.getEditAmount().compareTo(BigDecimal.ZERO)<0
                        && presist.getPayableFeesPenalty().compareTo(BigDecimal.ZERO)>0){
                    BigDecimal prePayableFeesPenalty = presist.getPayableFeesPenalty();
                    //违约金大于调整金额,够减
                    if (prePayableFeesPenalty.compareTo(bill.getEditAmount().abs())>=0){
                        BigDecimal afterPenalty = prePayableFeesPenalty.add(bill.getEditAmount());
                        save.setPayableFeesPenalty(afterPenalty);
                    }else{
                        BigDecimal afterPenalty = BigDecimal.ZERO;
                        save.setPayableFeesPenalty(afterPenalty);
                    }
                }
                BigDecimal afterOutstand = preOutstand.add(bill.getEditAmount());
                save.setOutstandingMoney(afterOutstand);
                updateById(save);
            }finally {
                redisCache.unlock(lockkey,requestId);
            }
        }
        //todo 记录金额修改记录
//        TFlowManagement flow = new TFlowManagement();
//        flow.setPayType(3);
//        flow.setPayer("管理员修改");
//        flow.setBusinessDeptId(presist.getBusinessDeptId());
//        flow.setPayTime(DateUtils.dateToLocalDateTime(new Date()));
//        flow.setSysSerialNumber(OrderNos.getDid(30));
//        flow.setFlowType(1);
//        flow.setPaymentBillId(back.getId());
//        flow.setDeductionMoney(back.getDeductionMoney());
//        flow.setFlowMoney(save.getOutstandingMoney());
//        flow.setRemainingMoney(back.getOutstandingMoney());
//        flow.setPreOutstand(back.getPreOutstand());
//        flow.setCreateBy(SecurityUtils.getUsername());
//        tFlowManagementService.save(flow);
    }
}