| | |
| | | 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.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.QueryWrappers; |
| | | import com.ruoyi.require.dto.SupplierManagementDto; |
| | | import com.ruoyi.require.mapper.SupplierManagementAttachmentMapper; |
| | | import com.ruoyi.require.mapper.SupplierManagementMapper; |
| | | import com.ruoyi.require.pojo.SupplierManagement; |
| | | import com.ruoyi.require.pojo.SupplierManagementAttachment; |
| | | import com.ruoyi.require.service.SupplierManagementService; |
| | | 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 jakarta.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | |
| | | @Autowired |
| | | private SupplierManagementMapper supplierManagementMapper; |
| | | |
| | | @Autowired |
| | | private SupplierManagementAttachmentMapper supplierManagementAttachmentMapper; |
| | | |
| | | @Value("${file.path}") |
| | | private String filePath; |
| | | |
| | | @Value("${wordUrl}") |
| | | private String wordUrl; |
| | | |
| | | public SupplierManagementServiceImpl(SupplierManagementMapper supplierManagementMapper) { |
| | | this.supplierManagementMapper = supplierManagementMapper; |
| | |
| | | public List<SupplierManagement> selectSupplierManagementAll() { |
| | | return supplierManagementMapper.selectList(null); |
| | | } |
| | | |
| | | @Override |
| | | public List<SupplierManagementAttachment> selectAttachments(Integer supplierManagementId) { |
| | | return supplierManagementAttachmentMapper.selectList(new QueryWrapper<SupplierManagementAttachment>().lambda() |
| | | .eq(SupplierManagementAttachment::getSupplierManagementId, supplierManagementId) |
| | | .orderByAsc(SupplierManagementAttachment::getSort, SupplierManagementAttachment::getId)); |
| | | } |
| | | |
| | | @Override |
| | | public SupplierManagementAttachment uploadAttachment(Integer supplierManagementId, MultipartFile file) { |
| | | if (supplierManagementMapper.selectById(supplierManagementId) == null) { |
| | | throw new BaseException("供应商不存在"); |
| | | } |
| | | if (file == null || file.isEmpty()) { |
| | | throw new BaseException("附件不能为空"); |
| | | } |
| | | // 系统全局上传上限为200MB,这里提前给出友好提示 |
| | | if (file.getSize() > 200L * 1024 * 1024) { |
| | | throw new BaseException("附件不能超过200MB"); |
| | | } |
| | | String originalName = file.getOriginalFilename(); |
| | | if (originalName == null || originalName.trim().isEmpty()) { |
| | | originalName = "未命名文件"; |
| | | } |
| | | // 个别浏览器可能带上路径前缀,只保留文件名本身 |
| | | originalName = originalName.replaceAll(".*[/\\\\]", ""); |
| | | try { |
| | | // 图片放图片目录(/img 静态路径),其余任意格式放 word 目录(/word 静态路径) |
| | | boolean image = originalName.matches("(?i).+\\.(jpg|jpeg|png|gif)$"); |
| | | File directory = new File(image ? filePath : wordUrl); |
| | | if (!directory.exists()) { |
| | | directory.mkdirs(); |
| | | } |
| | | String fileUrl = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS")) + "-" + originalName; |
| | | file.transferTo(new File(directory, fileUrl)); |
| | | SupplierManagementAttachment attachment = new SupplierManagementAttachment(); |
| | | attachment.setSupplierManagementId(supplierManagementId); |
| | | attachment.setFileName(originalName); |
| | | attachment.setFileUrl(fileUrl); |
| | | attachment.setSort(selectAttachments(supplierManagementId).size()); |
| | | supplierManagementAttachmentMapper.insert(attachment); |
| | | return attachment; |
| | | } catch (IOException e) { |
| | | throw new BaseException("附件上传失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean deleteAttachment(Long id) { |
| | | return supplierManagementAttachmentMapper.deleteById(id) > 0; |
| | | } |
| | | } |