新需求更新 同步更新
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
||||
package com.nanri.aiimage.modules.filetemplate;
|
||||
|
||||
import com.nanri.aiimage.common.util.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.modules.filetemplate.ModuleTemplateService.TemplateDownload;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/module-templates")
|
||||
@Tag(name = "模块输入模板", description = "下载各业务模块的固定 Excel 输入模板")
|
||||
public class ModuleTemplateController {
|
||||
|
||||
private final ModuleTemplateService moduleTemplateService;
|
||||
|
||||
@GetMapping("/{moduleCode}/download")
|
||||
@Operation(summary = "下载模块输入模板")
|
||||
@ApiResponses({
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(
|
||||
responseCode = "200",
|
||||
description = "Excel 模板文件",
|
||||
content = @Content(
|
||||
mediaType = ModuleTemplateService.XLSX_CONTENT_TYPE,
|
||||
schema = @Schema(type = "string", format = "binary"))),
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模块模板不存在"),
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "503", description = "模板存储暂不可用")
|
||||
})
|
||||
public ResponseEntity<byte[]> download(
|
||||
@Parameter(description = "模块编码", required = true, example = "publish")
|
||||
@PathVariable String moduleCode) {
|
||||
TemplateDownload download = moduleTemplateService.download(moduleCode);
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, DownloadHeaderUtil.contentDisposition(download.filename()))
|
||||
.contentType(MediaType.parseMediaType(download.contentType()))
|
||||
.contentLength(download.content().length)
|
||||
.body(download.content());
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.nanri.aiimage.modules.filetemplate;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ModuleTemplateRegistry {
|
||||
|
||||
private static final String RESOURCE_PREFIX = "templates/module-input/";
|
||||
private static final String OBJECT_PREFIX = "input/";
|
||||
|
||||
private static final List<ModuleTemplate> TEMPLATES = List.of(
|
||||
template("publish", "publish.xlsx", "上架 文档格式.xlsx"),
|
||||
template("delete-brand", "delete-brand.xlsx", "删除指定待售 文档格式.xlsx"),
|
||||
template("appearance-patent", "appearance-patent.xlsx", "外观专利检测 文档格式.xlsx"),
|
||||
template("price-track", "price-track.xlsx", "指定ASIN跟价 文档格式.xlsx"),
|
||||
template("collect-data", "collect-data.xlsx", "数据采集 文档格式.xlsx"),
|
||||
template("similar-asin", "similar-asin.xlsx", "货源查询 文档格式.xlsx")
|
||||
);
|
||||
|
||||
private final Map<String, ModuleTemplate> templatesByCode = TEMPLATES.stream()
|
||||
.collect(Collectors.toUnmodifiableMap(
|
||||
ModuleTemplate::moduleCode,
|
||||
Function.identity()));
|
||||
|
||||
public Optional<ModuleTemplate> find(String moduleCode) {
|
||||
if (moduleCode == null || moduleCode.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(templatesByCode.get(moduleCode.trim().toLowerCase(Locale.ROOT)));
|
||||
}
|
||||
|
||||
public List<ModuleTemplate> templates() {
|
||||
return TEMPLATES;
|
||||
}
|
||||
|
||||
private static ModuleTemplate template(String moduleCode, String resourceFilename, String downloadFilename) {
|
||||
return new ModuleTemplate(
|
||||
moduleCode,
|
||||
RESOURCE_PREFIX + resourceFilename,
|
||||
OBJECT_PREFIX + resourceFilename,
|
||||
downloadFilename);
|
||||
}
|
||||
|
||||
public record ModuleTemplate(
|
||||
String moduleCode,
|
||||
String resourcePath,
|
||||
String objectKey,
|
||||
String downloadFilename) {
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.nanri.aiimage.modules.filetemplate;
|
||||
|
||||
import com.nanri.aiimage.config.OssProperties;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.filetemplate.ModuleTemplateRegistry.ModuleTemplate;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ModuleTemplateService {
|
||||
|
||||
public static final String XLSX_CONTENT_TYPE =
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
|
||||
private final ModuleTemplateRegistry registry;
|
||||
private final OssProperties ossProperties;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
public TemplateDownload download(String moduleCode) {
|
||||
ModuleTemplate template = registry.find(moduleCode)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "模板不存在"));
|
||||
try {
|
||||
ensureStored(template);
|
||||
byte[] content = ossStorageService.readObjectBytes(templateBucket(), template.objectKey());
|
||||
return new TemplateDownload(template.downloadFilename(), XLSX_CONTENT_TYPE, content);
|
||||
} catch (ResponseStatusException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw storageUnavailable(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public int synchronizeAll() {
|
||||
try {
|
||||
String bucket = templateBucket();
|
||||
ossStorageService.ensureBucketExists(bucket);
|
||||
int uploaded = 0;
|
||||
for (ModuleTemplate template : registry.templates()) {
|
||||
if (!ossStorageService.objectExists(bucket, template.objectKey())) {
|
||||
uploadResource(bucket, template);
|
||||
uploaded++;
|
||||
}
|
||||
}
|
||||
return uploaded;
|
||||
} catch (ResponseStatusException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw storageUnavailable(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureStored(ModuleTemplate template) {
|
||||
String bucket = templateBucket();
|
||||
ossStorageService.ensureBucketExists(bucket);
|
||||
if (!ossStorageService.objectExists(bucket, template.objectKey())) {
|
||||
uploadResource(bucket, template);
|
||||
}
|
||||
}
|
||||
|
||||
private void uploadResource(String bucket, ModuleTemplate template) {
|
||||
ClassPathResource resource = new ClassPathResource(template.resourcePath());
|
||||
try (InputStream input = resource.getInputStream()) {
|
||||
ossStorageService.uploadBytes(bucket, template.objectKey(), input.readAllBytes(), XLSX_CONTENT_TYPE);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("failed to load module template resource: " + template.resourcePath(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private String templateBucket() {
|
||||
String bucket = ossProperties.getTemplateBucket();
|
||||
if (bucket == null || bucket.isBlank()) {
|
||||
throw new IllegalStateException("template bucket is not configured");
|
||||
}
|
||||
return bucket.trim();
|
||||
}
|
||||
|
||||
private ResponseStatusException storageUnavailable(Exception cause) {
|
||||
return new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "模板存储暂不可用", cause);
|
||||
}
|
||||
|
||||
public record TemplateDownload(String filename, String contentType, byte[] content) {
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.nanri.aiimage.modules.filetemplate;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ModuleTemplateStorageInitializer {
|
||||
|
||||
private final ModuleTemplateService moduleTemplateService;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void synchronize() {
|
||||
try {
|
||||
int uploaded = moduleTemplateService.synchronizeAll();
|
||||
log.info("[module-template] startup synchronization complete uploaded={}", uploaded);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[module-template] startup synchronization failed; downloads will retry lazily: {}",
|
||||
ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user