浏览代码

去掉递增值表相关代码

zhaojinyu 2 周之前
父节点
当前提交
6788389d5d

+ 0 - 15
base-modules/service-file/src/main/java/com/ruoyi/file/mapper/FileSequenceMapper.java

@@ -1,15 +0,0 @@
-package com.ruoyi.file.mapper;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.ruoyi.file.service.FileSequence;
-import org.apache.ibatis.annotations.Mapper;
-import org.apache.ibatis.annotations.Param;
-
-@Mapper
-public interface FileSequenceMapper extends BaseMapper<FileSequence> {
-
-    /**
-     * 根据年份和月份获取每月递增值
-     */
-    FileSequence getByYearAndMonth(@Param("year") Integer year, @Param("month") Integer month);
-}

+ 0 - 16
base-modules/service-file/src/main/java/com/ruoyi/file/service/FileSequence.java

@@ -1,16 +0,0 @@
-package com.ruoyi.file.service;
-
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import lombok.Data;
-
-@Data
-@TableName("file_sequence")
-public class FileSequence {
-    @TableId(type = IdType.AUTO)
-    private Integer id;
-    private Integer year;
-    private Integer month;
-    private Integer value;
-}

+ 25 - 19
base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesServiceImpl.java

@@ -3,7 +3,6 @@ package com.ruoyi.file.service;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 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;
@@ -30,8 +29,6 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
     @Value("${file.prefix}")
     private String filesPrefix;//文件前缀
 
-    @Autowired
-    private FileSequenceMapper fileSequenceMapper;
 
 
     @Override
@@ -49,13 +46,14 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
         // 获取当前日期时间
         LocalDateTime now = LocalDateTime.now();
         String timestamp = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
-        int monthIncrement = getMonthIncrement(now.getYear(), now.getMonthValue()); // 获取每月递增值
 
         //文件类型
         String type = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
 
         // 按照年月创建文件夹
         String yearMonth = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
+        // 获取每月递增值
+        int monthIncrement = getMonthIncrement(yearMonth);
         // 新文件名格式:时间戳+每月递增值+文件类型
         String fileUuid = timestamp + "A" + String.format("%04d", monthIncrement) + "." + type;
 
@@ -103,22 +101,30 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
         }
     }
 
-    private int getMonthIncrement(int year, int month) {
-        // 查询当前月份的递增值
-        FileSequence increment = fileSequenceMapper.getByYearAndMonth(year, month);
-        if (increment == null) {
-            // 如果没有记录,则初始化为1
-            increment = new FileSequence();
-            increment.setYear(year);
-            increment.setMonth(month);
-            increment.setValue(1);
-            fileSequenceMapper.insert(increment);
-        } else {
-            // 更新递增值
-            increment.setValue(increment.getValue() + 1);
-            fileSequenceMapper.updateById(increment);
+    /**
+     * 获取每月递增值
+     * @param yearMonth 年月(格式:yyyyMM)
+     * @return 递增值
+     */
+    private int getMonthIncrement(String yearMonth) {
+        // 目标文件夹路径
+        String destPath = filesUploadPath + "/" + yearMonth;
+        File folder = new File(destPath);
+
+        // 如果文件夹不存在,则递增值从1开始
+        if (!folder.exists()) {
+            folder.mkdirs();
+            return 1;
         }
-        return increment.getValue();
+
+        // 获取文件夹中已有的文件数量
+        File[] files = folder.listFiles();
+        if (files == null || files.length == 0) {
+            return 1;
+        }
+
+        // 递增值为文件夹中现有文件数量 + 1
+        return files.length + 1;
     }
 
     //将文件以流的形式一次性读取到内存,通过响应输出流输出到前端

+ 0 - 17
base-modules/service-file/src/main/resources/mapper/file/FileSequenceMapper.xml

@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="com.ruoyi.file.mapper.FileSequenceMapper">
-
-    <resultMap id="BaseResultMap" type="com.ruoyi.file.service.FileSequence">
-        <id column="id" property="id" jdbcType="INTEGER"/>
-        <result column="year" property="year" jdbcType="INTEGER"/>
-        <result column="month" property="month" jdbcType="INTEGER"/>
-        <result column="value" property="value" jdbcType="INTEGER"/>
-    </resultMap>
-
-    <!-- 添加 getByYearAndMonth 方法对应的 SQL -->
-    <select id="getByYearAndMonth" resultType="com.ruoyi.file.service.FileSequence">
-        SELECT * FROM file_sequence WHERE year = #{year} AND month = #{month}
-    </select>
-
-</mapper>