diff --git a/main/java/shkd/repc/repmd/template/AreaDataImportPlugin.java b/main/java/shkd/repc/repmd/template/AreaDataImportPlugin.java new file mode 100644 index 0000000..15b2f14 --- /dev/null +++ b/main/java/shkd/repc/repmd/template/AreaDataImportPlugin.java @@ -0,0 +1,91 @@ +package shkd.repc.repmd.template; + + +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.dataentity.entity.DynamicObjectCollection; +import kd.bos.dataentity.utils.StringUtils; +import kd.bos.form.CloseCallBack; +import kd.bos.form.FormShowParameter; +import kd.bos.form.ShowType; +import kd.bos.form.StyleCss; +import kd.bos.form.control.Control; +import kd.bos.form.control.events.ItemClickEvent; +import kd.bos.form.events.ClosedCallBackEvent; +import kd.bos.form.plugin.AbstractFormPlugin; +import kd.bos.list.ListShowParameter; +import kd.bos.servicehelper.BusinessDataServiceHelper; +import kd.sdk.plugin.Plugin; +import shkd.repc.repmd.template.util.ExportTemplateUtil; + +import java.util.EventObject; +import java.util.HashMap; +import java.util.List; + + +public class AreaDataImportPlugin extends AbstractFormPlugin implements Plugin { + + @Override + public void registerListener(EventObject e) { + super.registerListener(e); + this.addItemClickListeners("qeug_advcontoolbarap"); + } + + @Override + public void itemClick(ItemClickEvent evt) { + super.itemClick(evt); + if (StringUtils.equals("qeug_templatedownload", evt.getItemKey())) { + FormShowParameter showParameter = createFormParameter(); + this.getView().showForm(showParameter); + } + } + + private FormShowParameter createFormParameter() { + HashMap<String, Object> map = new HashMap<>(2); + map.put("logotype", "areaTemplate"); + FormShowParameter showParameter = new FormShowParameter(); + showParameter.setFormId("qeug_dynamicbill"); + showParameter.setCaption("面积数据模板列表"); + StyleCss inlineStyleCss = new StyleCss(); + inlineStyleCss.setHeight("580"); + inlineStyleCss.setWidth("960"); + showParameter.getOpenStyle().setInlineStyleCss(inlineStyleCss); + showParameter.getOpenStyle().setShowType(ShowType.Modal); + showParameter.setCustomParams(map); + showParameter.setCloseCallBack(new CloseCallBack(this, "template")); + return showParameter; + } + + @Override + public void closedCallBack(ClosedCallBackEvent e) { + super.closedCallBack(e); + String callBackId = e.getActionId(); + if ("template".equals(callBackId)) { + DynamicObjectCollection returnData = (DynamicObjectCollection) e.getReturnData(); + if (null != returnData && returnData.size() != 0) { + for (int i = 0; i < returnData.size(); i++) { + Long id = Long.parseLong(returnData.get(i).getString("qeug_id")); + DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(id, "qeug_areatemplate"); + if (null!=dynamicObject){ + DynamicObjectCollection areaEntry = dynamicObject.getDynamicObjectCollection("qeug_entryentity"); + Control productView= this.getView().getControl("productentry"); + Control areaView= this.getView().getControl("qeug_subentryentity"); + StringBuilder message =new StringBuilder(); + String url; + if (areaEntry.size()!=0){ + //导出自定义模板 + url = ExportTemplateUtil.exportTemplate(areaEntry, message, productView, areaView); + }else { + //导出默认模板 + url = ExportTemplateUtil.exportTemplate(message, productView, areaView); + } + if (null!=url){ + this.getView().openUrl(url); + } + } + } + } + + } + } + +} diff --git a/main/java/shkd/repc/repmd/template/AreaTemplateFromPlugin.java b/main/java/shkd/repc/repmd/template/AreaTemplateFromPlugin.java new file mode 100644 index 0000000..8dcf18f --- /dev/null +++ b/main/java/shkd/repc/repmd/template/AreaTemplateFromPlugin.java @@ -0,0 +1,77 @@ +package shkd.repc.repmd.template; + +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.dataentity.entity.DynamicObjectCollection; +import kd.bos.dataentity.utils.StringUtils; +import kd.bos.form.control.Button; +import kd.bos.form.control.Control; +import kd.bos.form.control.EntryGrid; +import kd.bos.form.events.BeforeClosedEvent; +import kd.bos.form.plugin.AbstractFormPlugin; +import kd.bos.servicehelper.QueryServiceHelper; +import kd.sdk.plugin.Plugin; +import shkd.repc.repmd.template.util.Template; + +import java.util.ArrayList; +import java.util.EventObject; +import java.util.List; + +public class AreaTemplateFromPlugin extends AbstractFormPlugin implements Plugin { + + private static final List<Template> ENTRY_LIST = new ArrayList<>(); + private static final DynamicObjectCollection ENTRY_COLLECTION = new DynamicObjectCollection(); + + @Override + public void afterBindData(EventObject e) { + super.afterBindData(e); + DynamicObjectCollection entry = this.getModel().getEntryEntity("qeug_entryentity"); + if (entry == null || entry.size() == 0) { + DynamicObjectCollection query = QueryServiceHelper.query("qeug_areatemplate", "id,qeug_number,qeug_remark", null); + if (null!=query&& query.size()!=0){ + for (int i = 0; i < query.size(); i++) { + DynamicObject dynamicObject = entry.addNew(); + dynamicObject.set("qeug_id",query.get(i).getString("id")); + dynamicObject.set("qeug_number",query.get(i).getString("qeug_number")); + dynamicObject.set("qeug_remark",query.get(i).getString("qeug_remark")); + } + this.getView().updateView("qeug_entryentity"); + } + } + } + + @Override + public void registerListener(EventObject e) { + super.registerListener(e); + Button button = this.getView().getControl("btnok"); + button.addClickListener(this); + } + + + @Override + public void click(EventObject evt) { + Control source = (Control) evt.getSource(); + String key = source.getKey(); + // 点击确定按钮 + if (StringUtils.equals("btnok", key)) { + ENTRY_COLLECTION.clear(); + EntryGrid entryGrid = this.getControl("qeug_entryentity"); + int[] selectRows = entryGrid.getSelectRows(); + DynamicObjectCollection entity = this.getModel().getEntryEntity("qeug_entryentity"); + if (selectRows != null && selectRows.length > 0) { + for (int selectRow : selectRows) { + DynamicObject dynamicObject = entity.get(selectRow); + ENTRY_COLLECTION.add(dynamicObject); + } + this.getView().close(); + } else { + this.getView().showMessage("请选中一条数据!"); + } + } + } + + @Override + public void beforeClosed(BeforeClosedEvent e) { + super.beforeClosed(e); + this.getView().returnDataToParent(ENTRY_COLLECTION); + } +} diff --git a/main/java/shkd/repc/repmd/template/util/ExportTemplateUtil.java b/main/java/shkd/repc/repmd/template/util/ExportTemplateUtil.java new file mode 100644 index 0000000..1f65211 --- /dev/null +++ b/main/java/shkd/repc/repmd/template/util/ExportTemplateUtil.java @@ -0,0 +1,380 @@ +package shkd.repc.repmd.template.util; + +import kd.bos.context.RequestContext; +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.dataentity.entity.DynamicObjectCollection; +import kd.bos.dataentity.entity.LocaleString; +import kd.bos.fileservice.FileItem; +import kd.bos.fileservice.FileService; +import kd.bos.fileservice.FileServiceFactory; +import kd.bos.form.control.Control; +import kd.bos.orm.query.QCP; +import kd.bos.orm.query.QFilter; +import kd.bos.servicehelper.BusinessDataServiceHelper; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.xssf.usermodel.*; + +import java.awt.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class ExportTemplateUtil { + + /** + * 导出默认模板 + * + * @param message 消息收集 + */ + public static String exportTemplate(StringBuilder message, Control productView, Control areaView){ + QFilter number = new QFilter("number", QCP.equals, "repmd_projectbill_IMPT_ENTRY"); + DynamicObject templateObj = BusinessDataServiceHelper.loadSingle("bos_importentry_template", number.toArray()); + if (null==templateObj){ + message.append("请先维护引入模板"); + return null; + } + //读取系统模板参数 + List<Template> templateList = readSysTemplate(templateObj, productView, areaView); + if (templateList.size()==0) return null; + //构建导出前四行数据 + // 创建Excel工作簿 + XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFSheet sheet = workbook.createSheet("产品构成 # productentry"); + // 第一行:明细标题 + XSSFRow row1 = sheet.createRow(0); + XSSFCell cell1 = row1.createCell(0); + cell1.setCellValue("产品构成 # productentry"); + XSSFCell cell2 = row1.createCell(1); + cell2.setCellValue("子单据体 # qeug_subentryentity"); + 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); + cell2.setCellStyle(style1); + + // 第二行:说明文本 + XSSFRow row2 = sheet.createRow(1); + XSSFCell cell3 = row2.createCell(0); + cell3.setCellValue("1、请将鼠标移到灰色标题行查看字段录入要求。2、红色带星号(*)的字段为必录字段。"); + XSSFCellStyle style2 = workbook.createCellStyle(); + XSSFFont font2 = workbook.createFont(); + font2.setFontName("Calibri"); + font2.setFontHeightInPoints((short) 11); + font2.setColor(IndexedColors.RED.getIndex()); + style2.setFont(font2); + style2.setAlignment(HorizontalAlignment.LEFT); + style2.setVerticalAlignment(VerticalAlignment.CENTER); + style2.setWrapText(true); // 自动换行 + row2.setHeightInPoints(60); + sheet.setColumnWidth(0, 31 * 256); // 设置列宽 + cell3.setCellStyle(style2); + + // 第三行:templateList中的number + XSSFRow row3 = sheet.createRow(2); + for (int i = 0; i < templateList.size(); i++) { + Template template = templateList.get(i); + String entityNumber=null; + String importProp = template.getImportProp(); + if (!("").equals(importProp)&&null!=importProp){ + entityNumber = template.getNumber()+"."+template.getImportProp(); + }else { + entityNumber = template.getNumber(); + } + + 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); + } + + // 第四行:templateList中的name + XSSFRow row4 = sheet.createRow(3); + for (int i = 0; i < templateList.size(); i++) { + Template template = templateList.get(i); + XSSFCell cell = row4.createCell(i); + String name=template.getName(); + XSSFCellStyle style4 = workbook.createCellStyle(); + XSSFFont font4 = workbook.createFont(); + font4.setFontName("Calibri"); + font4.setFontHeightInPoints((short) 11); + //必录 + if (template.isMustInput()){ + name="*"+template.getName(); + font4.setColor(IndexedColors.RED.getIndex()); + if ("number".equals(template.getImportProp())){ + name=name+".编码"; + }else if ("name".equals(template.getImportProp())){ + name=name+".名称"; + } + } + 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.setCellValue(name); + cell.setCellStyle(style4); + } + // 上传Excel文件并获取下载路径 + String uploadedFilePath = uploadExcel(workbook); + return RequestContext.get().getClientFullContextPath() + "/attachment/download.do?path=" + uploadedFilePath; + } + + /** + * 导出自定义模板 + * @param collections 配置的参数分录 + * @param message 消息收集 + */ + public static String exportTemplate(DynamicObjectCollection collections,StringBuilder message,Control productView, Control areaView){ + QFilter number = new QFilter("number", QCP.equals, "repmd_projectbill_IMPT_ENTRY"); + DynamicObject templateObj = BusinessDataServiceHelper.loadSingle("bos_importentry_template", number.toArray()); + if (null==templateObj){ + message.append("请先维护引入模板"); + return null; + } + //读取系统模板参数 + List<Template> templateList = readSysTemplate(templateObj, productView, areaView); + if (templateList.size()==0) return null; + //构建导出前四行数据 + // 创建Excel工作簿 + XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFSheet sheet = workbook.createSheet("产品构成 # productentry"); + // 第一行:明细标题 + XSSFRow row1 = sheet.createRow(0); + XSSFCell cell1 = row1.createCell(0); + cell1.setCellValue("产品构成 # productentry"); + XSSFCell cell2 = row1.createCell(1); + cell2.setCellValue("子单据体 # qeug_subentryentity"); + 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); + cell2.setCellStyle(style1); + + // 第二行:说明文本 + XSSFRow row2 = sheet.createRow(1); + XSSFCell cell3 = row2.createCell(0); + cell3.setCellValue("1、请将鼠标移到灰色标题行查看字段录入要求。2、红色带星号(*)的字段为必录字段。"); + XSSFCellStyle style2 = workbook.createCellStyle(); + XSSFFont font2 = workbook.createFont(); + font2.setFontName("Calibri"); + font2.setFontHeightInPoints((short) 11); + font2.setColor(IndexedColors.RED.getIndex()); + style2.setFont(font2); + style2.setAlignment(HorizontalAlignment.LEFT); + style2.setVerticalAlignment(VerticalAlignment.CENTER); + style2.setWrapText(true); // 自动换行 + row2.setHeightInPoints(60); + sheet.setColumnWidth(0, 31 * 256); // 设置列宽 + cell3.setCellStyle(style2); + + // 第三行:templateList中的number + XSSFRow row3 = sheet.createRow(2); + for (int i = 0; i < templateList.size(); i++) { + Template template = templateList.get(i); + String entityNumber=null; + String importProp = template.getImportProp(); + if (!("").equals(importProp)&&null!=importProp){ + entityNumber = template.getNumber()+"."+template.getImportProp(); + }else { + entityNumber = template.getNumber(); + } + + 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); + } + + // 第四行:templateList中的name + XSSFRow row4 = sheet.createRow(3); + for (int i = 0; i < templateList.size(); i++) { + Template template = templateList.get(i); + XSSFCell cell = row4.createCell(i); + String name=template.getName(); + XSSFCellStyle style4 = workbook.createCellStyle(); + XSSFFont font4 = workbook.createFont(); + font4.setFontName("Calibri"); + font4.setFontHeightInPoints((short) 11); + //必录 + if (template.isMustInput()){ + name="*"+template.getName(); + font4.setColor(IndexedColors.RED.getIndex()); + if ("number".equals(template.getImportProp())){ + name=name+".编码"; + }else if ("name".equals(template.getImportProp())){ + name=name+".名称"; + } + } + 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.setCellValue(name); + cell.setCellStyle(style4); + } + + //填充自定义模板数据 + for (int i = 0; i < collections.size(); i++) { + DynamicObject dynamicObject = collections.get(i); + String productNumber = dynamicObject.getString("qeug_productentry.number"); + String name = dynamicObject.getString("qeug_kmname"); + String type = dynamicObject.getString("qeug_fl"); + + // 从第5行开始填充数据 + XSSFRow dataRow = sheet.createRow(i + 4); + + // 获取列索引 + int productNumberColumnIndex = findColumnIndex(sheet, "productentry_producttype.number"); + int nameColumnIndex = findColumnIndex(sheet, "qeug_kmname"); + int typeColumnIndex = findColumnIndex(sheet, "qeug_fl"); + + // 填充数据 + XSSFCell productNumberCell = dataRow.createCell(productNumberColumnIndex); + productNumberCell.setCellValue(productNumber); + XSSFCellStyle productNumberStyle = createCellStyle(workbook); + productNumberCell.setCellStyle(productNumberStyle); + + XSSFCell nameCell = dataRow.createCell(nameColumnIndex); + nameCell.setCellValue(name); + XSSFCellStyle nameStyle = createCellStyle(workbook); + nameCell.setCellStyle(nameStyle); + + XSSFCell typeCell = dataRow.createCell(typeColumnIndex); + typeCell.setCellValue(type); + XSSFCellStyle typeStyle = createCellStyle(workbook); + typeCell.setCellStyle(typeStyle); + } + + // 上传Excel文件并获取下载路径 + String uploadedFilePath = uploadExcel(workbook); + return RequestContext.get().getClientFullContextPath() + "/attachment/download.do?path=" + uploadedFilePath; + } + + /** + * 上传Excel文件并返回路径 + */ + private static 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) { + throw new RuntimeException("上传Excel失败", e); + } + } + + /** + * 解析输出流为输入流 + */ + public static ByteArrayInputStream parse(final OutputStream out) throws Exception { + ByteArrayOutputStream outputStream = (ByteArrayOutputStream) out; + return new ByteArrayInputStream(outputStream.toByteArray()); + } + + /** + * 读取系统模板参数 + * @param templateObj 系统模板分录 + * @param productView 产品构成视图模型 + * @param areaView 面积数据分录视图模型 + * @return templateList + */ + public static List<Template> readSysTemplate(DynamicObject templateObj,Control productView, Control areaView){ + List<Template> templateList =new ArrayList<>(); + DynamicObjectCollection entry = templateObj.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")){ + Template template = new Template(); + String entityNumber = dynamicObject.getString("entitynumber"); + if (entityNumber!=null){ + LocaleString name = productView.getModel().getProperty(entityNumber).getDisplayName(); + if (name==null){ + name = areaView.getModel().getProperty(entityNumber).getDisplayName(); + } + String localeValue_zh_cn = name.getLocaleValue_zh_CN(); + template.setName(localeValue_zh_cn); + template.setNumber(entityNumber); + template.setMustInput(dynamicObject.getBoolean("ismustinput")); + template.setImport(dynamicObject.getBoolean("isimport")); + template.setImportProp(dynamicObject.getString("importprop")); + templateList.add(template); + } + } + } + } + return templateList; + } + + // 辅助方法:根据列名查找列索引 + private static int findColumnIndex(XSSFSheet sheet, String columnName) { + XSSFRow headerRow = sheet.getRow(2); + for (int colIndex = 0; colIndex < headerRow.getPhysicalNumberOfCells(); colIndex++) { + XSSFCell cell = headerRow.getCell(colIndex); + if (cell != null && columnName.equals(cell.getStringCellValue())) { + return colIndex; + } + } + return -1; + } + + // 辅助方法:创建统一的单元格样式 + private static XSSFCellStyle createCellStyle(XSSFWorkbook workbook) { + XSSFCellStyle style = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 11); + style.setFont(font); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + return style; + } +} diff --git a/main/java/shkd/repc/repmd/template/util/Template.java b/main/java/shkd/repc/repmd/template/util/Template.java new file mode 100644 index 0000000..234457f --- /dev/null +++ b/main/java/shkd/repc/repmd/template/util/Template.java @@ -0,0 +1,70 @@ +package shkd.repc.repmd.template.util; + +public class Template { + + //名称 + public String name; + + //编码 + public String number; + + //是否必录 + public boolean isMustInput; + + //是否引入 + public boolean isImport; + + //引入属性 + public String importProp; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public boolean isMustInput() { + return isMustInput; + } + + public void setMustInput(boolean mustInput) { + isMustInput = mustInput; + } + + public boolean isImport() { + return isImport; + } + + public void setImport(boolean anImport) { + isImport = anImport; + } + + public String getImportProp() { + return importProp; + } + + public void setImportProp(String importProp) { + this.importProp = importProp; + } + + @Override + public String toString() { + return "Template{" + + "name='" + name + '\'' + + ", number='" + number + '\'' + + ", isMustInput=" + isMustInput + + ", isImport=" + isImport + + ", importProp='" + importProp + '\'' + + '}'; + } +}