fix:修改文件关联关系表及相应功能
This commit is contained in:
parent
08310e1606
commit
f7275abe20
|
|
@ -1,15 +1,17 @@
|
||||||
package com.electromagnetic.industry.software.manage.pojo.models;
|
package com.electromagnetic.industry.software.manage.pojo.models;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
|
||||||
|
import com.electromagnetic.industry.software.common.util.IdWorker;
|
||||||
|
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("ed_file_relation")
|
@TableName("ed_file_relation")
|
||||||
public class EdFileRelation {
|
public class EdFileRelation extends BaseModel{
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
|
|
||||||
|
|
@ -30,21 +32,15 @@ public class EdFileRelation {
|
||||||
*/
|
*/
|
||||||
private String relationship;
|
private String relationship;
|
||||||
|
|
||||||
/**
|
public void newInit() {
|
||||||
* 创建者 用户id
|
String userId = UserThreadLocal.getUserId();
|
||||||
*/
|
String newId = IdWorker.getSnowFlakeIdString();
|
||||||
@TableField(value = "created_by")
|
Date now = new Date();
|
||||||
private String createdBy;
|
this.setId(newId);
|
||||||
|
this.setCreatedTime(now);
|
||||||
@TableField(value = "created_at", fill = FieldFill.INSERT)
|
this.setCreatedBy(userId);
|
||||||
private Date createdAt;
|
this.setUpdatedTime(now);
|
||||||
|
this.setUpdatedBy(userId);
|
||||||
/**
|
this.setEffectFlag(EffectFlagEnum.EFFECT.code);
|
||||||
* 更新者 用户id
|
}
|
||||||
*/
|
|
||||||
@TableField(value = "updated_by")
|
|
||||||
private String updatedBy;
|
|
||||||
|
|
||||||
@TableField(value = "updated_at", fill = FieldFill.UPDATE)
|
|
||||||
private Date updatedAt;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,7 @@ import cn.hutool.crypto.SecureUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
|
import com.electromagnetic.industry.software.common.enums.*;
|
||||||
import com.electromagnetic.industry.software.common.enums.EleDataSaveStatusEnum;
|
|
||||||
import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum;
|
|
||||||
import com.electromagnetic.industry.software.common.enums.PublishEnum;
|
|
||||||
import com.electromagnetic.industry.software.common.exception.BizException;
|
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||||
import com.electromagnetic.industry.software.common.util.*;
|
import com.electromagnetic.industry.software.common.util.*;
|
||||||
|
|
@ -52,6 +49,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@Transactional
|
||||||
public Boolean createRelation(EdFileRelation edFileRelation) throws BizException {
|
public Boolean createRelation(EdFileRelation edFileRelation) throws BizException {
|
||||||
// 无法建立已建立的关系
|
// 无法建立已建立的关系
|
||||||
String queryId1 = edFileRelation.getId1();
|
String queryId1 = edFileRelation.getId1();
|
||||||
|
|
@ -65,17 +63,24 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
queryWrapper.eq(EdFileRelation::getId1, queryId1).eq(EdFileRelation::getId2, queryId2)
|
queryWrapper.eq(EdFileRelation::getId1, queryId1).eq(EdFileRelation::getId2, queryId2)
|
||||||
.or()
|
.or()
|
||||||
.eq(EdFileRelation::getId1, queryId2).eq(EdFileRelation::getId2, queryId1);
|
.eq(EdFileRelation::getId1, queryId2).eq(EdFileRelation::getId2, queryId1);
|
||||||
List<EdFileRelation> list = this.list(queryWrapper);
|
EdFileRelation relation = this.getOne(queryWrapper);
|
||||||
if (!list.isEmpty()) {
|
if (relation != null && relation.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)) {
|
||||||
throw new BizException("请勿重复建立关系");
|
throw new BizException("请勿重复建立关系");
|
||||||
}
|
}
|
||||||
|
|
||||||
edFileRelation.setId(IdWorker.getSnowFlakeIdString());
|
boolean isSaved;
|
||||||
edFileRelation.setCreatedBy(UserThreadLocal.getUserId());
|
if (relation != null) {
|
||||||
boolean isSaved = this.save(edFileRelation);
|
relation.setRelationship(edFileRelation.getRelationship());
|
||||||
|
relation.setEffectFlag(EffectFlagEnum.EFFECT.code);
|
||||||
|
isSaved = this.updateById(relation);
|
||||||
|
} else {
|
||||||
|
EdFileRelation newRelation = new EdFileRelation();
|
||||||
|
newRelation.newInit();
|
||||||
|
BeanUtils.copyProperties(edFileRelation, newRelation);
|
||||||
|
isSaved = this.save(newRelation);
|
||||||
|
}
|
||||||
if (isSaved) {
|
if (isSaved) {
|
||||||
EdFileInfo fileInfo = edFileInfoMapper.selectById(edFileRelation.getId());
|
UserThreadLocal.setSuccessInfo("", edFileRelation.getId(), StrFormatter.format("关联了文件"));
|
||||||
UserThreadLocal.setSuccessInfo(Optional.ofNullable(fileInfo).map(EdFileInfo::getParentId).orElse(""), edFileRelation.getId(), StrFormatter.format("关联了文件"));
|
|
||||||
}
|
}
|
||||||
return isSaved;
|
return isSaved;
|
||||||
}
|
}
|
||||||
|
|
@ -89,10 +94,17 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
@Override
|
@Override
|
||||||
public Boolean cancelRelation(String id) {
|
public Boolean cancelRelation(String id) {
|
||||||
|
|
||||||
boolean isRemoved = this.removeById(id);
|
EdFileRelation edFileRelation = this.getById(id);
|
||||||
|
if (edFileRelation == null) {
|
||||||
|
throw new BizException("关系不存在");
|
||||||
|
}
|
||||||
|
if (edFileRelation.getEffectFlag().equals(EffectFlagEnum.NOT_EFFECTIVE.code)) {
|
||||||
|
throw new BizException("关系已取消");
|
||||||
|
}
|
||||||
|
edFileRelation.setEffectFlag(EffectFlagEnum.NOT_EFFECTIVE.code);
|
||||||
|
boolean isRemoved = this.updateById(edFileRelation);
|
||||||
if (isRemoved) {
|
if (isRemoved) {
|
||||||
EdFileInfo fileInfo = edFileInfoMapper.selectById(id);
|
UserThreadLocal.setSuccessInfo("", id, StrFormatter.format("取消了文件关联"));
|
||||||
UserThreadLocal.setSuccessInfo(Optional.ofNullable(fileInfo).map(EdFileInfo::getParentId).orElse(""), id, StrFormatter.format("取消了文件关联"));
|
|
||||||
}
|
}
|
||||||
return isRemoved;
|
return isRemoved;
|
||||||
}
|
}
|
||||||
|
|
@ -174,10 +186,13 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Edge> getEdges(String id) {
|
private List<Edge> getEdges(String id) {
|
||||||
|
|
||||||
LambdaQueryWrapper<EdFileRelation> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<EdFileRelation> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
queryWrapper.eq(EdFileRelation::getId1, id)
|
queryWrapper.eq(EdFileRelation::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
||||||
|
.and(wrapper -> wrapper.eq(EdFileRelation::getId1, id)
|
||||||
.or()
|
.or()
|
||||||
.eq(EdFileRelation::getId2, id);
|
.eq(EdFileRelation::getId2, id));
|
||||||
|
|
||||||
List<EdFileRelation> list = this.list(queryWrapper);
|
List<EdFileRelation> list = this.list(queryWrapper);
|
||||||
List<Edge> edges = new ArrayList<>();
|
List<Edge> edges = new ArrayList<>();
|
||||||
for (EdFileRelation edFileRelation : list) {
|
for (EdFileRelation edFileRelation : list) {
|
||||||
|
|
@ -202,23 +217,33 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ElectromagneticResult<?> uploadFileAndRelation(String parentId, String id, MultipartFile file, String desc, int dataOwnCode) {
|
public ElectromagneticResult<?> uploadFileAndRelation(String parentId, String id, MultipartFile file, String desc, int dataOwnCode) {
|
||||||
|
|
||||||
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), NAME_VALID_MSG);
|
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), NAME_VALID_MSG);
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
String mainName = FileUtil.mainName(fileName);
|
String mainName = FileUtil.mainName(fileName);
|
||||||
String suffix = FileUtil.getSuffix(fileName);
|
String suffix = FileUtil.getSuffix(fileName);
|
||||||
Assert.isTrue(StrUtil.isNotEmpty(suffix), "文件类型不能为空");
|
Assert.isTrue(StrUtil.isNotEmpty(suffix), "文件类型不能为空");
|
||||||
|
String destPath = commonService.getDbPathById(parentId);
|
||||||
|
Assert.isTrue(!file.isEmpty(), StrFormatter.format("文件 {} 为空,文件上传到 {} 失败", fileName, destPath));
|
||||||
|
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), StrFormatter.format("文件 {} {},上传到 {} 失败", fileName, NAME_VALID_MSG, destPath));
|
||||||
// 查找下一层,看是否存在顶级定义相关文件,如果存在,则该层属于管理员层级定义的,不允许上传文件
|
// 查找下一层,看是否存在顶级定义相关文件,如果存在,则该层属于管理员层级定义的,不允许上传文件
|
||||||
long dirCount = edFileInfoService.count(Wrappers.lambdaQuery(EdFileInfo.class)
|
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
.eq(EdFileInfo::getParentId, parentId)
|
.eq(EdFileInfo::getParentId, parentId)
|
||||||
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
||||||
.eq(EdFileInfo::getDataOwn, dataOwnCode)
|
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code);
|
||||||
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code));
|
DataOwnEnum obj = DataOwnEnum.getEnumByCode(dataOwnCode);
|
||||||
Assert.isTrue(dirCount == 0, "层级目录不允许上传文件");
|
switch (Objects.requireNonNull(obj)) {
|
||||||
|
case USER_FILE, USER_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code);
|
||||||
|
case SYS_FILE, SYS_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.SYS_PRJ.code);
|
||||||
|
case REPO_PRJ, REPO_FILE -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.REPO_PRJ.code);
|
||||||
|
default -> ElectromagneticResultUtil.fail("-1", "参数错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
long dirCount = edFileInfoMapper.selectCount(queryWrapper);
|
||||||
|
Assert.isTrue(dirCount == 0, "文件 {} 上传到 {} 失败,层级结构不允许上传文件", fileName, destPath);
|
||||||
EdFileInfo newEdFileInfo = new EdFileInfo();
|
EdFileInfo newEdFileInfo = new EdFileInfo();
|
||||||
|
EdFileInfo finalEdFileInfo;
|
||||||
newEdFileInfo.newInit();
|
newEdFileInfo.newInit();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Assert.isTrue(EleCommonUtil.isFileNameValid(fileName), NAME_VALID_MSG);
|
Assert.isTrue(EleCommonUtil.isFileNameValid(fileName), NAME_VALID_MSG);
|
||||||
Long count = edFileInfoService.count(Wrappers.lambdaQuery(EdFileInfo.class)
|
Long count = edFileInfoService.count(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
|
|
@ -227,44 +252,40 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
|
||||||
.eq(EdFileInfo::getFileType, suffix));
|
.eq(EdFileInfo::getFileType, suffix));
|
||||||
|
|
||||||
EdFileInfo parentFileInfo = edFileInfoMapper.selectOne(Wrappers.lambdaQuery(EdFileInfo.class)
|
if (count > 0) {
|
||||||
.select(EdFileInfo.class, obj -> !StrUtil.equals(obj.getColumn(), "file_content"))
|
finalEdFileInfo = edFileInfoService.handUploadRepeatFile(parentId, file, 3, dataOwnCode);
|
||||||
|
} else {
|
||||||
|
EdFileInfo parentFolderInfo = edFileInfoMapper.selectOne(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
.eq(EdFileInfo::getId, parentId)
|
.eq(EdFileInfo::getId, parentId)
|
||||||
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code));
|
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code));
|
||||||
String codePathByDbPath = commonService.getCodePathByDbPath(parentFileInfo.getFilePath());
|
|
||||||
|
String codePathByDbPath = commonService.getCodePathByDbPath(parentFolderInfo.getFilePath());
|
||||||
String fileCode = commonService.createFileCode(codePathByDbPath, suffix, FILE_START_VERSION, newEdFileInfo.getFileTime());
|
String fileCode = commonService.createFileCode(codePathByDbPath, suffix, FILE_START_VERSION, newEdFileInfo.getFileTime());
|
||||||
newEdFileInfo.setParentId(parentId)
|
newEdFileInfo.setParentId(parentId)
|
||||||
.setFileCode(fileCode)
|
|
||||||
.setFileName(mainName)
|
|
||||||
.setFileType(suffix)
|
|
||||||
.setFileVersion(FILE_START_VERSION)
|
|
||||||
.setFileTime(newEdFileInfo.getFileTime())
|
|
||||||
.setFileSize(file.getSize())
|
|
||||||
.setFilePath(parentFileInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + newEdFileInfo.getId())
|
|
||||||
.setDataType(EleDataTypeEnum.FILE.code)
|
|
||||||
.setDataStatus(PublishEnum.PUBLISHED.getCode())
|
|
||||||
.setFileCode(fileCode)
|
.setFileCode(fileCode)
|
||||||
.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code)
|
.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code)
|
||||||
.setDataOwn(dataOwnCode)
|
.setDataOwn(dataOwnCode)
|
||||||
|
.setFileName(mainName)
|
||||||
|
.setFileContent(EleCommonUtil.parse(file.getInputStream(), suffix))
|
||||||
|
.setFileType(suffix)
|
||||||
|
.setFileVersion(FILE_START_VERSION)
|
||||||
|
.setFileSize(file.getSize())
|
||||||
|
.setFilePath(parentFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + newEdFileInfo.getId())
|
||||||
|
.setDataType(EleDataTypeEnum.FILE.code)
|
||||||
|
.setDataStatus(PublishEnum.PUBLISHED.getCode())
|
||||||
.setEffectFlag(EffectFlagEnum.EFFECT.code);
|
.setEffectFlag(EffectFlagEnum.EFFECT.code);
|
||||||
|
|
||||||
// 文件名加”_1“,存为新文件
|
|
||||||
if (count > 0) {
|
|
||||||
edFileInfoService.resetFileInfoName(newEdFileInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
edFileInfoService.saveOrUpdate(newEdFileInfo);
|
edFileInfoService.saveOrUpdate(newEdFileInfo);
|
||||||
String fileDestPath = commonService.getFileSysPath(newEdFileInfo.getFilePath(), dataOwnCode);
|
String fileDestPath = commonService.getFileSysPath(newEdFileInfo.getFilePath(), dataOwnCode);
|
||||||
FileUtil.writeFromStream(file.getInputStream(), fileDestPath);
|
FileUtil.writeFromStream(file.getInputStream(), fileDestPath);
|
||||||
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
|
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
|
||||||
|
finalEdFileInfo = newEdFileInfo;
|
||||||
// 创建文件关系
|
// 创建文件关系
|
||||||
EdFileRelation relation = new EdFileRelation();
|
EdFileRelation relation = new EdFileRelation();
|
||||||
relation.setId1(id);
|
relation.setId1(id);
|
||||||
relation.setId2(newEdFileInfo.getId());
|
relation.setId2(newEdFileInfo.getId());
|
||||||
relation.setRelationship(desc);
|
relation.setRelationship(desc);
|
||||||
createRelation(relation);
|
createRelation(relation);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
newEdFileInfo.setParentId(parentId)
|
newEdFileInfo.setParentId(parentId)
|
||||||
.setFileName(mainName)
|
.setFileName(mainName)
|
||||||
|
|
@ -280,7 +301,14 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
||||||
log.error(info, e);
|
log.error(info, e);
|
||||||
throw new BizException(info);
|
throw new BizException(info);
|
||||||
}
|
}
|
||||||
UserThreadLocal.setSuccessInfo(newEdFileInfo.getParentId(), newEdFileInfo.getId(), "上传了文件并建立了文件关系");
|
String otherFileName = edFileInfoMapper.selectById(id).getFileName();
|
||||||
|
UserThreadLocal.setSuccessInfo(Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getParentId).orElse(""),
|
||||||
|
Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getFileId).orElse(""),
|
||||||
|
"文件 {} 为上传到 {} 成功,存入的文件名为 {},成功与文件 {} 建立了关系",
|
||||||
|
fileName,
|
||||||
|
destPath,
|
||||||
|
Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getFileName).orElse(fileName) + "." + Optional.ofNullable(finalEdFileInfo).map(EdFileInfo::getFileType).orElse(suffix),
|
||||||
|
otherFileName);
|
||||||
return ElectromagneticResultUtil.success(true);
|
return ElectromagneticResultUtil.success(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue