|
@@ -0,0 +1,208 @@
|
|
|
+package com.fjhx.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+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.params.FileInfoParam;
|
|
|
+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.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 {
|
|
|
+
|
|
|
+ @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 bindingFile(String applicationName, Long businessId, Integer businessType, FileInfoParam param) {
|
|
|
+
|
|
|
+ FileInfo fileInfo = BeanUtil.toBean(param, FileInfo.class);
|
|
|
+ fileInfo.setBusinessId(businessId);
|
|
|
+ fileInfo.setBusinessType(businessType);
|
|
|
+ fileInfo.setApplicationName(applicationName);
|
|
|
+
|
|
|
+ updateById(fileInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void bindingFile(String applicationName, Long businessId, Integer businessType, List<FileInfoParam> paramList) {
|
|
|
+ if (paramList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<FileInfo> collect = paramList.stream().map(item -> {
|
|
|
+ FileInfo fileInfo = BeanUtil.toBean(item, FileInfo.class);
|
|
|
+ fileInfo.setBusinessId(businessId);
|
|
|
+ fileInfo.setBusinessType(businessType);
|
|
|
+ fileInfo.setApplicationName(applicationName);
|
|
|
+ return fileInfo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ updateBatchById(collect);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void againBindingFile(String applicationName, Long businessId, Integer businessType, FileInfoParam param) {
|
|
|
+ if (ObjectUtil.isNotEmpty(param.getBusinessId())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ removeById(param.getId());
|
|
|
+
|
|
|
+ FileInfo fileInfo = BeanUtil.toBean(param, FileInfo.class);
|
|
|
+ fileInfo.setBusinessId(businessId);
|
|
|
+ fileInfo.setBusinessType(businessType);
|
|
|
+ fileInfo.setApplicationName(applicationName);
|
|
|
+ updateById(fileInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void againBindingFile(String applicationName, Long businessId, Integer businessType, List<FileInfoParam> paramList) {
|
|
|
+
|
|
|
+ List<Long> excludeIdList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<FileInfo> collect = paramList.stream().filter(fileInfo -> {
|
|
|
+ if (ObjectUtil.isNotEmpty(fileInfo.getBusinessId())) {
|
|
|
+ excludeIdList.add(fileInfo.getId());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }).map(item -> {
|
|
|
+ FileInfo fileInfo = BeanUtil.toBean(item, FileInfo.class);
|
|
|
+ fileInfo.setBusinessId(businessId);
|
|
|
+ fileInfo.setBusinessType(businessType);
|
|
|
+ fileInfo.setApplicationName(applicationName);
|
|
|
+ return fileInfo;
|
|
|
+ }).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) {
|
|
|
+ updateBatchById(collect);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void relieveBindingFile(Long businessId) {
|
|
|
+ remove(FileInfo::getBusinessId, businessId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileInfoParam getFileInfo(Long businessId, Integer businessType) {
|
|
|
+
|
|
|
+ LambdaQueryWrapper<FileInfo> wrapper = Wrappers.<FileInfo>lambdaQuery()
|
|
|
+ .eq(FileInfo::getBusinessId, businessId)
|
|
|
+ .eq(ObjectUtil.isNotEmpty(businessType), FileInfo::getBusinessType, businessType)
|
|
|
+ .orderByDesc(BaseEntity::getId)
|
|
|
+ .last("limit 1");
|
|
|
+
|
|
|
+ FileInfo fileInfo = getOne(wrapper);
|
|
|
+
|
|
|
+ return BeanUtil.toBean(fileInfo, FileInfoParam.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<FileInfoParam> getFileInfoList(Long businessId, Integer businessType) {
|
|
|
+
|
|
|
+ LambdaQueryWrapper<FileInfo> wrapper = Wrappers.<FileInfo>lambdaQuery()
|
|
|
+ .eq(FileInfo::getBusinessId, businessId)
|
|
|
+ .eq(ObjectUtil.isNotEmpty(businessType), FileInfo::getBusinessType, businessType);
|
|
|
+
|
|
|
+ List<FileInfo> list = list(wrapper);
|
|
|
+
|
|
|
+ return list.stream().map(item -> BeanUtil.toBean(item, FileInfoParam.class)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件保存路径
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getFilePath() {
|
|
|
+ StringJoiner joiner = new StringJoiner("/");
|
|
|
+ joiner.add(BladeApplication.isLocalDev() ? "D:/hx" : "/mnt/file");
|
|
|
+ joiner.add(DateUtil.format(new Date(), "yyyy-MM/dd/"));
|
|
|
+ return joiner.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|