|
|
@@ -0,0 +1,190 @@
|
|
|
+package shop.alien.lawyer.service.impl;
|
|
|
+
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.vo.LawyerOrderStatisticsVO;
|
|
|
+import shop.alien.lawyer.service.LawyerStatisticsService;
|
|
|
+import shop.alien.mapper.LawyerConsultationOrderMapper;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.temporal.TemporalAdjusters;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 律师统计 服务实现类
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LawyerStatisticsServiceImpl implements LawyerStatisticsService {
|
|
|
+
|
|
|
+ private final LawyerConsultationOrderMapper lawyerConsultationOrderMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 金额单位转换:分转元
|
|
|
+ */
|
|
|
+ private static final int FEN_TO_YUAN = 100;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<LawyerOrderStatisticsVO> getOrderStatistics(Integer lawyerUserId) {
|
|
|
+ log.info("LawyerStatisticsServiceImpl.getOrderStatistics?lawyerUserId={}", lawyerUserId);
|
|
|
+
|
|
|
+ if (lawyerUserId == null || lawyerUserId <= 0) {
|
|
|
+ log.warn("获取律师订单统计数据失败:律师用户ID为空或无效");
|
|
|
+ return R.fail("律师用户ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ LawyerOrderStatisticsVO statistics = new LawyerOrderStatisticsVO();
|
|
|
+
|
|
|
+ // 获取本月开始和结束时间
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+ LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
|
|
|
+ LocalDate firstDayOfNextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());
|
|
|
+ LocalDateTime startDateTime = firstDayOfMonth.atStartOfDay();
|
|
|
+ LocalDateTime endDateTime = firstDayOfNextMonth.atStartOfDay();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String startTime = startDateTime.format(formatter);
|
|
|
+ String endTime = endDateTime.format(formatter);
|
|
|
+
|
|
|
+ // 统计本月订单量和本月收益
|
|
|
+ Map<String, Object> monthStats = lawyerConsultationOrderMapper.getMonthStatistics(
|
|
|
+ lawyerUserId, startTime, endTime);
|
|
|
+ if (monthStats != null) {
|
|
|
+ statistics.setMonthOrderCount(getIntegerValue(monthStats.get("monthOrderCount")));
|
|
|
+ Long monthRevenueFen = getLongValue(monthStats.get("monthRevenue"));
|
|
|
+ statistics.setMonthRevenue(convertFenToYuan(monthRevenueFen));
|
|
|
+ } else {
|
|
|
+ statistics.setMonthOrderCount(0);
|
|
|
+ statistics.setMonthRevenue(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计总订单量和总收益
|
|
|
+ Map<String, Object> totalStats = lawyerConsultationOrderMapper.getTotalStatistics(lawyerUserId);
|
|
|
+ if (totalStats != null) {
|
|
|
+ statistics.setTotalOrderCount(getIntegerValue(totalStats.get("totalOrderCount")));
|
|
|
+ Long totalRevenueFen = getLongValue(totalStats.get("totalRevenue"));
|
|
|
+ statistics.setTotalRevenue(convertFenToYuan(totalRevenueFen));
|
|
|
+ } else {
|
|
|
+ statistics.setTotalOrderCount(0);
|
|
|
+ statistics.setTotalRevenue(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计进行中订单
|
|
|
+ Map<String, Object> inProgressStats = lawyerConsultationOrderMapper.getInProgressStatistics(lawyerUserId);
|
|
|
+ LawyerOrderStatisticsVO.OrderStatusStatistics inProgressOrder = new LawyerOrderStatisticsVO.OrderStatusStatistics();
|
|
|
+ if (inProgressStats != null) {
|
|
|
+ inProgressOrder.setOrderCount(getIntegerValue(inProgressStats.get("orderCount")));
|
|
|
+ Long inProgressAmountFen = getLongValue(inProgressStats.get("totalAmount"));
|
|
|
+ inProgressOrder.setTotalAmount(convertFenToYuan(inProgressAmountFen));
|
|
|
+ } else {
|
|
|
+ inProgressOrder.setOrderCount(0);
|
|
|
+ inProgressOrder.setTotalAmount(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ statistics.setInProgressOrder(inProgressOrder);
|
|
|
+
|
|
|
+ // 统计待接单订单
|
|
|
+ Map<String, Object> pendingAcceptStats = lawyerConsultationOrderMapper.getPendingAcceptStatistics(lawyerUserId);
|
|
|
+ LawyerOrderStatisticsVO.OrderStatusStatistics pendingAcceptOrder = new LawyerOrderStatisticsVO.OrderStatusStatistics();
|
|
|
+ if (pendingAcceptStats != null) {
|
|
|
+ pendingAcceptOrder.setOrderCount(getIntegerValue(pendingAcceptStats.get("orderCount")));
|
|
|
+ Long pendingAcceptAmountFen = getLongValue(pendingAcceptStats.get("totalAmount"));
|
|
|
+ pendingAcceptOrder.setTotalAmount(convertFenToYuan(pendingAcceptAmountFen));
|
|
|
+ } else {
|
|
|
+ pendingAcceptOrder.setOrderCount(0);
|
|
|
+ pendingAcceptOrder.setTotalAmount(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ statistics.setPendingAcceptOrder(pendingAcceptOrder);
|
|
|
+
|
|
|
+ // 统计已退款订单
|
|
|
+ Map<String, Object> refundedStats = lawyerConsultationOrderMapper.getRefundedStatistics(lawyerUserId);
|
|
|
+ LawyerOrderStatisticsVO.OrderStatusStatistics refundedOrder = new LawyerOrderStatisticsVO.OrderStatusStatistics();
|
|
|
+ if (refundedStats != null) {
|
|
|
+ refundedOrder.setOrderCount(getIntegerValue(refundedStats.get("orderCount")));
|
|
|
+ Long refundedAmountFen = getLongValue(refundedStats.get("totalAmount"));
|
|
|
+ refundedOrder.setTotalAmount(convertFenToYuan(refundedAmountFen));
|
|
|
+ } else {
|
|
|
+ refundedOrder.setOrderCount(0);
|
|
|
+ refundedOrder.setTotalAmount(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ statistics.setRefundedOrder(refundedOrder);
|
|
|
+
|
|
|
+ log.info("获取律师订单统计数据成功,律师ID={}, 本月订单量={}, 本月收益={}, 总订单量={}, 总收益={}",
|
|
|
+ lawyerUserId, statistics.getMonthOrderCount(), statistics.getMonthRevenue(),
|
|
|
+ statistics.getTotalOrderCount(), statistics.getTotalRevenue());
|
|
|
+
|
|
|
+ return R.data(statistics);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取律师订单统计数据异常,律师ID={}", lawyerUserId, e);
|
|
|
+ return R.fail("获取统计数据失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将分转换为元(保留2位小数)
|
|
|
+ *
|
|
|
+ * @param fen 分
|
|
|
+ * @return 元
|
|
|
+ */
|
|
|
+ private BigDecimal convertFenToYuan(Long fen) {
|
|
|
+ if (fen == null || fen == 0) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ return BigDecimal.valueOf(fen)
|
|
|
+ .divide(BigDecimal.valueOf(FEN_TO_YUAN), 2, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从Map中获取Integer值
|
|
|
+ *
|
|
|
+ * @param value 对象值
|
|
|
+ * @return Integer值
|
|
|
+ */
|
|
|
+ private Integer getIntegerValue(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return (Integer) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return ((Long) value).intValue();
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从Map中获取Long值
|
|
|
+ *
|
|
|
+ * @param value 对象值
|
|
|
+ * @return Long值
|
|
|
+ */
|
|
|
+ private Long getLongValue(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return (Long) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return ((Integer) value).longValue();
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).longValue();
|
|
|
+ }
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|