gaoluyang
21 小时以前 abd339106ed3a6465293edccd33e5f87bd43bdde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.ruoyi.manage.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
 
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.manage.mapper.ManageDocumentListMapper;
import com.ruoyi.manage.pojo.ManageDocumentList;
import com.ruoyi.manage.service.ManageDocumentListService;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import jakarta.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 文件清单
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-11-08 11:08:11
 */
@Service
public class ManageDocumentListServiceImpl extends ServiceImpl<ManageDocumentListMapper, ManageDocumentList> implements ManageDocumentListService {
 
    @Resource
    private ManageDocumentListMapper manageDocumentListMapper;
 
 
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    @Override
    public IPage<ManageDocumentList> pageManageDocumentList(Page page, ManageDocumentList manageDocumentList) {
        return manageDocumentListMapper.pageManageDocumentList(page, QueryWrappers.queryWrappers(manageDocumentList));
    }
 
 
    @Override
    public int uploadFile(Integer id, MultipartFile file) {
        String pathName;
        ManageDocumentList manageDocumentList = manageDocumentListMapper.selectById(id);
        if (manageDocumentList == null) {
            throw new ErrorException("文件清单不存在");
        }
        if (file == null || file.isEmpty()) {
            throw new ErrorException("请选择要上传的文件");
        }
        File realpath = new File(wordUrl).getAbsoluteFile();
        try {
            if (!realpath.exists() && !realpath.mkdirs()) {
                throw new IOException("目录创建失败: " + realpath);
            }
            pathName = UUID.randomUUID() + "_" + file.getOriginalFilename();
            // 用 Files.copy 而不是 transferTo:transferTo 遇到相对路径会按 Tomcat 临时目录解析
            Files.copy(file.getInputStream(), new File(realpath, pathName).toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorException("附件上传错误: " + e.getMessage());
        }
        if (ObjectUtils.isNotEmpty(manageDocumentList.getUrl())) {
            // 删除旧文件
            new File(realpath, manageDocumentList.getUrl()).delete();
        }
        manageDocumentList.setUrl(pathName);
        return manageDocumentListMapper.updateById(manageDocumentList);
    }
 
    @Override
    public void importExcel(List<ManageDocumentList> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        list = list.stream().filter(manageDocumentList -> ObjectUtils.isNotEmpty(manageDocumentList.getName())).collect(Collectors.toList());
        saveBatch(list);
    }
 
}