From 1b5604f8a88000e2f51d4c989fd179989468ee67 Mon Sep 17 00:00:00 2001
From: yaowanxin <3588231647@qq.com>
Date: 星期一, 11 八月 2025 09:48:08 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/pim_yys' into pim_ywx

---
 src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java |   96 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java b/src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java
index f228680..d43adf0 100644
--- a/src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java
+++ b/src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java
@@ -2,10 +2,15 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.other.mapper.TempFileMapper;
 import com.ruoyi.other.pojo.TempFile;
 import com.ruoyi.other.service.TempFileService;
+import com.ruoyi.sales.mapper.CommonFileMapper;
+import com.ruoyi.sales.pojo.CommonFile;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.catalina.util.URLEncoder;
+import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -13,10 +18,14 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 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.List;
 import java.util.UUID;
 
@@ -27,14 +36,27 @@
     @Autowired
     private TempFileMapper tempFileMapper;
 
+    @Autowired
+    private CommonFileMapper commonFileMapper;
+
+    @Value("${file.upload-dir}")
+    private String uploadDir;
+
     @Value("${file.temp-dir}")
     private String tempDir;
 
     // 涓婁紶鍒颁复鏃剁洰褰�
     @Override
-    public TempFile uploadFile(MultipartFile file) throws IOException {
+    public TempFile uploadFile(MultipartFile file,Integer type) throws IOException {
         // 1. 鐢熸垚涓存椂鏂囦欢ID鍜岃矾寰�
         String tempId = UUID.randomUUID().toString();
+        String originalFilename = file.getOriginalFilename();
+        if(originalFilename == null) throw new IOException("鏂囦欢鍚嶄笉鑳戒负绌�");
+//        URLEncoder urlEncoder = new URLEncoder();
+//        String encodedFilename = urlEncoder.encode(originalFilename, StandardCharsets.UTF_8);
+//        encodedFilename = encodedFilename.replaceAll("%2E",".");
+//        Path tempFilePath = Paths.get(tempDir, tempId + "_" + encodedFilename);
+
         Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename());
 
         // 2. 纭繚鐩綍瀛樺湪
@@ -52,11 +74,83 @@
         tempFileRecord.setOriginalName(file.getOriginalFilename());
         tempFileRecord.setTempPath(tempFilePath.toString());
         tempFileRecord.setExpireTime(LocalDateTime.now().plusHours(2)); // 2灏忔椂鍚庤繃鏈�
+        tempFileRecord.setType(type);
         tempFileMapper.insert(tempFileRecord);
 
         return tempFileRecord;
     }
 
+    /**
+     * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍
+     *
+     * @param businessId  涓氬姟ID锛堥攢鍞彴璐D锛�
+     * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃
+     * @throws IOException 鏂囦欢鎿嶄綔寮傚父
+     */
+    public void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds, Integer fileType) throws IOException {
+        if (com.baomidou.mybatisplus.core.toolkit.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) +
+                    (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(fileType);
+                commonFileMapper.insert(fileRecord);
+
+                // 鍒犻櫎涓存椂鏂囦欢璁板綍
+                tempFileMapper.deleteById(tempFile);
+
+                log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath);
+            } catch (IOException e) {
+                log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e);
+                // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠�
+                throw new IOException("鏂囦欢杩佺Щ寮傚父", e);
+            }
+        }
+    }
+
     @Scheduled(cron = "0 0 3 * * ?") // 姣忓ぉ鍑屾櫒3鐐规墽琛�
     public void cleanupExpiredTempFiles() {
         LambdaQueryWrapper<TempFile> wrapper = new LambdaQueryWrapper<>();

--
Gitblit v1.9.3