feiyu02
2023-11-14 aa94ef4e1f060b184e6ea9fb37254b376a1eb60e
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
package com.flightfeather.monitor.analysis.dust.risk
 
import com.flightfeather.monitor.domain.ds1.entity.*
import java.time.LocalDate
import java.time.ZoneId
import java.util.*
 
class DustRiskMonthAnalysis(val config: DustExceptionSetting) {
 
    //分析结果
    val resultList = mutableListOf<RiskValue>()
 
    /**
     * 初始化
     */
    fun init() {
        resultList.clear()
    }
 
    /**
     * 每轮次的初始化
     */
    fun roundInit(mnCode: String, date: LocalDate) {
        resultList.add(RiskValue().apply {
            this.mnCode = mnCode
            lst = Date.from(date.withDayOfMonth(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
            type = "month"
        })
    }
 
    /**
     * 每轮次的风险值计算
     */
    fun roundCal(list: List<RiskValue?>) {
        if (resultList.isEmpty()) throw IllegalStateException("结果集resultList不能为空!")
 
        val res = resultList.last()
 
        riskOnlineRate(res, list)
        riskValidRate(res, list)
        riskExceedRate(res, list)
        riskExceptionRepetitionRate(res, list)
        riskExceptionType(res, list)
    }
 
    fun riskOnlineRate(riskValue: RiskValue, list: List<RiskValue?>) {
        riskValue.onlineRisk = avg(list) { it.onlineRisk }
    }
 
    fun riskValidRate(riskValue: RiskValue, list: List<RiskValue?>) {
        riskValue.validRisk = avg(list) { it.validRisk }
    }
 
    fun riskExceedRate(riskValue: RiskValue, list: List<RiskValue?>) {
        riskValue.exceedRisk = avg(list) { it.exceedRisk }
    }
 
    fun riskExceptionRepetitionRate(riskValue: RiskValue, list: List<RiskValue?>) {
        riskValue.typicalExceptionRepetitionRate = avg(list) { it.typicalExceptionRepetitionRate }
    }
 
    fun riskExceptionType(riskValue: RiskValue, list: List<RiskValue?>) {
        riskValue.exceptionTypeAggregation = avg(list) { it.exceptionTypeAggregation }
    }
 
 
    private fun avg(list: List<RiskValue?>, onNext: (value: RiskValue) -> Double?): Double {
        var total = .0
        var count = 0
        list.forEach {
            if (it == null) return@forEach
            val a = onNext(it) ?: return@forEach
            total += a
            count++
        }
        return if (count == 0) .0 else total / count
    }
}