czt
9 天以前 db67639449287bcec461916a7dca6003ee5dd03c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.fzzy.igds.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzzy.igds.domain.FileInfo;
import com.fzzy.igds.mapper.FileMapper;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.config.FrameworkConfig;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
/**
 * @Description
 * @Author CZT
 * @Date 2025/12/4 17:36
 */
@Service
public class FileService {
 
    @Resource
    private FileMapper fileMapper;
 
    /**
     * 异步执行附件保存
     * @param files     附件信息
     * @param bizId     业务id
     * @param bizTag    标签
     * @param pathTag   文件路径标识
     */
    @Async
    public void saveInoutFiles(List<FileInfo> files, String bizId, String bizTag, String pathTag) {
 
        if (null == files || files.isEmpty()) {
            return;
        }
 
        for (FileInfo data : files) {
            // 如果没有附件名称,则不保存附件信息
            if (StringUtils.isBlank(data.getFileName())) {
                continue;
            }
 
            data.setId(ContextUtil.generateId());
            data.setCompanyId(ContextUtil.getCompanyId());
 
            //文件全路径
            String filePath = getFileSavePath(pathTag) + data.getFileName();
            filePath = filePath.replace(FrameworkConfig.getProfile(), "/profile/");
            data.setFilePath(filePath);
 
            if (StringUtils.isNotEmpty(bizId)) {
                data.setBizId(bizId);
            }
            if (StringUtils.isNotEmpty(bizTag)) {
                data.setBizTag(bizTag);
            }
 
            data.setCreateTime(new Date());
            data.setCreateBy(ContextUtil.getLoginUserName());
 
            data.setUpdateTime(new Date());
            data.setUpdateBy(ContextUtil.getLoginUserName());
            fileMapper.insert(data);
        }
    }
 
    /**
     * 根据条件查询数据
     * @param companyId
     * @param deptId
     * @param bizId
     * @return
     */
    public List<FileInfo> listFile(String companyId, String deptId, String bizId, String bizTag) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(companyId)) {
            queryWrapper.eq("company_id", companyId);
        }
        if (StringUtils.isNotBlank(deptId)) {
            queryWrapper.eq("dept_id", deptId);
        }
        if (StringUtils.isNotBlank(bizId)) {
            queryWrapper.eq("biz_id", bizId);
        }
        if (StringUtils.isNotBlank(bizTag)) {
            queryWrapper.eq("biz_tag", bizTag);
        }
        return fileMapper.selectList(queryWrapper);
    }
 
    /**
     *
     * @param id
     */
    public void delFile(String id) {
        fileMapper.deleteById(id);
    }
 
    /**
     * 获取出入库文件路径
     * @param pathTag
     * @return
     */
    public String getFileSavePath(String pathTag) {
        if(StringUtils.isBlank(pathTag)){
            pathTag = "COMMON";
        }
        if("INOUT".equals(pathTag)) return getInoutFilePath();
        if("PATROL".equals(pathTag)) return getPatrolFilePath();
        if("DEPT".equals(pathTag)) return getDeptFilePath();
 
        return getCommonFilePath();
    }
 
    /**
     * 获取出入库文件路径
     * @return
     */
    public String getPatrolFilePath() {
        String basePath = FrameworkConfig.getProfile() + "INOUT/" + DateFormatUtils.format(new Date(), "yyyyMM") + "/";
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 获取出入库文件路径
     * @return
     */
    public String getInoutFilePath() {
        String basePath = FrameworkConfig.getProfile() + "INOUT/" + DateFormatUtils.format(new Date(), "yyyyMM") + "/";
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 获取库区路径下文件
     * @return
     */
    public String getDeptFilePath() {
 
        String basePath = FrameworkConfig.getProfile() + "IMG/"+ FrameworkConfig.getCompanyId() + "/SECURITY/";
 
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 获取公共路径
     * @return
     */
    public String getCommonFilePath() {
 
        String basePath = FrameworkConfig.getProfile() + "COMMON/" + DateFormatUtils.format(new Date(), "yyyyMM") + "/";
 
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 压缩图片
     *
     * @param filePath   压缩前路径
     * @param scale      压缩比例
     * @param outputPath 压缩后路径
     * @throws IOException
     */
    public void compressedImage(String filePath, double scale, String outputPath) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(new File(filePath));
        int newWidth = (int) (bufferedImage.getWidth() * scale);
        int newHeight = (int) (bufferedImage.getHeight() * scale);
 
        //创建压缩后的图片
        BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = compressedImage.createGraphics();
 
        //绘制原始图片到压缩后的图片上
        graphics2D.drawImage(bufferedImage, 0, 0, newWidth, newHeight, null);
        graphics2D.dispose();
 
        ImageIO.write(compressedImage, "jpg", new File(outputPath));
    }
 
}