|
|
@@ -17,6 +17,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.Future;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 企业商标信息Service业务层处理
|
|
|
@@ -35,37 +39,51 @@ public class CrmEnterpriseTrademarkInformationServiceImpl extends ServiceImpl<Cr
|
|
|
|
|
|
@Override
|
|
|
public AjaxResult removeFileById(List<String> ids,String localFileServerMappingPath,String localFileServerUploadDir) {
|
|
|
- List<CrmEnterpriseTrademarkInformation> crmEnterpriseTrademarkInformation = mapper.selectBatchIds(ids);
|
|
|
- for(CrmEnterpriseTrademarkInformation entity : crmEnterpriseTrademarkInformation){
|
|
|
- String image = entity.getTrademarkImage();
|
|
|
- if (!"".equals(image)){
|
|
|
- int index = image.indexOf(localFileServerMappingPath);
|
|
|
- // 如果子串存在,则提取其后面的部分
|
|
|
- if (index != -1) {
|
|
|
- // 提取从子串之后开始直到字符串末尾的部分
|
|
|
- String fileName = image.substring(index + localFileServerMappingPath.length());
|
|
|
- String filePath = localFileServerUploadDir + fileName;
|
|
|
- File file = new File(filePath);
|
|
|
- if (file.exists()) {
|
|
|
- boolean isDeleted = file.delete();
|
|
|
- if (isDeleted) {
|
|
|
- //删除文件数据库数据信息
|
|
|
- QueryWrapper<CrmFile> queryWrapper =new QueryWrapper<>();
|
|
|
- queryWrapper.eq("fid",entity.getId());
|
|
|
- BaseMapper<CrmFile> baseMapper = crmFileService.getBaseMapper();
|
|
|
- baseMapper.delete(queryWrapper);
|
|
|
- } else {
|
|
|
- return AjaxResult.error("无法删除该文件");
|
|
|
- }
|
|
|
- } else {
|
|
|
- return AjaxResult.error("指定的文件不存在");
|
|
|
+ List<CrmEnterpriseTrademarkInformation> crmEnterpriseTrademarkInformationList = mapper.selectBatchIds(ids);
|
|
|
+
|
|
|
+ // 收集所有需要删除的文件路径
|
|
|
+ List<String> filePathsToDelete = crmEnterpriseTrademarkInformationList.stream()
|
|
|
+ .filter(entity -> !"".equals(entity.getTrademarkImage()))
|
|
|
+ .map(entity -> {
|
|
|
+ String image = entity.getTrademarkImage();
|
|
|
+ int index = image.indexOf(localFileServerMappingPath);
|
|
|
+ if (index == -1) {
|
|
|
+ throw new RuntimeException("文件路径解析异常");
|
|
|
}
|
|
|
- } else {
|
|
|
- return AjaxResult.error("文件路径解析异常");
|
|
|
+ return localFileServerUploadDir + image.substring(index + localFileServerMappingPath.length());
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 使用线程池异步删除文件
|
|
|
+ ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); // 根据实际情况调整线程数
|
|
|
+ List<Future<Boolean>> futures = filePathsToDelete.stream()
|
|
|
+ .map(filePath -> executor.submit(() -> {
|
|
|
+ File file = new File(filePath);
|
|
|
+ return file.exists() && file.delete();
|
|
|
+ }))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 等待所有文件删除完成
|
|
|
+ try {
|
|
|
+ futures.forEach(future -> {
|
|
|
+ try {
|
|
|
+ future.get(); // 阻塞等待文件删除完成
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("文件删除失败", e);
|
|
|
}
|
|
|
- }
|
|
|
- mapper.deleteBatchIds(ids);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ return AjaxResult.error("文件删除失败");
|
|
|
}
|
|
|
+
|
|
|
+ // 删除数据库记录
|
|
|
+ QueryWrapper<CrmFile> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.in("fid", ids);
|
|
|
+ BaseMapper<CrmFile> baseMapper = crmFileService.getBaseMapper();
|
|
|
+ baseMapper.delete(queryWrapper);
|
|
|
+
|
|
|
+ // 删除商标信息
|
|
|
+ mapper.deleteBatchIds(ids);
|
|
|
+
|
|
|
return AjaxResult.success("文件已成功删除");
|
|
|
}
|
|
|
|