gongchunyi
9 小时以前 5d314130e6f21bd265388b5210808baeba5f2d0c
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
package com.ruoyi.production.service.impl;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.production.pojo.ProductionProductRouteItemFile;
import com.ruoyi.production.mapper.ProductionProductRouteItemFileMapper;
import com.ruoyi.production.service.IProductionProductRouteItemFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.nio.file.Files;
 
/**
 * <p>
 * 生产报工记录的工序附件表 服务实现类
 * </p>
 *
 * @author deslrey
 * @since 2026-03-23
 */
@Slf4j
@Service
public class ProductionProductRouteItemFileServiceImpl extends ServiceImpl<ProductionProductRouteItemFileMapper, ProductionProductRouteItemFile> implements IProductionProductRouteItemFileService {
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFile(Long fileId) {
        if (fileId == null) {
            throw new ServiceException("附件删除失败,数据不能为空");
        }
        ProductionProductRouteItemFile productionProductRouteItemFile = baseMapper.selectById(fileId);
        if (productionProductRouteItemFile == null) {
            throw new ServiceException("附件删除失败,附件不存在");
        }
 
        String fileUrl = productionProductRouteItemFile.getFileUrl();
        if (fileUrl != null) {
            try {
                java.nio.file.Path path = java.nio.file.Paths.get(fileUrl);
 
                if (Files.exists(path)) {
                    Files.delete(path);
                } else {
                    log.warn("文件不存在,无需删除: {}", fileUrl);
                }
 
            } catch (Exception e) {
                log.error("删除文件失败: {}", fileUrl, e);
                throw new ServiceException("附件删除失败,物理文件删除异常");
            }
        }
        int result = baseMapper.deleteById(fileId);
        if (result == 0) {
            throw new ServiceException("附件删除失败,数据库删除失败");
        }
    }
}