yeg
2026-09-17 e3f412c5474311a471345ea508db4875c9ec648e
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.talentcard.server.service;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.talentcard.server.business.PageResult;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
import com.talentcard.server.entity.TransportParkingPlateOperationLog;
import com.talentcard.server.entity.TransportParkingPlateRegistration;
import com.talentcard.server.mapper.TransportParkingPlateOperationLogMapper;
import com.talentcard.server.mapper.TransportParkingPlateRegistrationMapper;
import com.talentcard.server.request.ParkingPlateStatusUpdateRequest;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class ParkingPlateSubmissionAdminService {
    private static final String STATUS_SUBMITTED = "SUBMITTED";
    private static final String STATUS_ACTIVE = "ACTIVE";
    private static final String STATUS_REJECTED = "REJECTED";
    private static final String STATUS_CANCELLED = "CANCELLED";
 
    private final TransportParkingPlateRegistrationMapper registrationMapper;
    private final TransportParkingPlateOperationLogMapper operationLogMapper;
 
    public ParkingPlateSubmissionAdminService(TransportParkingPlateRegistrationMapper registrationMapper,
                                               TransportParkingPlateOperationLogMapper operationLogMapper) {
        this.registrationMapper = registrationMapper;
        this.operationLogMapper = operationLogMapper;
    }
 
    @Transactional(readOnly = true)
    public PageResult<Map<String, Object>> page(Long merchantId,
                                                Long parkingLotId,
                                                String keyword,
                                                String talentName,
                                                String talentMobile,
                                                String plateNumber,
                                                String serviceKeyword,
                                                String cardLevel,
                                                String status,
                                                String submissionType,
                                                String submittedStart,
                                                String submittedEnd,
                                                int page,
                                                int size) {
        int safePage = Math.max(page, 1);
        int safeSize = Math.min(Math.max(size, 1), 100);
        LocalDateTime start = startTime(submittedStart, "提交开始日期格式不正确");
        LocalDateTime end = endTime(submittedEnd, "提交结束日期格式不正确");
        String normalizedStatus = normalizeFilterStatus(status);
        String normalizedSubmissionType = normalizeSubmissionType(submissionType, false);
        Long total = registrationMapper.countAdminPage(
                merchantId, parkingLotId, clean(keyword), clean(talentName), clean(talentMobile), clean(plateNumber),
                clean(serviceKeyword), clean(cardLevel), normalizedStatus, normalizedSubmissionType, start, end);
        List<Map<String, Object>> records = registrationMapper.selectAdminPage(
                        merchantId, parkingLotId, clean(keyword), clean(talentName), clean(talentMobile), clean(plateNumber),
                        clean(serviceKeyword), clean(cardLevel), normalizedStatus, normalizedSubmissionType, start, end,
                        safeSize, (safePage - 1) * safeSize)
                .stream()
                .map(this::listView)
                .toList();
        return new PageResult<>(records, safePage, safeSize, total == null ? 0 : total);
    }
 
    @Transactional(readOnly = true)
    public Map<String, Object> detail(Long id) {
        Map<String, Object> row = detailRow(id);
        Map<String, Object> result = listView(row);
 
        Map<String, Object> talent = new LinkedHashMap<>();
        talent.put("talentId", value(row, "talentId", "talent_id"));
        talent.put("talentName", text(row, "talentName", "talent_name"));
        talent.put("talentMobile", text(row, "talentMobile", "talent_mobile"));
        talent.put("cardId", value(row, "cardId", "card_id"));
        talent.put("cardNo", text(row, "cardNo", "card_no"));
        talent.put("cardLevel", text(row, "cardLevel", "card_level"));
        result.put("talentInfo", talent);
 
        Map<String, Object> submission = new LinkedHashMap<>();
        submission.put("plateNumber", text(row, "plateNumber", "plate_number"));
        submission.put("merchantId", value(row, "merchantId", "merchant_id"));
        submission.put("merchantName", text(row, "merchantName", "merchant_name"));
        submission.put("parkingLotId", value(row, "parkingLotId", "parking_lot_id"));
        submission.put("parkingLotName", text(row, "parkingLotName", "parking_lot_name"));
        submission.put("parkingLotAddress", text(row, "parkingLotAddress", "parking_lot_address"));
        submission.put("submissionType", text(row, "submissionType", "submission_type"));
        submission.put("submittedAt", value(row, "submittedAt", "submitted_at"));
        result.put("submissionInfo", submission);
 
        Map<String, Object> statusInfo = new LinkedHashMap<>();
        statusInfo.put("status", text(row, "status"));
        statusInfo.put("statusName", statusName(text(row, "status")));
        statusInfo.put("handledAt", value(row, "handledAt", "handled_at"));
        statusInfo.put("handledBy", value(row, "handledBy", "handled_by"));
        statusInfo.put("handledByName", first(text(row, "handledByName", "handled_by_name"), handlerFallback(value(row, "handledBy", "handled_by"))));
        statusInfo.put("handleRemark", text(row, "handleRemark", "handle_remark"));
        result.put("statusInfo", statusInfo);
        return result;
    }
 
    @Transactional(readOnly = true)
    public List<Map<String, Object>> operationLogs(Long id) {
        Long registrationId = requireId(id);
        detailRow(registrationId);
        return operationLogMapper.selectList(new QueryWrapper<TransportParkingPlateOperationLog>()
                        .eq("registration_id", registrationId)
                        .orderByAsc("created_at", "id"))
                .stream()
                .map(this::operationLogView)
                .toList();
    }
 
    @Transactional
    public Map<String, Object> updateStatus(Long id, ParkingPlateStatusUpdateRequest request, Long operatorId) {
        Long registrationId = requireId(id);
        TransportParkingPlateRegistration current = registrationMapper.selectByIdForUpdate(registrationId);
        if (current == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "车牌提交记录不存在");
        }
        String nextStatus = normalizeStatus(request.status());
        String remark = clean(request.remark());
        LocalDateTime handledAt = STATUS_SUBMITTED.equals(nextStatus) ? null : LocalDateTime.now();
        UpdateWrapper<TransportParkingPlateRegistration> update = new UpdateWrapper<TransportParkingPlateRegistration>()
                .set("status", nextStatus)
                .set("handled_by", handledAt == null ? null : operatorId)
                .set("handled_at", handledAt)
                .set("handle_remark", remark.isBlank() ? null : remark)
                .eq("id", registrationId)
                .eq("deleted", 0);
        int updated = registrationMapper.update(null, update);
        if (updated != 1) {
            throw new BusinessException(ErrorCode.CONFLICT, "车牌提交记录状态已变化,请刷新后重试");
        }
        TransportParkingPlateOperationLog log = new TransportParkingPlateOperationLog();
        log.setRegistrationId(current.getId());
        log.setRegistrationNo(current.getRegistrationNo());
        log.setOperationType("STATUS_UPDATE");
        log.setBeforeStatus(current.getStatus());
        log.setAfterStatus(nextStatus);
        log.setOperatorType("ADMIN");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName(operatorId));
        log.setRemark(remark.isBlank() ? null : remark);
        log.setCreatedAt(LocalDateTime.now());
        if (operationLogMapper.insert(log) != 1) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "车牌提交操作日志保存失败");
        }
        return detail(registrationId);
    }
 
    @Transactional(readOnly = true)
    public byte[] exportExcel(Long merchantId,
                              Long parkingLotId,
                              String keyword,
                              String talentName,
                              String talentMobile,
                              String plateNumber,
                              String serviceKeyword,
                              String cardLevel,
                              String status,
                              String submissionType,
                              String submittedStart,
                              String submittedEnd,
                              List<Long> ids) {
        LocalDateTime start = startTime(submittedStart, "提交开始日期格式不正确");
        LocalDateTime end = endTime(submittedEnd, "提交结束日期格式不正确");
        List<Long> selectedIds = exportIds(ids);
        List<Map<String, Object>> records;
        if (selectedIds.isEmpty()) {
            records = registrationMapper.selectAdminPage(
                    merchantId, parkingLotId, clean(keyword), clean(talentName), clean(talentMobile), clean(plateNumber),
                    clean(serviceKeyword), clean(cardLevel), normalizeFilterStatus(status),
                    normalizeSubmissionType(submissionType, false), start, end, 200000, 0);
        } else {
            records = selectedIds.stream().map(this::detailRow).toList();
        }
        String[] headers = {
                "序号", "提交单号", "持卡人名称", "持卡人电话", "人才等级", "人才卡号", "车牌号", "停车商家", "停车场",
                "提交时间", "提交类型", "状态", "处理人", "处理时间", "处理备注"
        };
        try (Workbook workbook = new XSSFWorkbook(); ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet("车牌提交");
            sheet.createFreezePane(0, 1);
            CellStyle headerStyle = headerStyle(workbook);
            Row headerRow = sheet.createRow(0);
            for (int index = 0; index < headers.length; index++) {
                headerRow.createCell(index).setCellValue(headers[index]);
                headerRow.getCell(index).setCellStyle(headerStyle);
            }
            int rowIndex = 1;
            for (Map<String, Object> record : records) {
                Row row = sheet.createRow(rowIndex);
                List<String> values = exportValues(rowIndex, record);
                for (int index = 0; index < values.size(); index++) {
                    row.createCell(index).setCellValue(values.get(index));
                }
                rowIndex++;
            }
            int[] widths = { 8, 24, 16, 18, 16, 20, 16, 28, 28, 24, 12, 12, 16, 24, 36 };
            for (int index = 0; index < widths.length; index++) {
                sheet.setColumnWidth(index, widths[index] * 256);
            }
            workbook.write(output);
            return output.toByteArray();
        } catch (IOException exception) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "车牌提交导出失败");
        }
    }
 
    private Map<String, Object> listView(Map<String, Object> row) {
        Map<String, Object> result = new LinkedHashMap<>();
        String status = text(row, "status");
        result.put("id", value(row, "id"));
        result.put("registrationNo", text(row, "registrationNo", "registration_no"));
        result.put("talentName", text(row, "talentName", "talent_name"));
        result.put("talentMobile", text(row, "talentMobile", "talent_mobile"));
        result.put("cardLevel", text(row, "cardLevel", "card_level"));
        result.put("cardNo", text(row, "cardNo", "card_no"));
        result.put("plateNumber", text(row, "plateNumber", "plate_number"));
        result.put("merchantId", value(row, "merchantId", "merchant_id"));
        result.put("merchantName", text(row, "merchantName", "merchant_name"));
        result.put("parkingLotId", value(row, "parkingLotId", "parking_lot_id"));
        result.put("parkingLotName", text(row, "parkingLotName", "parking_lot_name"));
        result.put("submittedAt", value(row, "submittedAt", "submitted_at"));
        result.put("submissionType", text(row, "submissionType", "submission_type"));
        result.put("status", status);
        result.put("statusName", statusName(status));
        result.put("handledAt", value(row, "handledAt", "handled_at"));
        result.put("handledByName", first(text(row, "handledByName", "handled_by_name"), handlerFallback(value(row, "handledBy", "handled_by"))));
        result.put("handleRemark", text(row, "handleRemark", "handle_remark"));
        return result;
    }
 
    private Map<String, Object> operationLogView(TransportParkingPlateOperationLog log) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("id", log.getId());
        result.put("registrationId", log.getRegistrationId());
        result.put("registrationNo", log.getRegistrationNo());
        result.put("operationType", log.getOperationType());
        result.put("beforeStatus", log.getBeforeStatus());
        result.put("beforeStatusName", statusName(log.getBeforeStatus()));
        result.put("afterStatus", log.getAfterStatus());
        result.put("afterStatusName", statusName(log.getAfterStatus()));
        result.put("operatorType", log.getOperatorType());
        result.put("operatorId", log.getOperatorId());
        result.put("operatorName", log.getOperatorName());
        result.put("remark", log.getRemark());
        result.put("createdAt", log.getCreatedAt());
        return result;
    }
 
    private List<String> exportValues(int index, Map<String, Object> row) {
        List<String> values = new ArrayList<>();
        values.add(String.valueOf(index));
        values.add(text(row, "registrationNo", "registration_no"));
        values.add(text(row, "talentName", "talent_name"));
        values.add(text(row, "talentMobile", "talent_mobile"));
        values.add(text(row, "cardLevel", "card_level"));
        values.add(text(row, "cardNo", "card_no"));
        values.add(text(row, "plateNumber", "plate_number"));
        values.add(text(row, "merchantName", "merchant_name"));
        values.add(text(row, "parkingLotName", "parking_lot_name"));
        values.add(string(value(row, "submittedAt", "submitted_at")));
        values.add(text(row, "submissionType", "submission_type"));
        values.add(statusName(text(row, "status")));
        values.add(first(text(row, "handledByName", "handled_by_name"), handlerFallback(value(row, "handledBy", "handled_by"))));
        values.add(string(value(row, "handledAt", "handled_at")));
        values.add(text(row, "handleRemark", "handle_remark"));
        return values;
    }
 
    private Map<String, Object> detailRow(Long id) {
        Long registrationId = requireId(id);
        Map<String, Object> row = registrationMapper.selectAdminDetail(registrationId);
        if (row == null || row.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "车牌提交记录不存在");
        }
        return row;
    }
 
    private String normalizeFilterStatus(String raw) {
        String value = clean(raw);
        return value.isBlank() ? "" : normalizeStatus(value);
    }
 
    private String normalizeStatus(String raw) {
        String value = clean(raw).toUpperCase(Locale.ROOT).replace("-", "_");
        return switch (value) {
            case "SUBMITTED", "PENDING", "待处理", "待审核" -> STATUS_SUBMITTED;
            case "ACTIVE", "PROCESSED", "PASSED", "APPROVED", "已处理", "处理完成", "通过", "已通过" -> STATUS_ACTIVE;
            case "REJECTED", "REFUSED", "DENIED", "已拒绝", "拒绝", "驳回", "已驳回" -> STATUS_REJECTED;
            case "CANCELLED", "CANCELED", "已取消", "取消" -> STATUS_CANCELLED;
            default -> throw new BusinessException(ErrorCode.BAD_REQUEST, "处理状态不正确");
        };
    }
 
    private String normalizeSubmissionType(String raw, boolean required) {
        String value = clean(raw);
        if (value.isBlank()) {
            if (required) {
                throw new BusinessException(ErrorCode.BAD_REQUEST, "提交类型不能为空");
            }
            return "";
        }
        String upper = value.toUpperCase(Locale.ROOT);
        return switch (upper) {
            case "CREATE", "ADD", "NEW", "新增" -> "新增";
            case "UPDATE", "MODIFY", "EDIT", "修改" -> "修改";
            default -> throw new BusinessException(ErrorCode.BAD_REQUEST, "提交类型不正确");
        };
    }
 
    private String statusName(String status) {
        return switch (clean(status).toUpperCase(Locale.ROOT)) {
            case STATUS_ACTIVE -> "已处理";
            case STATUS_REJECTED -> "已拒绝";
            case STATUS_CANCELLED -> "已取消";
            default -> "待处理";
        };
    }
 
    private LocalDateTime startTime(String value, String message) {
        LocalDate date = localDate(value, message);
        return date == null ? null : date.atStartOfDay();
    }
 
    private LocalDateTime endTime(String value, String message) {
        LocalDate date = localDate(value, message);
        return date == null ? null : date.plusDays(1).atStartOfDay();
    }
 
    private LocalDate localDate(String value, String message) {
        String text = clean(value);
        if (text.isBlank()) {
            return null;
        }
        try {
            return LocalDate.parse(text);
        } catch (DateTimeParseException exception) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, message);
        }
    }
 
    private CellStyle headerStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return style;
    }
 
    private Long requireId(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, "车牌提交记录ID不能为空");
        }
        return id;
    }
 
    private List<Long> exportIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return List.of();
        }
        Set<Long> uniqueIds = new LinkedHashSet<>();
        for (Long id : ids) {
            uniqueIds.add(requireId(id));
        }
        if (uniqueIds.size() > 1000) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, "单次最多导出1000条车牌记录");
        }
        return List.copyOf(uniqueIds);
    }
 
    private String operatorName(Long operatorId) {
        if (operatorId == null) {
            return "管理员";
        }
        return first(operationLogMapper.selectOperatorName(operatorId), "管理员" + operatorId);
    }
 
    private Object value(Map<String, Object> row, String... keys) {
        for (String key : keys) {
            if (row.containsKey(key)) {
                return row.get(key);
            }
        }
        return null;
    }
 
    private String text(Map<String, Object> row, String... keys) {
        Object value = value(row, keys);
        return value == null ? "" : String.valueOf(value).trim();
    }
 
    private String first(String... values) {
        for (String value : values) {
            String text = clean(value);
            if (!text.isBlank()) {
                return text;
            }
        }
        return "";
    }
 
    private String handlerFallback(Object value) {
        return value instanceof Number number ? "管理员" + number.longValue() : "";
    }
 
    private String string(Object value) {
        return value == null ? "" : String.valueOf(value);
    }
 
    private String clean(String value) {
        return value == null ? "" : value.trim();
    }
}