chenrui
2025-04-08 afd4b25f5744725c92be40217aae0760289b4671
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
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.WorkShopDto;
import com.ruoyi.basic.mapper.WorkShopFileMapper;
import com.ruoyi.basic.mapper.WorkShopMapper;
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;
 
/**
 * @description 针对表【work_shop(车间名称)】的数据库操作Service实现
 * @createDate 2024-03-12 16:17:55
 */
@Service
public class WorkShopServiceImpl extends ServiceImpl<WorkShopMapper, WorkShop> implements WorkShopService {
 
    @Resource
    private WorkShopMapper workShopMapper;
    @Resource
    private WorkShopFileMapper workShopFileMapper;
 
    @Resource
    private MinioUtils minioUtils;
 
    @Autowired
    private MinioConfig minioConfig;
 
 
    @Override
    public IPage<WorkShopDto> selectWorkShop(Page page, WorkShopDto workShopDto) {
        return workShopMapper.selectPage(page, QueryWrappers.queryWrappers(workShopDto));
    }
 
    @Override
    public int addWorkShop(WorkShopDto workShopDto) {
        if (workShopDto.getId() == null) {
            return workShopMapper.insert(workShopDto);
        } else {
            return workShopMapper.updateById(workShopDto);
        }
    }
 
    @Override
    public int delWorkShop(Integer id) {
        return workShopMapper.deleteById(id);
    }
 
    @Override
    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(file.getOriginalFilename());
        workShopFile.setName(SecurityUtils.getUsername());
 
        try {
            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("数据库插入失败");
            }
            return insertResult;
        } catch (Exception e) {
            throw new Exception("系统异常: ", e);
        }
    }
}