feiyu02
2025-09-18 baf2cc2ce3dfd1235c012a3750132769fcd9ad2f
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
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.QueryByCache
import cn.flightfeather.supervision.domain.ds1.entity.SceneConstructionSite
import cn.flightfeather.supervision.lightshare.service.DataProdBaseService
import cn.flightfeather.supervision.lightshare.service.DataProdMiddleService
import cn.flightfeather.supervision.lightshare.vo.dataprod.QueryOpt
import cn.flightfeather.supervision.lightshare.vo.dataprod.middle.DPEvaluationByArea
import cn.flightfeather.supervision.lightshare.vo.dataprod.middle.DPInspectionSummary
import cn.flightfeather.supervision.lightshare.vo.dataprod.middle.DPProblemCountByArea
import cn.flightfeather.supervision.lightshare.vo.dataprod.middle.DPProblemTypeCount
import org.springframework.stereotype.Service
 
/**
 *
 * @date 2025/9/15
 * @author feiyu02
 */
@Service
class DataProdMiddleServiceImpl(private val dataProdBaseService: DataProdBaseService) : DataProdMiddleService {
 
    override fun getInspectionSummary(queryOpt: QueryOpt): DPInspectionSummary {
        return QueryByCache.queryCache(
            cache = { return@queryCache null },
            calculate = {
                val res = DPInspectionSummary()
                val inspectionInfo = dataProdBaseService.getInspectionInfo(queryOpt)
                val sceneInfo = dataProdBaseService.getSceneInfo(queryOpt)
                res.sceneCount = sceneInfo.size
                res.pointCount = inspectionInfo.size
                res.reviewPointCount = inspectionInfo.size - inspectionInfo.distinctBy { it.subTask?.scenseid }.size
                sceneInfo.forEach {
                    when (it.scene?.typeid.toString()) {
                        // 对于建筑工地类型,根据其csStatus判断是否停工或完工
                        Constant.SceneType.TYPE1.value -> {
                            when ((it.subScene as SceneConstructionSite).csStatus) {
                                "停工" -> {
                                    res.stopSceneCount++
                                }
 
                                "完工" -> {
                                    res.completeSceneCount++
                                }
                            }
                        }
                        // 对于其他类型,根据其extension1(代表是否上线)判断是否完工
                        else -> {
                            if (it.scene?.extension1 != "1") {
                                res.completeSceneCount++
                            }
                        }
                    }
                }
                return@queryCache res
            },
            save = {}
        )
    }
 
    override fun getProblemTypeSummary(queryOpt: QueryOpt): List<DPProblemTypeCount> {
        return QueryByCache.queryCache(
            cache = { return@queryCache null },
            calculate = {
                val res = mutableListOf<DPProblemTypeCount>()
                val inspectionInfo = dataProdBaseService.getInspectionInfo(queryOpt)
                val allProblemList = inspectionInfo.flatMap { it.problems ?: emptyList() }
                allProblemList.groupBy { it.typeid }.forEach { (typeid, problemList) ->
                    res.add(DPProblemTypeCount().apply {
                        this.typeId = typeid
                        this.typeName = problemList.firstOrNull()?.typename
                        this.count = problemList.size
                        this.ratio = problemList.size.toDouble() / allProblemList.size
                    })
                }
                return@queryCache res
            },
            save = {}
        )
    }
 
    override fun getProblemCountByArea(queryOpt: QueryOpt): List<DPProblemCountByArea> {
        return QueryByCache.queryCache(
            cache = { return@queryCache null },
            calculate = {
                val res = mutableListOf<DPProblemCountByArea>()
                val inspectionInfo = dataProdBaseService.getInspectionInfo(queryOpt)
                inspectionInfo
                    .groupBy { it.subTask?.provincecode + it.subTask?.citycode + it.subTask?.districtcode + it.subTask?.towncode }
                    .forEach { (areaName, ins) ->
                        val firstSubtask = ins.first().subTask
                        val allProblemList = ins.flatMap { it.problems ?: emptyList() }
                        res.add(DPProblemCountByArea().apply {
                            this.provinceCode = firstSubtask?.provincecode
                            this.provinceName = firstSubtask?.provincename
                            this.cityCode = firstSubtask?.citycode
                            this.cityName = firstSubtask?.cityname
                            this.districtCode = firstSubtask?.districtcode
                            this.districtName = firstSubtask?.districtname
                            this.townCode = firstSubtask?.towncode
                            this.townName = firstSubtask?.townname
                            this.sceneCount = ins.distinctBy { it.subTask?.scenseid }.size
                            this.problemCount = allProblemList.size
                            this.ratio = problemCount.toDouble() / sceneCount
                        })
                    }
 
                return@queryCache res
            },
            save = {}
        )
    }
 
    override fun getEvaluationByArea(queryOpt: QueryOpt): List<DPEvaluationByArea> {
        return QueryByCache.queryCache(
            cache = { return@queryCache null },
            calculate = {
                val res = mutableListOf<DPEvaluationByArea>()
                val sceneInfo = dataProdBaseService.getSceneInfo(queryOpt)
                val evaluation = dataProdBaseService.getEvaluateInfo(queryOpt)
                evaluation
                    .groupBy { it.subTask?.provincecode + it.subTask?.citycode + it.subTask?.districtcode + it.subTask?.towncode }
                    .forEach { (areaName, evals) ->
                        val firstSubtask = evals.first().subTask
                        res.add(DPEvaluationByArea().apply {
                            this.provinceCode = firstSubtask?.provincecode
                            this.provinceName = firstSubtask?.provincename
                            this.cityCode = firstSubtask?.citycode
                            this.cityName = firstSubtask?.cityname
                            this.districtCode = firstSubtask?.districtcode
                            this.districtName = firstSubtask?.districtname
                            this.townCode = firstSubtask?.towncode
                            this.townName = firstSubtask?.townname
                            this.validSceneCount = sceneInfo
                                .filter {
                                    it.scene?.provincecode + it.scene?.citycode + it.scene?.districtcode + it.scene?.towncode == areaName
                                            && it.statusBool
                                }.size
                            this.evaluationCount = evals.size
                            this.evalLevelACount = evals.count { it.scoreLevel == Constant.EvaluationLevel.A.text }
                            this.evalLevelBCount = evals.count { it.scoreLevel == Constant.EvaluationLevel.B.text }
                            this.evalLevelCCount = evals.count { it.scoreLevel == Constant.EvaluationLevel.C.text }
                            this.evalLevelDCount = evals.count { it.scoreLevel == Constant.EvaluationLevel.D.text }
                            this.evalLevelRatioAB = (this.evalLevelACount + this.evalLevelBCount).toDouble() / this.evaluationCount
                        })
                    }
 
                return@queryCache res
            },
            save = {}
        )
    }
}