feiyu02
2024-09-27 72a384981ec78139dbe919597b54efedcc7f33aa
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.business.AutoScore2
import cn.flightfeather.supervision.business.bgtask.AopTaskCtrl
import cn.flightfeather.supervision.business.autooutput.score.AopEvaluation
import cn.flightfeather.supervision.business.bgtask.ReportTaskCtrl
import cn.flightfeather.supervision.business.report.DataSource
import cn.flightfeather.supervision.business.report.DbMapper
import cn.flightfeather.supervision.business.report.file.ReportTwo
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.executor.BgTaskStatus
import cn.flightfeather.supervision.domain.ds1.entity.Domainitem
import cn.flightfeather.supervision.domain.ds1.entity.Evaluation
import cn.flightfeather.supervision.domain.ds1.entity.Subtask
import cn.flightfeather.supervision.domain.ds1.mapper.DomainitemMapper
import cn.flightfeather.supervision.domain.ds1.mapper.EvaluationMapper
import cn.flightfeather.supervision.domain.ds1.mapper.SubtaskMapper
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.common.utils.Domain
import cn.flightfeather.supervision.domain.ds1.repository.EvaluationRep
import cn.flightfeather.supervision.domain.ds1.repository.TaskRep
import cn.flightfeather.supervision.lightshare.service.EvaluationService
import cn.flightfeather.supervision.lightshare.service.SearchService
import cn.flightfeather.supervision.lightshare.service.SubtaskService
import cn.flightfeather.supervision.lightshare.vo.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.io.File
import java.util.*
import javax.servlet.http.HttpServletResponse
import kotlin.Comparator
 
