feiyu02
2022-07-28 e844ef2fdab88508e7dff4bb9e7b1632fcce15b2
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
193
194
195
196
197
198
199
package cn.flightfeather.supervision.business.report.template
 
import cn.flightfeather.supervision.business.report.BaseCols
import cn.flightfeather.supervision.business.report.BaseTemplate
import cn.flightfeather.supervision.business.report.DataSource
import cn.flightfeather.supervision.business.report.cols.ColInspectionInfo
import cn.flightfeather.supervision.business.report.cols.ColStrategy
import cn.flightfeather.supervision.business.report.cols.ColTotalGrade
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.ExcelUtil
import cn.flightfeather.supervision.domain.ds1.entity.SceneConstructionSite
import cn.flightfeather.supervision.domain.ds1.entity.SceneMixingPlant
import cn.flightfeather.supervision.domain.ds1.entity.SceneStorageYard
import cn.flightfeather.supervision.domain.ds1.entity.SceneWharf
import kotlin.math.round
 
/**
 * 分街镇问题整改分析汇总表
 */
class ProAnalysisSummary(dataSource: DataSource) : BaseTemplate(dataSource) {
    override val cols: List<BaseCols> = listOf(ColInspectionInfo(), ColTotalGrade(), ColStrategy())
 
    override val templateName: String = "分街镇问题整改分析汇总表"
 
