gongchunyi
6 小时以前 5652acfd578f66353f59f398f42c88da901e245e
src/main/java/com/ruoyi/sales/service/impl/CommonFileServiceImpl.java
@@ -1,7 +1,11 @@
package com.ruoyi.sales.service.impl;
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.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.other.mapper.TempFileMapper;
import com.ruoyi.other.pojo.TempFile;
import com.ruoyi.sales.mapper.CommonFileMapper;
@@ -39,9 +43,29 @@
    @Value("${file.upload-dir}")
    private String uploadDir;
    public List<CommonFile> getFileListByBusinessId(Long businessId,Integer type) {
        return commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, businessId)
                .eq(CommonFile::getType, type));
    }
    public void deleteByBusinessId(Long businessId,Integer type) {
        commonFileMapper.delete(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, businessId)
                .eq(CommonFile::getType, type));
    }
    public void deleteByBusinessIds(List<Long> businessId,Integer type) {
        commonFileMapper.delete(new LambdaQueryWrapper<CommonFile>().in(CommonFile::getCommonId, businessId)
                .eq(CommonFile::getType, type));
    }
    @Override
    public int deleteSalesLedgerByIds(Long[] ids) {
       for (Long id : ids) {
           if (id == null) {
               return 1;
           }
       }
        return commonFileMapper.deleteBatchIds(Arrays.asList(ids));
    }
@@ -65,6 +89,7 @@
        commonFile.setCommonId(id);
        commonFile.setName(file.getOriginalFilename());
        commonFile.setUrl(tempFilePath.toString());
        commonFile.setLink(buildAccessLink(tempFilePath));
        commonFile.setType(type);
        commonFileMapper.insert(commonFile);
        return commonFile;
@@ -112,18 +137,21 @@
            String formalFilename = businessId + "_" +
                    System.currentTimeMillis() + "_" +
                    UUID.randomUUID().toString().substring(0, 8) +
                    (com.ruoyi.common.utils.StringUtils.hasText(fileExtension) ? "." + fileExtension : "");
                    (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)
@@ -131,6 +159,7 @@
                fileRecord.setCommonId(businessId);
                fileRecord.setName(originalFilename);
                fileRecord.setUrl(formalFilePath.toString());
                fileRecord.setLink(buildAccessLink(formalFilePath));
                fileRecord.setCreateTime(LocalDateTime.now());
                fileRecord.setType(tempFile.getType());
                commonFileMapper.insert(fileRecord);
@@ -143,4 +172,20 @@
            }
        }
    }
    private String buildAccessLink(Path formalFilePath) {
        String normalizedPath = formalFilePath.toString().replace("\\", "/");
        String profile = RuoYiConfig.getProfile();
        String normalizedProfile = profile == null ? "" : profile.replace("\\", "/");
        String relativePath = normalizedPath;
        if (StringUtils.hasText(normalizedProfile) && normalizedPath.startsWith(normalizedProfile)) {
            relativePath = normalizedPath.substring(normalizedProfile.length());
        }
        if (!relativePath.startsWith("/")) {
            relativePath = "/" + relativePath;
        }
        return Constants.RESOURCE_PREFIX + relativePath;
    }
}