This commit is contained in:
YAS\29473
2025-11-13 15:07:55 +08:00
parent 7f433e06c2
commit d672eff8c8
2 changed files with 17 additions and 17 deletions

View File

@@ -5955,18 +5955,19 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
dto = new QueryOrderDTO();
}
// 只允许 yyyy-MM 格式
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try {
// 处理 startTime
if (StringUtils.isNotEmpty(dto.getStartTime())) {
YearMonth startMonth = YearMonth.parse(dto.getStartTime(), monthFormatter);
dto.setStartTime(startMonth.atDay(1).atStartOfDay().format(fullFormatter));
dto.setStartTime(startMonth.atDay(1).atStartOfDay().format(dateTimeFormatter));
}
// 处理 endTime取下个月月初保证 < endTime 不丢数据
if (StringUtils.isNotEmpty(dto.getEndTime())) {
YearMonth endMonth = YearMonth.parse(dto.getEndTime(), monthFormatter).plusMonths(1);
dto.setEndTime(endMonth.atDay(1).atStartOfDay().format(fullFormatter));
YearMonth endMonth = YearMonth.parse(dto.getEndTime(), monthFormatter);
dto.setEndTime(endMonth.plusMonths(1).atDay(1).atStartOfDay().format(dateTimeFormatter));
}
} catch (Exception e) {
throw new IllegalArgumentException("时间格式错误,应为 yyyy-MM");
@@ -5975,42 +5976,41 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
// 默认本月
if (StringUtils.isEmpty(dto.getStartTime()) || StringUtils.isEmpty(dto.getEndTime())) {
YearMonth currentMonth = YearMonth.now();
dto.setStartTime(currentMonth.atDay(1).atStartOfDay().format(fullFormatter));
dto.setEndTime(currentMonth.plusMonths(1).atDay(1).atStartOfDay().format(fullFormatter));
dto.setStartTime(currentMonth.atDay(1).atStartOfDay().format(dateTimeFormatter));
dto.setEndTime(currentMonth.plusMonths(1).atDay(1).atStartOfDay().format(dateTimeFormatter));
}
// 查询数据库聚合结果(仅一次)
// 查询数据库聚合结果
List<OrderMonthStatVO> stats = orderBasicInfoMapper.selectOrderCountAndInsuranceByMonth(dto);
// 封装结果
// 封装返回结果
BigDecimal totalInsuranceAmount = BigDecimal.ZERO;
int totalCount = 0;
List<OrderCountByTimeVO.OrderCountByTimeListVO> result = new ArrayList<>();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
for (OrderMonthStatVO stat : stats) {
YearMonth ym = YearMonth.parse(stat.getMonth(), DateTimeFormatter.ofPattern("yyyy-MM"));
YearMonth ym = YearMonth.parse(stat.getMonth(), monthFormatter);
OrderCountByTimeVO.OrderCountByTimeListVO vo = new OrderCountByTimeVO.OrderCountByTimeListVO();
vo.setStartTime(ym.atDay(1).atStartOfDay().format(dateTimeFormatter));
vo.setEndTime(ym.plusMonths(1).atDay(1).atStartOfDay().format(dateTimeFormatter));
vo.setCount(stat.getOrderCount());
vo.setOrderCount(stat.getOrderCount());
vo.setInsuranceAmount(stat.getTotalInsuranceAmount());
BigDecimal insuranceAmount = stat.getInsuranceAmount() == null ? BigDecimal.ZERO : stat.getInsuranceAmount();
vo.setInsuranceAmount(totalInsuranceAmount);
result.add(vo);
totalCount += stat.getOrderCount();
totalInsuranceAmount = totalInsuranceAmount.add(stat.getTotalInsuranceAmount());
totalInsuranceAmount = totalInsuranceAmount.add(insuranceAmount);
}
// 汇总
OrderCountByTimeVO vo = new OrderCountByTimeVO();
vo.setOrderCountByTimeList(result);
vo.setTotalCount(totalCount);
vo.setTotalInsuranceAmount(totalInsuranceAmount);
logger.info("按月统计结果: {}", JSONObject.toJSONString(vo));
return vo;

View File

@@ -5,7 +5,7 @@ import java.math.BigDecimal;
@Data
public class OrderMonthStatVO {
private String month; // 2025-10
private String month;
private Integer orderCount;
private BigDecimal totalInsuranceAmount;
private BigDecimal insuranceAmount;
}