feat(fleet): enforce reviewed trips and original fee coverage for dispatch

This commit is contained in:
2026-10-03 04:18:52 +08:00
parent c50e9f97d1
commit 5e683da3b9
162 changed files with 9642 additions and 218 deletions
@@ -0,0 +1,31 @@
package com.baiyue.finance.controller;
import com.baiyue.common.core.api.ApiResponse;
import com.baiyue.finance.dto.DeliveryFeeCoverageModels;
import com.baiyue.finance.service.DeliveryFeeCoverageQueryService;
import com.baiyue.finance.service.FinanceEventException;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/internal/finance/v1/delivery-fee-coverage")
public class InternalDeliveryFeeCoverageController {
private final DeliveryFeeCoverageQueryService service;
@Value("${baiyue.security.internal-service-token}") private String expectedToken;
@PostMapping("/sources")
public ApiResponse<List<DeliveryFeeCoverageModels.Fee>> sources(
@RequestHeader(value = "X-Internal-Service-Token", required = false) String token,
@Valid @RequestBody DeliveryFeeCoverageModels.Query request) {
if (token == null || expectedToken == null || expectedToken.isBlank()
|| !MessageDigest.isEqual(token.getBytes(StandardCharsets.UTF_8), expectedToken.getBytes(StandardCharsets.UTF_8)))
throw new FinanceEventException("FIN-EVT-401", "内部服务令牌无效");
return FinanceResponses.success(service.query(request));
}
}
@@ -0,0 +1,14 @@
package com.baiyue.finance.dto;
import jakarta.validation.constraints.*;
import java.math.BigDecimal;
import java.util.List;
public final class DeliveryFeeCoverageModels {
private DeliveryFeeCoverageModels() { }
public record Query(@NotNull @Positive Long customerId,
@NotEmpty @Size(max = 2000) List<@NotNull @Positive Long> inboundIds) { }
public record Fee(long financeFeeId, long sourceFeeId, String sourceType, int sourceVersion,
long customerId, long sourceInboundId, String inboundCode, String feeItemCode, String feeItemName,
String currency, BigDecimal amount, String sourceHash, String status) { }
}
@@ -0,0 +1,40 @@
package com.baiyue.finance.service;
import com.baiyue.finance.dto.DeliveryFeeCoverageModels;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
/** Finance eligibility only. The business operator must explicitly confirm which services are included. */
@Service
@RequiredArgsConstructor
public class DeliveryFeeCoverageQueryService {
private final JdbcTemplate jdbc;
@Transactional(readOnly = true)
public List<DeliveryFeeCoverageModels.Fee> query(DeliveryFeeCoverageModels.Query request) {
return new NamedParameterJdbcTemplate(jdbc).query("""
SELECT f.id,f.source_fee_id,f.source_type,f.source_fee_version,f.customer_id,f.inbound_id,
CASE WHEN NULLIF(TRIM(f.inbound_code),'') IS NOT NULL THEN f.inbound_code
WHEN f.source_type='INBOUND_FEE' THEN f.source_no ELSE NULL END AS coverage_inbound_code,
f.fee_item_code,f.fee_item_name,f.currency_code,f.amount,f.source_hash,f.status
FROM fin_fee_source f
WHERE f.customer_id=:customerId AND f.inbound_id IN (:inboundIds)
AND f.source_system='FIRSTLEG' AND f.source_type IN ('INBOUND_FEE','CONTAINER_FEE')
AND f.direction='RECEIVABLE' AND f.review_status='APPROVED'
AND f.status IN ('AVAILABLE','SETTLED')
AND NOT EXISTS (SELECT 1 FROM fin_fee_source newer
WHERE newer.source_system=f.source_system AND newer.source_type=f.source_type
AND newer.source_fee_id=f.source_fee_id AND newer.source_fee_version>f.source_fee_version)
ORDER BY f.inbound_id,f.fee_item_code,f.source_fee_id
""", Map.of("customerId", request.customerId(), "inboundIds", request.inboundIds().stream().distinct().toList()),
(row, index) -> new DeliveryFeeCoverageModels.Fee(row.getLong("id"), row.getLong("source_fee_id"),
row.getString("source_type"), row.getInt("source_fee_version"), row.getLong("customer_id"), row.getLong("inbound_id"),
row.getString("coverage_inbound_code"), row.getString("fee_item_code"), row.getString("fee_item_name"),
row.getString("currency_code"), row.getBigDecimal("amount"), row.getString("source_hash"), row.getString("status")));
}
}