feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
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
180
181
182
183
184
185
186
187
188
189
190
191
192
package cn.flightfeather.supervision.common.risk
 
import cn.flightfeather.supervision.domain.entity.SceneType
import java.io.OutputStream
 
class RiskAnalysis(
    private val dataSource: DataSource,
    toDatabase: Boolean = false,
    toFile: Boolean = false,
) {
 
    private val riskLedger = RiskLedger()
    private val riskSelfLedger = RiskSelfLedger()
    private val riskAssessment = RiskAssessment()
    private val riskAuthenticated = RiskAuthenticated()
    private val riskCommitment = RiskCommitment()
    private var utilFile: UtilFile? = null
    private var utilDatabase: UtilDatabase? = null
 
    init {
        if (toDatabase) utilDatabase = UtilDatabase(dataSource.dbMapper)
        if (toFile) utilFile = UtilFile(dataSource.config)
    }
 
 
    // 总结开头
    private val summaryStart = listOf(
        "线上管理较规范,不存在不规范项,\n",
        "线上管理相对规范,但存在少量不规范项,\n",
        "线上管理不规范,存在较多不规范项,\n",
        "线上管理严重不规范,存在大量不规范项,\n"
    )
 
    private val sEnd = listOf(
        "建议现场执法检查核实,若现场确实较规范,可作为正面宣传典型。",
        "建议线上或现场执法检查督促整改。",
        "建议开展现场执法检查督促整改。",
        "建议重点开展现场执法检查督促整改。"
    )
    private val sEnd7 = listOf(
        "建议不定期进行线上抽查或现场核查,若线下环保管理与线上基本一致,可将企业作为正面宣传典型;若线下环保管理与线上差异较大,则将企业调整为高风险并按高风险处置。",
        "建议生态环境主管部门通过电话、微信、小程序等方式提醒企业完善自身环保守法合规管理。",
        "建议生态环境主管部门通过电话、微信、小程序等方式提醒企业完善自身环保守法合规管理。",
        "建议生态环境主管部门基于环信码小程序指导企业开展应急自巡查,或机动开展执法检查、专项约谈等。"
    )
 
    // 总结结尾
    private fun summaryEnd(index: Int) = when (dataSource.config.sceneType) {
        cn.flightfeather.supervision.domain.enumeration.SceneType.VehicleRepair.value.toString() -> sEnd7[index]
        else -> sEnd[index]
    }
 
    private fun exe() {
        utilFile?.reset()
        utilDatabase?.setConfig(dataSource.config.year, dataSource.config.month)
        dataSource.loop { u, c, d ->
            // 轮询各评判标准,得出各标准下的风险等级
            val r1 = riskLedger.getResult(u, c, d)
            val r2 = riskSelfLedger.getResult(u, c, d)
            val r3 = riskAssessment.getResult(u, c, d)
            val r4 = riskAuthenticated.getResult(u, c, d)
            val r5 = riskCommitment.getResult(u, c, d)
 
            // 根据各个子风险等级,得出综合风险等级和总结描述
            val riskLevel = judgeResult(r1, r2, r3.first, r4)
            val summary = getSummary(riskLevel, r1, r2, r3.first, r4, r5)
            utilDatabase?.insertToDataBase(u, riskLevel, summary)
            utilFile?.parseRow(u, r1, r2, r3, r4, r5, riskLevel, summary)
        }
    }
 
    fun execute() {
        exe()
        utilFile?.outPutToFile()
    }
 
    fun executeToStream(out: OutputStream) {
        exe()
        utilFile?.outPutToStream(out)
    }
 
    /**
     * 根据各标准完成情况,得出综合风险等级及对应评语
     * @return 风险等级,0:低风险,1:中风险;2:高风险
     */
    private fun judgeResult(r1: RiskLedger.Result, r2: RiskLedger.Result, r3: Boolean, r4: RiskAuthenticated.Result): Int {
        when (dataSource.config.sceneType) {
            cn.flightfeather.supervision.domain.enumeration.SceneType.VehicleRepair.value.toString() -> {
                // 高风险
                if (
                    (r1.rate2 < .6 || r2.rate2 < .6)
                    ||
                    (r1.rate3 > .3 && r2.rate3 > .6)
                    ||
                    (r1.specialResult && r2.specialResult)
                ) {
                    return 2
                }
                // 低风险
                else if (
                    r1.rate2 >= .8
                    && r2.rate2 >= .8
                    && r3
                    && r4.finished1
                    && (r1.rate3 < .3 && r2.rate3 < .6)
                ) {
                    return 0
                }
                // 中风险
                else {
                    return 1
                }
            }
            else -> {
                // 高风险
                if (
                // FIXME: 2024/9/29 临时调整高风险规则
                    (r1.rate2 == .0 || r2.rate2 == .0)
                    ||
                    (r1.rate2 < 0.6 || r2.rate2 < 0.6) && !r3
//                    (r1.rate2 == .0 && r2.rate2 == .0) && !r3
                ) {
                    return 2
                }
                // 低风险
                else if (
                    r1.rate2 >= .6
                    && r2.rate2 >= .6
                    && r3
                    // FIXME: 2024/9/29 临时调整高风险规则
                    && r4.finished1
                ) {
                    return 0
                }
                // 中风险
                else {
                    return 1
                }
            }
        }
    }
 
    /**
     * 根据综合风险等级,生成总结性描述
     */
    private fun getSummary(
        riskLevel: Int,
        r1: RiskLedger.Result,
        r2: RiskLedger.Result,
        r3: Boolean,
        r4: RiskAuthenticated.Result,
        r5: Int
    ): String {
        val conclusion = StringBuilder()
        when (riskLevel) {
            0 -> {
                if (r1.rate2 == 1.0 && r2.rate2 == 1.0 && r3 && r4.finished1 && r5 == 2) {
                    conclusion.append(summaryStart[0])
                    conclusion.append(getDetailSummary(riskLevel))
                    conclusion.append(summaryEnd(0))
                } else {
                    conclusion.append(summaryStart[1])
                    conclusion.append(getDetailSummary(riskLevel))
                    conclusion.append(summaryEnd(1))
                }
            }
            1 -> {
                conclusion.append(summaryStart[2])
                conclusion.append(getDetailSummary(riskLevel))
                conclusion.append(summaryEnd(2))
            }
            2 -> {
                conclusion.append(summaryStart[3])
                conclusion.append(getDetailSummary(riskLevel))
                conclusion.append(summaryEnd(3))
            }
        }
 
        return conclusion.toString()
    }
 
    private fun getDetailSummary(riskLevel: Int):String {
        val detail = StringBuilder()
        detail.append(riskLedger.getSummary())
        detail.append(riskSelfLedger.getSummary())
        detail.append(riskAuthenticated.getSummary())
        detail.append(riskCommitment.getSummary())
        if (detail.isBlank()) detail.append("线上配合度高\n")
        detail.append(riskAssessment.getSummary(riskLevel))
        return detail.toString()
    }
}