mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-05-03 09:29:59 +08:00
update
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
package com.jsowell.pile.mapper;
|
||||
|
||||
import com.alipay.api.domain.ChargeOrderInfo;
|
||||
import com.jsowell.pile.domain.OrderBasicInfo;
|
||||
import com.jsowell.pile.domain.OrderDetail;
|
||||
import com.jsowell.pile.domain.OrderSplitRecord;
|
||||
import com.jsowell.pile.domain.UserFrequentedStationInfo;
|
||||
import com.jsowell.pile.dto.*;
|
||||
import com.jsowell.pile.dto.nanrui.NRQueryOrderDTO;
|
||||
import com.jsowell.pile.dto.ningxiajiaotou.NXJTQueryOrdersInfoDTO;
|
||||
@@ -431,4 +428,10 @@ public interface OrderBasicInfoMapper {
|
||||
@Param("endTime") String endTime
|
||||
);
|
||||
|
||||
/**
|
||||
* 按月份统计订单数量和保险金额
|
||||
* @param dto 查询条件,startTime/endTime 必须是 yyyy-MM 拼装后的时间段
|
||||
* @return List<OrderMonthStatVO>
|
||||
*/
|
||||
List<OrderMonthStatVO> selectOrderCountAndInsuranceByMonth(@Param("dto") QueryOrderDTO dto);
|
||||
}
|
||||
|
||||
@@ -5955,89 +5955,65 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
||||
dto = new QueryOrderDTO();
|
||||
}
|
||||
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS);
|
||||
|
||||
// 开始时间默认:本月第一天
|
||||
if (StringUtils.isEmpty(dto.getStartTime())) {
|
||||
LocalDate firstDayOfMonth = LocalDate.now().withDayOfMonth(1);
|
||||
LocalDateTime monthStart = LocalDateTime.of(firstDayOfMonth, LocalTime.MIN);
|
||||
dto.setStartTime(monthStart.format(dateTimeFormatter));
|
||||
}
|
||||
|
||||
// 结束时间默认:当前时间
|
||||
if (StringUtils.isEmpty(dto.getEndTime())) {
|
||||
dto.setEndTime(LocalDateTime.now().format(dateTimeFormatter));
|
||||
}
|
||||
|
||||
// 查询订单数据
|
||||
List<OrderListVO> orderList = orderBasicInfoMapper.selectOrderBasicInfoList(dto);
|
||||
|
||||
List<OrderCountByTimeVO.OrderCountByTimeListVO> result = new ArrayList<>();
|
||||
BigDecimal totalInsuranceAmount = BigDecimal.ZERO;
|
||||
int totalCount = 0;
|
||||
// 只允许 yyyy-MM 格式
|
||||
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
try {
|
||||
LocalDateTime queryStart = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter);
|
||||
LocalDateTime queryEnd = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter);
|
||||
|
||||
// 按月分组订单
|
||||
Map<YearMonth, List<OrderListVO>> ordersByMonth = orderList.stream()
|
||||
.collect(Collectors.groupingBy(order -> {
|
||||
if (order.getCreateTime() != null) {
|
||||
LocalDateTime createTime = LocalDateTime.parse(order.getCreateTime(), dateTimeFormatter);
|
||||
return YearMonth.from(createTime);
|
||||
} else {
|
||||
logger.warn("订单创建时间为空,订单号:{}", order.getOrderCode());
|
||||
return YearMonth.from(LocalDateTime.now());
|
||||
}
|
||||
}));
|
||||
|
||||
// 循环每个月
|
||||
YearMonth currentMonth = YearMonth.from(queryStart);
|
||||
YearMonth endMonth = YearMonth.from(queryEnd);
|
||||
|
||||
while (!currentMonth.isAfter(endMonth)) {
|
||||
LocalDateTime monthStartTime = currentMonth.atDay(1).atStartOfDay();
|
||||
LocalDateTime monthEndTime = currentMonth.plusMonths(1).atDay(1).atStartOfDay();
|
||||
|
||||
List<OrderListVO> ordersInMonth = ordersByMonth.getOrDefault(currentMonth, Collections.emptyList());
|
||||
int monthCount = ordersInMonth.size();
|
||||
|
||||
BigDecimal monthInsuranceAmount = ordersInMonth.stream()
|
||||
.map(OrderListVO::getInsuranceAmount)
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
OrderCountByTimeVO.OrderCountByTimeListVO vo = new OrderCountByTimeVO.OrderCountByTimeListVO();
|
||||
vo.setStartTime(monthStartTime.format(dateTimeFormatter));
|
||||
vo.setEndTime(monthEndTime.format(dateTimeFormatter));
|
||||
vo.setCount(monthCount);
|
||||
vo.setOrderCount(monthCount);
|
||||
vo.setInsuranceAmount(monthInsuranceAmount);
|
||||
|
||||
result.add(vo);
|
||||
|
||||
totalCount += monthCount;
|
||||
totalInsuranceAmount = totalInsuranceAmount.add(monthInsuranceAmount);
|
||||
|
||||
currentMonth = currentMonth.plusMonths(1);
|
||||
if (StringUtils.isNotEmpty(dto.getStartTime())) {
|
||||
YearMonth startMonth = YearMonth.parse(dto.getStartTime(), monthFormatter);
|
||||
dto.setStartTime(startMonth.atDay(1).atStartOfDay().format(fullFormatter));
|
||||
}
|
||||
if (StringUtils.isNotEmpty(dto.getEndTime())) {
|
||||
YearMonth endMonth = YearMonth.parse(dto.getEndTime(), monthFormatter).plusMonths(1);
|
||||
dto.setEndTime(endMonth.atDay(1).atStartOfDay().format(fullFormatter));
|
||||
}
|
||||
|
||||
// 排序
|
||||
result.sort(Comparator.comparing(OrderCountByTimeVO.OrderCountByTimeListVO::getStartTime));
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("统计订单数量失败", e);
|
||||
throw new IllegalArgumentException("时间格式错误,应为 yyyy-MM");
|
||||
}
|
||||
|
||||
// 封装返回
|
||||
OrderCountByTimeVO orderCountByTimeVO = new OrderCountByTimeVO();
|
||||
orderCountByTimeVO.setOrderCountByTimeList(result);
|
||||
orderCountByTimeVO.setTotalCount(totalCount);
|
||||
orderCountByTimeVO.setTotalInsuranceAmount(totalInsuranceAmount);
|
||||
// 默认本月
|
||||
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));
|
||||
}
|
||||
|
||||
// 查询数据库聚合结果(仅一次)
|
||||
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"));
|
||||
|
||||
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());
|
||||
|
||||
result.add(vo);
|
||||
|
||||
totalCount += stat.getOrderCount();
|
||||
totalInsuranceAmount = totalInsuranceAmount.add(stat.getTotalInsuranceAmount());
|
||||
}
|
||||
|
||||
// 汇总
|
||||
OrderCountByTimeVO vo = new OrderCountByTimeVO();
|
||||
vo.setOrderCountByTimeList(result);
|
||||
vo.setTotalCount(totalCount);
|
||||
vo.setTotalInsuranceAmount(totalInsuranceAmount);
|
||||
|
||||
logger.info("按月统计结果: {}", JSONObject.toJSONString(vo));
|
||||
return vo;
|
||||
|
||||
logger.info("按月查询订单数量结果:{}", JSONObject.toJSONString(orderCountByTimeVO));
|
||||
return orderCountByTimeVO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3451,4 +3451,21 @@
|
||||
AND DATE_ADD(#{endTime,jdbcType=VARCHAR}, INTERVAL 1 DAY)
|
||||
ORDER BY t1.create_time DESC
|
||||
</select>
|
||||
|
||||
<select id="selectOrderCountAndInsuranceByMonth"
|
||||
parameterType="com.jsowell.pile.dto.QueryOrderDTO"
|
||||
resultType="com.jsowell.pile.vo.web.OrderMonthStatVO">
|
||||
SELECT
|
||||
DATE_FORMAT(create_time, '%Y-%m') AS month,
|
||||
COUNT(*) AS orderCount,
|
||||
IFNULL(SUM(insurance_amount), 0) AS insuranceAmount
|
||||
FROM order_basic_info
|
||||
WHERE create_time <![CDATA[ >= ]]> #{dto.startTime}
|
||||
AND create_time <![CDATA[ < ]]> #{dto.endTime}
|
||||
GROUP BY DATE_FORMAT(create_time, '%Y-%m')
|
||||
ORDER BY month ASC
|
||||
</select>
|
||||
|
||||
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user