@Service
class EvaluationServiceImpl(
    private val evaluationMapper: EvaluationMapper,
    private val evaluationRep: EvaluationRep,
    private val taskRep: TaskRep,
    private val aopTaskCtrl: AopTaskCtrl,
    private val searchService: SearchService,
    private val dbMapper: DbMapper,
    @Value("\${filePath}") private val filePath: String,
    private val reportTaskCtrl: ReportTaskCtrl,
) : EvaluationService {
 
    @Autowired
    lateinit var subtaskService: SubtaskService
 
    @Autowired
    lateinit var domainitemMapper: DomainitemMapper
 
    @Autowired
    lateinit var subtaskMapper: SubtaskMapper
 
    @Autowired
    lateinit var aopEvaluation: AopEvaluation
    private var isAutoScoreRunning = false
 
    //获取某顶层任务下某个场景的街道评分排名
    override fun getRankOfTown(tguid: String, scensetypeid: String?): List<EvaluateResVo> {
        val evaluationlist = getRankInfo(tguid, scensetypeid, null)
        var areaVolist = mutableListOf<EvaluateResVo>()
        //考核类型是规范考核
        if (evaluationlist.isNotEmpty() &&
            Objects.equals(evaluationlist.get(0).ertype, Constant.RuleType.STANDARD.value)
        ) {
            //获取规范考核的考核界限(问题数量)
            val example = Example(Domainitem::class.java)
            val criteria = example.createCriteria()
            criteria.andEqualTo("dcguid", Domain.STANDARDLEVEL.value)
            val standardlevel: String? = domainitemMapper.selectByExample(example).get(0).value
            //按街镇计算严重不规范场景数量并按百分比排序
            while (evaluationlist.isNotEmpty()) {
                val tmplist = mutableListOf<Evaluation>()
                tmplist.addAll(evaluationlist)
                val iterator: MutableIterator<Evaluation> = tmplist.iterator()
 
                val evaluation = evaluationlist.get(0)//获取一种街镇
                var areaVo = EvaluateResVo()
                areaVo = transform(areaVo, evaluation)
                while (iterator.hasNext()) {
                    val tmp = iterator.next()
                    if (Objects.equals(tmp.towncode, evaluation.towncode)) {
                        if (tmp.resultscorebef!!.toInt() >= standardlevel!!.toInt())
                            areaVo.notstandardnum++
                        areaVo.allsensenum++
                        evaluationlist.remove(tmp)
                    }
                }
                areaVolist.add(areaVo)
            }
            areaVolist = sort(areaVolist)
            return areaVolist
        }
        //考核类型是评分考核
        else if (evaluationlist.isNotEmpty() &&
            Objects.equals(evaluationlist.get(0).ertype, Constant.RuleType.SCORE.value)
        ) {
            //获取评分考核的评分界限
            val example = Example(Domainitem::class.java)
            val criteria = example.createCriteria()
            criteria.andEqualTo("dcguid", Domain.SCORELEVEL.value)
            example.orderBy("index")
            val scorelevellist = domainitemMapper.selectByExample(example)
            //按街镇计算严重不规范场景数量并按百分比排序
            while (evaluationlist.isNotEmpty()) {
                val tmplist = mutableListOf<Evaluation>()
                tmplist.addAll(evaluationlist)
                val iterator: MutableIterator<Evaluation> = tmplist.iterator()
 
                val evaluation = evaluationlist.get(0)//获取一种街镇
                var areaVo = EvaluateResVo()
                areaVo = transform(areaVo, evaluation)
                while (iterator.hasNext()) {
                    val tmp = iterator.next()
                    if (Objects.equals(tmp.towncode, evaluation.towncode)) {
                        if (tmp.resultscorebef!!.toInt() < scorelevellist.get(scorelevellist.size - 1).text!!.toInt())
                            areaVo.notstandardnum++
                        areaVo.allsensenum++
                        evaluationlist.remove(tmp)
                    }
                }
                areaVolist.add(areaVo)
            }
            areaVolist = sort(areaVolist)
            return areaVolist
        }
 
        return areaVolist
    }
 
    //获取某顶层任务下某个场景的综合评分排名
    override fun getRankOfSense(tguid: String, scensetypeid: String?, ruletypeid: ByteArray?): List<Evaluation> {
        val evaluationlist = getRankInfo(tguid, scensetypeid, ruletypeid)
        evaluationlist.sortWith(Comparator { o1, o2 -> o2!!.resultscorebef!!.toInt() - o1!!.resultscorebef!!.toInt() })
        return evaluationlist
    }
 
    override fun findOne(id: String): Evaluation = evaluationMapper.selectByPrimaryKey(id)
 
    override fun findAll(): MutableList<Evaluation> = evaluationMapper.selectAll()
 
    override fun save(evaluation: Evaluation): Int = evaluationMapper.insert(evaluation)
 
    override fun update(evaluation: Evaluation): Int = evaluationMapper.updateByPrimaryKey(evaluation)
 
    override fun delete(id: String): Int = evaluationMapper.deleteByPrimaryKey(id)
 
    //获取指定区域(顶层任务)指定场景的评分信息
    fun getRankInfo(tguid: String, scensetypeid: String?, ruletypeid: ByteArray?): MutableList<Evaluation> {
        val evaluationlist = mutableListOf<Evaluation>()
        val subtaskVolist = subtaskService.findByTaskID(tguid)
        subtaskVolist.forEach {
            val example = Example(Evaluation::class.java)
            val criteria = example.createCriteria()
 
            criteria.andEqualTo("stguid", it.stguid)//子任务id
            if (scensetypeid != null)//场景类别
                criteria.andEqualTo("scensetypeid", scensetypeid)
            if (ruletypeid != null)//评分类型
                criteria.andEqualTo("ertype", ruletypeid)
            else {
                criteria.andEqualTo("ertype", Constant.RuleType.STANDARD.value)
                val result = evaluationMapper.selectByExample(example)//查询是否有规范性评分表
                //没有规范表就查询评分表
                if (result.isEmpty()) {
                    val example1 = Example(Evaluation::class.java)
                    val criteria1 = example1.createCriteria()
                    criteria1.andEqualTo("stguid", it.stguid)//子任务id
                    if (scensetypeid != null)//场景类别
                        criteria1.andEqualTo("scensetypeid", scensetypeid)
                    criteria1.andEqualTo("ertype", Constant.RuleType.SCORE.value)
                    val result1 = evaluationMapper.selectByExample(example1)
                    if (result1.size == 1)
                        evaluationlist.add(result1.get(0))
                } else
                    if (result.size == 1)
                        evaluationlist.add(result.get(0))
            }
        }
        return evaluationlist
    }
 
    //Evaluation传递数据给AreaVo
    fun transform(areaVo: EvaluateResVo, evaluation: Evaluation): EvaluateResVo {
        areaVo.provincecode = evaluation.provincecode
        areaVo.provincename = evaluation.provincename
        areaVo.citycode = evaluation.citycode
        areaVo.cityname = evaluation.cityname
        areaVo.districtcode = evaluation.districtcode
        areaVo.districtname = evaluation.districtname
        areaVo.towncode = evaluation.towncode
        areaVo.townname = evaluation.townname
        return areaVo
    }
 
    //按街道严重不规范场景百分比排序
    fun sort(areaVolist: MutableList<EvaluateResVo>): MutableList<EvaluateResVo> {
        Collections.sort(areaVolist, object : Comparator<EvaluateResVo> {
            override fun compare(o1: EvaluateResVo?, o2: EvaluateResVo?): Int {
                var num1 = 0.0
                if (o1!!.allsensenum != 0)
                    num1 = (o1.notstandardnum / o1.allsensenum).toDouble()
                var num2 = 0.0
                if (o2!!.allsensenum != 0)
                    num2 = (o2.notstandardnum / o2.allsensenum).toDouble()
                return compareValues(num1, num2)
            }
        })
        return areaVolist
    }
 
    override fun autoScore(districtCode: String, time: String): List<String> {
        val resultList = mutableListOf<String>()
 
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("districtcode", districtCode)
        val date = DateUtil.StringToDate(time)
        criteria.andGreaterThanOrEqualTo("planstarttime", time)
        val subTaskList = subtaskMapper.selectByExample(example)
        subTaskList.forEach {
            val example1 = Example(Evaluation::class.java)
            val criteria1 = example1.createCriteria()
            criteria1.andEqualTo("stguid", it.stguid)
            val result = evaluationMapper.selectByExample(example1)
            if (result.isEmpty()) {
                val autoScore = AutoScore2()
                autoScore.subtask = it
                autoScore.calculateScore()
                resultList.add(it.stguid ?: "")
            }
        }
        return resultList
    }
 
    override fun autoScore2(subTaskId: String): List<String> {
        val resultList = mutableListOf<String>()
 
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("stguid", subTaskId)
        val subTaskList = subtaskMapper.selectByExample(example)
        subTaskList.forEach {
            val example1 = Example(Evaluation::class.java)
            val criteria1 = example1.createCriteria()
            criteria1.andEqualTo("stguid", it.stguid)
            val result = evaluationMapper.selectByExample(example1)
            if (result.isEmpty()) {
                val autoScore = AutoScore2()
                autoScore.subtask = it
                autoScore.calculateScore()
                resultList.add(it.stguid ?: "")
            }
        }
        return resultList
    }
 
    override fun autoScore3(tGuid: String, sceneTypeId: String): String {
        if (isAutoScoreRunning) return "自动评分执行中,请等待完成"
        isAutoScoreRunning = true
//        val t = Constant.ScenseType.getByValue(sceneTypeId)
//        autoScore.sceneType = t
//        autoScore.topTaskGrade(tGuid)
        aopEvaluation.executeByTopTask(tGuid, sceneTypeId.toInt())
        isAutoScoreRunning = false
        return "自动评分完成"
    }
 
    override fun findByInspectionId(inspectionId: String): List<Evaluation> {
        return evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
            createCriteria().andEqualTo("iguid", inspectionId)
        })
    }
 
    override fun autoEvaluate(areaVo: AreaVo): BgTaskStatus? {
        //执行自动评估
        return aopTaskCtrl.startNewTask(areaVo)
    }
 
    override fun findAutoEvaluation(areaVo: AreaVo): List<AutoScoreResultVo?>? {
        //1. 查找历史记录,查看评估是否已存在
//        areaVo.scensetypeid ?: throw ResponseErrorException("查询时必须选择一个场景类型")
//        val task = taskRep.findOneTask(areaVo) ?: throw ResponseErrorException("查询时必须选择一个场景类型")
        return evaluationRep.findAutoScore(areaVo)
    }
 
    override fun downloadAutoEvaluation(areaVo: AreaVo, forceUpdate: Boolean, response: HttpServletResponse): Boolean {
        areaVo.scensetypeid ?: throw BizException("必须选择一个场景类型")
        val topTask = taskRep.findOneTask(areaVo) ?: throw BizException("未找到符合条件的顶层任务")
        val config = ExcelConfigVo(
            topTask.tguid ?: "",
            topTask.starttime,
            topTask.endtime,
            topTask.provincecode,
            topTask.citycode,
            topTask.districtcode,
            topTask.towncode,
            areaVo.scensetypeid?.toInt(),
            forceUpdate = forceUpdate
        )
 
        val dataSource = DataSource(config, dbMapper)
        val t = ReportTwo(dataSource)
        val fileName = t.getReportName()
        val p = "$filePath/autoscore/"
        val file = File(p + fileName)
        if (config.forceUpdate || !file.exists()) {
            val downloadUrl = "/autoscore/${fileName}"
            reportTaskCtrl.startTask(t, downloadUrl)
            return false
        } else {
            val fName = Base64.getEncoder().encodeToString(fileName.toByteArray())
            response.apply {
                setHeader("Content-Disposition", "attachment;filename=$fName")
                setHeader("fileName", fName)
                addHeader("Access-Control-Expose-Headers", "fileName")
                contentType = "application/vnd.ms-excel;charset=UTF-8"
                setHeader("Pragma", "no-cache")
                setHeader("Cache-Control", "no-cache")
                setDateHeader("Expires", 0)
            }
            response.outputStream.write(file.readBytes())
            return true
        }
    }
 
    override fun downloadAutoEvaTemplate(areaVo: AreaVo, response: HttpServletResponse) {
        TODO("Not yet implemented")
    }
 
    override fun uploadAutoEvaluation(areaVo: AreaVo, file: MultipartFile): Boolean {
        TODO("Not yet implemented")
    }
}