Parcourir la source

经营数据统计接口修改

zhangchen il y a 2 mois
Parent
commit
bc13393dc7

+ 42 - 3
alien-store/src/main/java/shop/alien/store/controller/StoreOperationalStatisticsController.java

@@ -146,18 +146,25 @@ public class StoreOperationalStatisticsController {
                     value = "页容(默认10)",
                     dataType = "int",
                     paramType = "query"
+            ),
+            @ApiImplicitParam(
+                    name = "created_time",
+                    value = "查询日期(yyyy-MM-dd),可为空;传入时仅返回 query_time 为该日期的记录",
+                    dataType = "String",
+                    paramType = "query"
             )
     })
     @GetMapping("/history/list")
     public R<IPage<StoreOperationalStatisticsHistory>> getHistoryList(
             @RequestParam("storeId") Integer storeId,
             @RequestParam(defaultValue = "1") int page,
-            @RequestParam(defaultValue = "10") int size) {
-        log.info("StoreOperationalStatisticsController.getHistoryList - storeId={}, page={}, size={}", storeId, page, size);
+            @RequestParam(defaultValue = "10") int size,
+            @RequestParam(value = "created_time", required = false) String createdTime) {
+        log.info("StoreOperationalStatisticsController.getHistoryList - storeId={}, page={}, size={}, created_time={}", storeId, page, size, createdTime);
         try {
             int pageNum = page > 0 ? page : 1;
             int pageSize = size > 0 ? size : 10;
-            IPage<StoreOperationalStatisticsHistory> historyPage = storeOperationalStatisticsService.getHistoryList(storeId, pageNum, pageSize);
+            IPage<StoreOperationalStatisticsHistory> historyPage = storeOperationalStatisticsService.getHistoryList(storeId, pageNum, pageSize, createdTime);
             return R.data(historyPage);
         } catch (Exception e) {
             log.error("查询历史统计记录列表失败 - storeId={}, page={}, size={}, error={}", storeId, page, size, e.getMessage(), e);
@@ -375,5 +382,37 @@ public class StoreOperationalStatisticsController {
         }
     }
 
+    @ApiOperation("根据历史记录ID生成统计数据对比PDF报告并上传到OSS")
+    @ApiOperationSupport(order = 8)
+    @ApiImplicitParams({
+            @ApiImplicitParam(
+                    name = "storeId",
+                    value = "店铺ID",
+                    dataType = "Integer",
+                    paramType = "query",
+                    required = true
+            ),
+            @ApiImplicitParam(
+                    name = "historyId",
+                    value = "历史记录ID(从 store_operational_statistics_history 表取 statistics_data 解析为对比数据)",
+                    dataType = "Integer",
+                    paramType = "query",
+                    required = true
+            )
+    })
+    @GetMapping("/generateStatisticsComparisonPdfByHistoryId")
+    public R<String> generateStatisticsComparisonPdfByHistoryId(
+            @RequestParam("storeId") Integer storeId,
+            @RequestParam("historyId") Integer historyId) {
+        log.info("StoreOperationalStatisticsController.generateStatisticsComparisonPdfByHistoryId - storeId={}, historyId={}", storeId, historyId);
+        try {
+            String pdfUrl = storeOperationalStatisticsService.generateStatisticsComparisonPdfByHistoryId(storeId, historyId);
+            return R.data(pdfUrl);
+        } catch (Exception e) {
+            log.error("根据历史记录生成统计数据对比PDF失败 - storeId={}, historyId={}, error={}", storeId, historyId, e.getMessage(), e);
+            return R.fail("生成PDF报告失败: " + e.getMessage());
+        }
+    }
+
 
 }

+ 14 - 4
alien-store/src/main/java/shop/alien/store/service/StoreOperationalStatisticsService.java

@@ -42,12 +42,13 @@ public interface StoreOperationalStatisticsService {
     /**
      * 查询历史统计记录列表(分页)
      *
-     * @param storeId 店铺ID
-     * @param page    页码
-     * @param size    每页大小
+     * @param storeId    店铺ID
+     * @param page       页码
+     * @param size       每页大小
+     * @param createdTime 查询日期(yyyy-MM-dd),可为空;传入时仅返回 query_time 为该日期的记录
      * @return 历史统计记录分页列表
      */
-    IPage<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId, Integer page, Integer size);
+    IPage<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId, Integer page, Integer size, String createdTime);
 
     /**
      * 根据ID查询历史统计记录详情
@@ -115,4 +116,13 @@ public interface StoreOperationalStatisticsService {
      */
     String generateStatisticsComparisonPdf(Integer storeId, String currentStartTime, String currentEndTime,
                                            String previousStartTime, String previousEndTime);
+
+    /**
+     * 根据历史记录ID生成统计数据对比PDF报告并上传到OSS(对比数据从 history 的 statistics_data 解析)
+     *
+     * @param storeId   店铺ID
+     * @param historyId 历史记录ID
+     * @return PDF的OSS URL
+     */
+    String generateStatisticsComparisonPdfByHistoryId(Integer storeId, Integer historyId);
 }

