riku
2025-10-27 0f58aa8ea118c3bd0b28396febc58fdbd94eef75
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
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.bean.entity.*
import cn.flightfeather.thirdappmodule.bean.vo.EvaluationsubruleVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.EvaluationRepository
import cn.flightfeather.thirdappmodule.util.UUIDGenerator
import okhttp3.ResponseBody
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.abs
 
/**
 * @author riku
 * Date: 2019/5/28
 */
class MenuGradeViewModel  : BaseViewModel() {
 
    private val evaluationRepository = EvaluationRepository.instance
 
    val ruleItemList = MutableLiveData<ArrayList<EvaluationsubruleVo>>()
 
    val totalPoints = MutableLiveData<Int>()
 
    var allRules = ArrayList<EvaluationsubruleVo>()
 
    val baseRules = ArrayList<EvaluationsubruleVo>()
 
    val subRules = ArrayList<EvaluationsubruleVo>()
 
    val mData = ArrayList<EvaluationsubruleVo>()//ListView的item的数据
 
    var rulesCache = ArrayList<EvaluationsubruleVo>()//本地缓存中数据
 
    var tmpPoints = 0
 
    var evaluaterule: Evaluationrule? = null
 
    var evaluation: Evaluation? = null
 
    fun getEvaluationRule(subtask: Subtask?, sceneTypeId: Byte, s: (evaluaterule: Evaluationrule?) -> Unit) {
        evaluationRepository.getEvaluationRule(subtask?.provincecode ?: "", subtask?.citycode ?: "",
                subtask?.districtcode ?: "", sceneTypeId,
                object : ResultCallBack<List<Evaluationrule>> {
                    override fun onSuccess(result: List<Evaluationrule>?) {
                        result?.takeIf { it.isNotEmpty() }?.get(0)?.let {
                            evaluaterule = it
                            s(it)
                            getRuleItem(it.guid, subtask?.stguid ?: "")
                        }
                    }
 
                    override fun onFailure() {
 
                    }
                })
    }
 
    fun getEvaluation(inspectionGuid: String) {
        evaluationRepository.getEvaluation(inspectionGuid, object : ResultCallBack<List<Evaluation>> {
            override fun onSuccess(result: List<Evaluation>?) {
                // fixme: 2020/8/3 目前情况下,根据巡查id查找得到的评分记录只会有一条。返回结果采用列表形式以防有扩展的可能
                result?.takeIf { it.isNotEmpty() }?.get(0)?.let {
                    evaluation = it
                }
            }
 
            override fun onFailure() {
            }
        })
    }
 
    fun getRuleItem(evaluationruleGuid: String, subTaskGuid: String) {
        evaluationRepository.getRuleItem(evaluationruleGuid, subTaskGuid, object : ResultCallBack<ArrayList<EvaluationsubruleVo>> {
            override fun onCacheSuccess(result: ArrayList<EvaluationsubruleVo>?) {
                result?.let { rulesCache = it }
            }
 
            override fun onSuccess(result: ArrayList<EvaluationsubruleVo>?) {
                result?.let { list ->
                    if (list.isNotEmpty()) {
                        ruleItemList.value = list
                        list.forEach {
                            allRules.add(it.copy())
                        }
                    }
                }
            }
 
            override fun onFailure() {
                ruleItemList.value = rulesCache
                rulesCache.forEach {
                    allRules.add(it.copy())
                }
            }
 
        })
    }
 
    fun saveGrade(inspectionGuid: String?, subTaskGuid: String?, scense: Scense?, point: Int) {
        var e = this.evaluation
        val isUpdate = this.evaluation != null
        if (e == null) {
            e = Evaluation().apply {
                guid = UUIDGenerator.generate16ShortUUID()
                iguid = inspectionGuid
                stguid = subTaskGuid
                sguid = scense?.guid
                scensetypeid = scense?.typeid
                scensetype = scense?.type
                subscensetypeid = scense?.scensesubtypeid
                subscensetype = scense?.scensesubtype
//                ertype = evaluateRuleVo?.ruletype
                provincecode = scense?.provincecode
                provincename = scense?.provincename
                citycode = scense?.citycode
                cityname = scense?.cityname
                districtcode = scense?.districtcode
                districtname = scense?.districtname
                towncode = scense?.towncode
                townname = scense?.townname
                scensename = scense?.name
                scenseaddress = scense?.location
                evaluatetime = Date()
                evaluatorguid = userId
                evaluatorusername = acountName
                evaluatorrealname = realName
                resultscorebef = point.toString()
                createdate = Date()
                updatedate = Date()
            }
        } else {
            e.apply {
//                iguid = userId
//                evaluatorguid = userId
//                evaluatorusername = userName
                resultscorebef = point.toString()
                updatedate = Date()
            }
        }
 
        val itemEvaluationVos = ArrayList<Itemevaluation>()
        allRules.forEach {
            it.itemEvaluationVo?.let { vo ->
                vo.apply {
                    iguid = inspectionGuid
                    stguid = subTaskGuid
                    sguid = scense?.guid
                    sensename = scense?.name
                    erguid = this@MenuGradeViewModel.evaluaterule?.guid
//                    ruletype = evaluateRuleVo?.ruletype
                    rulename = this@MenuGradeViewModel.evaluaterule?.rulename
                    extension1 = it.isSelected.toString()
                }
                itemEvaluationVos.add(vo)
            }
        }
 
        evaluationRepository.savePoint(e, itemEvaluationVos, isUpdate,
                object : ResultCallBack<ResponseBody> {
                    override fun onSuccess(result: ResponseBody?) {
                        if (result != null) {
//                            EventBus.getDefault().post(SaveGradeEvent(e))
                        }
                    }
 
                    override fun onFailure() {
 
                    }
 
                })
    }
 
