Pārlūkot izejas kodu

开发 历史揭露查询功能
开发 删除和批量删除功能

lutong 2 mēneši atpakaļ
vecāks
revīzija
94c9c7c575

+ 60 - 0
alien-entity/src/main/java/shop/alien/entity/store/StoreOperationalStatisticsHistory.java

@@ -0,0 +1,60 @@
+package shop.alien.entity.store;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * 商家经营数据统计历史表
+ *
+ * @author auto-generated
+ * @since 2025-01-05
+ */
+@Data
+@JsonInclude
+@TableName("store_operational_statistics_history")
+@ApiModel(value = "StoreOperationalStatisticsHistory对象", description = "商家经营数据统计历史表")
+public class StoreOperationalStatisticsHistory {
+
+    @ApiModelProperty(value = "主键ID")
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    @ApiModelProperty(value = "商铺ID")
+    @TableField("store_id")
+    private Integer storeId;
+
+    @ApiModelProperty(value = "统计开始时间")
+    @TableField("start_time")
+    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
+    private Date startTime;
+
+    @ApiModelProperty(value = "统计结束时间")
+    @TableField("end_time")
+    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
+    private Date endTime;
+
+    @ApiModelProperty(value = "统计数据(JSON格式,包含所有统计字段)")
+    @TableField("statistics_data")
+    private String statisticsData;
+
+    @ApiModelProperty(value = "查询时间(统计数据的时间)")
+    @TableField("query_time")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date queryTime;
+
+    @ApiModelProperty(value = "删除标记, 0:未删除, 1:已删除")
+    @TableField("delete_flag")
+    @TableLogic
+    private Integer deleteFlag;
+
+    @ApiModelProperty(value = "创建时间")
+    @TableField(value = "created_time", fill = FieldFill.INSERT)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date createdTime;
+}

+ 16 - 0
alien-entity/src/main/java/shop/alien/mapper/StoreOperationalStatisticsHistoryMapper.java

@@ -0,0 +1,16 @@
+package shop.alien.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import shop.alien.entity.store.StoreOperationalStatisticsHistory;
+
+/**
+ * 商家经营数据统计历史表 Mapper 接口
+ *
+ * @author auto-generated
+ * @since 2025-01-05
+ */
+@Mapper
+public interface StoreOperationalStatisticsHistoryMapper extends BaseMapper<StoreOperationalStatisticsHistory> {
+
+}

+ 109 - 0
alien-store/src/main/java/shop/alien/store/controller/StoreOperationalStatisticsController.java

@@ -5,10 +5,14 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
+import shop.alien.entity.store.StoreOperationalStatisticsHistory;
 import shop.alien.entity.store.vo.StoreOperationalStatisticsComparisonVo;
 import shop.alien.entity.store.vo.StoreOperationalStatisticsVo;
 import shop.alien.store.service.StoreOperationalStatisticsService;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * 商家经营数据统计Controller
  *
@@ -113,4 +117,109 @@ public class StoreOperationalStatisticsController {
                 storeId, currentStartTime, currentEndTime, previousStartTime, previousEndTime);
         return R.data(comparison);
     }
