|
@@ -0,0 +1,151 @@
|
|
|
+package com.fjhx.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.base.BaseEntity;
|
|
|
+import com.fjhx.entity.FileInfo;
|
|
|
+import com.fjhx.mapper.FileInfoMapper;
|
|
|
+import com.fjhx.service.FileInfoService;
|
|
|
+import org.springblade.core.launch.BladeApplication;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.tool.utils.DateUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 文件 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author ${author}
|
|
|
+ * @since 2022-07-07
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
|
|
|
+
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ public String applicationName;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, String> uploadFile(MultipartFile file) {
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String suffix = FileUtil.getSuffix(fileName);
|
|
|
+
|
|
|
+ String path = getFilePath();
|
|
|
+ String filePath = path + UUID.randomUUID() + "." + suffix;
|
|
|
+
|
|
|
+ FileUtil.mkdir(path);
|
|
|
+ FileOutputStream fileOutputStream = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ fileOutputStream = new FileOutputStream(filePath);
|
|
|
+ int index;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+
|
|
|
+ while ((index = inputStream.read(bytes)) != -1) {
|
|
|
+ fileOutputStream.write(bytes, 0, index);
|
|
|
+ fileOutputStream.flush();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("上传文件失败");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (fileOutputStream != null) {
|
|
|
+ fileOutputStream.close();
|
|
|
+ }
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setFileName(fileName);
|
|
|
+ fileInfo.setFileSuffix(suffix);
|
|
|
+ fileInfo.setContentType(file.getContentType());
|
|
|
+ fileInfo.setFileSize(file.getSize());
|
|
|
+ fileInfo.setFilePath(filePath);
|
|
|
+ save(fileInfo);
|
|
|
+
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+
|
|
|
+ map.put("fileName", fileName);
|
|
|
+ map.put("filePath", filePath);
|
|
|
+ map.put("id", fileInfo.getId().toString());
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList) {
|
|
|
+ if (fileInfoList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileInfoList.forEach(item -> {
|
|
|
+ item.setBusinessId(businessId);
|
|
|
+ item.setBusinessType(businessType);
|
|
|
+ });
|
|
|
+ updateBatchById(fileInfoList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList) {
|
|
|
+
|
|
|
+ List<Long> excludeIdList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<FileInfo> collect = fileInfoList.stream().filter(fileInfo -> {
|
|
|
+ if (ObjectUtil.isNotEmpty(fileInfo.getBusinessId())) {
|
|
|
+ excludeIdList.add(fileInfo.getId());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ fileInfo.setId(businessId);
|
|
|
+ fileInfo.setBusinessType(businessType);
|
|
|
+ return true;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ remove(Wrappers.<FileInfo>lambdaQuery()
|
|
|
+ .eq(FileInfo::getBusinessId, businessId)
|
|
|
+ .eq(FileInfo::getBusinessType, businessType)
|
|
|
+ .notIn(excludeIdList.size() > 0, BaseEntity::getId, collect));
|
|
|
+
|
|
|
+ if (collect.size() > 0) {
|
|
|
+ saveBatch(collect);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFile(Long businessId) {
|
|
|
+ remove(FileInfo::getBusinessId, businessId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFilePath() {
|
|
|
+ StringJoiner joiner = new StringJoiner("/");
|
|
|
+ boolean localDev = BladeApplication.isLocalDev();
|
|
|
+ if (localDev) {
|
|
|
+ joiner.add("D:/hx");
|
|
|
+ } else {
|
|
|
+ joiner.add("/mnt/file");
|
|
|
+ }
|
|
|
+ joiner.add(applicationName).add(DateUtil.format(new Date(), "yyyy-MM/dd/"));
|
|
|
+
|
|
|
+ return joiner.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|