| | |
| | | package com.ruoyi.sales.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.sales.mapper.CommonFileMapper; |
| | | import com.ruoyi.sales.pojo.CommonFile; |
| | | import com.ruoyi.sales.service.ICommonFileService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.Arrays; |
| | | import java.util.UUID; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class CommonFileServiceImpl extends ServiceImpl<CommonFileMapper, CommonFile> implements ICommonFileService { |
| | | |
| | | private final CommonFileMapper commonFileMapper; |
| | | |
| | | @Value("${file.upload-dir}") |
| | | private String uploadDir; |
| | | private final TempFileMapper tempFileMapper; |
| | | |
| | | public List<CommonFile> getFileListByBusinessId(Long businessId,Integer type) { |
| | | return commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, businessId) |
| | | .eq(CommonFile::getType, type)); |
| | | } |
| | | |
| | | public void deleteByBusinessId(Long businessId,Integer type) { |
| | | commonFileMapper.delete(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, businessId) |
| | | .eq(CommonFile::getType, type)); |
| | | } |
| | | |
| | | public void deleteByBusinessIds(List<Long> businessId,Integer type) { |
| | | commonFileMapper.delete(new LambdaQueryWrapper<CommonFile>().in(CommonFile::getCommonId, businessId) |
| | | .eq(CommonFile::getType, type)); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int deleteSalesLedgerByIds(Long[] ids) { |
| | | for (Long id : ids) { |
| | | if (id == null) { |
| | | return 1; |
| | | } |
| | | } |
| | | return commonFileMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | |
| | | @Override |
| | | public CommonFile uploadFile(MultipartFile file, Long id, String type) throws IOException { |
| | | // 1. 生成正式文件ID和路径 |
| | | String tempId = UUID.randomUUID().toString(); |
| | | Path tempFilePath = Paths.get(uploadDir, tempId + "_" + file.getOriginalFilename()); |
| | | |
| | | // 2. 确保目录存在 |
| | | Path parentDir = tempFilePath.getParent(); |
| | | if (parentDir != null) { |
| | | Files.createDirectories(parentDir); // 递归创建目录 |
| | | } |
| | | |
| | | // 3. 保存文件到目录 |
| | | file.transferTo(tempFilePath.toFile()); |
| | | |
| | | // 4. 保存文件记录 |
| | | CommonFile commonFile = new CommonFile(); |
| | | commonFile.setCommonId(id); |
| | | commonFile.setName(file.getOriginalFilename()); |
| | | commonFile.setUrl(tempFilePath.toString()); |
| | | commonFile.setType(type); |
| | | commonFileMapper.insert(commonFile); |
| | | return commonFile; |
| | | } |
| | | |
| | | @Override |
| | | public int delCommonFileByIds(Long[] ids) { |