+
+    @ApiOperation("查询历史统计记录列表")
+    @ApiOperationSupport(order = 3)
+    @ApiImplicitParams({
+            @ApiImplicitParam(
+                    name = "storeId",
+                    value = "店铺ID",
+                    dataType = "Integer",
+                    paramType = "query",
+                    required = true
+            )
+    })
+    @GetMapping("/history/list")
+    public R<List<StoreOperationalStatisticsHistory>> getHistoryList(
+            @RequestParam("storeId") Integer storeId) {
+        log.info("StoreOperationalStatisticsController.getHistoryList - storeId={}", storeId);
+        try {
+            List<StoreOperationalStatisticsHistory> historyList = storeOperationalStatisticsService.getHistoryList(storeId);
+            return R.data(historyList);
+        } catch (Exception e) {
+            log.error("查询历史统计记录列表失败 - storeId={}, error={}", storeId, e.getMessage(), e);
+            return R.fail("查询失败: " + e.getMessage());
+        }
+    }
+
+    @ApiOperation("删除历史统计记录")
+    @ApiOperationSupport(order = 4)
+    @ApiImplicitParams({
+            @ApiImplicitParam(
+                    name = "id",
+                    value = "历史记录ID",
+                    dataType = "Integer",
+                    paramType = "query",
+                    required = true
+            )
+    })
+    @DeleteMapping("/history/delete")
+    public R<String> deleteHistory(@RequestParam("id") Integer id) {
+        log.info("StoreOperationalStatisticsController.deleteHistory - id={}", id);
+        try {
+            if (id == null || id <= 0) {
+                return R.fail("ID不能为空且必须大于0");
+            }
+            
+            boolean result = storeOperationalStatisticsService.deleteHistory(id);
+            if (result) {
+                return R.success("删除成功");
+            } else {
+                return R.fail("删除失败,记录不存在或已被删除");
+            }
+        } catch (Exception e) {
+            log.error("删除历史统计记录失败 - id={}, error={}", id, e.getMessage(), e);
+            return R.fail("删除失败: " + e.getMessage());
+        }
+    }
+
+    @ApiOperation("批量删除历史统计记录")
+    @ApiOperationSupport(order = 5)
+    @ApiImplicitParams({
+            @ApiImplicitParam(
+                    name = "ids",
+                    value = "历史记录ID列表(逗号分隔)",
+                    dataType = "String",
+                    paramType = "query",
+                    required = true,
+                    example = "1,2,3"
+            )
+    })
+    @DeleteMapping("/history/batchDelete")
+    public R<String> batchDeleteHistory(@RequestParam("ids") String ids) {
+        log.info("StoreOperationalStatisticsController.batchDeleteHistory - ids={}", ids);
+        try {
+            if (ids == null || ids.trim().isEmpty()) {
+                return R.fail("ID列表不能为空");
+            }
+            
+            // 解析ID列表
+            String[] idArray = ids.split(",");
+            List<Integer> idList = new ArrayList<>();
+            for (String idStr : idArray) {
+                try {
+                    int id = Integer.parseInt(idStr.trim());
+                    if (id > 0) {
+                        idList.add(id);
+                    }
+                } catch (NumberFormatException e) {
+                    log.warn("无效的ID格式: {}", idStr);
+                }
+            }
+            
+            if (idList.isEmpty()) {
+                return R.fail("没有有效的ID");
+            }
+            
+            boolean result = storeOperationalStatisticsService.batchDeleteHistory(idList);
+            if (result) {
+                return R.success("批量删除成功");
+            } else {
+                return R.fail("批量删除失败");
+            }
+        } catch (Exception e) {
+            log.error("批量删除历史统计记录失败 - ids={}, error={}", ids, e.getMessage(), e);
+            return R.fail("批量删除失败: " + e.getMessage());
+        }
+    }
 }

+ 27 - 0
alien-store/src/main/java/shop/alien/store/service/StoreOperationalStatisticsService.java

@@ -1,8 +1,11 @@
 package shop.alien.store.service;
 
+import shop.alien.entity.store.StoreOperationalStatisticsHistory;
 import shop.alien.entity.store.vo.StoreOperationalStatisticsComparisonVo;
 import shop.alien.entity.store.vo.StoreOperationalStatisticsVo;
 
+import java.util.List;
+
 /**
  * 商家经营数据统计服务接口
  *
@@ -34,4 +37,28 @@ public interface StoreOperationalStatisticsService {
      */
     StoreOperationalStatisticsComparisonVo getStatisticsComparison(Integer storeId, String currentStartTime, String currentEndTime,
                                                                     String previousStartTime, String previousEndTime);
+
+    /**
+     * 查询历史统计记录列表
+     *
+     * @param storeId 店铺ID
+     * @return 历史统计记录列表
+     */
+    List<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId);
+
+    /**
+     * 删除历史统计记录(逻辑删除)
+     *
+     * @param id 历史记录ID
+     * @return 是否成功
+     */
+    boolean deleteHistory(Integer id);
+
+    /**
+     * 批量删除历史统计记录(逻辑删除)
+     *
+     * @param ids 历史记录ID列表
+     * @return 是否成功
+     */
+    boolean batchDeleteHistory(List<Integer> ids);
 }

+ 90 - 1
alien-store/src/main/java/shop/alien/store/service/impl/StoreOperationalStatisticsServiceImpl.java

@@ -1,6 +1,8 @@
 package shop.alien.store.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -45,6 +47,7 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
     private final StoreCommentAppealMapper storeCommentAppealMapper;
     private final StorePriceMapper storePriceMapper;
     private final StoreUserMapper storeUserMapper;
+    private final StoreOperationalStatisticsHistoryMapper statisticsHistoryMapper;
 
     private static final String DATE_FORMAT = "yyyy-MM-dd";
 
@@ -85,6 +88,9 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
             // 6. 价目表排名统计
             statistics.setPriceListRanking(getPriceListRanking(storeId, startDate, endDate));
 
