优化
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
package com.baiyue.firstleg.loading.controller;
|
||||
|
||||
import com.baiyue.common.core.api.ApiResponse;
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.firstleg.loading.service.FinanceCostSnapshotService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/internal/firstleg/v1/finance-cost-snapshots")
|
||||
public class InternalFinanceCostSnapshotController {
|
||||
private final FinanceCostSnapshotService service;
|
||||
@Value("${baiyue.security.internal-service-token}") private String expectedToken;
|
||||
public InternalFinanceCostSnapshotController(FinanceCostSnapshotService service) { this.service = service; }
|
||||
|
||||
@GetMapping("/customers/{customerId}/containers/{containerId}")
|
||||
public ApiResponse<FinanceCostSnapshotService.ContainerSnapshot> container(
|
||||
@RequestHeader(value="X-Internal-Service-Token", required=false) String token,
|
||||
@PathVariable long customerId, @PathVariable long containerId) {
|
||||
if (token == null || !MessageDigest.isEqual(token.getBytes(StandardCharsets.UTF_8), expectedToken.getBytes(StandardCharsets.UTF_8))) {
|
||||
throw new BizException(ResultCode.FORBIDDEN, "内部服务令牌无效");
|
||||
}
|
||||
return ApiResponse.success(service.container(customerId, containerId));
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baiyue.firstleg.loading.service;
|
||||
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.firstleg.repository.SmartLoadingRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FinanceCostSnapshotService {
|
||||
private final SmartLoadingRepository repository;
|
||||
public FinanceCostSnapshotService(SmartLoadingRepository repository) { this.repository = repository; }
|
||||
|
||||
public ContainerSnapshot container(long customerId, long loadingPlanId) {
|
||||
if (customerId <= 0 || loadingPlanId <= 0 || repository.plan(loadingPlanId) == null) {
|
||||
throw new BizException(ResultCode.NOT_FOUND, "柜次不存在");
|
||||
}
|
||||
List<OrderVolume> orders = repository.items(loadingPlanId).stream()
|
||||
.filter(item -> item.getAccountId() != null && item.getAccountId() == customerId)
|
||||
.filter(item -> item.getOrderId() != null && item.getOrderId() > 0)
|
||||
.filter(item -> item.getVolumeCbm() != null && item.getVolumeCbm().signum() > 0)
|
||||
.map(item -> new OrderVolume(item.getOrderId(), item.getInboundId(), item.getVolumeCbm()))
|
||||
.toList();
|
||||
if (orders.isEmpty()) throw new BizException(ResultCode.BAD_REQUEST, "柜次没有该客户的有效订单体积");
|
||||
return new ContainerSnapshot(loadingPlanId, customerId, orders);
|
||||
}
|
||||
|
||||
public record ContainerSnapshot(long containerId, long customerId, List<OrderVolume> orders) {}
|
||||
public record OrderVolume(long orderId, Long inboundId, BigDecimal volumeCbm) {}
|
||||
}
|
||||
Reference in New Issue
Block a user