hyingbo
2025-05-30 5d4112fa1bbd47dc6e7b9c0d239e34b020776924
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
package org.jeecg.modules.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.FileUploadResult;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.ExceptionCast;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.FileUtil;
import org.jeecg.modules.system.entity.FileDocInfo;
import org.jeecg.modules.system.mapper.DocInfoMapper;
import org.jeecg.modules.system.service.IDocInfoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
@Service
public class DocInfoServiceImpl extends ServiceImpl<DocInfoMapper, FileDocInfo> implements IDocInfoService {
 
    @Override
    public boolean addDocInfo(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new JeecgBootException("非法参数");
        }
        FileUploadResult fileUploadResult = FileUtil.uploadFile(file);
        if(fileUploadResult == null)
            throw new JeecgBootException("文档导入失败");
 
        String fileName = FileUtil.getFilenameNonSuffix(file.getOriginalFilename());
        List<FileDocInfo> fileDocInfoList = this.list(new LambdaQueryWrapper<FileDocInfo>().eq(FileDocInfo::getFileName, fileName));
        if (CollectionUtils.isNotEmpty(fileDocInfoList))
            throw new JeecgBootException("该文档已存在");
 
        FileDocInfo docInfo = new FileDocInfo();
        String docId = IdWorker.getIdStr();
        docInfo.setId(docId);
        docInfo.setFileName(fileUploadResult.getFileName());
        docInfo.setFilePath(fileUploadResult.getFilePath());
        docInfo.setFileEncodeName(fileUploadResult.getFileEncodeName());
        docInfo.setFileSize(fileUploadResult.getFileSize());
        docInfo.setFileSuffix(fileUploadResult.getFileSuffix());
        return super.save(docInfo);
    }
 
    @Override
    public boolean deleteDocInfo(String id) {
 
        if(StringUtils.isBlank(id))
            throw new JeecgBootException("非法参数");
        FileDocInfo en = super.getById(id);
        if(en == null)
            throw new JeecgBootException("未查询到文件信息");
        FileUtil.deleteFile(en.getFilePath(), en.getFileName());
        return super.removeById(id);
    }
 
    @Override
    public Result<?> downloadDocFile(HttpServletResponse response, String id) {
 
        if(!StringUtils.isBlank(id))
            throw new JeecgBootException("非法参数");
        FileDocInfo en = super.getById(id);
        if(en == null)
            throw new JeecgBootException("未查询到文件信息");
 
        String fileName;
        if(StringUtils.isNotBlank(en.getFileSuffix())) {
            fileName = en.getFileName() + "." + en.getFileSuffix();
        }else {
            fileName = en.getFileName();
        }
        FileUtil.downLoadFile(response, en.getFileEncodeName(), en.getFilePath(), fileName);
        return null;
    }
}