浏览代码

完善查询条件

zhaojinyu 2 周之前
父节点
当前提交
997c0d3304

+ 5 - 5
base-modules/service-file/src/main/java/com/ruoyi/file/controller/FilesController.java

@@ -3,9 +3,8 @@ package com.ruoyi.file.controller;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.file.service.FileUploadResponse;
-import com.ruoyi.file.service.Files;
+import com.ruoyi.file.service.FilesUpload;
 import com.ruoyi.file.service.FilesService;
-import com.usky.common.core.bean.ApiResult;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -13,6 +12,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletResponse;
 import java.time.LocalDateTime;
 
+
 @RestController
 //@RequestMapping("/files")
 public class FilesController {
@@ -45,7 +45,7 @@ public class FilesController {
     }
 
     @GetMapping("/query")
-    public R<Page<Files>> queryFiles(
+    public R<Page<FilesUpload>> queryFiles(
             @RequestParam(required = false) String filesName,
             @RequestParam(required = false) LocalDateTime startTime,
             @RequestParam(required = false) LocalDateTime endTime,
@@ -54,8 +54,8 @@ public class FilesController {
             @RequestParam int current,
             @RequestParam int size) {
 
-        Page<Files> page = new Page<>(current, size);
-        Page<Files> resultPage = filesService.queryFiles(filesName, startTime, endTime, isDeleted, fileType, page);
+        Page<FilesUpload> page = new Page<>(current, size);
+        Page<FilesUpload> resultPage = filesService.queryFiles(filesName, startTime, endTime, isDeleted, fileType, page);
         return R.ok(resultPage);
     }
 }

+ 2 - 2
base-modules/service-file/src/main/java/com/ruoyi/file/mapper/FilesMapper.java

@@ -1,10 +1,10 @@
 package com.ruoyi.file.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.ruoyi.file.service.Files;
+import com.ruoyi.file.service.FilesUpload;
 import org.apache.ibatis.annotations.Mapper;
 
 @Mapper
-public interface FilesMapper extends BaseMapper<Files> {
+public interface FilesMapper extends BaseMapper<FilesUpload> {
 
 }

+ 3 - 3
base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesService.java

@@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletResponse;
 import java.time.LocalDateTime;
 
-public interface FilesService extends IService<Files> {
+public interface FilesService extends IService<FilesUpload> {
     // 修改返回类型为 FileUploadResponse
     FileUploadResponse upload(MultipartFile file);
 
@@ -17,7 +17,7 @@ public interface FilesService extends IService<Files> {
     // 删除文件
     void deleteFile(String filesUUID);
 
-    // 新增查询方法
-    Page<Files> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<Files> page);
+    // 查询方法
+    Page<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<FilesUpload> page);
 
 }

+ 28 - 6
base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesServiceImpl.java

@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.file.mapper.FileSequenceMapper;
 import com.ruoyi.file.mapper.FilesMapper;
+import com.usky.common.security.utils.SecurityUtils;
+import com.usky.system.model.LoginUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -18,7 +20,7 @@ import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 
 @Service
-public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {
+public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> implements FilesService {
     @Value("${file.path}")
     private String filesUploadPath;//获取文件路径
 
@@ -35,6 +37,11 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
     @Override
     public FileUploadResponse upload(MultipartFile file) {
 
+        // 获取当前登录用户昵称(如果可用)
+//        String userName = getUserNameFromSecurityContext();
+        String userName = SecurityUtils.getUsername();
+        System.out.println("当前登录用户昵称:" + userName);
+
         // 文件夹路径名称
         String originalFilename = file.getOriginalFilename();
 
@@ -74,7 +81,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
             String url = filesUploadDomain + filesPrefix + "/" + yearMonth + "/" + fileUuid;
 
             // 将文件存储到数据库
-            Files saveFile = new Files();
+            FilesUpload saveFile = new FilesUpload();
             saveFile.setFilesName(originalFilename);
             saveFile.setName(fileUuid);
             saveFile.setPath(destPath);
@@ -83,7 +90,9 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
             saveFile.setUrl(url);
             saveFile.setEnable(true);
             saveFile.setIsDelete(0);
+            saveFile.setCreateBy(userName);
             saveFile.setCreateTime(LocalDateTime.now());
+            saveFile.setUpdateBy(null);
             saveFile.setUpdateTime(null);
             // 保存操作
             save(saveFile);
@@ -164,7 +173,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
         try {
 
             // 根据文件名查询文件信息
-            Files file = getOne(new QueryWrapper<Files>().eq("name", filesUUID));
+            FilesUpload file = getOne(new QueryWrapper<FilesUpload>().eq("name", filesUUID));
 
             if (file == null) {
                 throw new RuntimeException("文件不存在,无法删除");
@@ -180,7 +189,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
             }
 
             // 更新数据库记录
-            Files updateFile = new Files();
+            FilesUpload updateFile = new FilesUpload();
             updateFile.setId(file.getId());
             updateFile.setFilesName(file.getFilesName());
             updateFile.setName(file.getName());
@@ -190,7 +199,9 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
             updateFile.setUrl(file.getUrl());
             updateFile.setEnable(false);
             updateFile.setIsDelete(1); // 设置为已删除
+            updateFile.setCreateBy(file.getCreateBy());
             updateFile.setCreateTime(file.getCreateTime()); // 保留原始的 createTime
+            updateFile.setUpdateBy(file.getUpdateBy());
             updateFile.setUpdateTime(LocalDateTime.now());
 
             // 提交更新到数据库
@@ -201,8 +212,8 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
     }
 
     @Override
-    public Page<Files> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<Files> page) {
-        QueryWrapper<Files> queryWrapper = new QueryWrapper<>();
+    public Page<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<FilesUpload> page) {
+        QueryWrapper<FilesUpload> queryWrapper = new QueryWrapper<>();
 
         // 默认查询未删除的文件
         if (isDeleted == null) {
@@ -230,4 +241,15 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
 
         return page(page, queryWrapper);
     }
+
+    private String getUserNameFromSecurityContext() {
+        try {
+            return SecurityUtils.getUsername();
+        } catch (Exception e) {
+            // 如果无法获取用户信息,记录日志并返回默认值或抛出自定义异常
+            log.error("无法获取用户信息");
+            System.out.println("无法获取用户信息");
+            return "未知用户";
+        }
+    }
 }

+ 6 - 2
base-modules/service-file/src/main/java/com/ruoyi/file/service/Files.java → base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesUpload.java

@@ -10,8 +10,8 @@ import java.io.Serializable;
 import java.time.LocalDateTime;
 
 @Data
-@TableName("files")
-public class Files implements Serializable {
+@TableName("file_upload")
+public class FilesUpload implements Serializable {
     private Integer id;//编号
 
     private String filesName;//文件真实名称
@@ -28,10 +28,14 @@ public class Files implements Serializable {
 
     private Boolean enable;//链接是否可用(1:是 0:否)
 
+    private String createBy;//创建者
+
     @TableField(fill = FieldFill.INSERT)
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private LocalDateTime createTime;//创建时间
 
+    private String updateBy;//更新者
+
     @TableField(fill = FieldFill.INSERT_UPDATE)
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private LocalDateTime updateTime;//更新时间