feiyu02
2022-07-20 39e208b6b0482a25c77e53590087c02d9d937563
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
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.*
import cn.flightfeather.supervision.common.utils.ExcelUtil
import kotlin.math.round
 
open class ProTypeRankSummary(dataSource: DataSource) : BaseTemplate(dataSource) {
    override val cols: List<BaseCols> = listOf()
    override val templateName: String = "问题与整改分类排名"
 
    override fun execute() {
        dataSource.reset()
 
        val proMap = mutableMapOf<String?, Summary>()
        dataSource.loop { _, rowData ->
            rowData.problems.forEach {
                val k = it.ptguid
                if (!proMap.containsKey(k)) {
                    proMap[k] = Summary().apply {
                        for (p in rowData.problemTypes) {
                            if (p.guid == k) {
                                proType = p.typename ?: ""
                                proDes = p.description ?: ""
                                break
                            }
                        }
                    }
                }
                proMap[k]?.apply {
                    count++
                    if (it.ischanged == true) changeNum++
                }
            }
        }
 
        //占比统计
        val summarys = mutableListOf<Summary>()
        var tPros = 0
        proMap.forEach {
            val v = it.value
            summarys.add(v)
            tPros += v.count
        }
        summarys.sortByDescending { it.count }
        for (i in summarys.indices) {
            val it = summarys[i]
            it.countPer = it.count.toDouble() / tPros
            if (i > 0 && summarys[i - 1].countPer == it.countPer) {
                it.countRank = summarys[i - 1].countRank
            } else {
                it.countRank = i + 1
            }
            it.changePer = it.changeNum.toDouble() / it.count
        }
        summarys.sortByDescending { it.changePer }
        for (i in summarys.indices) {
            val it = summarys[i]
            if (i > 0 && summarys[i - 1].changePer == it.changePer) {
                it.changeRank = summarys[i - 1].changeRank
            } else {
                it.changeRank = i + 1
            }
        }
 
        formatTable(summarys)
    }
 
    open fun formatTable(summarys: MutableList<Summary>) {
        head.clear()
        head.add(
            mutableListOf(
                ExcelUtil.MyCell("序号", rowSpan = 2),
                ExcelUtil.MyCell("年度", rowSpan = 2),
                ExcelUtil.MyCell("月份", rowSpan = 2),
                ExcelUtil.MyCell("场景类别", rowSpan = 2),
                ExcelUtil.MyCell("区域", rowSpan = 2),
                ExcelUtil.MyCell("问题大类(二级指标)", rowSpan = 2),
                ExcelUtil.MyCell("问题小类(三级指标)", rowSpan = 2),
                ExcelUtil.MyCell("问题占比排名分析", colSpan = 3),
                ExcelUtil.MyCell("整改率排名分析", colSpan = 3),
            )
        )
        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("整改排名"),
            )
        )
        for (i in summarys.indices) {
            val s = summarys[i]
//            contents.add(
//                mutableListOf(
//                    i + 1, dataSource.year, dataSource.month, dataSource.rowData.scene?.type ?: "", dataSource.area,
//                    s.proType, s.proDes,
//                    s.count, "${round(s.countPer * 1000) / 10}%", s.countRank,
//                    s.changeNum, "${round(s.changePer * 1000) / 10}%", s.changeRank
//                )
//            )
            contents.add(
                    mutableListOf(
                            i + 1, dataSource.year, dataSource.month, dataSource.rowData.scene?.type ?: "", dataSource.area,
                            s.proType, s.proDes,
                            s.count, ExcelUtil.MyCell(s.countPer.toString(), isPercent = true), s.countRank,
                            s.changeNum, ExcelUtil.MyCell(s.changePer.toString(), isPercent = true), s.changeRank
                    )
            )
        }
    }
 
    inner class Summary() {
        var proType = ""
        var proDes = ""
        var count = 0
        var countPer = .0
            set(value) {
                field = if (value.isNaN()) .0 else value
            }
        var countRank = 0
 
        var changeNum = 0
        var changePer = .0
            set(value) {
                field = if (value.isNaN()) .0 else value
            }
        var changeRank = 0
    }
}