|
|
@ -1,5 +1,6 @@
|
|
|
|
package com.supervision.manage.service.impl;
|
|
|
|
package com.supervision.manage.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
|
|
@ -15,8 +16,11 @@ import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
@RequiredArgsConstructor
|
|
|
@ -27,30 +31,85 @@ public class MaterialLibraryManageServiceImpl implements MaterialLibraryManageSe
|
|
|
|
private final FileManageService fileManageService;
|
|
|
|
private final FileManageService fileManageService;
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
public String uploadMaterial(MultipartFile multipartFile, String materialName, String materialType) throws Exception {
|
|
|
|
public String uploadMaterial(MultipartFile multipartFile,MaterialLibrary materialLibrary) throws Exception {
|
|
|
|
// 校验素材名是否已经存在
|
|
|
|
// 校验素材名是否已经存在
|
|
|
|
Assert.notEmpty(materialName, "素材名不能为空");
|
|
|
|
Assert.notEmpty(materialLibrary.getMaterialName(), "素材名不能为空");
|
|
|
|
Assert.notEmpty(materialType, "素材类型不能为空");
|
|
|
|
Assert.notEmpty(materialLibrary.getMaterialType(), "素材类型不能为空");
|
|
|
|
List<String> split = StrUtil.split(materialName, ".");
|
|
|
|
|
|
|
|
Integer count = materialLibraryService.lambdaQuery().likeRight(MaterialLibrary::getMaterialName, split.get(0)).count();
|
|
|
|
materialLibrary.setMaterialName(renameIfNecessary(materialLibrary.getMaterialName()));
|
|
|
|
|
|
|
|
// 保存素材
|
|
|
|
|
|
|
|
FileResource fileResource = fileManageService.uploadFile(multipartFile, multipartFile.getContentType());
|
|
|
|
|
|
|
|
materialLibrary.setFileResourceId(fileResource.getId());
|
|
|
|
|
|
|
|
materialLibraryService.save(materialLibrary);
|
|
|
|
|
|
|
|
return materialLibrary.getId();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 重命名文件名
|
|
|
|
|
|
|
|
* @param materialName 素材名字
|
|
|
|
|
|
|
|
* @return 新的文件名
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String renameIfNecessary(String materialName) {
|
|
|
|
|
|
|
|
int count = countMaterialName(materialName);
|
|
|
|
if (count > 0){
|
|
|
|
if (count > 0){
|
|
|
|
// 重新命名文件
|
|
|
|
// 重新命名文件
|
|
|
|
|
|
|
|
String excludeSuffix = stringExcludeSuffix(materialName);
|
|
|
|
|
|
|
|
List<String> split = StrUtil.split(materialName, ".");
|
|
|
|
if (split.size() > 1){
|
|
|
|
if (split.size() > 1){
|
|
|
|
materialName = split.get(0) + "(" + count + ")" + "." + split.get(split.size()-1);
|
|
|
|
materialName = excludeSuffix + "(" + count + ")" + "." + split.get(split.size()-1);
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
materialName = materialName + "(" + count + ")";
|
|
|
|
materialName = excludeSuffix + "(" + count + ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return materialName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存素材
|
|
|
|
/**
|
|
|
|
FileResource fileResource = fileManageService.uploadFile(multipartFile, multipartFile.getContentType());
|
|
|
|
* 统计素材库中已有的相同素材名字的数量
|
|
|
|
|
|
|
|
* @param materialName 素材名字
|
|
|
|
|
|
|
|
* @return 数量
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int countMaterialName(String materialName) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String cleanedName = stringExcludeSuffix(materialName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<MaterialLibrary> materialLibraryList = materialLibraryService.lambdaQuery().likeRight(MaterialLibrary::getMaterialName,cleanedName).list();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (MaterialLibrary materialLibrary : materialLibraryList) {
|
|
|
|
|
|
|
|
String name = materialLibrary.getMaterialName();
|
|
|
|
|
|
|
|
String regex = "\\(\\d+\\)";
|
|
|
|
|
|
|
|
Pattern pattern = Pattern.compile(regex);
|
|
|
|
|
|
|
|
Matcher matcher = pattern.matcher(name);
|
|
|
|
|
|
|
|
String modifiedText;
|
|
|
|
|
|
|
|
int previousEnd = 0;
|
|
|
|
|
|
|
|
int lastEnd = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
|
|
|
|
previousEnd = matcher.start();
|
|
|
|
|
|
|
|
lastEnd = matcher.end();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaterialLibrary materialLibrary = new MaterialLibrary();
|
|
|
|
if (lastEnd > 0) {
|
|
|
|
materialLibrary.setFileResourceId(fileResource.getId());
|
|
|
|
modifiedText = name.substring(0, previousEnd) + name.substring(lastEnd);
|
|
|
|
materialLibrary.setMaterialType(materialType);
|
|
|
|
} else {
|
|
|
|
materialLibrary.setMaterialName(materialName);
|
|
|
|
modifiedText = name;
|
|
|
|
materialLibraryService.save(materialLibrary);
|
|
|
|
}
|
|
|
|
return materialLibrary.getId();
|
|
|
|
if (StrUtil.equals(modifiedText,materialName)){
|
|
|
|
|
|
|
|
count ++ ;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String stringExcludeSuffix(String materialName) {
|
|
|
|
|
|
|
|
List<String> split = StrUtil.split(materialName, ".");
|
|
|
|
|
|
|
|
ArrayList<String> tmpList = CollUtil.newArrayList(split);
|
|
|
|
|
|
|
|
if(split.size() > 1){
|
|
|
|
|
|
|
|
tmpList.remove(split.size()-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.join(".",tmpList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|