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
205
package com.trafficaudit.dataimport.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trafficaudit.common.Result;
import com.trafficaudit.dataimport.entity.CityBusMonthly;
import com.trafficaudit.dataimport.entity.CityTaxiMonthly;
import com.trafficaudit.dataimport.entity.H2032EnterpriseMonthly;
import com.trafficaudit.dataimport.entity.PassengerEnterpriseMonthly;
import com.trafficaudit.dataimport.mapper.CityBusMonthlyMapper;
import com.trafficaudit.dataimport.mapper.CityTaxiMonthlyMapper;
import com.trafficaudit.dataimport.mapper.H2032EnterpriseMonthlyMapper;
import com.trafficaudit.dataimport.mapper.PassengerEnterpriseMonthlyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 企业解释:独立于审核结果的解释列表与 AI 分析入口。
 * 支持多报表类型:H2032 货运、H2031 公路旅客、CITY_BUS 城市公交、CITY_TAXI 巡游出租。
 */
@Slf4j
@RestController
@RequestMapping("/api/explain")
public class ExplainController {
 
    @Resource
    private H2032EnterpriseMonthlyMapper h2032Mapper;
    @Resource
    private PassengerEnterpriseMonthlyMapper passengerMapper;
    @Resource
    private CityBusMonthlyMapper cityBusMapper;
    @Resource
    private CityTaxiMonthlyMapper cityTaxiMapper;
 
    @GetMapping("/list")
    public Result<Map<String, Object>> list(@RequestParam(value = "reportType", defaultValue = "H2032") String reportType,
                                            @RequestParam(value = "period", required = false) String period,
                                            @RequestParam(value = "keyword", required = false) String keyword,
                                            @RequestParam(value = "hasExplanation", defaultValue = "false") boolean hasExplanation,
                                            @RequestParam(value = "page", defaultValue = "1") long page,
                                            @RequestParam(value = "size", defaultValue = "20") long size) {
        String kw = keyword == null ? "" : keyword.trim();
        boolean hasPeriod = period != null && !period.trim().isEmpty();
        long p = Math.max(page, 1);
        long s = Math.min(Math.max(size, 1), 200);
 
        List<Map<String, Object>> records = new ArrayList<>();
        long total = 0;
 
        if ("H2031".equals(reportType)) {
            LambdaQueryWrapper<PassengerEnterpriseMonthly> w = new LambdaQueryWrapper<PassengerEnterpriseMonthly>()
                .eq(hasPeriod, PassengerEnterpriseMonthly::getReportPeriod, period)
                .isNotNull(hasExplanation, PassengerEnterpriseMonthly::getVerifyExplanation)
                .ne(hasExplanation, PassengerEnterpriseMonthly::getVerifyExplanation, "")
                .like(!kw.isEmpty(), PassengerEnterpriseMonthly::getEnterpriseName, kw)
                .orderByDesc(PassengerEnterpriseMonthly::getId);
            Page<PassengerEnterpriseMonthly> r = passengerMapper.selectPage(new Page<>(p, s), w);
            for (PassengerEnterpriseMonthly e : r.getRecords()) {
                Map<String, Object> row = baseRow(e.getId(), e.getReportPeriod(), e.getRegionCode(),
                        e.getEnterpriseCode(), e.getEnterpriseName(), e.getVerifyExplanation());
                row.put("vehicleTotal", e.getVehicleTotal());
                row.put("seatTotal", e.getSeatTotal());
                row.put("passengerTotal", e.getPassengerTotal());
                row.put("turnoverTotal", e.getTurnoverTotal());
                row.put("avgDistance", e.getAvgDistanceTotal());
                records.add(row);
            }
            total = r.getTotal();
        } else if ("CITY_BUS".equals(reportType)) {
            LambdaQueryWrapper<CityBusMonthly> w = new LambdaQueryWrapper<CityBusMonthly>()
                .eq(hasPeriod, CityBusMonthly::getReportPeriod, period)
                .isNotNull(hasExplanation, CityBusMonthly::getVerifyExplanation)
                .ne(hasExplanation, CityBusMonthly::getVerifyExplanation, "")
                .like(!kw.isEmpty(), CityBusMonthly::getEnterpriseName, kw)
                .orderByDesc(CityBusMonthly::getId);
            Page<CityBusMonthly> r = cityBusMapper.selectPage(new Page<>(p, s), w);
            for (CityBusMonthly e : r.getRecords()) {
                Map<String, Object> row = baseRow(e.getId(), e.getReportPeriod(), e.getRegionCode(),
                        e.getEnterpriseCode(), e.getEnterpriseName(), e.getVerifyExplanation());
                row.put("city", e.getCity());
                row.put("opVehicles", e.getOpVehicles());
                row.put("passengerVolume", e.getPassengerVolume());
                row.put("turnover", e.getTurnover());
                row.put("avgDistance", e.getAvgDistance());
                records.add(row);
            }
            total = r.getTotal();
        } else if ("CITY_TAXI".equals(reportType)) {
            LambdaQueryWrapper<CityTaxiMonthly> w = new LambdaQueryWrapper<CityTaxiMonthly>()
                .eq(hasPeriod, CityTaxiMonthly::getReportPeriod, period)
                .isNotNull(hasExplanation, CityTaxiMonthly::getVerifyExplanation)
                .ne(hasExplanation, CityTaxiMonthly::getVerifyExplanation, "")
                .like(!kw.isEmpty(), CityTaxiMonthly::getCity, kw)
                .orderByDesc(CityTaxiMonthly::getId);
            Page<CityTaxiMonthly> r = cityTaxiMapper.selectPage(new Page<>(p, s), w);
            for (CityTaxiMonthly e : r.getRecords()) {
                Map<String, Object> row = baseRow(e.getId(), e.getReportPeriod(), e.getRegionCode(),
                        null, e.getCity(), e.getVerifyExplanation());
                row.put("city", e.getCity());
                row.put("opVehicles", e.getOpVehicles());
                row.put("tripTotal", e.getTripTotal());
                row.put("passengerVolume", e.getPassengerVolume());
                row.put("turnover", e.getTurnover());
                row.put("avgDistance", e.getAvgDistance());
                records.add(row);
            }
            total = r.getTotal();
        } else {
            LambdaQueryWrapper<H2032EnterpriseMonthly> w = new LambdaQueryWrapper<H2032EnterpriseMonthly>()
                .eq(hasPeriod, H2032EnterpriseMonthly::getReportPeriod, period)
                .isNotNull(hasExplanation, H2032EnterpriseMonthly::getVerifyExplanation)
                .ne(hasExplanation, H2032EnterpriseMonthly::getVerifyExplanation, "")
                .like(!kw.isEmpty(), H2032EnterpriseMonthly::getEnterpriseName, kw)
                .orderByDesc(H2032EnterpriseMonthly::getId);
            Page<H2032EnterpriseMonthly> r = h2032Mapper.selectPage(new Page<>(p, s), w);
            for (H2032EnterpriseMonthly e : r.getRecords()) {
                Map<String, Object> row = baseRow(e.getId(), e.getReportPeriod(), e.getRegionCode(),
                        e.getEnterpriseCode(), e.getEnterpriseName(), e.getVerifyExplanation());
                row.put("vehicleTotal", e.getVehicleTotal());
                row.put("tonsTotal", e.getTonsTotal());
                row.put("freightTotal", e.getFreightTotal());
                row.put("turnoverTotal", e.getTurnoverTotal());
                row.put("avgDistance", e.getAvgDistance());
                records.add(row);
            }
            total = r.getTotal();
        }
 
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("reportType", reportType);
        result.put("records", records);
        result.put("total", total);
        result.put("current", p);
        result.put("size", s);
        return Result.ok(result);
    }
 
