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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package cn.flightfeather.supervision.common.score
 
import cn.flightfeather.supervision.domain.entity.Evaluation
import cn.flightfeather.supervision.domain.entity.Evaluationrule
import cn.flightfeather.supervision.domain.entity.Evaluationsubrule
import cn.flightfeather.supervision.domain.entity.Itemevaluation
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.infrastructure.utils.ExcelUtil
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.util.HSSFColor
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.io.File
import java.io.FileOutputStream
import java.util.*
 
/**
 * 自评得分导出
 */
@Component
class ScoreUtil(
    private val evaluationruleMapper: EvaluationruleMapper,
    private val evaluationsubruleMapper: EvaluationsubruleMapper,
    private val evaluationMapper: EvaluationMapper,
    private val itemevaluationMapper: ItemevaluationMapper,
    private val userinfoMapper: UserinfoMapper
) {
 
    var head3 = true
 
    fun outPut() {
        val scoreItem = getScoreItem()
        val evaluations = getScoreResult()
        val titles = genTitles(scoreItem.second, scoreItem.third)
        val contents = genContents(evaluations, scoreItem.second, scoreItem.third)
        titles.addAll(contents)
        toFile(titles, SceneType.VehicleRepair)
    }
 
    /**
     * 获取评分规则
     */
    fun getScoreItem(sceneType: Int = -7, districtCode: String = "310104")
            : Triple<MutableList<Evaluationrule>,
            MutableList<Evaluationsubrule>,
            MutableList<Pair<Evaluationsubrule, MutableList<Evaluationsubrule>>>> {
        val subRules = mutableListOf<Pair<Evaluationsubrule, MutableList<Evaluationsubrule>>>()
        val baseRules = mutableListOf<Evaluationrule>()
        val topItems = mutableListOf<Evaluationsubrule>()
 
        val rule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
            createCriteria()
//                .andEqualTo("tasktypeid", 1)
                .andEqualTo("scensetypeid", sceneType.toByte())
                .andEqualTo("districtcode", districtCode)
        })
        if (rule.isNotEmpty()) {
            baseRules.addAll(rule)
 
            val ruleId = rule[0].guid
            val rules = evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule::class.java).apply {
                createCriteria().andEqualTo("erguid", ruleId)
            })
            rules.forEach {
                if (it.ertype == 2) {
                    topItems.add(it)
                }
            }
            topItems.sortBy { it.displayid }
 
            var t = 0
            topItems.forEach {
                t += it.maxscore ?: 0
                val tempRules = mutableListOf<Evaluationsubrule>()
                for (i in rules) {
                    if (i.fatherid == it.guid && i.ertype == 3) {
                        tempRules.add(i)
                    }
                }
                //评分大项如果没有找到评分小项,则说明其直接对应最小评分项,其本身变成评分项
                if (tempRules.isEmpty()) {
                    tempRules.add(it)
                }
                tempRules.sortBy { t -> t.displayid }
                tempRules.forEach { temp ->
                    val tempSubRules = mutableListOf<Evaluationsubrule>()
                    for (i in rules) {
                        if (i.fatherid == temp.guid && i.ertype == 4) {
                            tempSubRules.add(i)
                        }
                    }
                    tempSubRules.sortBy { ts -> ts.displayid }
                    subRules.add(Pair(temp, tempSubRules))
                }
            }
        }
 
        return Triple(baseRules, topItems, subRules)
    }
 
    fun getScoreResult(): List<Evaluation> {
        return evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
            createCriteria().andEqualTo("stguid", "3RNqhxpCe8lQoPss")
        })
    }
 
    fun genTitles(topItems: List<Evaluationsubrule>, rules: List<Pair<Evaluationsubrule,
            MutableList<Evaluationsubrule>>>): MutableList<Array<Any>> {
        val contents = mutableListOf<Array<Any>>()
        val h1 = mutableListOf<ExcelUtil.MyCell>()
        h1.add(ExcelUtil.MyCell(""))
        val h2 = mutableListOf<ExcelUtil.MyCell>()
        h2.add(ExcelUtil.MyCell(""))
        val h3 = mutableListOf<String>()
        h3.add("")
        topItems.forEach {
            h1.add(ExcelUtil.MyCell(it.itemname ?: "", 1, 0))
            for (r in rules) {
                if (r.first.fatherid == it.guid || r.first.guid == it.guid) {
                    h2.add(ExcelUtil.MyCell(r.first.itemname ?: "", 1, 0))
                    // FIXME: 2021/4/25 决定是否写h3
                    if (head3) {
                        r.second.forEach { s ->
                            h3.add(s.itemname ?: "")
                            h2.last().colSpan++
                            h1.last().colSpan++
                        }
                    } else {
                        h2.last().colSpan++
                        h1.last().colSpan++
                    }
                }
            }
        }
        // FIXME: 2021/4/25 决定是否写h3
        if (head3) {
            h1.add(ExcelUtil.MyCell("总分", 3, 1))
            h1.add(ExcelUtil.MyCell("环信码", 3, 1))
            h1.add(ExcelUtil.MyCell("周期", 3, 1))
        } else {
            h1.add(ExcelUtil.MyCell("总分", 2, 1))
            h1.add(ExcelUtil.MyCell("环信码", 2, 1))
            h1.add(ExcelUtil.MyCell("周期", 2, 1))
        }
        contents.add(h1.toTypedArray())
        contents.add(h2.toTypedArray())
        // FIXME: 2021/4/25 决定是否写h3
        if (head3) contents.add(h3.toTypedArray())
 
        return contents
    }
 
    fun genContents(
        evaluations: List<Evaluation>, topItems: MutableList<Evaluationsubrule>,
        rules: MutableList<Pair<Evaluationsubrule, MutableList<Evaluationsubrule>>>
    ): List<Array<Any>> {
        val contents = mutableListOf<Array<Any>>()
        evaluations.forEach {
            val cList = mutableListOf<Any>()
            val user = userinfoMapper.selectByPrimaryKey(it.evaluatorguid)
            cList.add("${user?.realname}")
            val itemevaluations = itemevaluationMapper.selectByExample(Example(Itemevaluation::class.java).apply {
                createCriteria().andEqualTo("sguid", it.guid)
            })
            //每一项具体得分
            topItems.forEach {sr ->
                for (r in rules) {
                    if (r.first.fatherid == sr.guid || r.first.guid == sr.guid) {
                        // FIXME: 2021/4/25 决定是否写h3
                        if (head3) {
                            r.second.forEach { s ->
                                val score = getScore(s.guid, itemevaluations)
                                cList.add(score)
                            }
                        } else {
                            val score = getScore(r.first.guid, itemevaluations)
                            cList.add(score)
                        }
                    }
                }
            }
 
            //总分和环信码
            cList.add(it.resultscorebef?.toDoubleOrNull() ?: -99.0)
            val code = when (it.resultscorebef?.toIntOrNull() ?: 0) {
                in 0..59 -> ExcelUtil.MyCell("红码", fontColor = HSSFColor.HSSFColorPredefined.RED.index)
                in 60..89 -> ExcelUtil.MyCell("黄码", fontColor = HSSFColor.HSSFColorPredefined.GOLD.index)
                in 90..120 -> ExcelUtil.MyCell("绿码", fontColor = HSSFColor.HSSFColorPredefined.BRIGHT_GREEN.index)
                else -> ExcelUtil.MyCell("超出范围:${it.resultscoreaft}", fontColor = HSSFColor.HSSFColorPredefined.BLACK.index)
            }
            cList.add(code)
            cList.add(it.scensename ?: "")
 
            contents.add(cList.toTypedArray())
        }
        return contents
    }
 
    private fun getScore(subRuleId: String?, scoreList: List<Itemevaluation>): String{
        val s = scoreList.find {
            it.esrguid == subRuleId
        }
        return if (s?.extension1 == "true") s.value ?: "" else ""
    }
 
    fun toFile(contents: MutableList<Array<Any>>, sceneType: SceneType) {
        // 写入文档
        val workbook = HSSFWorkbook()
        val fileName = "${sceneType.des}评分-${DateUtil.DateToString(Date(), "yyyy-MM-ddhhmmss")}.xls"
//        val filePath = "E:\\工作\\开发\\飞羽环境app\\自动评分\\${SCENE_TYPE.des}\\$fileName"
        val filePath = "C:\\work\\工作\\第三方监管\\用户自评\\${sceneType.des}\\$fileName"
        val file = File(filePath)
        if (!file.parentFile.exists()) {
            file.parentFile.mkdirs()
        }
        val out = FileOutputStream(file)
 
        ExcelUtil.write2(emptyList(), contents, workbook)
        workbook.write(out)
        workbook.close()
        out.flush()
        out.close()
    }
}