feiyu02
2025-03-21 e5bdf2e02090357cbd580d54e6cd2406dd541760
src/main/kotlin/com/flightfeather/uav/model/BaseModel.kt
@@ -3,8 +3,7 @@
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.common.utils.ExcelUtil
import com.flightfeather.uav.socket.eunm.FactorType
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.xssf.streaming.SXSSFWorkbook
import java.io.File
import java.io.FileOutputStream
import java.util.*
@@ -14,7 +13,18 @@
 * 污染源影响程度权重分析模型
 * 基类
 */
abstract class BaseModel<M : BaseMData, S : BaseSOP> {
abstract class BaseModel<M : BaseMData, S: BaseSOP> {
    data class ResultCell(
        var total: Double = 0.0,
        var count: Int = 0,
        var average: Double = 0.0
    ) {
        fun average(): Double {
            average = if (count == 0) .0 else round(total / count * 100) / 100
            return average
        }
    }
    abstract var dataPrep: BaseDataPrep<M, S>
@@ -25,7 +35,7 @@
    abstract var weights: List<BaseWeight<M, S>>
    // 计算结果
    private val result = mutableSetOf<BaseEffect>()
    private val rMap = mutableMapOf<String, MutableMap<String, MutableMap<String, ResultCell>>>()
    // 结果筛选方式
    abstract var sections: List<BaseSection<M, S>>
@@ -35,8 +45,8 @@
     * @param mDataList 监测数据集合
     * @param sopList 污染源集合
     */
    fun execute(mDataList: List<M>, sopList: List<S>) {
        result.clear()
    open fun execute(mDataList: List<M>, sopList: List<S>, hasNext: Boolean = false) {
        if (!hasNext) rMap.clear()
        //1. 数据预处理
        val mList = dataPrep.mDataPrep(mDataList)
@@ -73,20 +83,20 @@
        sections.forEach { it.filter(mData, sop, effect) }
        // 保存结果
        result.add(effect)
        formatConversion2(effect)
    }
    fun outputToExcel(
        fName: String? = null,
        _workbook: HSSFWorkbook? = null,
        _workbook: SXSSFWorkbook? = null,
        _out: FileOutputStream? = null,
        sheetName: String = "sheet1",
        done: Boolean = true
    ): Pair<HSSFWorkbook, FileOutputStream>? {
        val rMap = formatConversion()
    ): Pair<SXSSFWorkbook, FileOutputStream>? {
//        val rMap = formatConversion()
        val workbook = _workbook ?: HSSFWorkbook()
        val fileName = fName ?: "污染溯源权重模型${DateUtil().DateToString(Date(), "yyyy-MM-ddHHmmss")}.xls"
        val workbook = _workbook ?: SXSSFWorkbook()
        val fileName = fName ?: "污染溯源权重模型${DateUtil.instance.dateToString(Date(), "yyyy-MM-ddHHmmss")}.xls"
//        val filePath = "E:\\work\\export\\$fileName"
//        val filePath = "E:\\工作\\开发\\走航监测\\算法相关\\自动输出\\$fileName"
        val filePath = "E:\\工作\\开发\\走航监测\\算法相关\\自动输出\\网格化\\$fileName"
@@ -106,9 +116,8 @@
            // 新建一行
            val contentList = mutableListOf<Any>()
            // 添加污染源名称
            val l = source.split("(")
            val index = l[1].substring(0, l[1].lastIndex)
            contentList.add(index.toIntOrNull() ?: 0)
            val l = source.split(";")
            contentList.add(l[1].toIntOrNull() ?: 0)
            contentList.add(l[0])
            tMap.forEach { (factorType, lMap) ->
                sections.forEach {
@@ -118,11 +127,11 @@
                    }
                    var max = 0.0
                    var maxP = types[0]
                    types.forEach {se ->
                    types.forEach type@{se ->
                        val lKey = "$se($factorType)"
                        if (lMap.containsKey(lKey)) {
                            val dataList = lMap[lKey]
                            val size = dataList?.size
                            val resultCell = lMap[lKey] ?: return@type
//                            val size = resultCell.count
                            // 添加该分类作为表头
                            val h = lKey
//                            val h = "$lKey($size)"
@@ -130,17 +139,17 @@
                                h1.add(h)
                            }
                            // 将原始的数据换算得出结果,可以是求出均值、总和等等,后续修改为可由用户设定
                            // 将原始的数据换算得出结果,可以是求出均值、总和等等
                            // FIXME: 2021/6/23 此处先默认为求均值
                            val average = dataList?.average()
                            val average = resultCell.average()
                            if (average ?: 0.0 > max) {
                                max = average ?: 0.0
                            if (average > max) {
                                max = average
                                maxP = se
                            }
                            // 当前行添加该分类下的结果值
                            contentList.add(average ?: 0.0)
                            contentList.add(average)
                        }
                    }
                    if (isFirst) {
@@ -177,33 +186,45 @@
        }
    }
    private fun formatConversion(): Map<String, Map<String, Map<String?, MutableList<Double>>>> {
        val rMap = mutableMapOf<String, MutableMap<String, MutableMap<String?, MutableList<Double>>>>()
        println("结果长度:${result.size}")
        result.forEach { e ->
            val rKey = "${e.sourceName}(${e.index})"
            if (!rMap.containsKey(rKey)) {
                rMap[rKey] = mutableMapOf()
            }
            val lMap = rMap[rKey]!!
            e.value.forEach { v ->
                if (!lMap.containsKey(v.first.des)) {
                    lMap[v.first.des] = mutableMapOf()
                }
                val tMap = lMap[v.first.des]!!
                e.tag.forEach { t ->
                    val factorName = v.first.des
                    val lKey = t.levelName + "($factorName)"
                    if (!tMap.containsKey(lKey)) {
                        tMap[lKey] = mutableListOf()
                    }
                    tMap[lKey]?.add(v.second)
    fun outputResult(): MutableMap<String, MutableMap<String, MutableMap<String, ResultCell>>> {
        rMap.forEach { (_, v) ->
            v.forEach { (_, v2) ->
                v2.forEach { (_, v3) ->
                    v3.average()
                }
            }
        }
        return rMap
    }
    private fun formatConversion2(e: BaseEffect) {
        val rKey = "${e.sourceName};${e.index}"
        if (!rMap.containsKey(rKey)) {
            rMap[rKey] = mutableMapOf()
        }
        val lMap = rMap[rKey]!!
        e.value.forEach { v ->
            if (!lMap.containsKey(v.first.des)) {
                lMap[v.first.des] = mutableMapOf()
            }
            val tMap = lMap[v.first.des]!!
            e.tag.forEach { t ->
                val factorName = v.first.des
                val lKey = t.levelName + "($factorName)"
                if (!tMap.containsKey(lKey)) {
                    tMap[lKey] = ResultCell()
                }
                tMap[lKey]?.run {
                    total += v.second
                    if (factorName != FactorType.H2S.name || v.second > 0) {
                        count++
                    }
                }
            }
        }
    }
    /**
     * 监测数据合法性检查
     */