refactor(file): 优化文件上传逻辑并添加组织ID支持

- 在 FileService 中添加了新的 upload 方法,支持传入组织ID参数
- 修改了现有的 upload 方法,使其支持组织ID
- 在 SmartCamCarLicenseSnapshotDataService 中为捕获的图片添加了组织ID设置
- 优化了日期格式解析
- 更新了 SmartCamCarLicenseSnapshotDataDto 中的图片字段类型
- 修复了 InvoiceService 中的 findOne 方法返回值问题
This commit is contained in:
2025-02-25 23:12:52 +08:00
parent d9fda30c3d
commit c052ad24c9
4 changed files with 36 additions and 14 deletions

View File

@@ -174,7 +174,8 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
extension =
config.getMimeRepository()
.getRegisteredMimeType(fileEntity.getMimeType())
.getExtension().replace(".", "");
.getExtension()
.replace(".", "");
}
} catch (Exception e) {
log.warn("获取文件扩展名失败", e);
@@ -224,11 +225,8 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
}
@SneakyThrows
public FileDto upload(
InputStream file,
String name,
String dir) {
return upload(file, name, dir, null, null, null);
public FileDto upload(InputStream file, String name, String dir, String organizationId) {
return upload(file, name, dir, null, null, null, organizationId);
}
@SneakyThrows
@@ -239,11 +237,27 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
String sortKey,
String code,
String remarks) {
return upload(file, name, dir, sortKey, code, remarks, null);
}
@SneakyThrows
public FileDto upload(
InputStream file,
String name,
String dir,
String sortKey,
String code,
String remarks,
String organizationId) {
FileEntity fileEntity = new FileEntity();
fileEntity.setFileName(name);
fileEntity.setFileSize(Long.valueOf(file.available()));
fileEntity.setCode(code);
fileEntity.setRemarks(remarks);
if (organizationId != null) {
fileEntity.setOrganizationId(organizationId);
}
try {
fileEntity.setSortKey(Integer.parseInt(sortKey));

View File

@@ -39,6 +39,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.util.Optional;
@Service
@Slf4j
@@ -120,7 +121,7 @@ public class InvoiceService extends BaseService<InvoiceEntity, InvoiceRepository
log.info("发票 已经存在", invoice.getInvoiceCode(), invoice.getInvoiceNumber());
return this.repository.findOne(
Optional<InvoiceEntity> one = this.repository.findOne(
new Specification<InvoiceEntity>() {
@Override
public Predicate toPredicate(
@@ -138,6 +139,12 @@ public class InvoiceService extends BaseService<InvoiceEntity, InvoiceRepository
Ctx.currentUser().getOrganizationId()));
}
});
if (one.isPresent()) {
return getById(one.get().getId());
}else {
return null;
}
}
if (CollectionUtils.isNotEmpty(invoice.getInvoiceDetails())) {

View File

@@ -1,6 +1,7 @@
package cn.lihongjie.coal.smartCamCarLicenseSnapshotData.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.file.dto.FileDto;
import cn.lihongjie.coal.smartCam.dto.SmartCamDto;
import jakarta.persistence.ManyToOne;
@@ -124,7 +125,7 @@ public class SmartCamCarLicenseSnapshotDataDto extends OrgCommonDto {
@Comment("车牌背景图片信息 原始图片MD5值")
private String backgroundImagePictureMd5;
@ManyToOne private String captureImage;
@ManyToOne private FileDto captureImage;
@ManyToOne private String backgroundImage;
@ManyToOne private FileDto backgroundImage;
}

View File

@@ -133,11 +133,11 @@ public class SmartCamCarLicenseSnapshotDataService
entity.setInfoEventId(infoNode.path("eventId").asInt());
entity.setInfoTime(infoNode.path("time").asText());
// ISO8601
// ISO8601 2025-02-25T21:34:34+08:00
entity.setInfoTimeObj(
LocalDateTime.parse(
infoNode.path("time").asText(),
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssXXX")));
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")));
if (ObjectUtils.notEqual(entity.getOperator(), "CarLicenseSnapshot")) {
log.info("Event type is not CarLicenseSnapshot, ignore it {}", json);
@@ -210,7 +210,7 @@ public class SmartCamCarLicenseSnapshotDataService
entity.setBackgroundImageHeight(backgroundImageNode.path("height").asInt());
entity.setBackgroundImagePictureLength(backgroundImageNode.path("pictureLength").asInt());
entity.setBackgroundImagePictureMd5(backgroundImageNode.path("pictureMd5").asText());
entity.setOrganizationId(smartCam.getOrganizationId());
String captureBase64 =
StringUtils.defaultIfBlank(captureImageNode.path("picture").asText(), "")
.replace("data:image/jpg;base64,", "");
@@ -226,7 +226,7 @@ public class SmartCamCarLicenseSnapshotDataService
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
+ "_"
+ "plate",
"car_license_snapshot");
"car_license_snapshot", smartCam.getOrganizationId());
entity.setCaptureImage(em.getReference(FileEntity.class, captureDto.getId()));
}
@@ -247,7 +247,7 @@ public class SmartCamCarLicenseSnapshotDataService
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
+ "_"
+ "bg",
"car_license_snapshot");
"car_license_snapshot", smartCam.getOrganizationId());
entity.setBackgroundImage(em.getReference(FileEntity.class, backgroundDto.getId()));
}