    fun calculateGrade(adapters: ArrayList<AllRecyclerViewAdapter<EvaluationsubruleVo>>): Int {
        //清除原评分记录,防止分数累加出错
        allRules.forEach {
            it.grade = null
        }
        baseRules.forEach {
            it.grade = null
        }
        var tempAllRules = allRules.toList()
        val tempBaseRules = baseRules.toList()
        //将所有基础评分存储到tempAllRules中
        for (i in adapters.indices) {
            val tmpAdapter = adapters[i]
 
            for (x in 0 until tmpAdapter.itemCount) {
                val ruleItemVo = tmpAdapter.getItem(x)
 
                for (y in tempAllRules.indices) {
                    if (tempAllRules[y].guid == ruleItemVo?.guid) {
                        tempAllRules[y].grade = ruleItemVo?.grade//基本单元评分项
                        tempAllRules[y].isSelected = ruleItemVo?.isSelected ?: false
                    }
                }
            }
        }
 
        var totalPoints = 0
        //将所有父评分项计算填充至tempAllRules
        for (i in tempBaseRules.indices) {
            tempAllRules = calculateGrade(tempAllRules, tempBaseRules[i])
            tempAllRules.forEach {
                if (it.guid == tempBaseRules[i].guid) {
                    tempBaseRules[i].grade = it.grade
                    totalPoints = totalPoints.plus(
                            (tempBaseRules[i].maxscore ?: 0).minus(
                                    abs(tempBaseRules[i].grade?.toIntOrNull() ?: 0)
                            )
                    )
                }
            }
        }
 
        tempAllRules.forEach {
            it.itemEvaluationVo = refreshItemEvaluation(it)
        }
 
        return totalPoints
    }
 
    /**
     * 计算所有分数
     * @param allRules
     * @param baseRuleItemVo
     * @return
     */
    private fun calculateGrade(
        allRules: List<EvaluationsubruleVo>,
        baseRuleItemVo: EvaluationsubruleVo
    ): List<EvaluationsubruleVo> {
        var allRulesTemp = allRules
 
        val tmpList = getByFatherId(allRulesTemp, baseRuleItemVo.guid?:"")
 
        if (tmpList.size == 0) {//无子项的评分项
            return allRulesTemp
        } else {//其余有子项的评分项
            for (i in tmpList.indices) {
                val tmpVo = tmpList[i]
                allRulesTemp = calculateGrade(allRulesTemp, tmpVo)//递归
 
                for (t in allRulesTemp.indices) {
                    if (allRulesTemp[t].guid == baseRuleItemVo.guid) {
                        if (tmpVo.isSelected) {//选中的评分项的分数才会被计算
                            allRulesTemp[t].isSelected = true
                            if (allRulesTemp[t].grade != null) {
                                var resScore = allRulesTemp[t].grade?.toIntOrNull() ?: 0
                                val subScore = tmpVo.grade?.toIntOrNull() ?: 0
                                resScore = resScore.plus(subScore)
                                allRulesTemp[t].grade = resScore.toString()
                            } else {
                                allRulesTemp[t].grade = tmpVo.grade
                            }
                        }
                    }
                }
 
            }
        }
 
        return allRulesTemp
    }
 
    private fun refreshItemEvaluation(ruleItemVo: EvaluationsubruleVo): Itemevaluation {
 
        val itemEvaluationVo = ruleItemVo.itemEvaluationVo
 
        return itemEvaluationVo?.apply {
            value = ruleItemVo.grade?.toString() ?: "0"
        } ?: Itemevaluation().apply {
            ieguid = UUIDGenerator.generate16ShortUUID()
            esrguid = ruleItemVo.guid
            name = ruleItemVo.itemname
            value = ruleItemVo.grade?.toString() ?: "0"
        }
    }
 
    /**
     * 通过Guid作为fatherId找到所有下级子规范评分项
     * @param ruleItemVos
     * @param fatherId
     * @return
     */
    fun getByFatherId(
        ruleItemVos: List<EvaluationsubruleVo>,
        fatherId: String
    ): ArrayList<EvaluationsubruleVo> {
        val resList = ArrayList<EvaluationsubruleVo>()
 
        for (i in ruleItemVos.indices) {
            if (ruleItemVos[i].fatherid == fatherId) {
                resList.add(ruleItemVos[i])
            }
        }
 
        return resList
    }
}