feat: close overseas wallet and pallet cost flow
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
package com.baiyue.lastleg.procurement.gateway;
|
||||
|
||||
import com.baiyue.lastleg.config.LastlegRestClientFactory;
|
||||
import com.baiyue.lastleg.config.LastlegRestClientProperties;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.MultipartBodyBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class HttpProcurementFileGateway implements ProcurementFileGateway {
|
||||
private final RestClient.Builder builder;
|
||||
private final LastlegRestClientFactory factory;
|
||||
private final LastlegRestClientProperties properties;
|
||||
@Value("${baiyue.clients.file.url:http://127.0.0.1:9205}") private String baseUrl;
|
||||
@Value("${baiyue.security.internal-service-token:}") private String token;
|
||||
private RestClient client;
|
||||
@PostConstruct void initialize() { client = factory.build(builder, baseUrl, properties.getProcurementFile()); }
|
||||
|
||||
@Override
|
||||
public UploadedFile upload(String customerId, long orderId, long operatorId, String category, MultipartFile file) {
|
||||
if (file == null || file.isEmpty()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "请选择文件");
|
||||
MultipartBodyBuilder body = new MultipartBodyBuilder();
|
||||
try {
|
||||
body.part("file", new ByteArrayResource(file.getBytes()) {
|
||||
@Override public String getFilename() { return file.getOriginalFilename(); }
|
||||
}).contentType(file.getContentType() == null ? MediaType.APPLICATION_OCTET_STREAM : MediaType.parseMediaType(file.getContentType()));
|
||||
} catch (Exception exception) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "采购文件无法读取", exception); }
|
||||
String businessType = "RECEIPT".equals(category) ? "OVERSEAS_PROCUREMENT_RECEIPT" : "OVERSEAS_PROCUREMENT_VOUCHER";
|
||||
Envelope response = client.post().uri("/internal/file/v1/business-files/{tenantId}/{businessType}/{businessId}",
|
||||
Long.parseLong(customerId), businessType, orderId)
|
||||
.header("X-Internal-Service-Token", token).header("X-Operator-Id", String.valueOf(operatorId))
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA).body(body.build()).retrieve()
|
||||
.onStatus(HttpStatusCode::isError, (request, result) -> { throw new ResponseStatusException(
|
||||
result.getStatusCode().is4xxClientError() ? HttpStatus.BAD_REQUEST : HttpStatus.SERVICE_UNAVAILABLE,
|
||||
"采购文件上传失败"); }).body(Envelope.class);
|
||||
if (response == null || !"200".equals(response.code()) || response.data() == null || response.data().fileId() == null)
|
||||
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "文件服务未返回采购文件元数据");
|
||||
Metadata data = response.data();
|
||||
return new UploadedFile(data.fileId(), data.fileName(), data.contentType(), data.sizeBytes() == null ? 0 : data.sizeBytes());
|
||||
}
|
||||
private record Envelope(String code, String message, Metadata data) { }
|
||||
private record Metadata(Long fileId, String fileName, String contentType, Long sizeBytes) { }
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baiyue.lastleg.procurement.gateway;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
public interface ProcurementFileGateway {
|
||||
UploadedFile upload(String customerId, long orderId, long operatorId, String category, MultipartFile file);
|
||||
record UploadedFile(long fileId, String fileName, String contentType, long sizeBytes) { }
|
||||
}
|
||||
Reference in New Issue
Block a user