| | |
| | | package com.ruoyi.purchase.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.purchase.dto.PaymentRegistrationDto; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerDto; |
| | | import com.ruoyi.purchase.dto.TicketRegistrationDto; |
| | |
| | | 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; |
| | |
| | | import com.ruoyi.sales.service.ISalesLedgerProductService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | private final ProductRecordMapper productRecordMapper; |
| | | |
| | | @Autowired |
| | | private ISalesLedgerProductService salesLedgerProductService; |
| | | private final ISalesLedgerProductService salesLedgerProductService; |
| | | |
| | | @Autowired |
| | | private PaymentRegistrationMapper paymentRegistrationMapper; |
| | | |
| | | @Value("${file.upload-dir}") |
| | | private String uploadDir; |
| | | private final PaymentRegistrationMapper paymentRegistrationMapper; |
| | | |
| | | |
| | | @Override |
| | |
| | | BeanUtils.copyProperties(ticketRegistrationDto, ticketRegistration); |
| | | ticketRegistration.setPurchaseContractNumber(purchaseLedger.getPurchaseContractNumber()); |
| | | ticketRegistration.setTenantId(purchaseLedger.getTenantId()); |
| | | ticketRegistration.setIssueDate(ticketRegistrationDto.getIssueDate()); |
| | | ticketRegistration.setIssueDate(ticketRegistrationDto.getEntryDate()); |
| | | ticketRegistration.setContractAmount(purchaseLedger.getContractAmount()); |
| | | ticketRegistration.setSalesLedgerId(purchaseLedger.getSalesLedgerId()); |
| | | ticketRegistration.setEnterDate(ticketRegistrationDto.getEnterDate()); |
| | |
| | | productRecord.setPurchaseLedgerId(ticketRegistrationDto.getPurchaseLedgerId()); |
| | | productRecord.setCreatedAt(DateUtils.getNowDate()); |
| | | BeanUtils.copyProperties(salesLedgerProduct, productRecord); |
| | | //同步更新其他剩余数量和金额 |
| | | productRecordMapper.update( |
| | | null, |
| | | new LambdaUpdateWrapper<ProductRecord>() |
| | | .eq(ProductRecord::getPurchaseLedgerId, ticketRegistrationDto.getPurchaseLedgerId()) |
| | | .set(ProductRecord::getFutureTickets, salesLedgerProduct.getFutureTickets()) |
| | | .set(ProductRecord::getFutureTicketsAmount, salesLedgerProduct.getFutureTicketsAmount()) |
| | | ); |
| | | productRecord.setSaleLedgerProjectId(salesLedgerProduct.getId()); |
| | | productRecord.setId(null); |
| | | productRecord.setType("2"); |
| | | insert = productRecordMapper.insert(productRecord); |
| | | } |
| | | if (insert <= 0) { |
| | | throw new RuntimeException("产品开票数都为0,请检查"); |
| | | insert += productRecordMapper.insert(productRecord); |
| | | } |
| | | } |
| | | } |
| | | // 迁移临时文件到正式目录 |
| | | if (ticketRegistrationDto.getTempFileIds() != null && !ticketRegistrationDto.getTempFileIds().isEmpty()) { |
| | | migrateTempFilesToFormal(ticketRegistration.getId(), ticketRegistrationDto.getTempFileIds()); |
| | | if (insert <= 0) { |
| | | throw new RuntimeException("产品开票数都为0,请检查"); |
| | | } |
| | | } |
| | | return rowsAffected; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将临时文件迁移到正式目录 |
| | | * |
| | | * @param businessId 业务ID(销售台账ID) |
| | | * @param tempFileIds 临时文件ID列表 |
| | | * @throws IOException 文件操作异常 |
| | | */ |
| | | public void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException { |
| | | if (CollectionUtils.isEmpty(tempFileIds)) { |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int delRegistration(Long[] ids) { |
| | | |
| | | // 查询要删除的产品来票记录 |
| | | LambdaQueryWrapper<ProductRecord> productWrapper = new LambdaQueryWrapper<>(); |
| | | productWrapper.in(ProductRecord::getId, Arrays.asList(ids)); |
| | | List<ProductRecord> productRecords = productRecordMapper.selectList(productWrapper); |
| | | |
| | | if (CollectionUtils.isEmpty(productRecords)) { |
| | | return 0; |
| | | } |
| | | |
| | | // 收集关联ID |
| | | Set<Long> ticketRegistrationIds = productRecords.stream() |
| | | .map(ProductRecord::getTicketRegistrationId) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Set<Long> salesLedgerProductIds = productRecords.stream() |
| | | .map(ProductRecord::getSaleLedgerProjectId) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | // 查询来票登记 |
| | | List<TicketRegistration> ticketRegistrations = |
| | | ticketRegistrationMapper.selectBatchIds(ticketRegistrationIds); |
| | | |
| | | // 处理来票登记金额、付款流水 |
| | | for (TicketRegistration ticket : ticketRegistrations) { |
| | | |
| | | // 该来票登记下所有产品记录 |
| | | List<ProductRecord> recordsOfTicket = productRecords.stream() |
| | | .filter(r -> r.getTicketRegistrationId().equals(ticket.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 计算要回退的金额 |
| | | BigDecimal rollbackAmount = recordsOfTicket.stream() |
| | | .map(ProductRecord::getTicketsAmount) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | BigDecimal remain = ticket.getInvoiceAmount().subtract(rollbackAmount); |
| | | |
| | | if (remain.compareTo(BigDecimal.ZERO) <= 0) { |
| | | // 删除来票登记 |
| | | ticketRegistrationMapper.deleteById(ticket.getId()); |
| | | // 删除付款流水 |
| | | paymentRegistrationMapper.delete( |
| | | new LambdaQueryWrapper<PaymentRegistration>() |
| | | .eq(PaymentRegistration::getTicketRegistrationId, ticket.getId()) |
| | | ); |
| | | } else { |
| | | ticket.setInvoiceAmount(remain); |
| | | ticketRegistrationMapper.updateById(ticket); |
| | | } |
| | | } |
| | | |
| | | // 回退销售台账产品的未来票 |
| | | for (ProductRecord record : productRecords) { |
| | | |
| | | LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(SalesLedgerProduct::getId, record.getSaleLedgerProjectId()) |
| | | .eq(SalesLedgerProduct::getType, 2); |
| | | |
| | | SalesLedgerProduct product = salesLedgerProductMapper.selectOne(wrapper); |
| | | if (product != null) { |
| | | product.setFutureTickets( |
| | | product.getFutureTickets().add(record.getTicketsNum()) |
| | | ); |
| | | product.setFutureTicketsAmount( |
| | | product.getFutureTicketsAmount().add(record.getTicketsAmount()) |
| | | ); |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | } |
| | | |
| | | // 删除产品来票记录 |
| | | productRecordMapper.delete(productWrapper); |
| | | |
| | | // 重新计算 currentInvoiceAmount |
| | | for (Long productId : salesLedgerProductIds) { |
| | | refreshCurrentInvoiceAmount(productId); |
| | | } |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | private void refreshCurrentInvoiceAmount(Long salesLedgerProductId) { |
| | | |
| | | // 查询该产品最新一条来票记录 |
| | | LambdaQueryWrapper<ProductRecord> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(ProductRecord::getSaleLedgerProjectId, salesLedgerProductId) |
| | | .orderByDesc(ProductRecord::getCreatedAt) |
| | | .last("limit 1"); |
| | | |
| | | ProductRecord latestRecord = productRecordMapper.selectOne(wrapper); |
| | | |
| | | SalesLedgerProduct product = |
| | | salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | |
| | | if (product == null) { |
| | | return; |
| | | } |
| | | |
| | | // 构建正式目录路径(按业务类型和日期分组) |
| | | String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE); |
| | | |
| | | Path formalDirPath = Paths.get(formalDir); |
| | | |
| | | // 确保正式目录存在(递归创建) |
| | | if (!Files.exists(formalDirPath)) { |
| | | Files.createDirectories(formalDirPath); |
| | | if (latestRecord == null) { |
| | | // 没有任何来票记录 |
| | | product.setTicketsAmount(BigDecimal.ZERO); |
| | | } else { |
| | | // 永远取最新一条 |
| | | product.setTicketsAmount(latestRecord.getTicketsAmount()); |
| | | } |
| | | |
| | | for (String tempFileId : tempFileIds) { |
| | | // 查询临时文件记录 |
| | | TempFile tempFile = tempFileMapper.selectById(tempFileId); |
| | | if (tempFile == null) { |
| | | log.warn("临时文件不存在,跳过处理: {}", tempFileId); |
| | | continue; |
| | | } |
| | | |
| | | // 构建正式文件名(包含业务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) +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.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING); |
| | | Files.deleteIfExists(Paths.get(tempFile.getTempPath())); |
| | | log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath); |
| | | |
| | | // 更新文件记录(关联到业务ID) |
| | | CommonFile fileRecord = new CommonFile(); |
| | | fileRecord.setCommonId(businessId); |
| | | fileRecord.setName(originalFilename); |
| | | fileRecord.setUrl(formalFilePath.toString()); |
| | | fileRecord.setCreateTime(LocalDateTime.now()); |
| | | fileRecord.setType(4); |
| | | commonFileMapper.insert(fileRecord); |
| | | |
| | | log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath); |
| | | } catch (IOException e) { |
| | | log.error("文件迁移失败: {}", tempFile.getTempPath(), e); |
| | | // 可选择回滚事务或记录失败文件 |
| | | throw new IOException("文件迁移异常", e); |
| | | } |
| | | } |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int delRegistration(Long[] 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)); |
| | | }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())); |
| | | salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | |
| | | } |
| | | } |
| | | } |
| | | // 删除采购台账产品开票记录 |
| | | productRecordMapper.delete(productRecordLambdaQueryWrapper); |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | | public TicketRegistrationDto getRegistrationById(TicketRegistrationDto ticketRegistrationDto) { |
| | |
| | | ticketRegistrationIPage.setTotal(ticketRegistrationIPage.getRecords().size()); |
| | | } |
| | | } |
| | | ticketRegistrationIPage.getRecords().forEach(item -> { |
| | | // 已付款金额 == 待付款金额 |
| | | item.setStatusName(item.getPaymentAmountTotal().compareTo(item.getInvoiceAmount()) == 0 ? "已完成付款" : "未完成付款"); |
| | | }); |
| | | return ticketRegistrationIPage; |
| | | } |
| | | |
| | |
| | | BeanUtils.copyProperties(purchaseLedger, purchaseLedgerDto); |
| | | SalesLedgerProduct salesLedgerProduct = new SalesLedgerProduct(); |
| | | salesLedgerProduct.setSalesLedgerId(id); |
| | | salesLedgerProduct.setType(SalesLedgerType.SALES_LEDGER_TYPE_SALES_LEDGER.getValue()); |
| | | salesLedgerProduct.setType(SalesLedgerType.SALES_LEDGER_TYPE_SALES_LEDGER.getCode()); |
| | | List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductService.selectSalesLedgerProductList(salesLedgerProduct); |
| | | purchaseLedgerDto.setProductData(salesLedgerProducts); |
| | | List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>() |