From 6bd1dc0b355e279b709bc49dda5207121e9b41a9 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 15 五月 2025 15:41:26 +0800
Subject: [PATCH] 1.来票台账优化 2.上传优化

---
 src/main/java/com/ruoyi/purchase/service/impl/InvoicePurchaseServiceImpl.java |  215 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 204 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/InvoicePurchaseServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/InvoicePurchaseServiceImpl.java
index f9a2487..5a0a9b4 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/InvoicePurchaseServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/InvoicePurchaseServiceImpl.java
@@ -2,15 +2,38 @@
 
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.other.mapper.TempFileMapper;
+import com.ruoyi.other.pojo.TempFile;
+import com.ruoyi.project.system.domain.SysUser;
+import com.ruoyi.project.system.mapper.SysUserMapper;
+import com.ruoyi.purchase.dto.InvoicePurchaseDto;
 import com.ruoyi.purchase.mapper.InvoicePurchaseMapper;
+import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
 import com.ruoyi.purchase.pojo.InvoicePurchase;
+import com.ruoyi.purchase.pojo.PurchaseLedger;
 import com.ruoyi.purchase.service.IInvoicePurchaseService;
-import lombok.AllArgsConstructor;
+import com.ruoyi.sales.mapper.CommonFileMapper;
+import com.ruoyi.sales.pojo.CommonFile;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
-import java.util.Arrays;
-import java.util.List;
+import java.io.IOException;
+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.*;
+import java.util.stream.Collectors;
 
 /**
  * 鍙戠エ淇℃伅Service涓氬姟灞傚鐞�
@@ -19,14 +42,80 @@
  * @date 2025-05-14
  */
 @Service
