hcong
2024-12-12 9c315b4dbbb9f25d5373d1f228ed441a4e8ccbf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cn.flightfeather.supervision.business.report
 
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
 
/**
 * 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>
 
    // 中间结果基本信息对应数据库中的entity
    var dataProduct = DataProduct()
 
    // 中间结果具体信息对应数据库中的entity
    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()
            // 没有错误正常运行结束
            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()
        }
        // 获得和template对应的中间结果数据对象KCLASS对象
        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()
        }
        // 先执行toObject后将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)
    }
}