1.新增 log 日志上传功能

This commit is contained in:
2025-08-25 15:17:41 +08:00
parent 3f1c4df78a
commit 5ea65a8d25
10 changed files with 265 additions and 2 deletions

View File

@@ -0,0 +1,70 @@
package com.yupi.springbootinit.service.impl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.mapper.ServerAiLogMapper;
import com.yupi.springbootinit.model.entity.ServerAiLog;
import com.yupi.springbootinit.service.ServerAiLogService;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
/*
* @author: ziin
* @date: 2025/8/25 13:37
*/
@Service
@Slf4j
public class ServerAiLogServiceImpl extends ServiceImpl<ServerAiLogMapper, ServerAiLog> implements ServerAiLogService{
@Resource
private ServerAiLogMapper serverAiLogMapper;
@Value("${ai_log_path}")
private String basePath;
@Override
public Boolean saveLog(MultipartFile file, Long tenantId, Long userId) {
if (file.isEmpty()) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"上传文件为空");
}
Path dir = Paths.get(basePath, tenantId.toString(), userId.toString());
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"创建目录失败");
}
}
String originalFilename = file.getOriginalFilename();
Path target = dir.resolve(originalFilename);
try {
Files.copy(file.getInputStream(), target, StandardCopyOption.REPLACE_EXISTING);
log.info("文件名{},文件上传路径{},",originalFilename,target);
} catch (IOException e) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存文件失败");
}
ServerAiLog log = new ServerAiLog();
log.setLogName(originalFilename);
log.setLogFilePath(target.toString());
log.setLogTenantId(tenantId);
log.setLogUserId(userId);
log.setCreator(String.valueOf(userId)); // 可替换为当前登录用户
return serverAiLogMapper.insert(log) == 1;
}
}