clean code

This commit is contained in:
chenxudong 2025-04-08 16:24:14 +08:00
parent 1be0488bd6
commit fd81961ddb
8 changed files with 83 additions and 125 deletions

View File

@ -4,7 +4,13 @@ import java.util.concurrent.*;
public final class ThreadUtil {
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(1, 1, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(1,
1,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(100),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy());
public static ExecutorService getThreadPool() {
return THREAD_POOL;

View File

@ -1,7 +1,6 @@
package com.electromagnetic.industry.software.manage.aop;
import com.electromagnetic.industry.software.common.annotations.RequiredPermission;
import com.electromagnetic.industry.software.common.cons.ElectromagneticConstants;
import com.electromagnetic.industry.software.common.enums.FilePermission;
import com.electromagnetic.industry.software.common.exception.PermissionDeniedException;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;

View File

@ -136,52 +136,23 @@ public class LoginInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long accessStartTime = (long) request.getSession().getAttribute("accessStartTime");
long accessEndTime = System.currentTimeMillis();
UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class);
if (ObjectUtil.isEmpty(userOperation)) {
return;
}
try {
long accessStartTime = (long) request.getSession().getAttribute("accessStartTime");
long accessEndTime = System.currentTimeMillis();
UserOperation userOperation = ((HandlerMethod) handler).getMethod().getAnnotation(UserOperation.class);
String reqArgs = UserThreadLocal.getReqArgs();
UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo());
List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList();
if (CollUtil.isEmpty(successInfoList)) {
ElectromagneticResult res = user.getResult();
UserAccessLog userAccessLog = new UserAccessLog()
.setId(IdWorker.getSnowFlakeIdString())
.setUserId(user.getUserId())
.setAccessStartTime(DateUtil.date(accessStartTime))
.setAccessEndTime(DateUtil.date(accessEndTime))
.setAccessDuration(accessEndTime - accessStartTime)
.setAction(userOperation.value())
.setRequestUrl(request.getRequestURL().toString())
.setRequestIp(parseIpFromUrl(request.getRequestURL().toString()))
.setReqArgs(reqArgs)
.setRemoteAddr(getRealIp(request))
.setAccessSuccess(true)
.setCreateTime(new Date())
.setOperationModule(userOperation.modelName().key)
.setOperationMsg(res.getErrorMessage())
.setDataId("")
.setParentId("");
if (res != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(res));
if (!res.getSuccess()) {
userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(res.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
}
} else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
if (ObjectUtil.isEmpty(userOperation)) {
return;
}
userAccessLogMapper.insert(userAccessLog);
} else {
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
String reqArgs = UserThreadLocal.getReqArgs();
UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo());
List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList();
if (CollUtil.isEmpty(successInfoList)) {
ElectromagneticResult res = user.getResult();
UserAccessLog userAccessLog = new UserAccessLog()
.setId(IdWorker.getSnowFlakeIdString())
.setUserId(user.getUserId())
@ -196,26 +167,60 @@ public class LoginInterceptor implements HandlerInterceptor {
.setAccessSuccess(true)
.setCreateTime(new Date())
.setOperationModule(userOperation.modelName().key)
.setOperationMsg(accessSuccessInfo.getSuccessMsg())
.setDataId(accessSuccessInfo.getDataId())
.setParentId(accessSuccessInfo.getParentId());
.setOperationMsg(res.getErrorMessage())
.setDataId("")
.setParentId("");
ElectromagneticResult<?> result = user.getResult();
if (result != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(result));
if (!result.getSuccess()) {
if (res != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(res));
if (!res.getSuccess()) {
userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(result.getErrorMessage());
userAccessLog.setOperationMsg(res.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
}
} else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog);
}
}
} else {
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
UserAccessLog userAccessLog = new UserAccessLog()
.setId(IdWorker.getSnowFlakeIdString())
.setUserId(user.getUserId())
.setAccessStartTime(DateUtil.date(accessStartTime))
.setAccessEndTime(DateUtil.date(accessEndTime))
.setAccessDuration(accessEndTime - accessStartTime)
.setAction(userOperation.value())
.setRequestUrl(request.getRequestURL().toString())
.setRequestIp(parseIpFromUrl(request.getRequestURL().toString()))
.setReqArgs(reqArgs)
.setRemoteAddr(getRealIp(request))
.setAccessSuccess(true)
.setCreateTime(new Date())
.setOperationModule(userOperation.modelName().key)
.setOperationMsg(accessSuccessInfo.getSuccessMsg())
.setDataId(accessSuccessInfo.getDataId())
.setParentId(accessSuccessInfo.getParentId());
UserThreadLocal.remove();
ElectromagneticResult<?> result = user.getResult();
if (result != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(result));
if (!result.getSuccess()) {
userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(result.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
}
} else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog);
}
}
} catch (Exception e) {
log.warn("请求后置异常,原因 {}", e.getMessage(), e);
} finally {
UserThreadLocal.remove();
}
}
public Boolean isTokenValid(String tokenStr) {

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
@ -112,9 +113,14 @@ public class ChatService {
documents = new TokenTextSplitter().apply(documentReader.get());
Files.deleteIfExists(tempFile);
}
case "xls" -> {
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".xls";
List<PageFile> pageInfo = OfficeFileUtil.parseXlsByPage(filePath);
case "xls", "xlsx" -> {
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + "." + fileType;
List<PageFile> pageInfo;
if (StrUtil.equals("xlsx", filePath)) {
pageInfo = OfficeFileUtil.parseXlsxByPage(filePath);
} else {
pageInfo = OfficeFileUtil.parseXlsByPage(filePath);
}
for (PageFile pageFile : pageInfo) {
Map<String, Object> metaData = Map.of("page_number", pageFile.getPageNumber(), "file_name", file.getOriginalFilename());
Document document = new Document(pageFile.getContent(), metaData);
@ -122,20 +128,14 @@ public class ChatService {
}
FileUtil.del(filePath);
}
case "xlsx" -> {
String filePath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".xlsx";
List<PageFile> pageInfo = OfficeFileUtil.parseXlsxByPage(filePath);
for (PageFile pageFile : pageInfo) {
Map<String, Object> metaData = Map.of("page_number", pageFile.getPageNumber(), "file_name", file.getOriginalFilename());
Document document = new Document(pageFile.getContent(), metaData);
documents.add(document);
}
FileUtil.del(filePath);
}
case "doc" -> {
String wordPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".doc";
case "doc", "docx", "ppt", "pptx" -> {
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
OfficeFileUtil.doc2pdf(wordPath, pdfPath);
String srcPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + "." + fileType;
if (StrUtil.equals(fileType, "doc") || StrUtil.equals(fileType, "docx")) {
OfficeFileUtil.doc2pdf(srcPath, pdfPath);
} else {
OfficeFileUtil.ppt2pdf(srcPath, pdfPath);
}
Path path = new File(pdfPath).toPath();
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
List<Document> tmp = reader.get();
@ -146,55 +146,7 @@ public class ChatService {
documents.add(doc);
}
Files.deleteIfExists(path);
FileUtil.del(wordPath);
}
case "docx" -> {
String wordPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".docx";
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
OfficeFileUtil.doc2pdf(wordPath, pdfPath);
Path path = new File(pdfPath).toPath();
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
List<Document> tmp = reader.get();
for (Document document : tmp) {
Map<String, Object> metadata = document.getMetadata();
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
documents.add(doc);
}
Files.deleteIfExists(path);
FileUtil.del(wordPath);
}
case "ppt" -> {
String pptPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".ppt";
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
OfficeFileUtil.ppt2pdf(pptPath, pdfPath);
Path path = new File(pdfPath).toPath();
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
List<Document> tmp = reader.get();
for (Document document : tmp) {
Map<String, Object> metadata = document.getMetadata();
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
documents.add(doc);
}
Files.deleteIfExists(path);
FileUtil.del(pptPath);
}
case "pptx" -> {
String pptxPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pptx";
String pdfPath = elePropertyConfig.getEleTmpPath() + File.separator + fileMd5 + ".pdf";
OfficeFileUtil.ppt2pdf(pptxPath, pdfPath);
Path path = new File(pdfPath).toPath();
PagePdfDocumentReader reader = new PagePdfDocumentReader(String.valueOf(path.toUri().toURL()));
List<Document> tmp = reader.get();
for (Document document : tmp) {
Map<String, Object> metadata = document.getMetadata();
Map<String, Object> metaData = Map.of("page_number", metadata.get("page_number"), "file_name", file.getOriginalFilename());
Document doc = new Document(Objects.requireNonNull(document.getText()), metaData);
documents.add(doc);
}
Files.deleteIfExists(path);
FileUtil.del(pptxPath);
FileUtil.del(srcPath);
}
case "pdf" -> {
Path tempFile = saveUploadedFileToTemp(file);

View File

@ -29,7 +29,6 @@ import com.electromagnetic.industry.software.manage.service.FileFormatService;
import com.electromagnetic.industry.software.manage.service.FileSystemService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

View File

@ -27,7 +27,6 @@ import com.electromagnetic.industry.software.manage.pojo.resp.ProjectVO;
import com.electromagnetic.industry.software.manage.service.EdPrjService;
import com.electromagnetic.industry.software.manage.service.FileSystemService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

View File

@ -19,7 +19,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service

View File

@ -37,7 +37,6 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Component
@Slf4j