104 lines
3.6 KiB
Java
104 lines
3.6 KiB
Java
|
|
package com.gunshi.project.xyt.service;
|
||
|
|
|
||
|
|
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 + ":";
|
||
|
|
|
||
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#tableName+':'+#businessId", allEntries = true)
|
||
|
|
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());
|
||
|
|
|
||
|
|
if (CollectionUtils.isNotEmpty(files)) {
|
||
|
|
|
||
|
|
// 删除
|
||
|
|
if (this.lambdaUpdate()
|
||
|
|
.set(FileAssociations::getDel, 1)
|
||
|
|
.eq(FileAssociations::getTableName, tableName)
|
||
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
||
|
|
.in(FileAssociations::getFileId, fileIds)
|
||
|
|
.update()) {
|
||
|
|
log.info("delete file {} success!", fileIds);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// 做新增
|
||
|
|
if (CollectionUtils.isNotEmpty(files)) {
|
||
|
|
|
||
|
|
files.forEach(fileId -> {
|
||
|
|
FileAssociations fileAssociations = new FileAssociations();
|
||
|
|
fileAssociations.setTableName(tableName);
|
||
|
|
fileAssociations.setBusinessId(businessId);
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!this.saveBatch(files)) {
|
||
|
|
log.error("save file error!");
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#tableName+':'+#businessId", allEntries = true)
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Cacheable(value = THIS_REDIS_KEY, key = "#tableName+':'+#businessId")
|
||
|
|
public List<FileAssociations> getFiles(String tableName, String businessId) {
|
||
|
|
return this.baseMapper.getFiles(tableName,businessId);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|