From 46872a311da9480d3edb19223aca2e0833fb1e31 Mon Sep 17 00:00:00 2001
From: hcong <1050828145@qq.com>
Date: 星期二, 17 十二月 2024 11:41:00 +0800
Subject: [PATCH] 1. 完成数据产品中间结果基本信息和具体信息入库 2. 修改ColInspectionInfo.kt 监管时间格式修改为yyyy-mm-dd 3. 新增数据产品类型枚举类 DataProductType 4. 修改BaseTemplate实现类通过重写genData方法生成Template相关数据 5. TODO 其他未涉及到的产品对象的中间结果对象

---
 src/main/kotlin/cn/flightfeather/supervision/business/report/BaseTemplate.kt |  187 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 176 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..0c8047d 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,179 @@
 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(): 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
+    }
+
+    /**
+     * 鐢熸垚涓棿缁撴灉鍏蜂綋淇℃伅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