|
@@ -0,0 +1,72 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.entity.ReportBlock;
|
|
|
|
|
+import com.etotem.cfc.mapper.ReportBlockMapper;
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class ReportBlockService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ReportBlockMapper reportBlockMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+
|
|
|
|
|
+ /** 保存或覆盖 blocks(幂等 upsert) */
|
|
|
|
|
+ public void save(String reportType, Long reportId, List<Map<String, Object>> blocks) {
|
|
|
|
|
+ if (blocks == null || blocks.isEmpty()) return;
|
|
|
|
|
+ try {
|
|
|
|
|
+ ReportBlock existing = reportBlockMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<ReportBlock>()
|
|
|
|
|
+ .eq(ReportBlock::getReportType, reportType)
|
|
|
|
|
+ .eq(ReportBlock::getReportId, reportId));
|
|
|
|
|
+ String json = objectMapper.writeValueAsString(blocks);
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ if (existing != null) {
|
|
|
|
|
+ existing.setBlocks(json);
|
|
|
|
|
+ existing.setVersion(existing.getVersion() == null ? 1 : existing.getVersion() + 1);
|
|
|
|
|
+ existing.setUpdatedAt(now);
|
|
|
|
|
+ reportBlockMapper.updateById(existing);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ReportBlock nb = new ReportBlock();
|
|
|
|
|
+ nb.setReportType(reportType);
|
|
|
|
|
+ nb.setReportId(reportId);
|
|
|
|
|
+ nb.setBlocks(json);
|
|
|
|
|
+ nb.setVersion(1);
|
|
|
|
|
+ nb.setCreatedAt(now);
|
|
|
|
|
+ nb.setUpdatedAt(now);
|
|
|
|
|
+ reportBlockMapper.insert(nb);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("保存report_blocks失败 type={} reportId={}: {}", reportType, reportId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 读取 blocks,无记录返回空列表 */
|
|
|
|
|
+ public List<Map<String, Object>> getBlocks(String reportType, Long reportId) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ReportBlock existing = reportBlockMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<ReportBlock>()
|
|
|
|
|
+ .eq(ReportBlock::getReportType, reportType)
|
|
|
|
|
+ .eq(ReportBlock::getReportId, reportId));
|
|
|
|
|
+ if (existing == null || existing.getBlocks() == null || existing.getBlocks().isEmpty()) {
|
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
|
+ }
|
|
|
|
|
+ return objectMapper.readValue(existing.getBlocks(), new TypeReference<List<Map<String, Object>>>() {});
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("读取report_blocks失败 type={} reportId={}: {}", reportType, reportId, e.getMessage());
|
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|