jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.bstek.bdf2.export.controller;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
 
import com.bstek.bdf2.export.csv.CvsReportBuilder;
import com.bstek.bdf2.export.excel.ExcelReportBuilder;
import com.bstek.bdf2.export.excel.ExcelReportModelGenerater;
import com.bstek.bdf2.export.extension.ReportGenerater;
import com.bstek.bdf2.export.extension.ReportBuilder;
import com.bstek.bdf2.export.model.FileExtension;
import com.bstek.bdf2.export.model.ReportForm;
import com.bstek.bdf2.export.model.ReportGridHeader;
import com.bstek.bdf2.export.model.ReportGrid;
import com.bstek.bdf2.export.model.ReportTitle;
import com.bstek.bdf2.export.pdf.PdfReportBuilder;
import com.bstek.bdf2.export.pdf.PdfReportModelGenerater;
import com.bstek.bdf2.export.utils.ExportUtils;
import com.bstek.bdf2.export.view.SupportWidget;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;
import com.itextpdf.text.Document;
 
/**
 * @author matt.yao@bstek.com
 * @since 2.0
 */
@Component(Export2ReportController.BEAN_ID)
public class Export2ReportController implements InitializingBean, ApplicationContextAware {
 
    public static final String BEAN_ID = "bdf2.Export2ReportController";
 
    @Autowired
    @Qualifier(ExcelReportModelGenerater.BEAN_ID)
    public ExcelReportModelGenerater excelReportModelGenerater;
 
    @Autowired
    @Qualifier(ExcelReportBuilder.BEAN_ID)
    public ExcelReportBuilder excelReportBuilder;
 
    @Autowired
    @Qualifier(PdfReportModelGenerater.BEAN_ID)
    public PdfReportModelGenerater pdfReportModelGenerater;
 
    @Autowired
    @Qualifier(PdfReportBuilder.BEAN_ID)
    public PdfReportBuilder pdfReportBuilder;
 
    @Autowired
    @Qualifier(ReportGenerater.BEAN_ID)
    public ReportGenerater commonReportGenerater;
 
    @Autowired
    @Qualifier(CvsReportBuilder.BEAN_ID)
    public CvsReportBuilder cvsReportBuilder;
 
    public int rowAccessWindowSize = 500;
 
    @SuppressWarnings("unchecked")
    @Expose
    public Map<String, String> generateReportFile(Map<String, Object> parameter) throws Exception {
        String fileName = (String) parameter.get("fileName");
        String extension = (String) parameter.get("extension");
        int rowSpace = (Integer) parameter.get("rowSpace");
        String interceptorName = null;
        if (parameter.get("interceptorName") != null) {
            interceptorName = (String) parameter.get("interceptorName");
        }
        String id = UUID.randomUUID().toString();
        String location = ExportUtils.getFileStorePath() + id + "_" + fileName + "." + extension;
        Map<String, Object> titleInfos = (Map<String, Object>) parameter.get("titleInfos");
        List<Map<String, Object>> reportInfos = (List<Map<String, Object>>) parameter.get("reportInfos");
        ReportTitle reportTitle = excelReportModelGenerater.generateReportTitleModel(titleInfos);
        if (FileExtension.xls.equals(extension) || FileExtension.xlsx.equals(extension)) {
            this.generateExcelFile(reportTitle, reportInfos, rowSpace, fileName, location, interceptorName);
        } else if (FileExtension.pdf.equals(extension)) {
            this.generatePdfFile(reportTitle, reportInfos, rowSpace, location, interceptorName);
        } else {
            this.generateOtherFile(extension, reportTitle, reportInfos, rowSpace, location, interceptorName);
        }
        Map<String, String> outParameter = new HashMap<String, String>();
        outParameter.put("id", id);
        outParameter.put("name", fileName + "." + extension);
        return outParameter;
    }
 
    @SuppressWarnings("unchecked")
    private void generatePdfFile(ReportTitle reportTitle, List<Map<String, Object>> reportInfos, int rowSpace, String fileName, String interceptorName) throws Exception {
        FileOutputStream out = new FileOutputStream(fileName);
        Document doc = pdfReportBuilder.createDocument(reportTitle, out);
        try {
            for (Map<String, Object> map : reportInfos) {
                String type = (String) map.get("type");
                if (SupportWidget.grid.name().equals(type)) {
                    Map<String, Object> grid = (Map<String, Object>) map.get("grid");
                    pdfReportBuilder.addGridToDocument(doc, reportTitle, pdfReportModelGenerater.generateReportGridModel(grid, interceptorName));
                    pdfReportBuilder.addNewline(doc, rowSpace);
                } else if (SupportWidget.form.name().equals(type)) {
                    Map<String, Object> form = (Map<String, Object>) map.get("form");
                    ReportForm reportFormModel = pdfReportModelGenerater.generateReportFormModel(form, interceptorName);
                    pdfReportBuilder.addFormToDocument(doc, reportTitle, reportFormModel);
                    pdfReportBuilder.addNewline(doc, rowSpace);
                }
            }
        } finally {
            doc.close();
            out.close();
        }
    }
 
