liding
2025-04-09 3a77c500a010d12e913ff2b9e4cf27b1115c8224
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
94
95
96
97
98
99
100
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.mapper.*;
import com.ruoyi.basic.pojo.*;
import com.ruoyi.basic.service.WorkShopFileService;
import com.ruoyi.common.config.MinioConfig;
import com.ruoyi.common.utils.file.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * @description 针对表【work_shop_file(车间附件名称)】的数据库操作Service实现
 * @createDate 2024-03-12 16:17:55
 */
@Service
public class WorkShopFileServiceImpl extends ServiceImpl<WorkShopFileMapper, WorkShopFile> implements WorkShopFileService {
 
    @Resource
    private WorkShopFileMapper workShopFileMapper;
 
    @Resource
    private ProductPartMapper productPartMapper;
 
    @Resource
    private ProductMapper productMapper;
    @Resource
    private StructureTestObjectPartMapper structureTestObjectPartMapper;
 
    @Resource
    private StructureTestObjectMapper structureTestObjectMapper;
 
    @Resource
    private MinioUtils minioUtils;
 
    @Autowired
    private MinioConfig minioConfig;
 
    @Override
    public int delFile(Integer id) {
        // 若 id 为空,直接返回 0,表示未删除任何记录
        if (id == null) {
            return 0;
        }
        try {
            // 查询要删除的文件信息
            WorkShopFile file = workShopFileMapper.selectById(id);
            if (file != null && file.getFileUrl() != null) {
                // 检查 MinIO 中文件是否存在
                if (minioUtils.objectExists(minioConfig.getBucketName(), file.getFileUrl())) {
                    // 先删除 MinIO 中的对象
                    minioUtils.removeObjectsResult(minioConfig.getBucketName(), file.getFileUrl());
                }
            }
            // 执行数据库删除操作
            int deleteResult = workShopFileMapper.deleteById(id);
            return deleteResult;
        } catch (Exception e) {
            // 处理异常,可根据实际情况记录日志或抛出自定义异常
            e.printStackTrace();
            return 0;
        }
    }
 
    @Override
    public IPage<WorkShopFile> partFileList(Page page, String partNo) {
        StructureTestObject structureTestObject = findStructureTestObjectByPartNo(partNo);
        if (structureTestObject != null && structureTestObject.getWorkShopId() != null) {
            return workShopFileMapper.selectPage(page, new LambdaQueryWrapper<WorkShopFile>().eq(WorkShopFile::getWorkShopId, structureTestObject.getWorkShopId()));
        }
        return null;
    }
 
    @Override
    public List<WorkShopFile> fileListById(Integer workShopId) {
        return workShopFileMapper.selectList(new LambdaQueryWrapper<WorkShopFile>().eq(WorkShopFile::getWorkShopId,workShopId));
    }
 
    private StructureTestObject findStructureTestObjectByPartNo(String partNo) {
        ProductPart productPart = productPartMapper.selectOne(new LambdaQueryWrapper<ProductPart>().eq(ProductPart::getPartNo, partNo));
        if (productPart != null) {
            Product product = productMapper.selectById(productPart.getProductId());
            if (product != null) {
                return structureTestObjectMapper.selectById(product.getObjectId());
            }
        }
        StructureTestObjectPart structureTestObjectPart = structureTestObjectPartMapper.selectOne(new LambdaQueryWrapper<StructureTestObjectPart>().eq(StructureTestObjectPart::getPartNo, partNo));
        if (structureTestObjectPart != null) {
            return structureTestObjectMapper.selectById(structureTestObjectPart.getTestObjectId());
        }
        return null;
    }
}