|
@@ -0,0 +1,176 @@
|
|
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.entity.FileRecord;
|
|
|
|
|
+import com.etotem.cfc.entity.FileTag;
|
|
|
|
|
+import com.etotem.cfc.entity.FileTagRel;
|
|
|
|
|
+import com.etotem.cfc.mapper.FileRecordMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FileTagMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FileTagRelMapper;
|
|
|
|
|
+import com.etotem.cfc.service.StorageService;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/api/admin/file-record")
|
|
|
|
|
+public class FileRecordController {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(FileRecordController.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FileRecordMapper fileRecordMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FileTagMapper fileTagMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FileTagRelMapper fileTagRelMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private StorageService storageService;
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/list")
|
|
|
|
|
+ public Result<Map<String, Object>> list(@RequestBody Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
|
|
+ if (!"admin".equals(role)) return Result.error("无权限");
|
|
|
|
|
+ int pageNo = Integer.parseInt(params.getOrDefault("page", "1").toString());
|
|
|
|
|
+ int pageSize = Integer.parseInt(params.getOrDefault("pageSize", "20").toString());
|
|
|
|
|
+ String category = params.get("category") != null ? params.get("category").toString() : null;
|
|
|
|
|
+ String fileType = params.get("fileType") != null ? params.get("fileType").toString() : null;
|
|
|
|
|
+ String search = params.get("search") != null ? params.get("search").toString() : null;
|
|
|
|
|
+ String dateFrom = params.get("dateFrom") != null ? params.get("dateFrom").toString() : null;
|
|
|
|
|
+ String dateTo = params.get("dateTo") != null ? params.get("dateTo").toString() : null;
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ List<Integer> tagIds = params.get("tagIds") != null ? (List<Integer>) params.get("tagIds") : null;
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<FileRecord> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ if (category != null && !category.isEmpty()) wrapper.eq(FileRecord::getCategory, category);
|
|
|
|
|
+ if (fileType != null && !fileType.isEmpty()) wrapper.eq(FileRecord::getFileType, fileType);
|
|
|
|
|
+ if (search != null && !search.isEmpty()) {
|
|
|
|
|
+ wrapper.and(w -> w.like(FileRecord::getOriginalFilename, search)
|
|
|
|
|
+ .or().like(FileRecord::getDescription, search));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dateFrom != null && !dateFrom.isEmpty()) wrapper.ge(FileRecord::getCreatedAt, dateFrom + " 00:00:00");
|
|
|
|
|
+ if (dateTo != null && !dateTo.isEmpty()) wrapper.le(FileRecord::getCreatedAt, dateTo + " 23:59:59");
|
|
|
|
|
+ wrapper.orderByDesc(FileRecord::getCreatedAt);
|
|
|
|
|
+
|
|
|
|
|
+ IPage<FileRecord> page = fileRecordMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
|
|
|
|
|
+
|
|
|
|
|
+ List<Long> fileIds = page.getRecords().stream().map(FileRecord::getId).collect(Collectors.toList());
|
|
|
|
|
+ Map<Long, List<FileTag>> fileTagsMap = new HashMap<>();
|
|
|
|
|
+ if (!fileIds.isEmpty()) {
|
|
|
|
|
+ List<FileTagRel> rels = fileTagRelMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<FileTagRel>().in(FileTagRel::getFileId, fileIds));
|
|
|
|
|
+ if (!rels.isEmpty()) {
|
|
|
|
|
+ Set<Long> tagIdSet = rels.stream().map(FileTagRel::getTagId).collect(Collectors.toSet());
|
|
|
|
|
+ List<FileTag> tags = fileTagMapper.selectBatchIds(tagIdSet);
|
|
|
|
|
+ Map<Long, FileTag> tagMap = tags.stream().collect(Collectors.toMap(FileTag::getId, t -> t));
|
|
|
|
|
+ for (FileTagRel rel : rels) {
|
|
|
|
|
+ fileTagsMap.computeIfAbsent(rel.getFileId(), k -> new ArrayList<>()).add(tagMap.get(rel.getTagId()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<FileRecord> filteredRecords = page.getRecords();
|
|
|
|
|
+ if (tagIds != null && !tagIds.isEmpty()) {
|
|
|
|
|
+ filteredRecords = filteredRecords.stream()
|
|
|
|
|
+ .filter(r -> {
|
|
|
|
|
+ List<FileTag> ft = fileTagsMap.get(r.getId());
|
|
|
|
|
+ return ft != null && ft.stream().anyMatch(t -> tagIds.contains(t.getId().intValue()));
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> records = new ArrayList<>();
|
|
|
|
|
+ for (FileRecord r : filteredRecords) {
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("id", r.getId());
|
|
|
|
|
+ item.put("hash", r.getHash());
|
|
|
|
|
+ item.put("url", r.getUrl());
|
|
|
|
|
+ item.put("fileSize", r.getFileSize());
|
|
|
|
|
+ item.put("contentType", r.getContentType());
|
|
|
|
|
+ item.put("originalFilename", r.getOriginalFilename());
|
|
|
|
|
+ item.put("category", r.getCategory());
|
|
|
|
|
+ item.put("fileType", r.getFileType());
|
|
|
|
|
+ item.put("description", r.getDescription());
|
|
|
|
|
+ item.put("createdAt", r.getCreatedAt());
|
|
|
|
|
+ item.put("updatedAt", r.getUpdatedAt());
|
|
|
|
|
+ item.put("tags", fileTagsMap.getOrDefault(r.getId(), Collections.emptyList()));
|
|
|
|
|
+ records.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
|
|
+ result.put("records", records);
|
|
|
|
|
+ result.put("total", page.getTotal());
|
|
|
|
|
+ result.put("page", pageNo);
|
|
|
|
|
+ result.put("pageSize", pageSize);
|
|
|
|
|
+ return Result.success(result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/update")
|
|
|
|
|
+ public Result<String> update(@RequestBody Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
|
|
+ if (!"admin".equals(role)) return Result.error("无权限");
|
|
|
|
|
+ Long id = Long.valueOf(params.get("id").toString());
|
|
|
|
|
+ FileRecord record = fileRecordMapper.selectById(id);
|
|
|
|
|
+ if (record == null) return Result.error("文件不存在");
|
|
|
|
|
+ if (params.get("description") != null) record.setDescription(params.get("description").toString());
|
|
|
|
|
+ fileRecordMapper.updateById(record);
|
|
|
|
|
+ if (params.get("tagIds") != null) {
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ List<Integer> tagIds = (List<Integer>) params.get("tagIds");
|
|
|
|
|
+ fileTagRelMapper.delete(new LambdaQueryWrapper<FileTagRel>().eq(FileTagRel::getFileId, id));
|
|
|
|
|
+ for (Integer tagId : tagIds) {
|
|
|
|
|
+ FileTagRel rel = new FileTagRel();
|
|
|
|
|
+ rel.setFileId(id);
|
|
|
|
|
+ rel.setTagId(tagId.longValue());
|
|
|
|
|
+ fileTagRelMapper.insert(rel);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.success("更新成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/delete")
|
|
|
|
|
+ public Result<String> deleteFile(@RequestBody Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
|
|
+ if (!"admin".equals(role)) return Result.error("无权限");
|
|
|
|
|
+ Long id = Long.valueOf(params.get("id").toString());
|
|
|
|
|
+ FileRecord record = fileRecordMapper.selectById(id);
|
|
|
|
|
+ if (record == null) return Result.error("文件不存在");
|
|
|
|
|
+ try { storageService.deleteFile(record.getUrl()); }
|
|
|
|
|
+ catch (Exception e) { log.warn("OSS删除失败: {}", record.getUrl(), e); }
|
|
|
|
|
+ fileTagRelMapper.delete(new LambdaQueryWrapper<FileTagRel>().eq(FileTagRel::getFileId, id));
|
|
|
|
|
+ fileRecordMapper.deleteById(id);
|
|
|
|
|
+ return Result.success("删除成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/tags")
|
|
|
|
|
+ public Result<List<FileTag>> getTags(@RequestAttribute("role") String role) {
|
|
|
|
|
+ if (!"admin".equals(role)) return Result.error("无权限");
|
|
|
|
|
+ return Result.success(fileTagMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<FileTag>().orderByAsc(FileTag::getSortOrder)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/stats")
|
|
|
|
|
+ public Result<Map<String, Object>> stats(@RequestAttribute("role") String role) {
|
|
|
|
|
+ if (!"admin".equals(role)) return Result.error("无权限");
|
|
|
|
|
+ Map<String, Object> stats = new LinkedHashMap<>();
|
|
|
|
|
+ stats.put("byCategory", fileRecordMapper.selectMaps(
|
|
|
|
|
+ new QueryWrapper<FileRecord>().select("category, COUNT(*) AS cnt").groupBy("category")));
|
|
|
|
|
+ stats.put("byFileType", fileRecordMapper.selectMaps(
|
|
|
|
|
+ new QueryWrapper<FileRecord>().select("file_type, COUNT(*) AS cnt").groupBy("file_type")));
|
|
|
|
|
+ stats.put("total", fileRecordMapper.selectCount(null));
|
|
|
|
|
+ List<Map<String, Object>> ts = fileRecordMapper.selectMaps(
|
|
|
|
|
+ new QueryWrapper<FileRecord>().select("SUM(file_size) AS total_bytes"));
|
|
|
|
|
+ stats.put("totalBytes", ts.get(0).get("total_bytes"));
|
|
|
|
|
+ return Result.success(stats);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|