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());
|
}
|
}
|