chenrui
2025-04-03 cf396d3076a064998fbac84f76861f5ed918c5e5
basic-server/src/main/java/com/ruoyi/basic/service/impl/WorkShopServiceImpl.java
@@ -1,6 +1,5 @@
package com.ruoyi.basic.service.impl;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -10,16 +9,18 @@
import com.ruoyi.basic.pojo.WorkShop;
import com.ruoyi.basic.pojo.WorkShopFile;
import com.ruoyi.basic.service.WorkShopService;
import com.ruoyi.common.config.MinioConfig;
import com.ruoyi.common.core.domain.MinioResult;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.MinioUtils;
import com.ruoyi.framework.exception.ErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
 * @description 针对表【work_shop(车间名称)】的数据库操作Service实现
@@ -33,11 +34,11 @@
    @Resource
    private WorkShopFileMapper workShopFileMapper;
    @Value("${wordUrl}")
    private String wordUrl;
    @Resource
    private MinioUtils minioUtils;
    @Value("${file.path}")
    private String imgUrl;
    @Autowired
    private MinioConfig minioConfig;
    @Override
@@ -58,58 +59,37 @@
    public int delWorkShop(Integer id) {
        return workShopMapper.deleteById(id);
    }
    @Override
    public int uploadFile(Integer id, MultipartFile file) {
        String username = SecurityUtils.getUsername();
        String urlString;
        String pathName;
        String path;
        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
    public int uploadFile(Integer id, MultipartFile file) throws Exception {
        // 基础校验
        if (file == null || file.isEmpty()) {
            throw new ErrorException("上传文件为空");
        }
        WorkShopFile workShopFile = new WorkShopFile();
        workShopFile.setWorkShopId(id);
        workShopFile.setFileName(filename);
        if (contentType != null && contentType.startsWith("image/")) {
            // 是图片
            path = imgUrl;
            workShopFile.setType(1);
        } else {
            // 是文件
            path = wordUrl;
            workShopFile.setType(2);
        }
        workShopFile.setFileName(file.getOriginalFilename());
        workShopFile.setName(SecurityUtils.getUsername());
        try {
            // 1. 解析绝对路径(优先使用配置的绝对路径,否则基于项目根目录)
            String basePath = imgUrl.startsWith(File.separator) ? imgUrl : System.getProperty("user.dir") + File.separator + imgUrl;
            File realpath = new File(basePath);
            // 2. 创建目录(记录日志,检查是否成功)
            if (!realpath.exists()) {
                if (!realpath.mkdirs()) {
                    System.err.println("目录创建失败:{}");
                    return 0;
                }
            String contentType = file.getContentType();
            String category = contentType != null && contentType.startsWith("image/") ? "images" : "docs";
            MinioResult upload = minioUtils.upload(minioConfig.getBucketName(), file, true);
            // 记录存储路径
            workShopFile.setFileUrl(upload.getBucketFileName());
            workShopFile.setFileMinioUrl(upload.getPreviewExpiry());
            workShopFile.setType(category.equals("images") ? 1 : 2);
            // 数据库操作
            int insertResult = workShopFileMapper.insert(workShopFile);
            if (insertResult <= 0) {
                throw new ErrorException("数据库插入失败");
            }
            // 3. 生成唯一文件名
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String suffix = filename.substring(filename.lastIndexOf("."));
            pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "_" + uuid + suffix;
            File targetFile = new File(realpath, pathName);
            // 4. 保存文件
            file.transferTo(targetFile);
            workShopFile.setFileUrl(pathName);
            workShopFile.setName(username);
            return workShopFileMapper.insert(workShopFile);
            return insertResult;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("附件上传错误");
            return 0;
            throw new Exception("系统异常: ", e);
        }
    }
}