feiyu02
2024-08-13 b8cc591541b88dd2bb93f111f8e8075842dce7ca
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
package cn.flightfeather.supervision.business
 
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.*
import cn.flightfeather.supervision.lightshare.vo.InspectionVo
import cn.flightfeather.supervision.lightshare.vo.ProblemlistVo
import cn.flightfeather.supervision.lightshare.vo.ScenseVo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.*
import javax.annotation.PostConstruct
import kotlin.collections.ArrayList
 
/**
 * @author riku
 * Date: 2020/8/7
 */
 
@Component
class AutoScore2 {
 
    companion object {
        private lateinit var autoScore: AutoScore2
 
    }
 
    @Autowired
    lateinit var problemlistService: ProblemlistService
    @Autowired
    lateinit var evaluationruleService: EvaluationruleService
    @Autowired
    lateinit var evaluationsubruleService: EvaluationsubruleService
    @Autowired
    lateinit var scenseService: ScenseService
    @Autowired
    lateinit var inspectionService: InspectionService
    @Autowired
    lateinit var evaluationService: EvaluationService
    @Autowired
    lateinit var itemevaluationService: ItemevaluationService
 
    private var problems: ArrayList<ProblemlistVo> = ArrayList()
 
    private var allRules: ArrayList<Evaluationsubrule> = ArrayList()
 
    private var inspection = InspectionVo()
 
    private var evaluationrule: Evaluationrule? = null
 
    private var scense: ScenseVo? = null
 
    private var totalPoint = 0
 
    //子评分项得分列表
    val childScorelist = ArrayList<Itemevaluation>()
    //总得分
    val totalScore = Evaluation()
 
    var subtask = Subtask()
 
    @PostConstruct
    fun init() {
        autoScore = this
        autoScore.problemlistService = this.problemlistService
        autoScore.evaluationruleService = this.evaluationruleService
        autoScore.evaluationsubruleService = this.evaluationsubruleService
        autoScore.scenseService = this.scenseService
        autoScore.inspectionService = this.inspectionService
        autoScore.evaluationService = this.evaluationService
        autoScore.itemevaluationService = this.itemevaluationService
    }
 
    fun calculateScore() {
        //获取子任务对应的问题列表
        this.problems.addAll(autoScore.problemlistService.findBySubtaskId(subtask.stguid!!))
        //获取子任务对应的场景信息
        scense = autoScore.scenseService.findOne(subtask.scenseid!!)
        //获取对应任务类型、场景类型、区县的评分表名称
        val evaluationrule = autoScore.evaluationruleService.findBySpecificRule(subtask.typeno, scense!!.typeid, subtask.districtcode)
        if (evaluationrule === null) return
        this.evaluationrule = evaluationrule
        //获取对应的具体评分细则
        allRules = autoScore.evaluationsubruleService.findByRuleId(evaluationrule.guid!!) as ArrayList<Evaluationsubrule>
        //获取巡查信息
        inspection = autoScore.inspectionService.findBySubTaskID(subtask.stguid!!)
 
 
        val titleRule = Evaluationsubrule()
        val titleOptionRange = ArrayList<Int>()
        val titleIsGood = true
 
        //满分
        totalPoint = getTotalPoint(allRules, titleRule.guid)
 
 
        //得到的point是扣分分数
        val point = autoGeneration(titleRule, totalPoint)
        createTotalScore(point)
        update()
    }
 
 
    /**
     * 自动打分
     * @param rule 当前评分项
     * @param maxScore 父项的最大分
     */
    fun autoGeneration(rule: Evaluationsubrule, maxScore: Int): Int? {
 
        val nextSubRules = getSubRulesByFatherId(allRules, rule.guid)
        //无子项的最小评分项,给出得分
        if (nextSubRules.isEmpty()) {
            val totalScore = writeScore4th(rule, maxScore)
            createNewChildScore(rule, totalScore.first, totalScore.second)
            return totalScore.first
        }
        //有子项的评分项,以每项子项为父项继续递归,获取总分
        else {
            var totalScore = 0//有子项的评分项一定会有分数,不会出现null的情况
 
            var nextTmpTotalScore = 0//所有子项的总得分
            nextSubRules.forEach {
                val nextFatherRule = it
                val score = autoGeneration(nextFatherRule, nextFatherRule.maxscore?:0)
                if (score != null) {
                    nextTmpTotalScore += score
                }
            }
 
            totalScore += nextTmpTotalScore
            //取得总分后,生成对应的评分信息并存入列表
            if (!rule.guid.isNullOrBlank()) {
                createNewChildScore(rule, totalScore, true)
            }
 
            return totalScore
        }
    }
 
