|
|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|