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
package com.ruoyi.manage.excel;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.manage.service.ManageDocumentControlledService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
/**
 * 文件受控申请批量导入
 */
public class ManageDocumentControlledImportListener extends AnalysisEventListener<ManageDocumentControlledImportDto> {
 
    private static final List<String> HEADERS = Arrays.asList("文件名称", "文件版本", "发布日期", "实施日期");
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-M-d");
 
    private final ManageDocumentControlledService manageDocumentControlledService;
    private final List<ManageDocumentControlledImportDto> rows = new ArrayList<>();
 
    public ManageDocumentControlledImportListener(ManageDocumentControlledService manageDocumentControlledService) {
        this.manageDocumentControlledService = manageDocumentControlledService;
    }
 
    @Override
    public void invoke(ManageDocumentControlledImportDto data, AnalysisContext context) {
        if (isBlankRow(data)) {
            return;
        }
        int rowNum = context.readRowHolder().getRowIndex() + 1;
        if (!StringUtils.hasText(data.getName())) {
            throw new BaseException("第" + rowNum + "行:文件名称不能为空");
        }
        data.setPublishDateValue(parseDate(data.getPublishDate(), rowNum, "发布日期"));
        data.setImplementDateValue(parseDate(data.getImplementDate(), rowNum, "实施日期"));
        rows.add(data);
    }
 
    private LocalDate parseDate(String value, int rowNum, String column) {
        if (!StringUtils.hasText(value)) {
            return null;
        }
        String text = value.trim();
        int space = text.indexOf(' ');
        if (space > 0) {
            text = text.substring(0, space);
        }
        text = text.replace("年", "-").replace("月", "-").replace("日", "")
                .replace("/", "-").replace(".", "-");
        try {
            // Excel 里日期单元格可能被读成数字序列(1899-12-30 起算)
            if (text.matches("\\d{5}(\\.\\d+)?")) {
                return LocalDate.of(1899, 12, 30).plusDays((long) Double.parseDouble(text));
            }
            return LocalDate.parse(text, DATE_FORMAT);
        } catch (Exception e) {
            throw new BaseException("第" + rowNum + "行:" + column + "格式不正确,请按 yyyy-MM-dd 填写");
        }
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (rows.isEmpty()) {
            throw new BaseException("导入数据不得为空");
        }
        manageDocumentControlledService.importManageDocumentControlled(rows);
    }
 
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        if (CollectionUtils.isEmpty(headMap) || headMap.size() != HEADERS.size()) {
            throw new BaseException("导入模板有误,请使用最新模板");
        }
        for (int i = 0; i < HEADERS.size(); i++) {
            String head = headMap.get(i);
            if (head == null || !HEADERS.get(i).equals(head.trim())) {
                throw new BaseException("导入模板有误,表头必须为:文件名称、文件版本、发布日期、实施日期");
            }
        }
    }
 
    private boolean isBlankRow(ManageDocumentControlledImportDto data) {
        return data == null
                || (!StringUtils.hasText(data.getName())
                && !StringUtils.hasText(data.getVersion())
                && !StringUtils.hasText(data.getPublishDate())
                && !StringUtils.hasText(data.getImplementDate()));
    }
}