From d68bebb440cf137e2adda2dd5eae55c115556ff5 Mon Sep 17 00:00:00 2001 From: chenshaoxin <1981897232@qq.com> Date: Sun, 28 Sep 2025 12:03:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BA=BA=EF=BC=9A=E9=99=88?= =?UTF-8?q?=E7=BB=8D=E9=91=AB=20=E6=97=A5=E6=9C=9F=EF=BC=9A2025/9/28=2012?= =?UTF-8?q?=EF=BC=9A00=20=E5=86=85=E5=AE=B9:=E6=8A=A5=E8=A1=A8=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/AccountbankReportFormPlugin.java | 234 ++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/report/AccountbankReportFormPlugin.java diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/report/AccountbankReportFormPlugin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/report/AccountbankReportFormPlugin.java new file mode 100644 index 0000000..50e95dd --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/report/AccountbankReportFormPlugin.java @@ -0,0 +1,234 @@ +package shkd.sys.sys.plugin.report; + +import dm.jdbc.util.StringUtil; +import kd.bos.cache.CacheFactory; +import kd.bos.cache.TempFileCache; +import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.dataentity.entity.DynamicObjectCollection; +import kd.bos.dataentity.metadata.IMetadata; +import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection; +import kd.bos.entity.MainEntityType; +import kd.bos.entity.NumberFormatProvider; +import kd.bos.entity.ValueMapItem; +import kd.bos.entity.datamodel.FmtField; +import kd.bos.entity.report.*; +import kd.bos.form.control.Toolbar; +import kd.bos.form.control.events.ItemClickEvent; +import kd.bos.report.ReportList; +import kd.bos.report.plugin.AbstractReportFormPlugin; +import kd.bos.report.proxy.ReportListProxy; +import shkd.sys.sys.plugin.report.util.CsvExportUtil; + +import java.io.IOException; +import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; + +public class AccountbankReportFormPlugin extends AbstractReportFormPlugin { + + @Override + public void registerListener(EventObject e) { + super.registerListener(e); + // 获取工具栏,监听整个工具栏 + Toolbar export = this.getView().getControl("toolbarap"); + // 监听工具栏这个按钮的点击事件 + export.addItemClickListener(this); + export.addClickListener(this); + + } + + /** + * 处理列表项点击事件,当点击的项为 "shkd_exportcsv" 时,执行导出 CSV 文件的操作。 + * + * @param evt 列表项点击事件对象,包含被点击项的相关信息 + */ + @Override + public void itemClick(ItemClickEvent evt) { + super.itemClick(evt); + String itemKey = evt.getItemKey(); + if (StringUtil.equals("shkd_exportcsv", itemKey)) { + // 获取数据实体类型及其属性集合 + MainEntityType dataEntityType = this.getModel().getDataEntityType(); + DataEntityPropertyCollection properties = dataEntityType.getProperties(); + String reportName = dataEntityType.getDisplayName().getLocaleValue(); + + // 获取报表列表控件及模型,并取得行数和列信息 + ReportList reportList = this.getControl("reportlistap"); + IReportListModel reportListModel = reportList.getReportModel(); + int rowCount = reportListModel.getRowCount(); + ReportListProxy reportListProxy = new ReportListProxy(reportList); + List columns = reportListProxy.getColumnList(reportListModel); + List cols = reportListProxy.getReportColumnList(columns, true); + + // 获取所有行数据并初始化数字格式化提供器 + DynamicObjectCollection rowData = reportListModel.getRowData(0, rowCount, false); + NumberFormatProvider numberFormatProvider = ReportColumn.initNumberFormatProvider(rowData, cols); + + Map> type = getType(reportListModel); + + // 提取需要导出的字段名(排除 fseq 字段) + List headerKeys = numberFormatProvider.getFmtFields().stream() + .filter(o -> !Objects.equals(o.getFieldName(), "fseq")) + .map(FmtField::getFieldName) + .collect(Collectors.toList()); + + // 构建属性名到显示名称的映射表,便于快速查找 + Map nameToDisplay = + properties.stream() + .filter(Objects::nonNull) + .filter(p -> p.getDisplayName() != null) + .collect(Collectors.toMap( + IMetadata::getName, + p -> p.getDisplayName().getLocaleValue(), + (v1, v2) -> v1)); + + // 按照字段顺序获取对应的显示名称,缺失则使用空字符串 + List headers = headerKeys.stream() + .map(k -> nameToDisplay.getOrDefault(k, "")) + .collect(Collectors.toList()); + + // 构造每一行的数据列表,处理复杂对象的name属性及本地化 + List> rows2 = rowData.stream() + .map(obj -> headerKeys.stream() + .map(key -> this.processFieldValue(obj.get(key),type,key)) + .collect(Collectors.toList())) + .collect(Collectors.toList()); + + // 生成时间戳和随机数作为文件名的一部分 + String timestamp = LocalDateTime.now() + .format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")); + int random = ThreadLocalRandom.current().nextInt(100, 1000); // 3 位随机数 + String reportNameExt = timestamp + random; + + try { + Path tempFile = Files.createTempFile(reportName + reportNameExt, ".csv"); + CsvExportUtil.writeCsvToFile(tempFile, headers, rows2); + // 上传到临时文件服务器 获取压缩包下载地址 + TempFileCache tempFileCache = CacheFactory.getCommonCacheFactory().getTempFileCache(); + String url = tempFileCache.saveAsUrl(tempFile.getFileName().toString(), Files.newInputStream(tempFile), 3 * 60); + // 下载压缩包 + this.getView().download(url); + Files.delete(tempFile); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + } + + /** + * 处理字段值,支持简单值和复杂对象(如含name属性的对象) + * + * @param value 原始字段值 + * @return 处理后的字符串值 + */ + public Object processFieldValue(Object value,Map> type,String key) { + if (value == null) { + return ""; + } + + String disString="";//展示的文本 + //获取配置 + Map stringObjectMap = type.get(key); + System.out.println(stringObjectMap); + String Stringtype = (String) stringObjectMap.get("type"); + switch (Stringtype) { + case "DecimalReportColumn"://数值 + int Scale= (int) stringObjectMap.get("value"); + // 设置小数点后保留两位 + BigDecimal roundedValue = ((BigDecimal)value).setScale(Scale, BigDecimal.ROUND_HALF_UP); + // 转换为字符串 + disString= roundedValue.toString(); + break; + case "ComboReportColumn"://下拉框 + Map Combomap= (Map) stringObjectMap.get("value"); + disString=Combomap.get(value); + break; + case "DateTimeReportColumn"://时间 + // 获取当前日期 + String Mask= (String) stringObjectMap.get("value"); + if (Mask==null){ + Mask="yyyy-MM-dd"; + } + // 指定日期格式 + SimpleDateFormat sdf = new SimpleDateFormat(Mask); + // 格式化日期 + Timestamp timestamp = ((Timestamp) value); + // 使用getTime()方法将Timestamp转换为Date + disString = sdf.format(new Date(timestamp.getTime())); + break; + case "TextColumn"://文本 + disString=value.toString(); + break; + case "DynamicObjectColumn"://基础资料 + String getDisplayProp= (String) stringObjectMap.get("value"); + disString=((DynamicObject)value).getString(getDisplayProp); + break; + default: + disString=""; + break; + } + + return disString; + } + + private Map> getType(IReportListModel reportListModel){ + Map> map = new HashMap<>(); + List columnstype = reportListModel.getColumns(); + for (AbstractReportColumn abstractReportColumn : columnstype) { + if (abstractReportColumn instanceof DecimalReportColumn) { + DecimalReportColumn reportColumn=(DecimalReportColumn)abstractReportColumn; + String fieldKey = reportColumn.getFieldKey(); + Map map1 = new HashMap<>(); + map1.put("type","DecimalReportColumn"); + map1.put("value",reportColumn.getScale()); + map.put(fieldKey,map1); + } else if (abstractReportColumn instanceof ComboReportColumn) { + ComboReportColumn reportColumn=(ComboReportColumn)abstractReportColumn; + String fieldKey = reportColumn.getFieldKey(); + Map column = new HashMap<>(); + List comboItems = reportColumn.getComboItems(); + for (ValueMapItem comboItem : comboItems) { + column.put(comboItem.getValue(),comboItem.getName().getLocaleValue_zh_CN()); + } + Map map1 = new HashMap<>(); + map1.put("type","ComboReportColumn"); + map1.put("value",column); + map.put(fieldKey,map1); + }else if (abstractReportColumn instanceof DateTimeReportColumn) { + DateTimeReportColumn reportColumn=(DateTimeReportColumn)abstractReportColumn; + String fieldKey = reportColumn.getFieldKey(); + Map map1 = new HashMap<>(); + map1.put("type","DateTimeReportColumn"); + map1.put("value",reportColumn.getMask()); + map.put(fieldKey,map1); + }else { + ReportColumn reportColumn=(ReportColumn)abstractReportColumn; + if ("text".equals(reportColumn.getFieldType())||"integer".equals(reportColumn.getFieldType())){ + //文本 + String fieldKey = reportColumn.getFieldKey(); + Map map1 = new HashMap<>(); + map1.put("type","TextColumn"); + map1.put("value","TextColumn"); + map.put(fieldKey,map1); + }else { + //基础资料 + String fieldKey = reportColumn.getFieldKey(); + Map map1 = new HashMap<>(); + map1.put("type","DynamicObjectColumn"); + map1.put("value",reportColumn.getDisplayProp()); + map.put(fieldKey,map1); + } + } + } + return map; + } + +}