+ 59 - 3
alien-store/src/main/java/shop/alien/store/service/impl/StoreOperationalStatisticsServiceImpl.java

@@ -1114,8 +1114,8 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
     }
 
     @Override
-    public IPage<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId, Integer page, Integer size) {
-        log.info("StoreOperationalStatisticsServiceImpl.getHistoryList - storeId={}, page={}, size={}", storeId, page, size);
+    public IPage<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId, Integer page, Integer size, String createdTime) {
+        log.info("StoreOperationalStatisticsServiceImpl.getHistoryList - storeId={}, page={}, size={}, createdTime={}", storeId, page, size, createdTime);
         
         // 参数校验
         if (storeId == null || storeId <= 0) {
@@ -1133,6 +1133,11 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
                .eq(StoreOperationalStatisticsHistory::getDeleteFlag, 0)
                .orderByDesc(StoreOperationalStatisticsHistory::getQueryTime);
         
+        // created_time 可选:与 query_time 为同一天(query_time 格式 yyyy-MM-dd HH:mm:ss,created_time 格式 yyyy-MM-dd)
+        if (StringUtils.hasText(createdTime)) {
+            wrapper.apply("DATE(query_time) = {0}", createdTime.trim());
+        }
+        
         // 执行分页查询
         IPage<StoreOperationalStatisticsHistory> result = statisticsHistoryMapper.selectPage(pageObj, wrapper);
         
@@ -2298,7 +2303,58 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
             throw new RuntimeException("生成图片并转PDF上传失败: " + e.getMessage(), e);
         }
     }
-    
+
+    @Override
+    public String generateStatisticsComparisonPdfByHistoryId(Integer storeId, Integer historyId) {
+        log.info("StoreOperationalStatisticsServiceImpl.generateStatisticsComparisonPdfByHistoryId - storeId={}, historyId={}", storeId, historyId);
+
+        if (storeId == null || storeId <= 0) {
+            throw new IllegalArgumentException("店铺ID不能为空且必须大于0");
+        }
+        if (historyId == null || historyId <= 0) {
+            throw new IllegalArgumentException("历史记录ID不能为空且必须大于0");
+        }
+
+        // 1. 根据 historyId 查询历史记录
+        StoreOperationalStatisticsHistory history = getHistoryById(historyId);
+        if (!storeId.equals(history.getStoreId())) {
+            throw new IllegalArgumentException("历史记录与店铺ID不匹配");
+        }
+        String statisticsDataJson = history.getStatisticsData();
+        if (statisticsDataJson == null || statisticsDataJson.trim().isEmpty()) {
+            throw new IllegalArgumentException("该历史记录无统计数据(statistics_data为空)");
+        }
+
+        // 2. 将 statistics_data 的 JSON 转为 StoreOperationalStatisticsComparisonVo
+        StoreOperationalStatisticsComparisonVo comparison = JSON.parseObject(statisticsDataJson.trim(), StoreOperationalStatisticsComparisonVo.class);
+        if (comparison == null) {
+            throw new RuntimeException("解析统计数据对比JSON失败");
+        }
+
+        try {
+            // 3. 生成图片
+            byte[] imageBytes = StatisticsComparisonImageUtil.generateImage(comparison);
+            java.io.ByteArrayInputStream imageInputStream = new java.io.ByteArrayInputStream(imageBytes);
+            byte[] pdfBytes = ImageToPdfUtil.imageToPdfBytes(imageInputStream);
+            if (pdfBytes == null || pdfBytes.length == 0) {
+                throw new RuntimeException("图片转PDF失败");
+            }
+
+            // 4. 上传PDF到OSS
+            String ossFilePath = "statistics/comparison_" + storeId + "_" + RandomCreateUtil.getRandomNum(8) + ".pdf";
+            java.io.ByteArrayInputStream pdfInputStream = new java.io.ByteArrayInputStream(pdfBytes);
+            String pdfUrl = aliOSSUtil.uploadFile(pdfInputStream, ossFilePath);
+            log.info("根据历史记录生成统计数据对比PDF并上传成功 - storeId={}, historyId={}, pdfUrl={}", storeId, historyId, pdfUrl);
+
+            // 5. 更新该历史记录的 PDF URL
+            updateHistoryPdfUrl(historyId, pdfUrl);
+            return pdfUrl;
+        } catch (Exception e) {
+            log.error("根据历史记录生成统计数据对比PDF失败 - storeId={}, historyId={}, error={}", storeId, historyId, e.getMessage(), e);
+            throw new RuntimeException("生成PDF报告失败: " + e.getMessage(), e);
+        }
+    }
+
     /**
      * 获取统计数据对比(不保存历史记录)
      * 用于 generateStatisticsComparisonPdf 方法,避免重复保存历史记录