|
@@ -0,0 +1,170 @@
|
|
|
+package com.usky.dxtop.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.usky.dxtop.common.config.DxConfig;
|
|
|
+import com.usky.dxtop.common.exception.CustomException;
|
|
|
+import com.usky.dxtop.common.utils.StringUtils;
|
|
|
+import com.usky.dxtop.common.utils.file.FileTypeUtils;
|
|
|
+import com.usky.dxtop.common.utils.file.FileUploadUtils;
|
|
|
+import com.usky.dxtop.common.utils.file.FileUtils;
|
|
|
+import com.usky.dxtop.common.utils.uuid.IdUtils;
|
|
|
+import com.usky.dxtop.mapper.SysFileMapper;
|
|
|
+import com.usky.dxtop.model.SysFile;
|
|
|
+import com.usky.dxtop.service.SysFileService;
|
|
|
+import com.usky.dxtop.service.vo.SysFileQueryRequest;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static java.util.stream.Collectors.toList;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author yq
|
|
|
+ * @since 2021-10-09
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
|
|
|
+
|
|
|
+ private static final String FILE = "file";
|
|
|
+
|
|
|
+ private static final String URL = "url";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SysFile> addFile(SysFile sysFile) {
|
|
|
+ if (StringUtils.isNotBlank(sysFile.getUploadType())) {
|
|
|
+ sysFile.setUploadType(FILE);
|
|
|
+ }
|
|
|
+ sysFile.setSuccess(true);
|
|
|
+ switch (sysFile.getUploadType()) {
|
|
|
+ case FILE:
|
|
|
+ return sysFile.getFiles().stream().map(m -> this.uploadMultipartFile(m, sysFile)).collect(toList());
|
|
|
+ case URL:
|
|
|
+ return sysFile.getUrls().stream().map(m -> this.addExternalLink(m, sysFile)).collect(toList());
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysFile uploadMultipartFile(MultipartFile m, SysFile sysFile) {
|
|
|
+ try {
|
|
|
+ // 上传文件路径
|
|
|
+ String filePath = DxConfig.getUploadPath();
|
|
|
+ // 上传并返回新文件名称
|
|
|
+ String fileName = FileUploadUtils.upload(filePath, m);
|
|
|
+ sysFile.setOriName(m.getOriginalFilename());
|
|
|
+ sysFile.setFileSize(Integer.parseInt(String.valueOf(m.getSize())));
|
|
|
+ sysFile.setFileType(m.getContentType());
|
|
|
+ sysFile.setUrl(String.format("%s%s", filePath, fileName));
|
|
|
+ } catch (Exception e) {
|
|
|
+ sysFile.setSuccess(false);
|
|
|
+ sysFile.setMessage(e.getMessage());
|
|
|
+ }
|
|
|
+ this.save(sysFile);
|
|
|
+ return sysFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysFile addExternalLink(String url, SysFile sysFile) {
|
|
|
+ try {
|
|
|
+ String filePath = DxConfig.getUploadPath();
|
|
|
+ String format = String.format("%s%s.%s", filePath, System.currentTimeMillis() + "", FileTypeUtils.getFileType(url));
|
|
|
+ FileUtils.downloadFile(url,format);
|
|
|
+ }catch (Exception e){
|
|
|
+ sysFile.setSuccess(false);
|
|
|
+ sysFile.setMessage(e.getMessage());
|
|
|
+ }
|
|
|
+ this.save(sysFile);
|
|
|
+ return sysFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateBatchNo() {
|
|
|
+ return IdUtils.randomUUID();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
|
|
|
+ public Boolean active(String batchNo, List<String> fileIds) {
|
|
|
+ if (CollectionUtils.isEmpty(fileIds)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ LambdaUpdateWrapper<SysFile> updateWrapper = Wrappers.lambdaUpdate();
|
|
|
+ updateWrapper.set(SysFile::getActiveFlag,1)
|
|
|
+ .eq(SysFile::getBatchNo,batchNo)
|
|
|
+ .in(SysFile::getFiles,fileIds);
|
|
|
+ return this.update(updateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SysFile> getFile(SysFileQueryRequest req) {
|
|
|
+ if(StringUtils.isBlank(req.getBatchNo()) && CollectionUtils.isEmpty(req.getIds())){
|
|
|
+ throw new CustomException("批次或者文件编号不能为空");
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<SysFile> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(StringUtils.isNotBlank(req.getBatchNo()), SysFile::getBatchNo, req.getBatchNo())
|
|
|
+ .eq(SysFile::getActiveFlag, 1)
|
|
|
+ .in(!CollectionUtils.isEmpty(req.getIds()), SysFile::getId, req.getIds());
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SysFile> getFileByExpireAt(Date date) {
|
|
|
+ LambdaQueryWrapper<SysFile> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysFile::getDelFlag, 0)
|
|
|
+ .and(wrapper -> wrapper.eq(SysFile::getActiveFlag, 0)
|
|
|
+ .or()
|
|
|
+ .eq(SysFile::getActiveFlag, 1))
|
|
|
+ .between(SysFile::getCreateTime,0,date);
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer deleteFiles(List<SysFile> sysFiles) {
|
|
|
+ Date now = new Date();
|
|
|
+ sysFiles = sysFiles.stream().peek(s -> {
|
|
|
+ s.setDelFlag(1);
|
|
|
+ s.setActiveFlag(0);
|
|
|
+ s.setExpriceAt(now);
|
|
|
+ }).collect(toList());
|
|
|
+ return updateBatchById(sysFiles) ? sysFiles.size() : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer removeByBatchNo(String batchNo) {
|
|
|
+ LambdaQueryWrapper<SysFile> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(SysFile::getBatchNo, batchNo);
|
|
|
+ List<SysFile> sysFileList = list(queryWrapper);
|
|
|
+ return this.deleteFiles(sysFileList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer clearFiles(List<SysFile> fileList) {
|
|
|
+ if (CollectionUtils.isEmpty(fileList)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ fileList.forEach(sysFile -> {
|
|
|
+ try {
|
|
|
+ FileUtils.deleteFile(sysFile.getUrl());
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("删除文件失败:"+e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return this.deleteFiles(fileList);
|
|
|
+ }
|
|
|
+}
|