riku
2025-10-31 1897c4ad5fa73b3f0a36e1aa0e1e9000302a6ace
app/src/main/java/cn/flightfeather/thirdappmodule/databinding/SceneHistoryViewModel.kt
@@ -1,7 +1,9 @@
package cn.flightfeather.thirdappmodule.databinding
import android.arch.lifecycle.MutableLiveData
import android.view.View
import cn.flightfeather.thirdappmodule.bean.vo.DPChangeInfo
import cn.flightfeather.thirdappmodule.bean.vo.DPProblemRecurrence
import cn.flightfeather.thirdappmodule.bean.vo.QueryOptSingle
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
@@ -9,6 +11,7 @@
import cn.flightfeather.thirdappmodule.util.DateUtil
import org.jetbrains.anko.toast
import java.util.*
import kotlin.math.round
/**
 * 场景历史情况
@@ -16,16 +19,40 @@
 * Date: 2025/10/30
 */
class SceneHistoryViewModel : BaseViewModel() {
    companion object {
        const val CHANGE_PER = 0.8
    }
    private val dataProdSingleSceneRepository = DataProdSingleSceneRepository.instance
    // 上个月整改率最差的情况
    var worstChangeInfo = MutableLiveData<DPChangeInfo>()
    fun getChangeInfoList(sceneId: String) {
    var changeInfoTxt = MutableLiveData<String>()
    var changeEfficiencyTxt = MutableLiveData<String>()
    // 问题复发情况
//    var problemRecurrenceList = MutableLiveData<List<DPProblemRecurrence>>()
    var worstProblemRecurrence = MutableLiveData<DPProblemRecurrence>()
    //
    fun fetchChangeInfoList(sceneId: String) {
        val queryOpts = getQueryOptSingleList(sceneId)
        dataProdSingleSceneRepository.getChangeInfoList(queryOpts, object : ResultCallBack<List<DPChangeInfo>> {
            override fun onSuccess(result: List<DPChangeInfo>?) {
                if (!result.isNullOrEmpty()) {
                    changeInfoTxt.value = result.mapIndexed { i, info ->
                        // 筛选存在问题,并且整改率小于80%的月份
                        if (info.subTasks.isNullOrEmpty() && info.changePer >= CHANGE_PER) {
                            ""
                        } else {
                            val time = DateUtil.parseYearMonth(queryOpts[i].startTime) ?: return@mapIndexed ""
                            val cal = Calendar.getInstance().apply { setTime(time) }
                            "${cal.get(Calendar.MONTH) + 1}月整改率为${round(info.changePer * 100)}%"
                        }
                    }.filter { it.isNotBlank() }.joinToString(",") + ",整改率较低,需加强监管。"
                    worstChangeInfo.value = result[0]
                }
            }
@@ -36,6 +63,44 @@
        })
    }
    fun fetchProblemRecurrence(sceneId:String) {
        // 获取前三个月的内的记录
        val queryOpt = QueryOptSingle().apply {
            this.sceneId = sceneId
            val now = Calendar.getInstance().apply {
                set(Calendar.DAY_OF_MONTH, 1)
                set(Calendar.HOUR_OF_DAY, 0)
                set(Calendar.MINUTE, 0)
                set(Calendar.SECOND, 0)
            }
            // 得到上月最后一天
            now.add(Calendar.SECOND, -1)
            endTime = DateUtil.getDateStr(now.time)
            // 得到前4个月第一天
            now.apply {
                set(Calendar.DAY_OF_MONTH, 1)
                set(Calendar.HOUR_OF_DAY, 0)
                set(Calendar.MINUTE, 0)
                set(Calendar.SECOND, 0)
            }
            now.add(Calendar.MONTH, 2)
            startTime = DateUtil.getDateStr(now.time)
        }
        dataProdSingleSceneRepository.getProblemRecurrence(queryOpt, object : ResultCallBack<List<DPProblemRecurrence>> {
            override fun onSuccess(result: List<DPProblemRecurrence>?) {
                if (!result.isNullOrEmpty()) {
                    worstProblemRecurrence.value = result[0]
                }
            }
            override fun onFailure() {
                application.toast("获取问题复发情况失败")
            }
        })
    }
    private fun getQueryOptSingleList(sceneId: String): List<QueryOptSingle> {
        val queryOpts = mutableListOf<QueryOptSingle>()