zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package org.jeecg.modules.doc.service.impl;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.doc.component.DocFileDealComp;
import org.jeecg.modules.doc.component.FileConstant;
import org.jeecg.modules.doc.component.PathPermissionComp;
import org.jeecg.modules.doc.constant.Constant;
import org.jeecg.modules.doc.entity.DocFilePath;
import org.jeecg.modules.doc.entity.RecoveryFile;
import org.jeecg.modules.doc.mapper.DocFilePathMapper;
import org.jeecg.modules.doc.mapper.RecoveryFileMapper;
import org.jeecg.modules.doc.service.IDocFilePathService;
import org.jeecg.modules.doc.service.IPathPermissionService;
import org.jeecg.modules.doc.vo.QiwenFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
 
/**
 * @Description: doc_file_path
 * @Author: jeecg-boot
 * @Date:   2022-07-04
 * @Version: V1.0
 */
@Service
public class DocFilePathServiceImpl extends ServiceImpl<DocFilePathMapper, DocFilePath> implements IDocFilePathService {
 
    @Resource
    RecoveryFileMapper recoveryFileMapper;
 
    @Autowired
    DocFileDealComp docFileDealComp;
 
    @Autowired
    private IPathPermissionService pathPermissionService;
 
    @Autowired
    private PathPermissionComp pathPermissionComp;
 
    public static Executor executor = Executors.newFixedThreadPool(20);
    @Override
    public void initDocFilePath() {
        DocFilePath filePath = new DocFilePath();
        filePath.setPathStatus(0);
 
        baseMapper.update(filePath, new LambdaQueryWrapper<DocFilePath>().ne(DocFilePath::getPathStatus,0));
    }
 
