Эх сурвалжийг харах

新增律师订单统计逻辑

zhangchen 1 долоо хоног өмнө
parent
commit
3d23afccd9

+ 160 - 78
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerStatisticsServiceImpl.java

@@ -34,120 +34,197 @@ public class LawyerStatisticsServiceImpl implements LawyerStatisticsService {
      */
     private static final int FEN_TO_YUAN = 100;
 
+    /**
+     * 日期时间格式
+     */
+    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
+
+    /**
+     * 默认小数位数
+     */
+    private static final int DECIMAL_SCALE = 2;
+
+    /**
+     * Map key:订单数量
+     */
+    private static final String KEY_ORDER_COUNT = "orderCount";
+
+    /**
+     * Map key:总金额
+     */
+    private static final String KEY_TOTAL_AMOUNT = "totalAmount";
+
+    /**
+     * Map key:本月订单量
+     */
+    private static final String KEY_MONTH_ORDER_COUNT = "monthOrderCount";
+
+    /**
+     * Map key:本月收益
+     */
+    private static final String KEY_MONTH_REVENUE = "monthRevenue";
+
+    /**
+     * Map key:总订单量
+     */
+    private static final String KEY_TOTAL_ORDER_COUNT = "totalOrderCount";
+
+    /**
+     * Map key:总收益
+     */
+    private static final String KEY_TOTAL_REVENUE = "totalRevenue";
+
+    /**
+     * 日期格式化器
+     */
+    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
+
     @Override
     public R<LawyerOrderStatisticsVO> getOrderStatistics(Integer lawyerUserId) {
-        log.info("LawyerStatisticsServiceImpl.getOrderStatistics?lawyerUserId={}", lawyerUserId);
+        log.info("获取律师订单统计数据开始,lawyerUserId={}", lawyerUserId);
 
+        // 参数校验
         if (lawyerUserId == null || lawyerUserId <= 0) {
-            log.warn("获取律师订单统计数据失败:律师用户ID为空或无效");
+            log.warn("获取律师订单统计数据失败:律师用户ID为空或无效,lawyerUserId={}", lawyerUserId);
             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);
-            }
+            // 统计本月数据
+            buildMonthStatistics(statistics, lawyerUserId);
+
+            // 统计总数据
+            buildTotalStatistics(statistics, lawyerUserId);
 
             // 统计进行中订单
-            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);
+            statistics.setInProgressOrder(buildOrderStatusStatistics(
+                    lawyerConsultationOrderMapper.getInProgressStatistics(lawyerUserId)));
 
             // 统计待接单订单
-            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);
+            statistics.setPendingAcceptOrder(buildOrderStatusStatistics(
+                    lawyerConsultationOrderMapper.getPendingAcceptStatistics(lawyerUserId)));
 
             // 统计已退款订单
-            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={}, 本月订单量={}, 本月收益={}, 总订单量={}, 总收益={}",
+            statistics.setRefundedOrder(buildOrderStatusStatistics(
+                    lawyerConsultationOrderMapper.getRefundedStatistics(lawyerUserId)));
+
+            log.info("获取律师订单统计数据成功,lawyerUserId={}, monthOrderCount={}, monthRevenue={}, "
+                            + "totalOrderCount={}, totalRevenue={}",
                     lawyerUserId, statistics.getMonthOrderCount(), statistics.getMonthRevenue(),
                     statistics.getTotalOrderCount(), statistics.getTotalRevenue());
 
             return R.data(statistics);
