264 lines
12 KiB
Java
264 lines
12 KiB
Java
package shkd.repc.recon.formplugin;
|
||
|
||
import com.alibaba.fastjson.JSON;
|
||
import kd.bos.context.RequestContext;
|
||
import kd.bos.dataentity.entity.DynamicObject;
|
||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||
import kd.bos.dataentity.resource.ResManager;
|
||
import kd.bos.dataentity.utils.StringUtils;
|
||
import kd.bos.entity.EntityMetadataCache;
|
||
import kd.bos.entity.MainEntityType;
|
||
import kd.bos.entity.operate.result.OperationResult;
|
||
import kd.bos.exception.KDBizException;
|
||
import kd.bos.fileservice.FileItem;
|
||
import kd.bos.fileservice.FileService;
|
||
import kd.bos.fileservice.FileServiceFactory;
|
||
import kd.bos.form.FormShowParameter;
|
||
import kd.bos.form.ShowType;
|
||
import kd.bos.form.control.Control;
|
||
import kd.bos.form.control.events.ItemClickEvent;
|
||
import kd.bos.form.dto.ImportPermissionParam;
|
||
import kd.bos.form.events.AfterDoOperationEventArgs;
|
||
import kd.bos.form.plugin.AbstractFormPlugin;
|
||
import kd.bos.form.plugin.importentry.ImportEntryModelExporter;
|
||
import kd.bos.form.plugin.importentry.ImportEntryWriterBuilder;
|
||
import kd.bos.form.util.ImportOperationLog;
|
||
import kd.bos.list.BillList;
|
||
import kd.bos.orm.query.QCP;
|
||
import kd.bos.orm.query.QFilter;
|
||
import kd.bos.service.metadata.export.ExportWriterFormat;
|
||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||
import kd.bos.url.UrlService;
|
||
import kd.sdk.plugin.Plugin;
|
||
import org.apache.poi.ss.usermodel.*;
|
||
import org.apache.poi.xssf.usermodel.*;
|
||
|
||
import java.awt.*;
|
||
import java.awt.Color;
|
||
import java.io.*;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.*;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 二开导入模板
|
||
*/
|
||
public class ContractMaterialTemplatePlugin extends AbstractFormPlugin implements Plugin {
|
||
|
||
|
||
@Override
|
||
public void registerListener(EventObject e) {
|
||
super.registerListener(e);
|
||
//工具栏控件
|
||
this.addItemClickListeners("qeug_advcontoolbarap21");
|
||
}
|
||
|
||
@Override
|
||
public void afterDoOperation(AfterDoOperationEventArgs evt) {
|
||
String operateKey = evt.getOperateKey();
|
||
if (StringUtils.equals("exporttemplate",operateKey)){
|
||
//根据导入模板配置导出数据
|
||
QFilter number = new QFilter("number", QCP.equals, "recon_contractbill_IMPT_ENTRY");
|
||
DynamicObject template = BusinessDataServiceHelper.loadSingle("bos_importentry_template", number.toArray());
|
||
if (null == template) {
|
||
this.getView().showMessage("请先维护引入模板");
|
||
return;
|
||
}
|
||
// //表头字段
|
||
// List<DynamicObject> list = new ArrayList<>();
|
||
// DynamicObjectCollection entry = template.getDynamicObjectCollection("treeentryentity");
|
||
// if (null != entry && entry.size() != 0) {
|
||
// for (int i = 0; i < entry.size(); i++) {
|
||
// DynamicObject dynamicObject = entry.get(i);
|
||
// if (dynamicObject.getBoolean("isimport")) {
|
||
// list.add(dynamicObject);
|
||
// }
|
||
// }
|
||
// }
|
||
// if (list.size() == 0) return;
|
||
// //构建导出前四行数据
|
||
//
|
||
// // 创建Excel工作簿
|
||
// XSSFWorkbook workbook = new XSSFWorkbook();
|
||
// XSSFSheet sheet = workbook.createSheet("明细 # qeug_orderformentry");
|
||
//
|
||
// // 第一行:明细标题
|
||
// XSSFRow row1 = sheet.createRow(0);
|
||
// XSSFCell cell1 = row1.createCell(0);
|
||
// cell1.setCellValue("明细 # qeug_orderformentry");
|
||
// XSSFCellStyle style1 = workbook.createCellStyle();
|
||
// XSSFFont font1 = workbook.createFont();
|
||
// font1.setFontName("宋体");
|
||
// font1.setFontHeightInPoints((short) 11);
|
||
// style1.setFont(font1);
|
||
// style1.setAlignment(HorizontalAlignment.CENTER);
|
||
// style1.setVerticalAlignment(VerticalAlignment.CENTER);
|
||
// sheet.setColumnWidth(0, 31 * 256); // 设置列宽
|
||
// row1.setHeightInPoints(14.4f);
|
||
// cell1.setCellStyle(style1);
|
||
//
|
||
// // 第二行:说明文本
|
||
// XSSFRow row2 = sheet.createRow(1);
|
||
// XSSFCell cell2 = row2.createCell(0);
|
||
// cell2.setCellValue("1、请将鼠标移到灰色标题行查看字段录入要求。2、红色带星号(*)的字段为必录字段。");
|
||
// XSSFCellStyle style2 = workbook.createCellStyle();
|
||
// XSSFFont font2 = workbook.createFont();
|
||
// font2.setFontName("Calibri");
|
||
// font2.setFontHeightInPoints((short) 11);
|
||
// style2.setFont(font2);
|
||
// style2.setAlignment(HorizontalAlignment.LEFT);
|
||
// style2.setVerticalAlignment(VerticalAlignment.CENTER);
|
||
// style2.setWrapText(true); // 自动换行
|
||
// row2.setHeightInPoints(60);
|
||
// sheet.setColumnWidth(0, 31 * 256); // 设置列宽
|
||
// cell2.setCellStyle(style2);
|
||
//
|
||
// // 第三行:list中的entitynumber
|
||
// XSSFRow row3 = sheet.createRow(2);
|
||
// for (int i = 0; i < list.size(); i++) {
|
||
// DynamicObject dynamicObject = list.get(i);
|
||
// String entityNumber = null;
|
||
// String importProp = dynamicObject.getString("importprop");
|
||
// if (!("").equals(importProp)) {
|
||
// entityNumber = dynamicObject.getString("entitynumber") + "." + dynamicObject.getString("importprop");
|
||
// } else {
|
||
// entityNumber = dynamicObject.getString("entitynumber");
|
||
// }
|
||
//
|
||
// XSSFCell cell = row3.createCell(i);
|
||
// cell.setCellValue(entityNumber);
|
||
// XSSFCellStyle style3 = workbook.createCellStyle();
|
||
// XSSFFont font3 = workbook.createFont();
|
||
// font3.setFontName("宋体");
|
||
// font3.setFontHeightInPoints((short) 11);
|
||
// style3.setFont(font3);
|
||
// style3.setAlignment(HorizontalAlignment.CENTER);
|
||
// style3.setVerticalAlignment(VerticalAlignment.CENTER);
|
||
// sheet.setColumnWidth(i, 31 * 256); // 设置列宽
|
||
// row3.setHeightInPoints(14.4f);
|
||
// cell.setCellStyle(style3);
|
||
// }
|
||
//
|
||
// // 第四行:list中的entityname
|
||
// XSSFRow row4 = sheet.createRow(3);
|
||
// for (int i = 0; i < list.size(); i++) {
|
||
// DynamicObject dynamicObject = list.get(i);
|
||
// String entityName = dynamicObject.getString("entitydescription");
|
||
// XSSFCell cell = row4.createCell(i);
|
||
// cell.setCellValue(entityName);
|
||
// XSSFCellStyle style4 = workbook.createCellStyle();
|
||
// XSSFFont font4 = workbook.createFont();
|
||
// font4.setFontName("Calibri");
|
||
// font4.setFontHeightInPoints((short) 11);
|
||
// font4.setColor(IndexedColors.RED.getIndex());
|
||
// style4.setFont(font4);
|
||
// style4.setAlignment(HorizontalAlignment.CENTER);
|
||
// style4.setVerticalAlignment(VerticalAlignment.CENTER);
|
||
// style4.setFillForegroundColor(new XSSFColor(new Color(192, 192, 192), new DefaultIndexedColorMap())); // 灰色背景
|
||
// style4.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||
// sheet.setColumnWidth(i, 31 * 256); // 设置列宽
|
||
// row4.setHeightInPoints(14.4f);
|
||
// cell.setCellStyle(style4);
|
||
// }
|
||
// // 上传Excel文件并获取下载路径
|
||
// String uploadedFilePath = uploadExcel(workbook);
|
||
// String downloadUrl = RequestContext.get().getClientFullContextPath() + "/attachment/download.do?path=" + uploadedFilePath;
|
||
// this.getView().openUrl(downloadUrl);
|
||
|
||
try {
|
||
this.pushBillTemplate(template);
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
private void pushBillTemplate(DynamicObject templateObj) throws IOException {
|
||
String billFormId = "recon_contractbill";
|
||
Object pkValue = templateObj.getPkValue();
|
||
Object[] templates = new Object[]{pkValue};
|
||
if (templates != null && templates.length != 0) {
|
||
int maxTemplate = this.getMaxDownloadTemplateCountProperty();
|
||
if (templates.length > maxTemplate) {
|
||
this.getView().showMessage(String.format(ResManager.loadKDString("只允许同时下载%s个模板。", "DownloadTemplatePlugin_5", "bos-import", new Object[0]), maxTemplate));
|
||
} else {
|
||
MainEntityType mainEntityType = EntityMetadataCache.getDataEntityType(billFormId);
|
||
Object[] var6 = templates;
|
||
int var7 = templates.length;
|
||
|
||
for(int var8 = 0; var8 < var7; ++var8) {
|
||
Object template = var6[var8];
|
||
String fileName = this.createFileName(mainEntityType, (Long)template);
|
||
ExportWriterFormat format = (new ImportEntryWriterBuilder()).build(billFormId, (Long)template);
|
||
ImportEntryModelExporter dataExporter = new ImportEntryModelExporter(format);
|
||
|
||
try {
|
||
dataExporter.init((String)null);
|
||
dataExporter.write();
|
||
String url = dataExporter.flush(this.getModel().getDataEntityType(), fileName);
|
||
url = UrlService.getAttachmentFullUrl(url);
|
||
this.getView().download(url);
|
||
this.getView().sendFormAction(this.getView());
|
||
} finally {
|
||
dataExporter.close();
|
||
}
|
||
}
|
||
|
||
ImportOperationLog.getInstance().createAppLog(billFormId, ResManager.loadKDString("下载", "DownLoadEntryTemplatePlugin_0", "bos-import", new Object[0]), ResManager.loadKDString("单据体引入模板下载成功", "DownLoadEntryTemplatePlugin_1", "bos-import", new Object[0]));
|
||
}
|
||
}
|
||
//}
|
||
}
|
||
|
||
private String createFileName(MainEntityType mainEntityType, Long templateId) {
|
||
DynamicObject templateData = BusinessDataServiceHelper.loadSingle(templateId, "bos_importentry_template");
|
||
String fileName = mainEntityType.getDisplayName().getLocaleValue() + "_";
|
||
if (templateData != null) {
|
||
fileName = fileName + templateData.get("name");
|
||
} else {
|
||
fileName = fileName + ResManager.loadKDString("无模板名称", "ImportTemplatePlugin_24", "bos-import", new Object[0]);
|
||
}
|
||
|
||
return fileName;
|
||
}
|
||
|
||
private int getMaxDownloadTemplateCountProperty() {
|
||
try {
|
||
return Integer.parseInt(System.getProperty("max.download.template.count", "20"));
|
||
} catch (NumberFormatException var2) {
|
||
//log.warn(var2);
|
||
return 20;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 上传Excel文件并返回路径
|
||
*/
|
||
private String uploadExcel(XSSFWorkbook workbook) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
|
||
String fileName = "导入模板_" + sdf.format(new Date()) + ".xlsx";
|
||
String pathName = "/offices/" + fileName;
|
||
try {
|
||
OutputStream outputStream = new ByteArrayOutputStream();
|
||
workbook.write(outputStream);
|
||
InputStream inputStream = parse(outputStream);
|
||
|
||
FileService fs = FileServiceFactory.getAttachmentFileService();
|
||
return fs.upload(new FileItem(fileName, pathName, inputStream));
|
||
} catch (Exception e) {
|
||
//logger.error("上传Excel失败", e);
|
||
throw new RuntimeException("上传Excel失败", e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解析输出流为输入流
|
||
*/
|
||
public ByteArrayInputStream parse(final OutputStream out) throws Exception {
|
||
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) out;
|
||
return new ByteArrayInputStream(outputStream.toByteArray());
|
||
}
|
||
} |