gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/FileAssociationsService.java

182 lines
6.7 KiB
Java
Raw Normal View History

package com.gunshi.project.xyt.service;
2024-08-22 10:44:12 +08:00
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
2024-08-28 15:20:16 +08:00
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.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)) {
// 删除
2024-08-19 09:55:32 +08:00
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);
}
2024-08-22 10:44:12 +08:00
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());
2024-08-28 15:20:16 +08:00
if(StringUtils.isNotEmpty(businessId)){
attach.setBusinessId(businessId);
}
2024-08-22 10:44:12 +08:00
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) {
2024-08-28 09:27:34 +08:00
return this.baseMapper.queryFileList(businessId,tableName,type);
2024-08-22 10:44:12 +08:00
}
2024-08-28 15:20:16 +08:00
public boolean removeByBzIds(List<String> businessIds) {
return this.remove(new QueryWrapper<FileAssociations>().in("business_id", businessIds));
}
2024-09-24 13:33:32 +08:00
public List<FileAssociations> getFilesByIds(List<String> ids) {
return this.baseMapper.getFilesByIds(ids);
}
}