From 09c8120288ea7df454c10d67911ab8643f2f4235 Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期二, 17 十二月 2024 11:06:33 +0800 Subject: [PATCH] 1. 完成数据产品中间结果基本信息和具体信息入库 --- src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt | 186 +++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 175 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..f143323 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,178 @@ 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 cn.flightfeather.supervision.common.utils.UUIDGenerator +import cn.flightfeather.supervision.domain.ds1.entity.DataProduct +import org.apache.poi.hssf.usermodel.HSSFWorkbook +import java.io.FileOutputStream +import java.io.OutputStream +import java.util.* +import kotlin.collections.ArrayList +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> + + // 涓棿缁撴灉鍩烘湰淇℃伅瀵瑰簲鏁版嵁搴撲腑鐨別ntity + var dataProduct = DataProduct() + + // 涓棿缁撴灉鍏蜂綋淇℃伅瀵瑰簲鏁版嵁搴撲腑鐨別ntity + val entities = ArrayList<Any>() + + // 鎵ц鐘舵�� 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() { + 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()) + } + } + + /** + * 鐢熸垚涓棿缁撴灉鍏蜂綋淇℃伅entity + * by hc 2024.12.12 + */ + fun toDBEntity() { + entities.clear() + if (!isExecuted) { + execute() + } + // 鍏堟墽琛宼oObject鍚庡皢toObject鐨勭粨鏋滆浆鍖栦负DBEntity + toObject() + resultObjects.forEach { + entities.add(it.convertToDBEntity()) + } + } + + /** + * 鐢熸垚涓棿缁撴灉鍩烘湰淇℃伅entity + * by hc 2024.12.12 + */ + fun toDBBaseInfoEntity() { + dataProduct = DataProduct() + dataProduct.guid = UUIDGenerator.generate16ShortUUID() + dataProduct.townCode = dataSource.config.townCode + dataProduct.cityCode = dataSource.config.cityCode + dataProduct.districtCode = dataSource.config.districtCode + dataProduct.endTime = dataSource.config.endTime + dataProduct.startTime = dataSource.config.startTime + dataProduct.provinceCode = dataSource.config.provinceCode + dataProduct.sceneTypeId = dataSource.config.sceneType?.toByte() ?: -1 + dataProduct.taskGuid = dataSource.config.topTaskGuid + } + + /** + * 杈撳嚭涓烘枃妗� + */ + 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