maven
2 天以前 0dd70afb7815d34a02ba7f5cd4f0ad8457072607
src/main/java/com/ruoyi/purchase/service/impl/TicketRegistrationServiceImpl.java
@@ -21,6 +21,7 @@
import com.ruoyi.purchase.mapper.ProductRecordMapper;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.mapper.TicketRegistrationMapper;
import com.ruoyi.purchase.pojo.PaymentRegistration;
import com.ruoyi.purchase.pojo.ProductRecord;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.purchase.pojo.TicketRegistration;
@@ -30,7 +31,6 @@
import com.ruoyi.sales.pojo.CommonFile;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.impl.SalesLedgerProductServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
@@ -52,6 +52,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
/**
@@ -128,6 +129,8 @@
            throw new IllegalArgumentException("采购台账记录不存在,ID: " + ticketRegistrationDto.getPurchaseLedgerId());
        }
        // 3. 创建或更新票据登记实体
        TicketRegistration ticketRegistration = new TicketRegistration();
        BeanUtils.copyProperties(ticketRegistrationDto, ticketRegistration);
@@ -150,15 +153,24 @@
        // 6. 增加采购台账产品开票记录
        List<SalesLedgerProduct> salesLedgerProducts = ticketRegistrationDto.getProductData();
        if (CollectionUtils.isNotEmpty(salesLedgerProducts)) {
            int insert = 0 ;
            for (SalesLedgerProduct salesLedgerProduct : salesLedgerProducts) {
                ProductRecord productRecord = new ProductRecord();
                productRecord.setTicketRegistrationId(ticketRegistration.getId());
                productRecord.setPurchaseLedgerId(ticketRegistrationDto.getPurchaseLedgerId());
                productRecord.setCreatedAt(DateUtils.getNowDate());
                BeanUtils.copyProperties(salesLedgerProduct, productRecord);
                productRecord.setId(null);
                productRecord.setType("2");
                productRecordMapper.insert(productRecord);
                //排除掉开票为0的数据
                if (salesLedgerProduct.getTicketsNum() != null && salesLedgerProduct.getTicketsNum().compareTo(BigDecimal.ZERO) > 0) {
                    ProductRecord productRecord = new ProductRecord();
                    productRecord.setTicketRegistrationId(ticketRegistration.getId());
                    productRecord.setPurchaseLedgerId(ticketRegistrationDto.getPurchaseLedgerId());
                    productRecord.setCreatedAt(DateUtils.getNowDate());
                    BeanUtils.copyProperties(salesLedgerProduct, productRecord);
                    productRecord.setSaleLedgerProjectId(salesLedgerProduct.getId());
                    productRecord.setId(null);
                    productRecord.setType("2");
                     insert = productRecordMapper.insert(productRecord);
                }
                if (insert <= 0) {
                    throw new RuntimeException("产品开票数都为0,请检查");
                }
            }
        }
        // 迁移临时文件到正式目录
@@ -176,7 +188,7 @@
     * @param tempFileIds 临时文件ID列表
     * @throws IOException 文件操作异常
     */
    private void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException {
    public void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException {
        if (CollectionUtils.isEmpty(tempFileIds)) {
            return;
        }
@@ -202,21 +214,25 @@
            // 构建正式文件名(包含业务ID和时间戳,避免冲突)
            String originalFilename = tempFile.getOriginalName();
            String fileExtension = FilenameUtils.getExtension(originalFilename);
            String baseName = FilenameUtils.getBaseName(originalFilename);
            String formalFilename = businessId + "_" +
                    System.currentTimeMillis() + "_" +
                    UUID.randomUUID().toString().substring(0, 8) +
                    UUID.randomUUID().toString().substring(0, 8) +baseName+
                    (com.ruoyi.common.utils.StringUtils.hasText(fileExtension) ? "." + fileExtension : "");
            Path formalFilePath = formalDirPath.resolve(formalFilename);
            try {
                // 执行文件迁移(使用原子操作确保安全性)
                Files.move(
                        Paths.get(tempFile.getTempPath()),
                        formalFilePath,
                        StandardCopyOption.REPLACE_EXISTING,
                        StandardCopyOption.ATOMIC_MOVE
                );
//                Files.move(
//                        Paths.get(tempFile.getTempPath()),
//                        formalFilePath,
//                        StandardCopyOption.REPLACE_EXISTING,
//                        StandardCopyOption.ATOMIC_MOVE
//                );
                // 原子移动失败,使用复制+删除
                Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING);
                Files.deleteIfExists(Paths.get(tempFile.getTempPath()));
                log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath);
                // 更新文件记录(关联到业务ID)
@@ -240,7 +256,47 @@
    @Override
    public int delRegistration(Long[] ids) {
        return ticketRegistrationMapper.deleteBatchIds(Arrays.asList(ids));
        // 删除采购台账产品开票记录对象
        LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productRecordLambdaQueryWrapper.in(ProductRecord::getId, Arrays.asList(ids));
        List<ProductRecord> productRecords = productRecordMapper.selectList(productRecordLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(productRecords)){
            return 0;
        }
        LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getId, productRecords.stream().map(ProductRecord::getTicketRegistrationId).collect(Collectors.toList()));
        List<TicketRegistration> ticketRegistrations = ticketRegistrationMapper.selectList(ticketRegistrationLambdaQueryWrapper);
        // 修改产品信息
        for (ProductRecord productRecord : productRecords) {
            BigDecimal subtract = ticketRegistrations.get(0).getInvoiceAmount().subtract(productRecords.get(0).getTicketsAmount());
            // 小于等于0删除 ,大于0修改
            if(subtract.compareTo(BigDecimal.ZERO) <= 0){
                ticketRegistrationMapper.deleteById(ticketRegistrations.get(0));
                // 删除付款流水
                paymentRegistrationMapper.delete(new LambdaQueryWrapper<PaymentRegistration>().eq(PaymentRegistration::getTicketRegistrationId, ticketRegistrations.get(0).getId()));
            }else if(subtract.compareTo(BigDecimal.ZERO) > 0){
                ticketRegistrations.get(0).setInvoiceAmount(subtract);
                ticketRegistrationMapper.updateById(ticketRegistrations.get(0));
            }
            LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
            salesLedgerProductLambdaQueryWrapper.eq(SalesLedgerProduct::getId, productRecord.getSaleLedgerProjectId())
                    .eq(SalesLedgerProduct::getType, 2);
            List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(salesLedgerProducts)){
                for (SalesLedgerProduct salesLedgerProduct : salesLedgerProducts) {
                    salesLedgerProduct.setFutureTickets(salesLedgerProduct.getFutureTickets().add(productRecord.getTicketsNum()));
                    salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getFutureTicketsAmount().add(productRecord.getTicketsAmount()));
                    salesLedgerProduct.setTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getFutureTicketsAmount()));
                    salesLedgerProduct.setTicketsNum(salesLedgerProduct.getQuantity().subtract(salesLedgerProduct.getFutureTickets()));
                    salesLedgerProductMapper.updateById(salesLedgerProduct);
                }
            }
        }
        // 删除采购台账产品开票记录
        productRecordMapper.delete(productRecordLambdaQueryWrapper);
        return 1;
    }
    @Override