    @Override
    public List<DocFilePath> listFiles(Set<String> pathIds) {
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
        if (pathIds != null && pathIds.size()>0) {
            queryWrapper.in(DocFilePath::getPathId, pathIds);
        }
        queryWrapper.eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE)
                .orderByAsc(DocFilePath::getFilePath)
                .orderByAsc(DocFilePath::getFileName);
        return baseMapper.selectList(queryWrapper);
    }
 
    /**
     *
     * @param docFilePath 接收权限分配时模糊查询的文件名参数
     * @param pathIds 有访问权限的文件id集合
     * @return
     */
    @Override
    public List<DocFilePath> listDirTree(DocFilePath docFilePath, Set<String> pathIds) {
        LambdaQueryWrapper<DocFilePath> queryWrapper = new LambdaQueryWrapper<>();
 
        queryWrapper.and(i -> {
            i.apply("{0} = {1}", 1,2);
            if (pathIds != null && pathIds.size()>0) {
                i.or().in(DocFilePath::getPathId, pathIds);
            }
        });
 
        if (docFilePath != null && StringUtils.isNotBlank(docFilePath.getFileName())) {
            queryWrapper.like(DocFilePath::getFileName, docFilePath.getFileName());
        }
        queryWrapper.eq(DocFilePath::getIsDir,Constant.IS_DIR)
                .eq(DocFilePath::getDeleteFlag,Constant.DEL_FALSE)
                //.apply("{0} = {0}",UUID.randomUUID().toString())
                .orderByAsc(DocFilePath::getFilePath)
                .orderByAsc(DocFilePath::getFileName);
        return baseMapper.selectList(queryWrapper);
    }
 
 
 
    @Override
    public void deleteUserFile(String userFileId, String sessionUserId) {
        DocFilePath userFile = baseMapper.selectById(userFileId);
        String uuid = UUID.randomUUID().toString();
        if (userFile.getIsDir() == Constant.IS_DIR) { // 是目录,修改当前目录状态,后修改所有子文件删除状态
            LambdaUpdateWrapper<DocFilePath> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<DocFilePath>();
            userFileLambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
                    .set(DocFilePath::getDeleteBatchNum, uuid)
                    .set(DocFilePath::getDeleteTime, DateUtils.getDate())
                    .eq(DocFilePath::getPathId, userFileId);
            baseMapper.update(null, userFileLambdaUpdateWrapper);
 
            String filePath = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true).getPath();
            updateFileDeleteStateByFilePath(filePath, uuid, sessionUserId);
 
        } else { // 是文件
            DocFilePath userFileTemp = baseMapper.selectById(userFileId);
            LambdaUpdateWrapper<DocFilePath> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            userFileLambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(1, FileConstant.deleteFileRandomSize))
                    .set(DocFilePath::getDeleteTime, DateUtils.getDate())
                    .set(DocFilePath::getDeleteBatchNum, uuid)
                    .eq(DocFilePath::getPathId, userFileTemp.getPathId());
            baseMapper.update(null, userFileLambdaUpdateWrapper);
        }
 
        RecoveryFile recoveryFile = new RecoveryFile();
        recoveryFile.setPathId(userFileId);
        recoveryFile.setDeleteTime(DateUtils.getDate());
        recoveryFile.setDeleteBatchNum(uuid);
        recoveryFileMapper.insert(recoveryFile);
 
 
    }
    private void updateFileDeleteStateByFilePath(String filePath, String deleteBatchNum, String userId) {
//        executor.execute(() -> {
//            List<DocFilePath> fileList = selectUserFileByLikeRightFilePath(filePath);
//            for (int i = 0; i < fileList.size(); i++) {
//                DocFilePath userFileTemp = fileList.get(i);
//                //标记删除标志
//                LambdaUpdateWrapper<DocFilePath> userFileLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
//                userFileLambdaUpdateWrapper1.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
//                        .set(DocFilePath::getDeleteTime, DateUtils.getDate())
//                        .set(DocFilePath::getDeleteBatchNum, deleteBatchNum)
//                        .eq(DocFilePath::getPathId, userFileTemp.getPathId())
//                        .eq(DocFilePath::getDeleteFlag, 0);
//                baseMapper.update(null, userFileLambdaUpdateWrapper1);
//
//            }
//        });
        LambdaUpdateWrapper<DocFilePath> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize))
                .set(DocFilePath::getDeleteTime, DateUtils.getDate())
                .set(DocFilePath::getDeleteBatchNum, deleteBatchNum)
                .likeRight(DocFilePath::getFilePath,filePath)
                .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE);
        baseMapper.update(null, lambdaUpdateWrapper);
    }
 
    @Override
    public List<DocFilePath> selectUserFileByLikeRightFilePath(String filePath) {
        return baseMapper.selectUserFileByLikeRightFilePath(filePath);
    }
 
 
    @Override
    public List<DocFilePath> selectUserFileByNameAndPath(String fileName, String filePath, String userId) {
        LambdaQueryWrapper<DocFilePath> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFilePath::getFileName, fileName)
                .eq(DocFilePath::getFilePath, filePath)
                .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE);
        return baseMapper.selectList(lambdaQueryWrapper);
    }
 
 
 
    @Override
    public void userFileCopy(String userFileId, String newfilePath) {
        DocFilePath userFile = baseMapper.selectById(userFileId);
        String oldfilePath = userFile.getFilePath();
        String fileName = userFile.getFileName();
 
        userFile.setFilePath(newfilePath);
        userFile.setPathId(IdUtil.getSnowflakeNextIdStr());
        DocFilePath parent = docFileDealComp.getParent(newfilePath);
        userFile.setParentId(parent.getPathId());
        if (userFile.getIsDir() == Constant.IS_NOT_DIR) {
            String repeatFileName = docFileDealComp.getRepeatFileName(userFile, userFile.getFilePath());
            userFile.setFileName(repeatFileName);
        }
        baseMapper.insert(userFile);
 
        oldfilePath = new QiwenFile(oldfilePath, fileName, true).getPath();
        newfilePath = new QiwenFile(newfilePath, fileName, true).getPath();
 
 
        if (userFile.isDirectory()) { //为null说明是目录,则需要移动子目录
            List<DocFilePath> subUserFileList = baseMapper.selectUserFileByLikeRightFilePath(oldfilePath);
            Map<String, List<DocFilePath>> subMap = new HashMap<>();
            List<DocFilePath> childList = null;
            for (DocFilePath oldDfp: subUserFileList ) {
                childList = subMap.get(oldDfp.getParentId());
                if(childList == null) {
                    childList = new ArrayList<>();
                }
                childList.add(oldDfp);
                subMap.put(oldDfp.getParentId(), childList);
            }
 
 
            copyChildren(subMap, userFileId, userFile.getPathId(),newfilePath,oldfilePath);
 
 
//            for (DocFilePath newUserFile : subUserFileList) {
//                newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath, newfilePath));
//                newUserFile.setPathId(IdUtil.getSnowflakeNextIdStr());
//                if (!newUserFile.isFile()) {
//                    String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath());
//                    newUserFile.setFileName(repeatFileName);
//                }
//                baseMapper.insert(newUserFile);
//            }
        }
 
    }
 
    private void copyChildren(Map<String, List<DocFilePath>> subMap, String oldParentId, String newParentId, String newfilePath, String oldfilePath) {
        List<DocFilePath> filePaths = subMap.get(oldParentId);
        if (filePaths == null) return;
        for (DocFilePath newUserFile: filePaths) {
            String pathId = newUserFile.getPathId();
            newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath.replace("(","\\(").replace(")","\\)"), newfilePath));
            newUserFile.setPathId(IdUtil.getSnowflakeNextIdStr());
            newUserFile.setParentId(newParentId);
            if (!newUserFile.isFile()) {
                String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath());
                newUserFile.setFileName(repeatFileName);
            }
            baseMapper.insert(newUserFile);
            if (newUserFile.isDirectory()) {
                copyChildren(subMap, pathId, newUserFile.getPathId(),newfilePath,oldfilePath );
            }
 
        }
    }
 
 
    @Override
    public void updateFilepathByUserFileId(String userFileId, String newfilePath) {
        DocFilePath userFile = baseMapper.selectById(userFileId);
        String oldfilePath = userFile.getFilePath();
        String fileName = userFile.getFileName();
        DocFilePath parent = docFileDealComp.getParent(newfilePath);
        userFile.setParentId(parent.getPathId());
        userFile.setFilePath(newfilePath);
        if (userFile.getIsDir() == Constant.IS_NOT_DIR) {
            String repeatFileName = docFileDealComp.getRepeatFileName(userFile, userFile.getFilePath());
            userFile.setFileName(repeatFileName);
        }
        baseMapper.updateById(userFile);
 
        //移动子目录
        oldfilePath = new QiwenFile(oldfilePath, fileName, true).getPath();
        newfilePath = new QiwenFile(newfilePath, fileName, true).getPath();
 
        if (userFile.isDirectory()) { //为空说明是目录,则需要移动子目录
            List<DocFilePath> list = selectUserFileByLikeRightFilePath(oldfilePath);
 
            for (DocFilePath newUserFile : list) {
                newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath.replace("(","\\(").replace(")","\\)"), newfilePath));
                if (newUserFile.getIsDir() == Constant.IS_NOT_DIR) {
                    String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath());
                    newUserFile.setFileName(repeatFileName);
                }
                baseMapper.updateById(newUserFile);
            }
        }
 
    }
 
    @Override
    public boolean selectPermissionByPath(String pathId, Map<String, DocFilePath> manage) {
        if (pathId == null ) return false;
        DocFilePath docFilePath = baseMapper.selectById(pathId);
//
//        Map<String, Map<String, DocFilePath>> pathPermission = pathPermissionComp.getPathPermission();
//
//
//        Map<String, DocFilePath> manage = pathPermission.get(Constant.MANAGE);
 
        //Map<String, DocFilePath> managePrem = pathPermissionComp.getSubPremMap(manage);
 
        QiwenFile qf = null;
        DocFilePath dfp = null;
 
        if (manage.containsKey(pathId)) {
            return true;
        }
 
        for (String str : manage.keySet()
             ) {
            dfp = manage.get(str);
            if (dfp == null) continue;
            try {
                qf = new QiwenFile(dfp.getFilePath(),dfp.getFileName(), true);
            } catch (Exception e) {
                System.out.println();
                System.out.println(e.getMessage());
            }
 
            if (docFilePath != null && docFilePath.getFilePath().startsWith(qf.getPath())) {
                return true;
            }
        }
 
//
//        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        LambdaQueryWrapper<PathPermission> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(PathPermission::getPathId, pathId).eq(PathPermission::getUserId, loginUser.getId()).eq(PathPermission::getManage, Constant.HAVE);
//        List<PathPermission> list = pathPermissionService.list(wrapper);
//        if (list!=null && list.size()>0) {
//            return true;
//        }
        return false;
    }
 
 
    @Override
    public List<Map<String, Object>> querySubCount(String[] fileIds) {
        return baseMapper.querySubCount(fileIds);
    }
    @Override
    public List<DocFilePath> selectSameUserFile(String fileName, String filePath, String extendName) {
        LambdaQueryWrapper<DocFilePath> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFilePath::getFileName, fileName)
                .eq(DocFilePath::getFilePath, filePath)
                .eq(DocFilePath::getExtendName, extendName)
                .eq(DocFilePath::getDeleteFlag, "0");
        return baseMapper.selectList(lambdaQueryWrapper);
    }
 
    @Override
    @Cacheable(value = CacheConstant.DOC_FILE_PATH_CACHE,key = "#parentFilePath+':'+#fileName", unless = "#result == null || #result.size() < 1")
    public List<DocFilePath> getDocFilePathsByPathName(String parentFilePath, String fileName) {
        LambdaQueryWrapper<DocFilePath> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFilePath::getFilePath, parentFilePath)
                .eq(DocFilePath::getFileName, fileName)
                .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE)
                .eq(DocFilePath::getIsDir, Constant.IS_DIR);
        List<DocFilePath> userFileList = baseMapper.selectList(lambdaQueryWrapper);
        return userFileList;
    }
 
 
 
}