给商品和单据的批量删除做图片的逻辑删除

This commit is contained in:
jishenghua 2024-12-02 00:47:18 +08:00
parent f2703d0b0a
commit e15e981006
4 changed files with 129 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import com.jsh.erp.service.depotHead.DepotHeadService;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.orgaUserRel.OrgaUserRelService;
import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
@ -51,6 +52,8 @@ public class AccountHeadService {
@Resource
private SupplierService supplierService;
@Resource
private SystemConfigService systemConfigService;
@Resource
private LogService logService;
@Resource
private AccountItemMapperEx accountItemMapperEx;
@ -216,13 +219,16 @@ public class AccountHeadService {
sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
User userInfo=userService.getCurrentUser();
String [] idArray=ids.split(",");
List<AccountHead> list = getAccountHeadListByIds(ids);
//删除主表
accountItemMapperEx.batchDeleteAccountItemByHeadIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
//删除子表
accountHeadMapperEx.batchDeleteAccountHeadByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
List<AccountHead> list = getAccountHeadListByIds(ids);
//路径列表
List<String> pathList = new ArrayList<>();
for(AccountHead accountHead: list){
sb.append("[").append(accountHead.getBillNo()).append("]");
pathList.add(accountHead.getFileName());
if("1".equals(accountHead.getStatus())) {
throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_CODE,
String.format(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));
@ -234,6 +240,8 @@ public class AccountHeadService {
}
}
}
//逻辑删除文件
systemConfigService.deleteFileByPathList(pathList);
logService.insertLog("财务", sb.toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
return 1;

View File

@ -445,9 +445,12 @@ public class DepotHeadService {
public int batchDeleteBillByIds(String ids)throws Exception {
StringBuffer sb = new StringBuffer();
sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
//路径列表
List<String> pathList = new ArrayList<>();
List<DepotHead> dhList = getDepotHeadListByIds(ids);
for(DepotHead depotHead: dhList){
sb.append("[").append(depotHead.getNumber()).append("]");
pathList.add(depotHead.getFileName());
//只有未审核的单据才能被删除
if("0".equals(depotHead.getStatus())) {
User userInfo = userService.getCurrentUser();
@ -567,6 +570,8 @@ public class DepotHeadService {
String.format(ExceptionConstants.DEPOT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));
}
}
//逻辑删除文件
systemConfigService.deleteFileByPathList(pathList);
logService.insertLog("单据", sb.toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
return 1;

View File

@ -15,6 +15,7 @@ import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.materialCategory.MaterialCategoryService;
import com.jsh.erp.service.materialExtend.MaterialExtendService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.unit.UnitService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.BaseResponseInfo;
@ -77,6 +78,8 @@ public class MaterialService {
private DepotService depotService;
@Resource
private MaterialExtendService materialExtendService;
@Resource
private SystemConfigService systemConfigService;
@Value(value="${file.uploadType}")
private Long fileUploadType;
@ -291,9 +294,12 @@ public class MaterialService {
//记录日志
StringBuffer sb = new StringBuffer();
sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
//路径列表
List<String> pathList = new ArrayList<>();
List<Material> list = getMaterialListByIds(ids);
for(Material material: list){
sb.append("[").append(material.getName()).append("]");
pathList.add(material.getImgName());
}
logService.insertLog("商品", sb.toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
@ -304,6 +310,8 @@ public class MaterialService {
materialMapperEx.batchDeleteMaterialByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
//逻辑删除商品价格扩展
materialExtendMapperEx.batchDeleteMaterialExtendByMIds(idArray);
//逻辑删除文件
systemConfigService.deleteFileByPathList(pathList);
return 1;
}catch(Exception e){
JshException.writeFail(logger, e);

View File

@ -6,6 +6,7 @@ import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.jsh.erp.constants.BusinessConstants;
@ -42,6 +43,7 @@ import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -61,9 +63,14 @@ public class SystemConfigService {
@Resource
private LogService logService;
@Value(value="${file.uploadType}")
private Long fileUploadType;
@Value(value="${file.path}")
private String filePath;
private static String DELETED = "deleted";
public SystemConfig getSystemConfig(long id)throws Exception {
SystemConfig result=null;
try{
@ -349,6 +356,106 @@ public class SystemConfigService {
return linkUrl + filePath + "/" + imgPath;
}
/**
* 逻辑删除文件
* @param pathList
*/
public void deleteFileByPathList(List<String> pathList) throws Exception {
if(fileUploadType == 1) {
//本地
for(String pathStr: pathList) {
String[] pathArr = pathStr.split(",");
for (int i = 0; i < pathArr.length; i++) {
String path = pathArr[i];
// 提取文件的路径
String pathDir = getDirByPath(path);
if(StringUtil.isNotEmpty(pathDir)) {
// 源文件路径
Path sourcePath = Paths.get(filePath + File.separator + path);
// 目标文件路径注意这里是新文件的完整路径包括文件名
Path targetPath = Paths.get(filePath + File.separator + DELETED + File.separator + path);
try {
File file = new File(filePath + File.separator + DELETED + File.separator + pathDir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
// 复制文件如果目标文件已存在则替换它
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
// 删除源文件
Files.delete(sourcePath);
logger.info("File copied successfully.");
} catch (NoSuchFileException e) {
logger.error("Source file not found: " + e.getMessage());
} catch (IOException e) {
logger.error("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
logger.error("No permission to copy file: " + e.getMessage());
}
}
}
}
} else if(fileUploadType == 2) {
//oss
String endpoint = platformConfigService.getPlatformConfigByKey("aliOss_endpoint").getPlatformValue();
String accessKeyId = platformConfigService.getPlatformConfigByKey("aliOss_accessKeyId").getPlatformValue();
String accessKeySecret = platformConfigService.getPlatformConfigByKey("aliOss_accessKeySecret").getPlatformValue();
String bucketName = platformConfigService.getPlatformConfigByKey("aliOss_bucketName").getPlatformValue();
for(String pathStr: pathList) {
String[] pathArr = pathStr.split(",");
for (int i = 0; i < pathArr.length; i++) {
String path = pathArr[i];
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
String filePathStr = StringUtil.isNotEmpty(filePath)? filePath.substring(1):"";
String sourceObjectKey = filePathStr + "/" + path;
String sourceSmallObjectKey = filePathStr + "-small/" + path;
String destinationObjectKey = DELETED + "/" + sourceObjectKey;
String destinationSmallObjectKey = DELETED + "/" + sourceSmallObjectKey;
this.copySourceToDest(ossClient, bucketName, sourceObjectKey, destinationObjectKey);
this.copySourceToDest(ossClient, bucketName, sourceSmallObjectKey, destinationSmallObjectKey);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
// 关闭OSSClient
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
}
}
/**
*
* @param ossClient
* @param bucketName
* @param sourceObjectKey 源文件路径包括目录和文件名
* @param destinationObjectKey 目标文件路径包括新目录和文件名
*/
public void copySourceToDest(OSS ossClient, String bucketName, String sourceObjectKey, String destinationObjectKey) {
// 复制文件
CopyObjectResult copyResult = ossClient.copyObject(bucketName, sourceObjectKey, bucketName, destinationObjectKey);
// 确认复制成功
if (copyResult != null && copyResult.getETag() != null) {
logger.info("文件复制成功ETag: " + copyResult.getETag());
// 删除源文件
ossClient.deleteObject(bucketName, sourceObjectKey);
logger.info("源文件已删除:" + sourceObjectKey);
} else {
logger.info("文件复制失败");
}
}
public String getDirByPath(String path) {
if(path.lastIndexOf("/")>-1) {
return path.substring(0, path.lastIndexOf("/"));
} else {
return null;
}
}
public BufferedImage getImageMini(InputStream inputStream, int w) throws Exception {
BufferedImage img = ImageIO.read(inputStream);
//获取图片的长和宽