    //填写详细评分项的分数
    fun writeScore4th(rule: Evaluationsubrule, maxScore: Int): Pair<Int, Boolean> {
 
        problems.forEach {
            if (it.guid != null) {
                if (rule.problemlist?.contains(it.guid!!) == true) {
                    return Pair(rule.maxscore?.minus(maxScore) ?: 0, true)
                }
            }
        }
        //若不需要评分,此处将其设置为空
        return Pair(0, false)
    }
 
    //按照父id查找子评分项
    fun getSubRulesByFatherId(rules: ArrayList<Evaluationsubrule>, fatherId: String?): List<Evaluationsubrule> {
        val subRules = mutableListOf<Evaluationsubrule>()
        rules.forEach {
            if (it.fatherid == fatherId
                    || (fatherId.isNullOrBlank() && it.fatherid.isNullOrBlank())) {
                subRules.add(it)
//                rules.remove(it)
            }
        }
        return subRules
    }
 
    //创建单项评分对象
    fun createNewChildScore(subRule: Evaluationsubrule, score: Int, isSelected: Boolean): Int? {
        val itemevaluation = Itemevaluation()
        itemevaluation.ieguid = UUIDGenerator.generate16ShortUUID()
        itemevaluation.iguid = inspection.guid
        itemevaluation.stguid = subtask.stguid
        itemevaluation.sguid = subtask.scenseid
        itemevaluation.sensename = subtask.scensename
        itemevaluation.erguid = evaluationrule!!.guid
        itemevaluation.rulename = evaluationrule!!.rulename
        itemevaluation.ruletype = 2
//        itemevaluation.ertype = subRule.ertype?.toInt()
        itemevaluation.esrguid = subRule.guid
        itemevaluation.name = subRule.itemname
 
        itemevaluation.value = score.toString()
        itemevaluation.extension1 = isSelected.toString()
 
        childScorelist.add(itemevaluation)
 
        return itemevaluation.value?.toInt()
    }
 
    //创建总分对象
    fun createTotalScore(point: Int?) {
        totalScore.guid = UUIDGenerator.generate16ShortUUID()
        totalScore.iguid = inspection.guid
        totalScore.stguid = subtask.stguid
        totalScore.sguid = scense?.guid
        totalScore.scensetypeid = scense?.typeid
        totalScore.scensetype  =scense?.type
        totalScore.subscensetypeid = scense?.scensesubtypeid
        totalScore.subscensetype = scense?.scensesubtype
        totalScore.ertype = evaluationrule?.ruletype?.toByte()
        totalScore.provincecode = scense?.provincecode
        totalScore.provincename = scense?.provincename
        totalScore.citycode = scense?.citycode
        totalScore.cityname = scense?.cityname
        totalScore.districtcode = scense?.districtcode
        totalScore.districtname = scense?.districtname
        totalScore.towncode = scense?.towncode
        totalScore.townname = scense?.townname
        totalScore.scensename = scense?.name
        totalScore.scenseaddress = scense?.location
        totalScore.evaluatetime = Date()
        totalScore.evaluatorguid = "admin"
        totalScore.evaluatorusername = "admin"
        totalScore.evaluatorrealname = "admin"
        totalScore.createdate = Date()
        totalScore.resultscorebef = (totalPoint - Math.abs(point!!)).toInt().toString()
    }
 
    //获取总分
    fun getTotalPoint(rules: ArrayList<Evaluationsubrule>, fatherId: String?): Int {
        val rules1 = getSubRulesByFatherId(rules, fatherId)
        var point = 0
        rules1.forEach {
            point += it.maxscore?: 0
        }
        return point
    }
 
    //数据库更新
    fun update() {
//        println(totalScore)
        autoScore.evaluationService.save(totalScore)
        childScorelist.forEach {
            autoScore.itemevaluationService.save(it)
//            println(it)
        }
    }
 
}