gongchunyi
7 小时以前 84746c6a1fe8fb6c0216495110895cb5d346563e
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.ruoyi.aftersalesservice.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.aftersalesservice.mapper.AfterSalesServiceFileMapper;
import com.ruoyi.aftersalesservice.pojo.AfterSalesService;
import com.ruoyi.aftersalesservice.pojo.AfterSalesServiceFile;
import com.ruoyi.aftersalesservice.service.AfterSalesServiceFileService;
import com.ruoyi.aftersalesservice.service.AfterSalesServiceService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.io.FilenameUtils;
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 java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
 
/**
 * <br>
 * 售后服务附件表接口实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/02 11:19
 */
@Service
public class AfterSalesServiceFileServiceImpl extends ServiceImpl<AfterSalesServiceFileMapper, AfterSalesServiceFile> implements AfterSalesServiceFileService {
 
    @Autowired
    private AfterSalesServiceService afterSalesServiceService;
 
    @Value("${file.upload-dir}")
    private String uploadDir;
 
    @Override
    public void fileUpload(MultipartFile file, Long afterSalesServiceId) {
        if (file == null || file.isEmpty()) {
            throw new ServiceException("上传文件不能为空");
        }
        if (afterSalesServiceId == null) {
            throw new ServiceException("售后服务ID不能为空");
        }
        AfterSalesService afterSalesService = afterSalesServiceService.getById(afterSalesServiceId);
        if (afterSalesService == null) {
            throw new ServiceException("售后服务记录不存在");
        }
        Long currentTenantId = SecurityUtils.getLoginUser().getTenantId();
        if (!currentTenantId.equals(afterSalesService.getTenantId())) {
            throw new ServiceException("无权操作该售后服务记录");
        }
 
        try {
            String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
            Path formalDirPath = Paths.get(formalDir);
            //  正式目录存在
            if (!Files.exists(formalDirPath)) {
                Files.createDirectories(formalDirPath);
            }
            //  正式文件名(包含业务ID + 时间戳 + UUID)
            String originalFilename = file.getOriginalFilename();
            String fileExtension = FilenameUtils.getExtension(originalFilename);
            String formalFilename = afterSalesServiceId + "_" +
                    System.currentTimeMillis() + "_" +
                    UUID.randomUUID().toString().substring(0, 8) +
                    (StringUtils.hasText(fileExtension) ? "." + fileExtension : "");
 
            Path formalFilePath = formalDirPath.resolve(formalFilename);
            file.transferTo(formalFilePath.toFile());
 
            AfterSalesServiceFile serviceFile = new AfterSalesServiceFile();
            serviceFile.setServiceId(afterSalesServiceId);
            serviceFile.setFileName(originalFilename);
            serviceFile.setFileUrl(formalFilePath.toString());
            serviceFile.setFileSize(file.getSize());
            serviceFile.setFileSuffix(fileExtension);
            serviceFile.setDelFlag("0");
            serviceFile.setCreateUser(SecurityUtils.getUserId());
            serviceFile.setCreateTime(LocalDateTime.now());
            serviceFile.setTenantId(currentTenantId);
            save(serviceFile);
        } catch (Exception e) {
            throw new ServiceException("文件上传失败:" + e.getMessage());
        }
    }
 
    @Override
    public IPage<AfterSalesServiceFile> fileList(Page<AfterSalesServiceFile> page, Long afterSalesServiceId) {
        if (afterSalesServiceId == null) {
            throw new ServiceException("售后服务ID不能为空");
        }
 
        LambdaQueryWrapper<AfterSalesServiceFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(
                AfterSalesServiceFile::getId,
                AfterSalesServiceFile::getFileName,
                AfterSalesServiceFile::getFileUrl
        );
        wrapper.eq(AfterSalesServiceFile::getServiceId, afterSalesServiceId)
                .eq(AfterSalesServiceFile::getDelFlag, "0")
                .orderByDesc(AfterSalesServiceFile::getCreateTime);
 
        return page(page, wrapper);
    }
 
    @Override
    public void delFile(Long fileId) {
        if (fileId == null) {
            throw new ServiceException("附件ID不能为空");
        }
 
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
 
        AfterSalesServiceFile file = getById(fileId);
        if (file == null) {
            throw new ServiceException("附件不存在");
        }
        if (!tenantId.equals(file.getTenantId())) {
            throw new ServiceException("无权限删除");
        }
        removeById(fileId);
 
        FileUtils.deleteFile(file.getFileUrl());
    }
}