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
package cn.flightfeather.supervision.business.report.bean
 
import cn.flightfeather.supervision.common.utils.ExcelUtil
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.findAnnotation
 
/**
 * excel报告模板中间结果对象基类
 * hc 2024.12.06
 */
abstract class BaseTemplateResult {
    // 定义注解
    annotation class ExcelHead(val index: Int, val des: String)
 
    /**
     * 转换为数据库表对应的实体类对象
     */
    abstract fun convertToDBEntity(): Any
 
    fun setProperties(values: MutableList<Any>) {
        // 遍历所有属性并赋值
        var index = 0
        this::class.declaredMemberProperties.sortedBy { it.findAnnotation<ExcelHead>()?.index }.forEach { property ->
            // 检查属性是否可以设置值
            if (property is KMutableProperty<*>) {
                try {
                    // 暂存最后的属性值的变量 默认就是values[index]
                    var c: Any = values[index]
                    // 仅仅为了简写values[index]
                    val v = values[index++]
                    // 从MyCell对象中拿出百分比类型
                    if (v is ExcelUtil.MyCell) {
                        if (v.isPercent) {
                            val percent = v.text.toBigDecimalOrNull()
                            if (percent != null) {
                                percent.setScale(2)
                                c = percent
                            }else {
                                c = ""
                            }
                        }
                    }
                    // 向属性中赋值
                    property.setter.call(this, c)
                } catch (e: Exception) {
 
                }
            }
        }
    }
}