181 lines
6.7 KiB
Java
181 lines
6.7 KiB
Java
package com.gunshi.project.xyt.service;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
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.Objects;
|
|
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 = "#p1 +':*'", 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 (CollectionUtils.isNotEmpty(fileIds) && this.lambdaUpdate()
|
|
.set(FileAssociations::getDel, DEL)
|
|
.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 -> {
|
|
fileId.setId(IdWorker.getId());
|
|
fileId.setTableName(tableName);
|
|
fileId.setBusinessId(businessId);
|
|
});
|
|
|
|
if (!this.saveBatch(files)) {
|
|
log.error("save file error!");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#p1 +':*'", allEntries = true)
|
|
public void saveFileNotDel(List<FileAssociations> files, String tableName, String businessId, String type) {
|
|
|
|
// 查询是否添加
|
|
List<FileAssociations> dbList = this.lambdaQuery()
|
|
.eq(FileAssociations::getTableName, tableName)
|
|
.eq(FileAssociations::getBusinessId, businessId)
|
|
.eq(FileAssociations::getType, type)
|
|
.eq(FileAssociations::getDel, REC)
|
|
.list();
|
|
|
|
if (CollectionUtils.isEmpty(files) && CollectionUtils.isNotEmpty(dbList)) {
|
|
log.info("fileIds is null!");
|
|
Set<Long> fileIds = dbList.stream().map(FileAssociations::getFileId)
|
|
.collect(Collectors.toSet());
|
|
// 删除
|
|
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);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 做新增
|
|
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!");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@CacheEvict(value = THIS_REDIS_KEY, key = "#p0 +':*'", 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 = "#p0 +':'+ #p1", unless = "false")
|
|
public List<FileAssociations> getFiles(String tName, String bId) {
|
|
return this.baseMapper.getFiles(tName,bId);
|
|
}
|
|
|
|
public void save(List<FileAssociations> attachList,String businessId, String tableName,String type) {
|
|
if (attachList != null && !attachList.isEmpty()) {
|
|
for (FileAssociations attach : attachList) {
|
|
attach.setId(IdWorker.getId());
|
|
if(StringUtils.isNotEmpty(businessId)){
|
|
attach.setBusinessId(businessId);
|
|
}
|
|
attach.setTableName(tableName);
|
|
attach.setType(type);
|
|
}
|
|
this.saveBatch(attachList);
|
|
}
|
|
}
|
|
|
|
public boolean removeByBzIdAndType(String businessId, String tableName,String type) {
|
|
return this.remove(new QueryWrapper<FileAssociations>().eq("business_id", businessId).eq("table_name", tableName).eq("type",type));
|
|
}
|
|
|
|
public List<FileAssociations> queryFileList(String businessId, String tableName,String type) {
|
|
return this.baseMapper.queryFileList(businessId,tableName,type);
|
|
}
|
|
|
|
public boolean removeByBzIds(List<String> businessIds) {
|
|
return this.remove(new QueryWrapper<FileAssociations>().in("business_id", businessIds));
|
|
}
|
|
|
|
}
|