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
|
}
|
}
|