-@AllArgsConstructor
+@RequiredArgsConstructor
+@Slf4j
 public class InvoicePurchaseServiceImpl extends ServiceImpl<InvoicePurchaseMapper, InvoicePurchase> implements IInvoicePurchaseService {
 
-    private InvoicePurchaseMapper invoicePurchaseMapper;
+    private final InvoicePurchaseMapper invoicePurchaseMapper;
+
+    private final PurchaseLedgerMapper purchaseLedgerMapper;
+
+    private final SysUserMapper userMapper;
+
+    private final CommonFileMapper commonFileMapper;
+
+    private final TempFileMapper tempFileMapper;
+
+    @Value("${file.upload-dir}")
+    private String uploadDir;
 
     @Override
-    public List<InvoicePurchase> selectInvoicePurchaseList(InvoicePurchase invoicePurchase) {
-        return invoicePurchaseMapper.selectList(new LambdaQueryWrapper<>());
+    public List<InvoicePurchaseDto> selectInvoicePurchaseList(InvoicePurchaseDto invoicePurchaseDto) {
+        // 鏋勫缓鍙戠エ鏌ヨ鏉′欢
+        LambdaQueryWrapper<InvoicePurchase> queryWrapper = new LambdaQueryWrapper<>();
+        Optional.ofNullable(invoicePurchaseDto)
+                .ifPresent(dto -> {
+                    if (StringUtils.hasText(dto.getPurchaseContractNo())) {
+                        queryWrapper.like(InvoicePurchase::getPurchaseContractNo, dto.getPurchaseContractNo());
+                    }
+                    if (StringUtils.hasText(dto.getSupplierName())) {
+                        queryWrapper.like(InvoicePurchase::getSupplierName, dto.getSupplierName());
+                    }
+                    // 澶勭悊鏃ユ湡绫诲瀷瀛楁
+                    if (dto.getIssueDate() != null) {
+                        queryWrapper.eq(InvoicePurchase::getIssueDate,invoicePurchaseDto.getIssueDate());
+                    }
+                });
+
+        // 鏌ヨ鍙戠エ鍒楄〃
+        List<InvoicePurchase> invoiceList = invoicePurchaseMapper.selectList(queryWrapper);
+
+        // 濡傛灉娌℃湁鏌ヨ鍒板彂绁紝鐩存帴杩斿洖绌哄垪琛�
+        if (CollectionUtils.isEmpty(invoiceList)) {
+            return Collections.emptyList();
+        }
+
+        // 鎻愬彇鎵�鏈夊彂绁↖D
+        List<Long> invoiceIds = invoiceList.stream()
+                .map(InvoicePurchase::getId)
+                .collect(Collectors.toList());
+
+        // 鎵归噺鏌ヨ杩欎簺鍙戠エ鍏宠仈鐨勬枃浠朵俊鎭�
+        LambdaQueryWrapper<CommonFile> fileQueryWrapper = new LambdaQueryWrapper<>();
+        fileQueryWrapper.in(CommonFile::getCommonId, invoiceIds)
+                .eq(CommonFile::getType,"3");
+        List<CommonFile> fileList = commonFileMapper.selectList(fileQueryWrapper);
+
+        // 灏嗘枃浠朵俊鎭槧灏勫埌瀵瑰簲鐨勫彂绁↖D
+        Map<Long, String> fileMap = fileList.stream()
+                .collect(Collectors.toMap(
+                        CommonFile::getCommonId,
+                        CommonFile::getName,
+                        (existing, replacement) -> existing // 濡傛灉鏈夊涓枃浠讹紝鍙栫涓�涓�
+                ));
+
+        // 灏嗘枃浠朵俊鎭缃埌鍙戠エDTO涓�
+        List<InvoicePurchaseDto> resultList = new ArrayList<>();
+        for (InvoicePurchase invoice : invoiceList) {
+            InvoicePurchaseDto dto = new InvoicePurchaseDto();
+            // 灏咺nvoicePurchase鐨勫睘鎬у鍒跺埌DTO
+            BeanUtils.copyProperties(invoice, dto);
+            // 璁剧疆鏂囦欢鍚嶏紝濡傛灉瀛樺湪鐨勮瘽
+            dto.setFileName(fileMap.getOrDefault(invoice.getId(), null));
+            resultList.add(dto);
+        }
+
+        return resultList;
     }
 
     @Override
@@ -35,11 +124,115 @@
     }
 
     @Override
-    public int addOrUpdateInvoice(InvoicePurchase invoicePurchase) {
-        if (invoicePurchase.getId() == null) {
-            return invoicePurchaseMapper.insert(invoicePurchase);
+    public int addOrUpdateInvoice(InvoicePurchaseDto invoicePurchaseDto) throws IOException {
+        int i;
+        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(invoicePurchaseDto.getPurchaseLedgerId());
+        InvoicePurchase invoicePurchase = new InvoicePurchase();
+        BeanUtils.copyProperties(invoicePurchaseDto, invoicePurchase);
+        invoicePurchase.setPurchaseContractNo(purchaseLedger.getPurchaseContractNumber());
+        invoicePurchase.setSalesContractNo(purchaseLedger.getSalesContractNo());
+        SysUser sysUser = userMapper.selectUserById(invoicePurchase.getIssUerId());
+        invoicePurchase.setIssUer(sysUser.getNickName());
+        invoicePurchase.setTenantId(purchaseLedger.getTenantId());
+        if (invoicePurchaseDto.getId() == null) {
+            i = invoicePurchaseMapper.insert(invoicePurchase);
         } else {
-            return invoicePurchaseMapper.updateById(invoicePurchase);
+            i = invoicePurchaseMapper.updateById(invoicePurchase);
         }
+
+        // 杩佺Щ涓存椂鏂囦欢鍒版寮忕洰褰�
+        if (invoicePurchaseDto.getTempFileIds() != null && !invoicePurchaseDto.getTempFileIds().isEmpty()) {
+            migrateTempFilesToFormal(invoicePurchase.getId(), invoicePurchaseDto.getTempFileIds());
+        }
+
+        return i;
+    }
+
+    /**
+     * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍
+     *
+     * @param businessId  涓氬姟ID锛堥攢鍞彴璐D锛�
+     * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃
+     * @throws IOException 鏂囦欢鎿嶄綔寮傚父
+     */
+    private void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException {
+        if (CollectionUtils.isEmpty(tempFileIds)) {
+            return;
+        }
+
+        // 鏋勫缓姝e紡鐩綍璺緞锛堟寜涓氬姟绫诲瀷鍜屾棩鏈熷垎缁勶級
+        String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
+
+        Path formalDirPath = Paths.get(formalDir);
+
+        // 纭繚姝e紡鐩綍瀛樺湪锛堥�掑綊鍒涘缓锛�
+        if (!Files.exists(formalDirPath)) {
+            Files.createDirectories(formalDirPath);
+        }
+
+        for (String tempFileId : tempFileIds) {
+            // 鏌ヨ涓存椂鏂囦欢璁板綍
+            TempFile tempFile = tempFileMapper.selectById(tempFileId);
+            if (tempFile == null) {
+                log.warn("涓存椂鏂囦欢涓嶅瓨鍦紝璺宠繃澶勭悊: {}", tempFileId);
+                continue;
+            }
+
+            // 鏋勫缓姝e紡鏂囦欢鍚嶏紙鍖呭惈涓氬姟ID鍜屾椂闂存埑锛岄伩鍏嶅啿绐侊級
+            String originalFilename = tempFile.getOriginalName();
+            String fileExtension = FilenameUtils.getExtension(originalFilename);
+            String formalFilename = businessId + "_" +
+                    System.currentTimeMillis() + "_" +
+                    UUID.randomUUID().toString().substring(0, 8) +
+                    (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
+                );
+                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(tempFile.getType());
+                commonFileMapper.insert(fileRecord);
+
+                log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath);
+            } catch (IOException e) {
+                log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e);
+                // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠�
+                throw new IOException("鏂囦欢杩佺Щ寮傚父", e);
+            }
+        }
+    }
+
+    @Override
+    public InvoicePurchaseDto getInvoiceById(InvoicePurchaseDto invoicePurchaseDto) {
+        InvoicePurchase invoicePurchase = invoicePurchaseMapper.selectById(invoicePurchaseDto.getId());
+        InvoicePurchaseDto resultDto = new InvoicePurchaseDto();
+        BeanUtils.copyProperties(invoicePurchase, resultDto);
+
+        // 鏌ヨ涓婁紶鏂囦欢
+        LambdaQueryWrapper<CommonFile> commonFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        commonFileLambdaQueryWrapper.eq(CommonFile::getCommonId, invoicePurchaseDto.getId())
+                .eq(CommonFile::getType, "3");
+        List<CommonFile> commonFiles = commonFileMapper.selectList(commonFileLambdaQueryWrapper);
+        resultDto.setCommonFiles(commonFiles);
+        return resultDto;
+    }
+
+    @Override
+    public List<InvoicePurchase> selectInvoicePurchaseLists(InvoicePurchase invoicePurchase) {
+        return invoicePurchaseMapper.selectList(new LambdaQueryWrapper<>());
     }
 }

--
Gitblit v1.9.3