xyc
9 小时以前 1c9505d00bd076898cdc5a7d916106e175a91119
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.trafficaudit.freightforecast.controller;
 
import com.trafficaudit.common.Result;
import com.trafficaudit.freightforecast.dto.BelowForecastRequest;
import com.trafficaudit.freightforecast.dto.FreightForecastAnalysis;
import com.trafficaudit.freightforecast.entity.FreightForecastAboveEnterprise;
import com.trafficaudit.freightforecast.entity.FreightForecastBaseMonth;
import com.trafficaudit.freightforecast.entity.FreightForecastBelowResult;
import com.trafficaudit.freightforecast.entity.FreightForecastReportRound;
import com.trafficaudit.freightforecast.service.FreightForecastExcelExporter;
import com.trafficaudit.freightforecast.service.FreightForecastService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/api/freight-forecast")
public class FreightForecastController {
 
    @Resource
    private FreightForecastService service;
    @Resource
    private FreightForecastExcelExporter exporter;
 
    @GetMapping("/overview")
    public Result<Map<String, Object>> overview(@RequestParam String period) {
        try {
            return Result.ok(service.overview(period));
        } catch (Exception e) {
            return error(e);
        }
    }
 
    @GetMapping("/base")
    public Result<List<FreightForecastBaseMonth>> base(@RequestParam int year) {
        try {
            return Result.ok(service.baseMonths(year));
        } catch (Exception e) {
            return error(e);
        }
    }
 
    @PostMapping("/base/import")
    public Result<Map<String, Object>> importBase(@RequestParam("file") MultipartFile file,
                                                  @RequestParam int year) {
        try {
            List<FreightForecastBaseMonth> rows = service.importBase(file, year);
            Map<String, Object> data = new LinkedHashMap<>();
            data.put("count", rows.size());
            data.put("year", year);
            data.put("rows", rows);
            return Result.ok(data);
        } catch (Exception e) {
            log.error("Freight forecast base import failed", e);
            return error(e);
        }
    }
 
    @PostMapping("/above/calculate")
    public Result<Map<String, Object>> calculateAbove(@RequestParam String period) {
        try {
            return Result.ok(service.calculateAbove(period));
        } catch (Exception e) {
            log.error("Freight forecast above calculation failed", e);
            return error(e);
        }
    }
 
    @GetMapping("/above/history")
    public Result<Map<String, Object>> aboveHistory(@RequestParam int statYear,
                                                    @RequestParam String period) {
        try {
            return Result.ok(service.aboveHistory(statYear, period));
        } catch (Exception e) {
            log.error("Freight forecast above history query failed", e);
            return error(e);
        }
    }
 
    @GetMapping("/above")
    public Result<List<FreightForecastAboveEnterprise>> above(
            @RequestParam String period,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String keyword) {
        try {
            return Result.ok(service.aboveRows(period, city, keyword));
        } catch (Exception e) {
            return error(e);
        }
    }
 
    @GetMapping("/below")
    public Result<List<FreightForecastBelowResult>> below(@RequestParam String period) {
        try {
            return Result.ok(service.belowResults(period));
        } catch (Exception e) {
            return error(e);
        }
    }
 
    @PostMapping("/below/calculate")
    public Result<FreightForecastBelowResult> calculateBelow(@RequestBody BelowForecastRequest request) {
        try {
            return Result.ok(service.calculateBelow(request));
        } catch (Exception e) {
            log.error("Freight forecast below calculation failed", e);
            return error(e);
        }
    }
 
    @GetMapping("/report/rounds")
    public Result<List<FreightForecastReportRound>> reportRounds(@RequestParam String period) {
        try {
            return Result.ok(service.reportRounds(period));
        } catch (Exception e) {
            return error(e);
        }
    }
 
    @PostMapping("/report/import")
    public Result<FreightForecastReportRound> importReport(
            @RequestParam("file") MultipartFile file,
            @RequestParam String period,
            @RequestParam String stage,
            @RequestParam(required = false) String note) {
        try {
            return Result.ok(service.importReport(file, period, stage, note));
        } catch (Exception e) {
            log.error("Freight forecast report import failed", e);
            return error(e);
        }
    }
 
    @GetMapping("/report/analysis")
    public Result<FreightForecastAnalysis> analysis(
            @RequestParam String period,
            @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource,
            @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) {
        try {
            return Result.ok(service.analyze(period, belowHistorySource, forecastHistorySource));
        } catch (Exception e) {
            log.error("Freight forecast analysis failed", e);
            return error(e);
        }
    }
 
    @GetMapping("/report/export")
    public ResponseEntity<byte[]> export(
            @RequestParam String period,
            @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource,
            @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) throws Exception {
        FreightForecastAnalysis analysis = service.analyze(period, belowHistorySource, forecastHistorySource);
        byte[] bytes = exporter.exportDifferenceRateReport(analysis);
        String year = period.substring(0, 4);
        int month = Integer.parseInt(period.substring(5, 7));
        String fileName = year + "年" + month + "月规上上报差异率情况.xlsx";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        headers.setContentDisposition(ContentDisposition.attachment()
                .filename(fileName, StandardCharsets.UTF_8).build());
        headers.setContentLength(bytes.length);
        return ResponseEntity.ok().headers(headers).body(bytes);
    }
 
    @GetMapping("/report/enterprise-export-by-city")
    public ResponseEntity<byte[]> exportEnterprisesByCity(
            @RequestParam String period,
            @RequestParam(defaultValue = "SCALE_SPLIT_HISTORY_PLUS_MODULE") String belowHistorySource,
            @RequestParam(defaultValue = "HISTORY_PLUS_MODULE") String forecastHistorySource) throws Exception {
        FreightForecastAnalysis analysis = service.analyze(period, belowHistorySource, forecastHistorySource);
        byte[] bytes = exporter.exportCityEnterpriseReports(analysis);
        String year = period.substring(0, 4);
        int month = Integer.parseInt(period.substring(5, 7));
        String fileName = year + "年" + month + "月规上企业上报差异率_分市州.zip";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/zip"));
        headers.setContentDisposition(ContentDisposition.attachment()
                .filename(fileName, StandardCharsets.UTF_8).build());
        headers.setContentLength(bytes.length);
        return ResponseEntity.ok().headers(headers).body(bytes);
    }
 
    private <T> Result<T> error(Exception e) {
        String message = e.getMessage();
        return Result.error(message == null || message.trim().isEmpty() ? "货运测算处理失败" : message);
    }
}