    override fun execute() {
        //数据源重置
        dataSource.reset()
 
        cols.forEach { it.combineHead(head,dataSource) }
 
        val districtMap = mutableMapOf<String?, Summary>()
        dataSource.loop { _, rowData ->
            if (rowData.noRecord()) return@loop
 
            val r = cols[0].getOneRow(rowData)
            val r1 = cols[1].getOneRow(rowData)
//            val r2 = cols[2].getOneRow(rowData)
 
            val k = rowData.scene?.townname
            if (!districtMap.containsKey(k)) {
                districtMap[k] = Summary().apply {
                    townCode = rowData.scene?.towncode ?: ""
                    townName = rowData.scene?.townname ?: ""
                    type = rowData.scene?.type ?: ""
                }
            }
            districtMap[k]?.apply {
                sceneCount++
                val status = when (dataSource.config.sceneType.toString()) {
                    Constant.ScenseType.TYPE1.value -> {
                        (rowData.baseScene as SceneConstructionSite?)?.csStatus
                    }
                    Constant.ScenseType.TYPE2.value -> {
                        (rowData.baseScene as SceneWharf?)?.getwStatus()
                    }
                    Constant.ScenseType.TYPE3.value -> {
                        (rowData.baseScene as SceneMixingPlant?)?.mpStatus
                    }
                    Constant.ScenseType.TYPE14.value -> {
                        (rowData.baseScene as SceneStorageYard?)?.syStatus
                    }
                    else -> ""
                }
                // FIXME: 2022/7/18 后续场景的施工状态改为布尔值存储
                if ((status?.contains("完工") == true) || (status?.contains("未施工") == true) || (status?.contains("停工") == true)
                    || (status?.contains("关") == true)
                ) {
                    inactiveScenes++
                } else {
                    activeScenes++
                }
                val pNum = r[5] as Int
                val cNum = r[9] as Int
                if (pNum > 0 && cNum > 0) changeScenes++
                proNum += pNum
                changeNum += cNum
 
 
                val standard = r1[1] as ExcelUtil.MyCell
                if (standard.text.contains("严重不规范")) {
                    focusSceneNum++
                }
 
                //根据监管策略结果判断是否为重点监管对象
//                if (r2[0].toString().contains("7、")) {
//                    focusSceneNum++
//                }
            }
        }
 
        val summarys = mutableListOf<Summary>()
        var totalPro = 0
        districtMap.forEach {
            summarys.add(it.value)
            totalPro += it.value.proNum
        }
        districtMap.forEach {
            val v = it.value
            v.changeScenePer = v.changeScenes.toDouble() / v.sceneCount
            v.proPer = v.proNum.toDouble() / totalPro
            v.changePer = v.changeNum.toDouble() / v.proNum
            v.focusScenePer = v.focusSceneNum.toDouble() / v.sceneCount
        }
        //整改单位比排名
        summarys.sortByDescending {
            it.changeScenePer
        }
        for (i in summarys.indices) {
            if (i > 0 && summarys[i - 1].changeScenePer == summarys[i].changeScenePer) {
                summarys[i].changeSceneRank = summarys[i - 1].changeSceneRank
            } else {
                summarys[i].changeSceneRank = i + 1
            }
        }
        //问题整改率排名
        summarys.sortByDescending {
            it.changePer
        }
        for (i in summarys.indices) {
            if (i > 0 && summarys[i - 1].changePer == summarys[i].changePer) {
                summarys[i].proChangeRank = summarys[i - 1].proChangeRank
            } else {
                summarys[i].proChangeRank = i + 1
            }
        }
 
        //更新表头
        head.clear()
        head.add(mutableListOf(
            ExcelUtil.MyCell("街镇序号", rowSpan = 2),
            ExcelUtil.MyCell("区域"),
            ExcelUtil.MyCell("场景类别", rowSpan = 2),
            ExcelUtil.MyCell("场景状态", colSpan = 3),
            ExcelUtil.MyCell("整改单位", colSpan = 2),
            ExcelUtil.MyCell("问题与整改", colSpan = 4),
            ExcelUtil.MyCell("排名", colSpan = 2),
            ExcelUtil.MyCell("区域监管策略", colSpan = 2),
        ))
        head.add(mutableListOf(
            ExcelUtil.MyCell(""),
            ExcelUtil.MyCell("街镇/工业区"),
            ExcelUtil.MyCell(""),
            ExcelUtil.MyCell("场景数"),
            ExcelUtil.MyCell("完工、未施工、停工或停业、关闭等"),
            ExcelUtil.MyCell("施工中、运营中总数"),
            ExcelUtil.MyCell("整改单位数"),
            ExcelUtil.MyCell("整改单位占比"),
            ExcelUtil.MyCell("问题数"),
            ExcelUtil.MyCell("问题占比"),
            ExcelUtil.MyCell("整改数"),
            ExcelUtil.MyCell("整改率"),
            ExcelUtil.MyCell("整改单位比排名"),
            ExcelUtil.MyCell("问题整改率排名"),
            ExcelUtil.MyCell("拟列入重点监管数"),
            ExcelUtil.MyCell("拟列入重点监管占比"),
        ))
        //更新内容
        summarys.sortBy { it.townCode }
        for (i in summarys.indices) {
            val it = summarys[i]
            contents.add(
                mutableListOf(
                    i+1, it.townName, it.type, it.sceneCount, it.inactiveScenes, it.activeScenes, it.changeScenes,
                        ExcelUtil.MyCell(it.changeScenePer.toString(), isPercent = true),
                    it.proNum, ExcelUtil.MyCell(it.proPer.toString(), isPercent = true), it.changeNum, ExcelUtil.MyCell(it.changePer.toString(), isPercent = true), it.changeSceneRank, it.proChangeRank,
                        it.focusSceneNum, ExcelUtil.MyCell(it.focusScenePer.toString(), isPercent = true)
                )
            )
        }
    }
 
    inner class Summary(){
        var townCode = ""
        var townName = ""
        var type = ""
        var sceneCount = 0
        //停运的场景数
        var inactiveScenes = 0
        var activeScenes = 0
 
        var changeScenes = 0
        var changeScenePer = .0
            set(value) { field = if (value.isNaN()) .0 else value }
 
        var proNum = 0
        var proPer = .0
            set(value) { field = if (value.isNaN()) .0 else value }
 
        var changeNum = 0
        var changePer = .0
            set(value) { field = if (value.isNaN()) .0 else value }
 
        var changeSceneRank = 0
        var proChangeRank = 0
 
        var focusSceneNum = 0
        var focusScenePer = .0
    }
}