Junjie
2 天以前 63b01db83d9aad8a15276b4236a9a22e4aeef065
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.zy.ai.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zy.ai.entity.AiDataAnalysisReport;
import com.zy.ai.service.DataAnalysisFileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
 
@Slf4j
@Service("dataAnalysisFileStorageService")
public class DataAnalysisFileStorageServiceImpl implements DataAnalysisFileStorageService {
 
    @Value("${dataAnalysisStorage.loggingPath:../stock/out/wcs/aiAnalysis}")
    private String basePath;
 
    @Override
    public String saveReport(AiDataAnalysisReport report) {
        try {
            SimpleDateFormat dirFormat = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat fileFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String dateDir = dirFormat.format(report.getCreateTime());
            String timestamp = fileFormat.format(report.getCreateTime());
 
            File dir = new File(basePath, dateDir);
            if (!dir.exists() && !dir.mkdirs()) {
                log.warn("Failed to create analysis storage directory: {}", dir.getAbsolutePath());
                return null;
            }
 
            String fileName = "analysis_" + report.getPeriodType() + "_" + timestamp + ".json";
            File file = new File(dir, fileName);
 
            Map<String, Object> content = new LinkedHashMap<>();
            content.put("periodType", report.getPeriodType());
            content.put("periodStart", report.getPeriodStart());
            content.put("periodEnd", report.getPeriodEnd());
            content.put("triggerType", report.getTriggerType());
            content.put("status", report.getStatus());
            content.put("summary", report.getSummary());
            content.put("structuredData", report.getStructuredData());
            content.put("llmCallCount", report.getLlmCallCount());
            content.put("totalTokens", report.getTotalTokens());
            content.put("createTime", report.getCreateTime());
            content.put("finishTime", report.getFinishTime());
 
            try (OutputStreamWriter writer = new OutputStreamWriter(
                    new FileOutputStream(file), StandardCharsets.UTF_8)) {
                writer.write(JSON.toJSONString(content, SerializerFeature.PrettyFormat,
                        SerializerFeature.WriteDateUseDateFormat));
            }
 
            String relativePath = dateDir + "/" + fileName;
            log.info("Data analysis report saved to file: {}", relativePath);
            return relativePath;
        } catch (Exception e) {
            log.error("Failed to save data analysis report to file", e);
            return null;
        }
    }
}