2024-07-17 17:38:06 +08:00
|
|
|
package com.gunshi.project.xyt.service;
|
|
|
|
|
|
2024-07-18 18:00:27 +08:00
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
2024-07-17 17:38:06 +08:00
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.gunshi.project.xyt.mapper.FileAssociationsMapper;
|
|
|
|
|
import com.gunshi.project.xyt.model.FileAssociations;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static com.gunshi.project.xyt.entity.MyConstant.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 描述: 文件关联业务表
|
|
|
|
|
* author: xusan
|
|
|
|
|
* date: 2024-07-17 10:09:40
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@Transactional
|
|
|
|
|
public class FileAssociationsService extends ServiceImpl<FileAssociationsMapper, FileAssociations> {
|
|
|
|
|
|
|
|
|
|
private static final String THIS_REDIS_KEY = REDIS_KEY + FileAssociations.thisTableName + ":";
|
|
|
|
|
|
2024-08-15 14:10:51 +08:00
|
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#p1 +':*'", allEntries = true)
|
2024-07-17 17:38:06 +08:00
|
|
|
public void saveFile(List<FileAssociations> files, String tableName, String businessId) {
|
|
|
|
|
if (CollectionUtils.isEmpty(files)) {
|
|
|
|
|
log.info("fileIds is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询是否添加
|
|
|
|
|
List<FileAssociations> dbList = this.lambdaQuery()
|
|
|
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
|
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
|
|
|
.eq(FileAssociations::getDel, REC)
|
|
|
|
|
.list();
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(dbList)) {
|
|
|
|
|
Set<Long> fileIds = dbList.stream().map(FileAssociations::getFileId)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
// 删除已添加数据, 删除重复数据 删除不成功即新数据
|
|
|
|
|
files = files.stream()
|
|
|
|
|
.filter(fileAssociations -> !fileIds.remove(fileAssociations.getFileId()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
2024-08-13 15:03:45 +08:00
|
|
|
// if (CollectionUtils.isNotEmpty(files)) {
|
2024-07-17 17:38:06 +08:00
|
|
|
|
|
|
|
|
// 删除
|
|
|
|
|
if (this.lambdaUpdate()
|
2024-08-14 17:09:58 +08:00
|
|
|
.set(FileAssociations::getDel, DEL)
|
2024-07-17 17:38:06 +08:00
|
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
|
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
|
|
|
.in(FileAssociations::getFileId, fileIds)
|
|
|
|
|
.update()) {
|
|
|
|
|
log.info("delete file {} success!", fileIds);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 15:03:45 +08:00
|
|
|
// }
|
2024-07-17 17:38:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 做新增
|
|
|
|
|
if (CollectionUtils.isNotEmpty(files)) {
|
|
|
|
|
|
|
|
|
|
files.forEach(fileId -> {
|
2024-07-18 18:00:27 +08:00
|
|
|
fileId.setId(IdWorker.getId());
|
|
|
|
|
fileId.setTableName(tableName);
|
|
|
|
|
fileId.setBusinessId(businessId);
|
2024-07-17 17:38:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!this.saveBatch(files)) {
|
|
|
|
|
log.error("save file error!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 14:10:51 +08:00
|
|
|
}
|
|
|
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#p1 +':*'", allEntries = true)
|
|
|
|
|
public void saveFile(List<FileAssociations> files, String tableName, String businessId, String type) {
|
|
|
|
|
if (CollectionUtils.isEmpty(files)) {
|
|
|
|
|
log.info("fileIds is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询是否添加
|
|
|
|
|
List<FileAssociations> dbList = this.lambdaQuery()
|
|
|
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
|
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
|
|
|
.eq(FileAssociations::getType, type)
|
|
|
|
|
.eq(FileAssociations::getDel, REC)
|
|
|
|
|
.list();
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(dbList)) {
|
|
|
|
|
Set<Long> fileIds = dbList.stream().map(FileAssociations::getFileId)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
// 删除已添加数据, 删除重复数据 删除不成功即新数据
|
|
|
|
|
files = files.stream()
|
|
|
|
|
.filter(fileAssociations -> !fileIds.remove(fileAssociations.getFileId()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// if (CollectionUtils.isNotEmpty(files)) {
|
|
|
|
|
|
|
|
|
|
// 删除
|
|
|
|
|
if (this.lambdaUpdate()
|
|
|
|
|
.set(FileAssociations::getDel, DEL)
|
|
|
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
|
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
|
|
|
.eq(FileAssociations::getType, type)
|
|
|
|
|
.in(FileAssociations::getFileId, fileIds)
|
|
|
|
|
.update()) {
|
|
|
|
|
log.info("delete file {} success!", fileIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 做新增
|
|
|
|
|
if (CollectionUtils.isNotEmpty(files)) {
|
|
|
|
|
|
|
|
|
|
files.forEach(fileId -> {
|
|
|
|
|
fileId.setId(IdWorker.getId());
|
|
|
|
|
fileId.setTableName(tableName);
|
|
|
|
|
fileId.setType(type);
|
|
|
|
|
fileId.setBusinessId(businessId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!this.saveBatch(files)) {
|
|
|
|
|
log.error("save file error!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 17:38:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-30 17:52:25 +08:00
|
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#p0 +':*'", allEntries = true)
|
2024-07-17 17:38:06 +08:00
|
|
|
public void deleteFile(String tableName, String businessId) {
|
|
|
|
|
// 删除
|
|
|
|
|
if (this.lambdaUpdate()
|
|
|
|
|
.set(FileAssociations::getDel, DEL)
|
|
|
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
|
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
|
|
|
.update()) {
|
|
|
|
|
log.info("delete file {} success!", businessId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 17:52:25 +08:00
|
|
|
@Cacheable(value = THIS_REDIS_KEY, key = "#p0 +':'+ #p1", unless = "false")
|
|
|
|
|
public List<FileAssociations> getFiles(String tName, String bId) {
|
|
|
|
|
return this.baseMapper.getFiles(tName,bId);
|
2024-07-17 17:38:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|