@@ -282,9 +338,13 @@
    @Override
    public IPage<TicketRegistration> selectTicketRegistrationListPage(Page page, TicketRegistration ticketRegistration) {
        LambdaQueryWrapper<TicketRegistration> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(ticketRegistration.getPurchaseContractNumber())) {
            queryWrapper.like(TicketRegistration::getPurchaseContractNumber, ticketRegistration.getPurchaseContractNumber())
                    .like(TicketRegistration::getSupplierName, ticketRegistration.getSupplierName());
        if (StringUtils.isNotBlank(ticketRegistration.getSupplierNameOrContractNo())) {
            queryWrapper.and(wrapper -> wrapper
                    .like(TicketRegistration::getPurchaseContractNumber, ticketRegistration.getSupplierNameOrContractNo())
                    .or()
                    .like(TicketRegistration::getSupplierName, ticketRegistration.getSupplierNameOrContractNo())
                    .or()
                    .like(TicketRegistration::getSalesContractNo, ticketRegistration.getSupplierNameOrContractNo()));
        }
        if (!ObjectUtils.isEmpty(ticketRegistration.getIssueDateStart()) && !ObjectUtils.isEmpty(ticketRegistration.getIssueDateEnd())) {
            queryWrapper.between(TicketRegistration::getIssueDate, LocalDate.parse(ticketRegistration.getIssueDateStart(), DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalDate.parse(ticketRegistration.getIssueDateEnd(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
@@ -312,8 +372,13 @@
            if (ticketRegistration.getStatus()) {
                ticketRegistrationIPage.getRecords().removeIf(receiptPaymentDto1 -> new BigDecimal("0.00").equals(receiptPaymentDto1.getUnPaymentAmountTotal()));
                ticketRegistrationIPage.setTotal(ticketRegistrationIPage.getRecords().size());
            }
        }
        ticketRegistrationIPage.getRecords().forEach(item -> {
            // 已付款金额 == 待付款金额
            item.setStatusName(item.getPaymentAmountTotal().compareTo(item.getInvoiceAmount()) == 0 ? "已完成付款" : "未完成付款");
        });
        return ticketRegistrationIPage;
    }
@@ -328,10 +393,16 @@
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductService.selectSalesLedgerProductList(salesLedgerProduct);
        purchaseLedgerDto.setProductData(salesLedgerProducts);
        List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
                .eq(CommonFile::getType, FileNameType.PURCHASELEDGER.getValue())
                .eq(CommonFile::getType, FileNameType.PURCHASE.getValue())
                .eq(CommonFile::getCommonId, id));
        purchaseLedgerDto.setSalesLedgerFiles(commonFiles);
        return purchaseLedgerDto;
    }
    @Override
    public List<PaymentRegistrationDto> getPaymentRegistrationDtoById(Long id) {
        List<PaymentRegistrationDto> paymentRegistrationDtos =purchaseLedgerMapper.getPaymentRegistrationDtoById(id);
        return paymentRegistrationDtos;
    }
    private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
@@ -350,19 +421,20 @@
        // 批量更新(需要 MyBatis 提供批量更新方法)
        if (!updateList.isEmpty()) {
            AtomicBoolean isUpdated = new AtomicBoolean(true);
            updateList.forEach(product -> {
                // 非空校验,任一字段为空则抛出异常
                if (product.getQuantity() == null) {
                    throw new BaseException("数量不能为空");
                if (product.getQuantity() != null) {
                    isUpdated.set(false);
                }
                if (product.getTicketsNum() == null) {
                    throw new BaseException("已开票数量不能为空");
                if (product.getTicketsNum() != null) {
                    isUpdated.set(false);
                }
                if (product.getTaxInclusiveTotalPrice() == null) {
                    throw new BaseException("含税总价不能为空");
                if (product.getTaxInclusiveTotalPrice() != null) {
                    isUpdated.set(false);
                }
                if (product.getTicketsAmount() == null) {
                    throw new BaseException("本次来票金额(元)不能为空");
                if (product.getTicketsAmount() != null) {
                    isUpdated.set(false);
                }
                // 计算 futureTickets(直接使用 BigDecimal 计算,避免精度丢失)
@@ -380,6 +452,9 @@
                product.setType(type);
                salesLedgerProductMapper.updateById(product);
            });
            if(isUpdated.get()){
                throw new RuntimeException("来票数不可全部为空");
            }
        }
    }