    @SuppressWarnings("unchecked")
    private void generateExcelFile(ReportTitle reportTitle, List<Map<String, Object>> reportInfos, int rowSpace, String fileName, String location, String interceptorName) throws Exception {
        Workbook workbook = null;
        if (location.endsWith(FileExtension.xls)) {
            workbook = excelReportBuilder.createWorkBook2003();
        } else if (location.endsWith(FileExtension.xlsx)) {
            workbook = excelReportBuilder.createWorkBook2007(rowAccessWindowSize);
        }
        Assert.notNull(workbook, "the workbook must not be null");
        Sheet sheet = excelReportBuilder.createSheet(workbook, fileName);
 
        int index = 0;
        int nextRow = 0;
        int bottomColumnHeaderCount = 0;
        for (Map<String, Object> map : reportInfos) {
            String type = (String) map.get("type");
            if (SupportWidget.grid.name().equals(type)) {
                Map<String, Object> grid = (Map<String, Object>) map.get("grid");
                ReportGrid reportGridModel = excelReportModelGenerater.generateReportGridModel(grid, interceptorName);
                if (index == 0) {
                    List<ReportGridHeader> bottomColumnHeaderModelList = new ArrayList<ReportGridHeader>();
                    excelReportBuilder.calculateBottomColumnHeader(reportGridModel.getGridHeaderModelList(), bottomColumnHeaderModelList);
                    bottomColumnHeaderCount = bottomColumnHeaderModelList.size();
                    reportGridModel.setColumnCount(bottomColumnHeaderCount);
                    nextRow = excelReportBuilder.addTitleToSheet(reportTitle, sheet, bottomColumnHeaderCount - 1);
                }
                nextRow = excelReportBuilder.addGridToSheet(reportGridModel, sheet, nextRow);
                nextRow = nextRow + rowSpace;
 
            } else if (SupportWidget.form.name().equals(type)) {
                Map<String, Object> form = (Map<String, Object>) map.get("form");
                ReportForm reportFormModel = excelReportModelGenerater.generateReportFormModel(form, interceptorName);
                if (index == 0) {
                    int lastColumn = reportFormModel.getColumnCount() * 2 - 1;
                    nextRow = excelReportBuilder.addTitleToSheet(reportTitle, sheet, lastColumn);
                }
                nextRow = excelReportBuilder.addFormToSheet(reportFormModel, sheet, nextRow);
                nextRow = nextRow + rowSpace;
            }
            index++;
        }
 
 
        //打印参数设置
        PrintSetup print = sheet.getPrintSetup(); //拿到sheet对象首先
        print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
        
        if(bottomColumnHeaderCount > 10){
            print.setLandscape(true);//横向打印
        }else{
            print.setLandscape(false);//横向打印    
        }
        print.setScale((short) 68);
        print.setFitWidth((short)1);
        sheet.setAutobreaks(false);
        print.setLeftToRight(true);//列从左向右显示
        sheet.setHorizontallyCenter(true);//设置打印页面水平居中
 
        sheet.setMargin(Sheet.BottomMargin,( double ) 0.5 );// 页边距(下)
        sheet.setMargin(Sheet.LeftMargin,( double ) 0.1 );// 页边距(左)
        sheet.setMargin(Sheet.RightMargin,( double ) 0.1 );// 页边距(右)
        sheet.setMargin(Sheet.TopMargin,( double ) 0.5 );// 页边距(上)
 
 
        excelReportBuilder.writeFile(workbook, location);
    }
 
    @SuppressWarnings("unchecked")
    private void generateOtherFile(String extension, ReportTitle reportTitle, List<Map<String, Object>> reportInfos, int rowSpace, String fileName, String interceptorName) throws Exception {
        FileOutputStream out = new FileOutputStream(fileName);
        try {
            for (Map<String, Object> map : reportInfos) {
                String type = (String) map.get("type");
                ReportBuilder builder = this.getReportBuilder(extension);
                Assert.notNull(builder, "ReportBuilder is null.");
                if (SupportWidget.grid.name().equals(type)) {
                    Map<String, Object> grid = (Map<String, Object>) map.get("grid");
                    ReportGrid report = commonReportGenerater.generateReportGridModel(grid, interceptorName);
                    builder.execute(out, report);
                }
            }
        } finally {
            out.close();
        }
    }
 
    public void afterPropertiesSet() throws Exception {
        String size = Configure.getString("bdf2.export.cacheSize");
        if (StringUtils.isNotEmpty(size)) {
            rowAccessWindowSize = Integer.parseInt(size);
        }
        String fileLocation = ExportUtils.getFileStorePath();
        File f = new File(fileLocation);
        if (!f.exists()) {
            f.mkdirs();
        }
        builders = applicationContext.getBeansOfType(ReportBuilder.class).values();
    }
 
    private Collection<ReportBuilder> builders;
 
    private ApplicationContext applicationContext;
 
    public Collection<ReportBuilder> getBuilders() {
        return builders;
    }
 
    public ReportBuilder getReportBuilder(String extension) {
        for (ReportBuilder builder : getBuilders()) {
            if (builder.support(extension)) {
                return builder;
            }
        }
        return null;
    }
 
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
 
}