+            // 保存统计数据到历史表
+            saveStatisticsHistory(storeId, startTime, endTime, statistics);
+
         } catch (ParseException e) {
             log.error("StoreOperationalStatisticsServiceImpl.getStatistics - 时间解析错误", e);
             throw new RuntimeException("时间格式错误: " + e.getMessage());
@@ -105,7 +111,7 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
         comparison.setPreviousStartTime(previousStartTime);
         comparison.setPreviousEndTime(previousEndTime);
 
-        // 获取当期和上期的统计数据
+        // 获取当期和上期的统计数据(getStatistics方法内部已经保存了历史记录)
         StoreOperationalStatisticsVo currentStatistics = getStatistics(storeId, currentStartTime, currentEndTime);
         StoreOperationalStatisticsVo previousStatistics = getStatistics(storeId, previousStartTime, previousEndTime);
 
@@ -120,6 +126,34 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
     }
 
     /**
+     * 保存统计数据到历史表
+     */
+    private void saveStatisticsHistory(Integer storeId, String startTime, String endTime, StoreOperationalStatisticsVo statistics) {
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+            Date startDate = sdf.parse(startTime);
+            Date endDate = sdf.parse(endTime);
+
+            StoreOperationalStatisticsHistory history = new StoreOperationalStatisticsHistory();
+            history.setStoreId(storeId);
+            history.setStartTime(startDate);
+            history.setEndTime(endDate);
+            history.setQueryTime(new Date());
+            
+            // 将统计数据序列化为JSON字符串
+            String statisticsJson = JSON.toJSONString(statistics);
+            history.setStatisticsData(statisticsJson);
+
+            statisticsHistoryMapper.insert(history);
+            log.info("保存统计数据到历史表成功 - storeId={}, startTime={}, endTime={}", storeId, startTime, endTime);
+        } catch (Exception e) {
+            log.error("保存统计数据到历史表失败 - storeId={}, startTime={}, endTime={}, error={}", 
+                    storeId, startTime, endTime, e.getMessage(), e);
+            // 保存失败不影响主流程,只记录日志
+        }
+    }
+
+    /**
      * 获取流量数据
      */
     private StoreOperationalStatisticsVo.TrafficData getTrafficData(Integer storeId, Date startDate, Date endDate) {
@@ -571,4 +605,59 @@ public class StoreOperationalStatisticsServiceImpl implements StoreOperationalSt
             return BigDecimal.ZERO;
         }
     }
+
+    @Override
+    public List<StoreOperationalStatisticsHistory> getHistoryList(Integer storeId) {
+        log.info("StoreOperationalStatisticsServiceImpl.getHistoryList - storeId={}", storeId);
+        
+        LambdaQueryWrapper<StoreOperationalStatisticsHistory> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(StoreOperationalStatisticsHistory::getStoreId, storeId)
+               .eq(StoreOperationalStatisticsHistory::getDeleteFlag, 0)
+               .orderByDesc(StoreOperationalStatisticsHistory::getQueryTime);
+        
+        return statisticsHistoryMapper.selectList(wrapper);
+    }
+
+    @Override
+    public boolean deleteHistory(Integer id) {
+        log.info("StoreOperationalStatisticsServiceImpl.deleteHistory - id={}", id);
+        
+        try {
+            // 使用逻辑删除
+            LambdaUpdateWrapper<StoreOperationalStatisticsHistory> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.eq(StoreOperationalStatisticsHistory::getId, id)
+                   .eq(StoreOperationalStatisticsHistory::getDeleteFlag, 0)
+                   .set(StoreOperationalStatisticsHistory::getDeleteFlag, 1);
+            
+            int result = statisticsHistoryMapper.update(null, wrapper);
+            return result > 0;
+        } catch (Exception e) {
+            log.error("删除历史统计记录失败 - id={}, error={}", id, e.getMessage(), e);
+            return false;
+        }
+    }
+
+    @Override
+    public boolean batchDeleteHistory(List<Integer> ids) {
+        log.info("StoreOperationalStatisticsServiceImpl.batchDeleteHistory - ids={}", ids);
+        
+        if (ids == null || ids.isEmpty()) {
+            log.warn("批量删除历史统计记录失败,ID列表为空");
+            return false;
+        }
+        
+        try {
+            // 使用逻辑删除
+            LambdaUpdateWrapper<StoreOperationalStatisticsHistory> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.in(StoreOperationalStatisticsHistory::getId, ids)
+                   .eq(StoreOperationalStatisticsHistory::getDeleteFlag, 0)
+                   .set(StoreOperationalStatisticsHistory::getDeleteFlag, 1);
+            
+            int result = statisticsHistoryMapper.update(null, wrapper);
+            return result > 0;
+        } catch (Exception e) {
+            log.error("批量删除历史统计记录失败 - ids={}, error={}", ids, e.getMessage(), e);
+            return false;
+        }
+    }
 }