Commit 643d6e5d by 章家宝

feat(report):车辆库存分析改成从收车端获取数据

1 parent bd73019f
......@@ -20,6 +20,7 @@ import java.util.List;
* 报表数据接口(供收车端调用)
* <p>
* 提供收车端所需的原始数据查询接口,按车辆明细返回,不做聚合计算。
* 所有车辆维度数据统一过滤 vehicleSource IN (RECEIVE收车端推送, AUCTION拍卖端自建)。
* 所有接口均有日志记录,便于排查问题。
* </p>
*
......@@ -91,13 +92,12 @@ public class ReportDataController {
return reportDataService.getDailyDisposalSnapshot(date);
}
@GetMapping("/inventory/summary")
@Operation(summary = "在库车辆汇总", description = "统计指定日期仍处于在库状态(已入库且未处置完成)的车辆台数、金额、平均在库天数、超期台数")
public InventorySummaryVO getInventorySummary(
@Parameter(description = "快照日期(yyyy-MM-dd)", required = true)
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
@Parameter(description = "超期天数阈值", required = true) @RequestParam int overdueDays) {
log.info("【报表接口】在库车辆汇总 - date={}, overdueDays={}", date, overdueDays);
return reportDataService.getInventorySummary(date, overdueDays);
@GetMapping("/vehicle-sync-time")
@Operation(summary = "车辆同步时间", description = "按评估单号批量返回车辆档案生成时间(即收车端推送/同步到拍卖端的时刻),供在库周转天数计算")
public List<VehicleSyncTimeVO> getVehicleSyncTimes(
@Parameter(description = "评估单号列表(逗号分隔)", required = true)
@RequestParam List<String> evalOrderNos) {
log.info("【报表接口】车辆同步时间 - evalOrderNos数量={}", evalOrderNos == null ? 0 : evalOrderNos.size());
return reportDataService.getVehicleSyncTimes(evalOrderNos);
}
}
\ No newline at end of file
......@@ -21,16 +21,18 @@ public interface AppraisePriceDao extends BaseRepository<AppraisePrice, Integer>
List<AppraisePrice> findByVehicle_VehicleIdAndAuditStateNotOrderByCreateTimeDesc(int vehicleId, AuditState auditState);
/**
* 按月查询定价完成的记录(含车辆信息)
* 按月查询定价完成的记录(含车辆信息,仅限指定车辆来源)
* 用于收车端日报拉取
*
* @param year 年份
* @param month 月份
* @param auditState 审核状态
* @param vehicleSources 车辆来源集合
* @return 定价记录列表
*/
@EntityGraph(attributePaths = {"vehicle"})
@Query("select a from AppraisePrice a where year(a.createTime) = ?1 and month(a.createTime) = ?2 and a.auditState = ?3")
List<AppraisePrice> findByCreateTimeYearAndMonthAndAuditState(int year, int month, AuditState auditState);
@Query("select a from AppraisePrice a where year(a.createTime) = ?1 and month(a.createTime) = ?2 and a.auditState = ?3 and a.vehicle.vehicleSource in ?4")
List<AppraisePrice> findByCreateTimeYearAndMonthAndAuditState(int year, int month, AuditState auditState, List<Vehicle.VehicleSource> vehicleSources);
/**
* 批量查询车辆的审核通过定价记录(按定价时间倒序)
......
......@@ -216,27 +216,30 @@ public interface AuctionOrderDao extends BaseRepository<AuctionOrder, Integer>,
Optional<ProfitRadioProjection> calOrderProfitRadio(AuctionOrder.OrderState orderState);
/**
* 按月查询处置成交明细(含车辆和拍场信息)
* 按月查询处置成交明细(含车辆和拍场信息,仅限指定车辆来源)
* 用于收车端日报拉取
*
* @param year 年份
* @param month 月份
* @param orderState 订单状态
* @param vehicleSources 车辆来源集合
* @return 拍卖订单列表
*/
@EntityGraph(attributePaths = {"vehicle", "auction"})
@Query("select o from AuctionOrder o where year(o.transferredTime) = ?1 and month(o.transferredTime) = ?2 and o.orderState = ?3")
List<AuctionOrder> findByTransferredTimeYearAndMonth(int year, int month, AuctionOrder.OrderState orderState);
@Query("select o from AuctionOrder o where year(o.transferredTime) = ?1 and month(o.transferredTime) = ?2 and o.orderState = ?3 and o.vehicle.vehicleSource in ?4")
List<AuctionOrder> findByTransferredTimeYearAndMonth(int year, int month, AuctionOrder.OrderState orderState, List<Vehicle.VehicleSource> vehicleSources);
/**
* 按日查询处置快照(含车辆信息)
* 按日查询处置快照(含车辆信息,仅限指定车辆来源)
* 用于收车端每日快照拉取
*
* @param date 日期
* @param vehicleSources 车辆来源集合
* @return 拍卖订单列表
*/
@EntityGraph(attributePaths = {"vehicle"})
@Query("select o from AuctionOrder o where date(o.createTime) = ?1")
List<AuctionOrder> findByCreateTimeDate(java.time.LocalDate date);
@Query("select o from AuctionOrder o where date(o.createTime) = ?1 and o.vehicle.vehicleSource in ?2")
List<AuctionOrder> findByCreateTimeDate(java.time.LocalDate date, List<Vehicle.VehicleSource> vehicleSources);
}
interface AuctionOrderDaoPlus {
......
......@@ -118,16 +118,17 @@ public interface AuctionVehicleDao extends BaseRepository<AuctionVehicle, Intege
List<VehicleIdCountProjection> countByVehicleIdIn(List<Integer> vehicleIds);
/**
* 按拍场名称模糊查询拍卖车辆(用于按拍场名统计)
* 按拍场名称模糊查询拍卖车辆(用于按拍场名统计,仅限指定车辆来源)
*
* @param auctionName 拍场名称模糊关键字
* @param year 年份
* @param month 月份
* @param vehicleSources 车辆来源集合
* @return 拍卖车辆列表
*/
@EntityGraph(attributePaths = {"vehicle", "auction"})
@Query("select av from AuctionVehicle av where av.auction.auctionName like concat('%', ?1, '%') and year(av.auctionStartTime) = ?2 and month(av.auctionStartTime) = ?3")
List<AuctionVehicle> findByAuctionNameLikeAndYearMonth(String auctionName, int year, int month);
@Query("select av from AuctionVehicle av where av.auction.auctionName like concat('%', ?1, '%') and year(av.auctionStartTime) = ?2 and month(av.auctionStartTime) = ?3 and av.vehicle.vehicleSource in ?4")
List<AuctionVehicle> findByAuctionNameLikeAndYearMonth(String auctionName, int year, int month, List<Vehicle.VehicleSource> vehicleSources);
/**
* 按月查询某车辆的所有拍卖记录(按拍卖开始时间升序)
......
......@@ -20,7 +20,6 @@ import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -32,6 +31,7 @@ import java.util.stream.Collectors;
* 报表数据查询服务(拍卖端原始数据接口)
* <p>
* 提供收车端所需的原始数据查询接口,按车辆明细返回,不做聚合计算。
* 所有车辆维度数据统一过滤 vehicleSource IN (RECEIVE收车端推送, AUCTION拍卖端自建)。
* 所有接口均有日志记录,便于排查问题。
* </p>
*
......@@ -43,6 +43,9 @@ import java.util.stream.Collectors;
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ReportDataService {
private static final List<Vehicle.VehicleSource> REPORT_VEHICLE_SOURCES =
List.of(Vehicle.VehicleSource.RECEIVE, Vehicle.VehicleSource.AUCTION);
@Resource
private AuctionOrderDao auctionOrderDao;
......@@ -65,7 +68,7 @@ public class ReportDataService {
public List<MonthlyDisposalDetailVO> getMonthlyDisposalDetail(int year, int month) {
log.info("【报表数据查询】按月处置成交明细 - year={}, month={}", year, month);
List<AuctionOrder> orders = auctionOrderDao.findByTransferredTimeYearAndMonth(year, month, AuctionOrder.OrderState.COMPLETED);
List<AuctionOrder> orders = auctionOrderDao.findByTransferredTimeYearAndMonth(year, month, AuctionOrder.OrderState.COMPLETED, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(orders)) {
log.info("【报表数据查询】按月处置成交明细 - 无数据, year={}, month={}", year, month);
return new ArrayList<>();
......@@ -114,7 +117,7 @@ public class ReportDataService {
public List<MonthlyPricingDetailVO> getMonthlyPricingDetail(int year, int month) {
log.info("【报表数据查询】按月定价明细 - year={}, month={}", year, month);
List<AppraisePrice> prices = appraisePriceDao.findByCreateTimeYearAndMonthAndAuditState(year, month, AuditState.AUDITED);
List<AppraisePrice> prices = appraisePriceDao.findByCreateTimeYearAndMonthAndAuditState(year, month, AuditState.AUDITED, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(prices)) {
log.info("【报表数据查询】按月定价明细 - 无数据, year={}, month={}", year, month);
return new ArrayList<>();
......@@ -146,7 +149,7 @@ public class ReportDataService {
public List<MonthlyVehicleArchiveVO> getMonthlyVehicleArchive(int year, int month) {
log.info("【报表数据查询】按月车辆档案 - year={}, month={}", year, month);
List<Vehicle> vehicles = vehicleDao.findByTransferredTimeYearAndMonth(year, month);
List<Vehicle> vehicles = vehicleDao.findByTransferredTimeYearAndMonth(year, month, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(vehicles)) {
log.info("【报表数据查询】按月车辆档案 - 无数据, year={}, month={}", year, month);
return new ArrayList<>();
......@@ -180,7 +183,7 @@ public class ReportDataService {
public List<AuctionNameStatVO> getAuctionNameStat(int year, int month, String auctionName) {
log.info("【报表数据查询】按拍场名统计 - year={}, month={}, auctionName={}", year, month, auctionName);
List<AuctionVehicle> list = auctionVehicleDao.findByAuctionNameLikeAndYearMonth(auctionName, year, month);
List<AuctionVehicle> list = auctionVehicleDao.findByAuctionNameLikeAndYearMonth(auctionName, year, month, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(list)) {
log.info("【报表数据查询】按拍场名统计 - 无数据, year={}, month={}, auctionName={}", year, month, auctionName);
return new ArrayList<>();
......@@ -212,7 +215,7 @@ public class ReportDataService {
log.info("【报表数据查询】按拍卖次数区分成交 - year={}, month={}", year, month);
// 先获取当月所有处置完成的车辆ID
List<AuctionOrder> orders = auctionOrderDao.findByTransferredTimeYearAndMonth(year, month, AuctionOrder.OrderState.COMPLETED);
List<AuctionOrder> orders = auctionOrderDao.findByTransferredTimeYearAndMonth(year, month, AuctionOrder.OrderState.COMPLETED, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(orders)) {
log.info("【报表数据查询】按拍卖次数区分成交 - 当月无处置完成数据, year={}, month={}", year, month);
return new ArrayList<>();
......@@ -275,7 +278,7 @@ public class ReportDataService {
public List<DailyDisposalSnapshotVO> getDailyDisposalSnapshot(LocalDate date) {
log.info("【报表数据查询】按日处置快照 - date={}", date);
List<AuctionOrder> orders = auctionOrderDao.findByCreateTimeDate(date);
List<AuctionOrder> orders = auctionOrderDao.findByCreateTimeDate(date, REPORT_VEHICLE_SOURCES);
if (CollectionUtils.isEmpty(orders)) {
log.info("【报表数据查询】按日处置快照 - 无数据, date={}", date);
return new ArrayList<>();
......@@ -299,52 +302,31 @@ public class ReportDataService {
}
/**
* 统计指定日期的在库车辆汇总(已入库且未处置完成)
* <p>在库口径:入库时间早于快照日期次日零点,且无早于快照日期次日零点的处置完成时间(transferredTime),
* 排除待入库/已取消状态。在库天数=快照日期-入库日期,均值四舍五入取整;
* 超期=在库天数超过 overdueDays 阈值;金额为收购价合计。</p>
* 按评估单号批量查询车辆同步时间
*
* @param date 快照日期(yyyy-MM-dd)
* @param overdueDays 超期天数阈值(由调用方传入)
* @return 在库车辆汇总
* @param evalOrderNos 评估单号列表
* @return 同步时间列表(仅返回拍卖端存在的车辆档案)
*/
public InventorySummaryVO getInventorySummary(LocalDate date, int overdueDays) {
log.info("【报表数据查询】在库车辆汇总 - date={}, overdueDays={}", date, overdueDays);
LocalDateTime snapshotEnd = date.plusDays(1).atStartOfDay();
List<Vehicle> vehicles = vehicleDao.findInStockBefore(snapshotEnd,
List.of(Vehicle.VehicleState.INVENTORY, Vehicle.VehicleState.CANCELED));
int totalQuantity = vehicles.size();
BigDecimal totalAmount = BigDecimal.ZERO;
long daySum = 0;
int overdueQuantity = 0;
for (Vehicle vehicle : vehicles) {
if (vehicle.getEvaluationTransaction() != null) {
totalAmount = totalAmount.add(BigDecimal.valueOf(
vehicle.getEvaluationTransaction().getPurchasePrice()));
}
if (vehicle.getWarehousingTime() != null) {
long days = Math.max(0, ChronoUnit.DAYS.between(
vehicle.getWarehousingTime().toLocalDate(), date));
daySum += days;
if (days > overdueDays) {
overdueQuantity++;
}
}
public List<VehicleSyncTimeVO> getVehicleSyncTimes(List<String> evalOrderNos) {
log.info("【报表数据查询】车辆同步时间 - evalOrderNos数量={}", CollectionUtils.isEmpty(evalOrderNos) ? 0 : evalOrderNos.size());
if (CollectionUtils.isEmpty(evalOrderNos)) {
return new ArrayList<>();
}
List<String> distinctOrderNos = evalOrderNos.stream().filter(Objects::nonNull).distinct().toList();
if (distinctOrderNos.isEmpty()) {
return new ArrayList<>();
}
InventorySummaryVO result = InventorySummaryVO.builder()
.totalQuantity(totalQuantity)
.totalAmount(totalAmount)
.avgInStockDays(vehicles.isEmpty() ? 0
: (int) Math.round((double) daySum / vehicles.size()))
.overdueQuantity(overdueQuantity)
.build();
log.info("【报表数据查询】在库车辆汇总 - date={}, 在库{}台, 金额{}, 均值{}天, 超期{}台",
date, result.getTotalQuantity(), result.getTotalAmount(),
result.getAvgInStockDays(), result.getOverdueQuantity());
List<Vehicle> vehicles = vehicleDao.findByEvaluationTransaction_EvalOrderNoInAndVehicleSourceIn(distinctOrderNos, REPORT_VEHICLE_SOURCES);
List<VehicleSyncTimeVO> result = vehicles.stream().map(vehicle -> VehicleSyncTimeVO.builder()
.evalOrderNo(vehicle.getEvaluationTransaction() != null
? vehicle.getEvaluationTransaction().getEvalOrderNo() : null)
.syncTime(vehicle.getCreateTime())
.build()
).toList();
log.info("【报表数据查询】车辆同步时间 - 返回 {} 条记录", result.size());
return result;
}
}
\ No newline at end of file
......@@ -6,33 +6,26 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 在库车辆汇总 VO
* 车辆同步时间VO
*
* <p>统计指定日期仍处于"在库"状态(已入库且未处置完成)的车辆:
* 台数、收购金额合计、平均在库天数(周转天数)、超期台数。</p>
* <p>车辆档案由收车端推送时创建,createTime 即同步时刻,供收车端在库周转天数计算。</p>
*
* @author Claude
* @since 2026-09-03
* @since 2026-09-11
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "在库车辆汇总")
public class InventorySummaryVO {
@Schema(description = "车辆同步时间VO")
public class VehicleSyncTimeVO {
@Schema(description = "在库合计车辆数(台)")
private Integer totalQuantity;
@Schema(description = "评估单号")
private String evalOrderNo;
@Schema(description = "在库合计金额(元,收购价合计)")
private BigDecimal totalAmount;
@Schema(description = "平均在库天数(快照日期-入库日期的均值,四舍五入取整)")
private Integer avgInStockDays;
@Schema(description = "超期台数(在库天数超过调用方传入的overdueDays阈值)")
private Integer overdueQuantity;
@Schema(description = "同步时间(车辆档案生成时间)")
private LocalDateTime syncTime;
}
......@@ -10,7 +10,6 @@ import org.summerframework.orm.BaseRepository;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
......@@ -63,27 +62,24 @@ public interface VehicleDao extends BaseRepository<Vehicle, Integer>, JpaSpecifi
List<String> findBySourceNoVehicleStateNot(List<Integer> vehicleIds, Vehicle.VehicleState vehicleState);
/**
* 按月查询车辆档案(含已完成的车辆)
* 按月查询车辆档案(含已完成的车辆,仅限指定车辆来源)
* 用于收车端日报拉取
*
* @param year 年份
* @param month 月份
* @param vehicleSources 车辆来源集合
* @return 车辆列表
*/
@Query("select v from Vehicle v where year(v.transferredTime) = ?1 and month(v.transferredTime) = ?2")
List<Vehicle> findByTransferredTimeYearAndMonth(int year, int month);
@Query("select v from Vehicle v where year(v.transferredTime) = ?1 and month(v.transferredTime) = ?2 and v.vehicleSource in ?3")
List<Vehicle> findByTransferredTimeYearAndMonth(int year, int month, List<Vehicle.VehicleSource> vehicleSources);
/**
* 查询指定时刻的在库车辆(已入库、未处置完成)
* 用于收车端日报在库车辆分析:入库时间早于快照时刻,且无早于快照时刻的处置完成时间
* 按评估单号批量查询车辆档案(车辆档案由收车端推送时创建,createTime 即同步时刻,仅限指定车辆来源)
*
* @param snapshotEnd 快照时刻(开区间上界,通常取快照日期次日零点)
* @param excludedStates 排除的车辆状态(待入库/已取消)
* @return 在库车辆列表
* @param evalOrderNos 评估单号列表
* @param vehicleSources 车辆来源集合
* @return 车辆列表
*/
@Query("select v from Vehicle v where v.warehousingTime is not null and v.warehousingTime < ?1"
+ " and (v.transferredTime is null or v.transferredTime >= ?1)"
+ " and v.vehicleState not in ?2")
List<Vehicle> findInStockBefore(LocalDateTime snapshotEnd, Collection<Vehicle.VehicleState> excludedStates);
List<Vehicle> findByEvaluationTransaction_EvalOrderNoInAndVehicleSourceIn(List<String> evalOrderNos, List<Vehicle.VehicleSource> vehicleSources);
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!