    /**
     * 保存企业解释:更新对应来源表(H2032/H2031/CITY_BUS/CITY_TAXI)行的 verifyExplanation。
     * 审核结果页内嵌解释模块与「企业解释」页共用此接口。
     */
    @PostMapping("/save")
    public Result<String> save(@RequestParam("reportType") String reportType,
                               @RequestParam("reportId") Long reportId,
                               @RequestParam(value = "explanation", required = false) String explanation) {
        String exp = explanation == null ? "" : explanation.trim();
        boolean ok = false;
        String t = reportType == null ? "" : reportType;
        try {
            switch (t) {
                case "H2032": {
                    H2032EnterpriseMonthly e = h2032Mapper.selectById(reportId);
                    if (e != null) { e.setVerifyExplanation(exp); h2032Mapper.updateById(e); ok = true; }
                    break;
                }
                case "H2031": {
                    PassengerEnterpriseMonthly e = passengerMapper.selectById(reportId);
                    if (e != null) { e.setVerifyExplanation(exp); passengerMapper.updateById(e); ok = true; }
                    break;
                }
                case "CITY_BUS": {
                    CityBusMonthly e = cityBusMapper.selectById(reportId);
                    if (e != null) { e.setVerifyExplanation(exp); cityBusMapper.updateById(e); ok = true; }
                    break;
                }
                case "CITY_TAXI": {
                    CityTaxiMonthly e = cityTaxiMapper.selectById(reportId);
                    if (e != null) { e.setVerifyExplanation(exp); cityTaxiMapper.updateById(e); ok = true; }
                    break;
                }
                default:
                    throw new RuntimeException("不支持的报表类型:" + reportType);
            }
        } catch (Exception e) {
            log.error("explain save error", e);
            return Result.error("保存失败: " + e.getMessage());
        }
        if (!ok) {
            return Result.error("未找到对应的数据行(reportId=" + reportId + ")");
        }
        return Result.ok(exp);
    }
 
    private Map<String, Object> baseRow(Long id, String reportPeriod, String regionCode,
                                        String enterpriseCode, String enterpriseName, String verifyExplanation) {
        Map<String, Object> row = new LinkedHashMap<>();
        row.put("id", id);
        row.put("reportPeriod", reportPeriod);
        row.put("regionCode", regionCode);
        row.put("enterpriseCode", enterpriseCode);
        row.put("enterpriseName", enterpriseName);
        row.put("verifyExplanation", verifyExplanation);
        return row;
    }
}