-        } catch (Exception e) {
-            log.error("获取律师订单统计数据异常,律师ID={}", lawyerUserId, e);
+        } catch (RuntimeException e) {
+            log.error("获取律师订单统计数据异常,lawyerUserId={}", lawyerUserId, e);
             return R.fail("获取统计数据失败:" + e.getMessage());
         }
     }
 
     /**
+     * 构建本月统计数据
+     *
+     * @param statistics  统计对象
+     * @param lawyerUserId 律师用户ID
+     */
+    private void buildMonthStatistics(LawyerOrderStatisticsVO statistics, Integer lawyerUserId) {
+        String[] timeRange = getCurrentMonthTimeRange();
+        Map<String, Object> monthStats = lawyerConsultationOrderMapper.getMonthStatistics(
+                lawyerUserId, timeRange[0], timeRange[1]);
+
+        if (monthStats != null) {
+            statistics.setMonthOrderCount(getIntegerValue(monthStats.get(KEY_MONTH_ORDER_COUNT)));
+            Long monthRevenueFen = getLongValue(monthStats.get(KEY_MONTH_REVENUE));
+            statistics.setMonthRevenue(convertFenToYuan(monthRevenueFen));
+        } else {
+            statistics.setMonthOrderCount(0);
+            statistics.setMonthRevenue(BigDecimal.ZERO);
+        }
+    }
+
+    /**
+     * 构建总统计数据
+     *
+     * @param statistics  统计对象
+     * @param lawyerUserId 律师用户ID
+     */
+    private void buildTotalStatistics(LawyerOrderStatisticsVO statistics, Integer lawyerUserId) {
+        Map<String, Object> totalStats = lawyerConsultationOrderMapper.getTotalStatistics(lawyerUserId);
+
+        if (totalStats != null) {
+            statistics.setTotalOrderCount(getIntegerValue(totalStats.get(KEY_TOTAL_ORDER_COUNT)));
+            Long totalRevenueFen = getLongValue(totalStats.get(KEY_TOTAL_REVENUE));
+            statistics.setTotalRevenue(convertFenToYuan(totalRevenueFen));
+        } else {
+            statistics.setTotalOrderCount(0);
+            statistics.setTotalRevenue(BigDecimal.ZERO);
+        }
+    }
+
+    /**
+     * 构建订单状态统计数据
+     *
+     * @param statsMap 统计数据Map
+     * @return 订单状态统计对象
+     */
+    private LawyerOrderStatisticsVO.OrderStatusStatistics buildOrderStatusStatistics(
+            Map<String, Object> statsMap) {
+        LawyerOrderStatisticsVO.OrderStatusStatistics orderStatusStatistics =
+                new LawyerOrderStatisticsVO.OrderStatusStatistics();
+
+        if (statsMap != null) {
+            orderStatusStatistics.setOrderCount(getIntegerValue(statsMap.get(KEY_ORDER_COUNT)));
+            Long amountFen = getLongValue(statsMap.get(KEY_TOTAL_AMOUNT));
+            orderStatusStatistics.setTotalAmount(convertFenToYuan(amountFen));
+        } else {
+            orderStatusStatistics.setOrderCount(0);
+            orderStatusStatistics.setTotalAmount(BigDecimal.ZERO);
+        }
+
+        return orderStatusStatistics;
+    }
+
+    /**
+     * 获取当前月份的时间范围
+     *
+     * @return 时间范围数组,[0]为开始时间,[1]为结束时间
+     */
+    private String[] getCurrentMonthTimeRange() {
+        LocalDate now = LocalDate.now();
+        LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
+        LocalDate firstDayOfNextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());
+        LocalDateTime startDateTime = firstDayOfMonth.atStartOfDay();
+        LocalDateTime endDateTime = firstDayOfNextMonth.atStartOfDay();
+
+        return new String[]{
+                startDateTime.format(DATE_TIME_FORMATTER),
+                endDateTime.format(DATE_TIME_FORMATTER)
+        };
+    }
+
+    /**
      * 将分转换为元(保留2位小数)
      *
-     * @param fen 分
-     * @return 元
+     * @param fen 分,单位:分
+     * @return 元,单位:元,保留2位小数
      */
     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);
+                .divide(BigDecimal.valueOf(FEN_TO_YUAN), DECIMAL_SCALE, RoundingMode.HALF_UP);
     }
 
     /**
      * 从Map中获取Integer值
+     * <p>
+     * 支持Integer、Long、Number类型的转换
+     * </p>
      *
-     * @param value 对象值
-     * @return Integer值
+     * @param value 对象值,可能为Integer、Long或其他Number类型
+     * @return Integer值,如果value为null或无法转换则返回0
      */
     private Integer getIntegerValue(Object value) {
         if (value == null) {
@@ -162,14 +239,18 @@ public class LawyerStatisticsServiceImpl implements LawyerStatisticsService {
         if (value instanceof Number) {
             return ((Number) value).intValue();
         }
+        log.warn("无法将对象转换为Integer,value={}, type={}", value, value.getClass().getName());
         return 0;
     }
 
     /**
      * 从Map中获取Long值
+     * <p>
+     * 支持Long、Integer、Number类型的转换
+     * </p>
      *
-     * @param value 对象值
-     * @return Long值
+     * @param value 对象值,可能为Long、Integer或其他Number类型
+     * @return Long值,如果value为null或无法转换则返回0L
      */
     private Long getLongValue(Object value) {
         if (value == null) {
@@ -184,6 +265,7 @@ public class LawyerStatisticsServiceImpl implements LawyerStatisticsService {
         if (value instanceof Number) {
             return ((Number) value).longValue();
         }
+        log.warn("无法将对象转换为Long,value={}, type={}", value, value.getClass().getName());
         return 0L;
     }
 }