From 1a45c6fe9ea814e432cf90c0169be9b7d991a266 Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期二, 10 十二月 2024 13:51:00 +0800 Subject: [PATCH] 1. 新增 BaseExcel.kt 和 BaseTemplate.kt 的 toObject输出到对象方法 2. 新增 BaseTemplate 是否执行的状态属性 3. 在cn.flightfeather.supervision.business.report.bean包下新增 BaseTemplateResult.kt 结果对象抽象类 以及 ProAnalysisSummaryResult.kt 和 ProDetailSummaryResult.kt 对应实现:问题整改动态跟踪和分街镇问题整改分析的中间结果对象 注:除了问题整改动态跟踪和分街镇问题整改分析其他的汇总表对象中都没有对应中间结果对象,因此这里还有报错信息 --- src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt | 145 ++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 134 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt b/src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt index 5e0d3d4..a053d99 100644 --- a/src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt +++ b/src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt @@ -1,14 +1,137 @@ package cn.flightfeather.supervision.business.report -/** - * 鎶ュ憡妯℃澘鍩虹被 - */ -abstract class BaseTemplate { +import cn.flightfeather.supervision.common.utils.ExcelUtil +import cn.flightfeather.supervision.business.report.bean.BaseTemplateResult +import org.apache.poi.hssf.usermodel.HSSFWorkbook +import java.io.FileOutputStream +import java.io.OutputStream +import java.util.* +import kotlin.reflect.full.createInstance - //鍥哄畾鍒楃粍鍚� - //鍚嶇О - //鍩烘湰淇℃伅 - //闂鎵e垎鍒嗗竷 - //闂鏁存敼鍒嗗竷 - //鐩戠璇︽儏 -} \ No newline at end of file +/** + * excel鎶ュ憡妯℃澘鍩虹被 + * 鍗曚釜鏁版嵁婧� + */ +// FIXME: 2022/7/15 妯℃澘绠�鍖栵細鍙湁琛ㄥご浼氭秹鍙婂崟鍏冩牸鍚堝苟锛岄粯璁よ〃鍐呭涓病鏈夊悎骞� +abstract class BaseTemplate(val dataSource: DataSource) : BaseOutputInterface { + + //鍒楃粍鍚� + abstract val cols: List<BaseCols> + + //妯℃澘鍚嶇О + abstract val templateName: String + + // 涓棿缁撴灉瀵硅薄 by hc 2024.12.5 + abstract var resultObjects: MutableList<BaseTemplateResult> + + // 鎵ц鐘舵�� by hc 2024.12.5 + var isExecuted: Boolean = false + + //琛ㄥご + val head = mutableListOf<MutableList<ExcelUtil.MyCell>>() + + //鍐呭 + val contents = mutableListOf<MutableList<Any>>() + + // 鐢熸垚Template鐩稿叧鏁版嵁 by hc 2024.12.9 + open fun genData() { + //鏁版嵁婧愰噸缃� + dataSource.reset() + //鍚堟垚琛ㄥご + cols.forEach { + it.combineHead(head, dataSource) + } + //鍚堟垚鏁版嵁 + dataSource.loop { index, rowData -> + cols.forEach { col -> + val r = col.getOneRow(rowData) + if (index >= contents.size) { + contents.add(mutableListOf()) + } + contents[index].addAll(r) + } + } + } + + open fun execute() { + try { + genData() + // 娌℃湁閿欒姝e父杩愯缁撴潫 + isExecuted = true + } catch (e: Exception) { + // TODO: handle exception + } + } + + override fun toWorkBook(wb: HSSFWorkbook) { + val f = tableFormat() + ExcelUtil.write(f.first, f.second, wb, templateName) + } + + override fun toOutputStream(out: OutputStream, sheetName: String?) { + val f = tableFormat() + ExcelUtil.write2(out, f.first, f.second, sheetName ?: templateName) + } + + /** + * 杈撳嚭鍒板璞� + * hc 2024.12.06 + */ + fun toObject(): MutableList<BaseTemplateResult> { + if (!isExecuted) { + execute() + } + // 鑾峰緱鍜宼emplate瀵瑰簲鐨勪腑闂寸粨鏋滄暟鎹璞CLASS瀵硅薄 + val classType = resultObjects.first()::class + try { + // 娓呯┖鏁扮粍 + resultObjects.clear() + contents.forEach { + // 鍒涘缓瀵瑰簲鐨勪腑闂寸粨鏋滄暟鎹璞� + val resultObj = classType.createInstance() + resultObj.setProperties(it) + resultObjects.add(resultObj) + } + }catch (e: Exception) { + // 濡傛灉鍑虹幇寮傚父鎭㈠鍒板垵濮嬬姸鎬� 閬垮厤涓嬫璋冪敤toObject鏁版嵁閲嶅 + resultObjects.clear() + resultObjects.add(classType.createInstance()) + } + return resultObjects + } + + /** + * 杈撳嚭涓烘枃妗� + */ + override fun toFile(path: String) { + val fileName = "${dataSource.areaName()}-${templateName}-${Date().time}.xls" + val out = FileOutputStream(path + fileName) + toOutputStream(out) + } + + /** + * 琛ㄥご鍜岃〃鍐呭鏍煎紡杞寲 + */ + fun tableFormat(): Pair<MutableList<Array<Any>>, MutableList<Array<Any>>> { + val h = mutableListOf<Array<Any>>() + val c = mutableListOf<Array<Any>>() + head.forEach { h.add(it.toTypedArray()) } + contents.forEach { c.add(it.toTypedArray()) } + val index = c[0][0] + //鎸夌収绗竴鍒楄繘琛屾帓搴� + if (index is Int) { + c.sortBy { + if (it[0] is Int) { + it[0] as Int + } else { + 0 + } + } + } else { + c.sortBy { + it[0].toString() + } + } + return Pair(h, c) + } +} -- Gitblit v1.9.3