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()
|
}
|
}
|