feiyu02
2022-07-19 b041775dae4438e428d2b70d2b22cfe8c49844fe
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
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
            it.countRank = i + 1
            it.changePer = it.changeNum.toDouble() / it.count
        }
        summarys.sortByDescending { it.changePer }
        for (i in summarys.indices) {
            val it = summarys[i]
            it.changeRank = i + 1
        }
 
        formatTable(summarys)
    }
 
    open fun formatTable(summarys: List<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.rowData.scene?.type ?: "", "",
                    s.proType, s.proDes,
                    s.count, "${round(s.countPer * 1000) / 10}%", s.countRank,
                    s.changeNum, "${round(s.changePer * 1000) / 10}%", 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
    }
}