package cn.flightfeather.thirdapp.module.inspection
|
|
import android.arch.lifecycle.MutableLiveData
|
import cn.flightfeather.thirdapp.bean.Inspection
|
import cn.flightfeather.thirdapp.bean.Scense
|
import cn.flightfeather.thirdapp.bean.Subtask
|
import cn.flightfeather.thirdapp.bean.vo.InspectionVo
|
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo
|
import cn.flightfeather.thirdapp.common.net.ResultCallBack
|
import cn.flightfeather.thirdapp.module.base.BaseViewModel
|
import cn.flightfeather.thirdapp.repository.InspectionRepository
|
import cn.flightfeather.thirdapp.repository.SceneRepository
|
import cn.flightfeather.thirdapp.util.Domain
|
import okhttp3.ResponseBody
|
import org.jetbrains.anko.toast
|
import java.util.*
|
import kotlin.collections.ArrayList
|
|
/**
|
* @author riku
|
* Date: 2019/7/31
|
*/
|
class InspectionDetailViewModel : BaseViewModel() {
|
|
private val inspectionRepository = InspectionRepository()
|
private val sceneRepository = SceneRepository()
|
|
var inspection = MutableLiveData<Inspection>()
|
|
var subTask = MutableLiveData<Subtask>()
|
|
var scene = MutableLiveData<Scense>()
|
|
//已有问题列表
|
var problemList = MutableLiveData<ArrayList<ProblemlistVo>>()
|
|
//新问题
|
var newProblem = MutableLiveData<ProblemlistVo>()
|
|
/**
|
* 获取巡查信息
|
*/
|
fun getInspectionData(subTaskId: String?) {
|
subTaskId?.let {
|
inspectionRepository.getInspectionData(subTaskId, object : ResultCallBack<InspectionVo> {
|
override fun onSuccess(result: InspectionVo?) {
|
result?.let {
|
inspection.value = it.transToInspection()
|
getProblems(it.guid)
|
}
|
}
|
|
override fun onFailure() {
|
application.toast("获取巡查信息失败,请检查网络")
|
}
|
|
})
|
}
|
}
|
|
/**
|
* 获取问题列表
|
*/
|
fun getProblems(inspectionId: String) {
|
inspectionRepository.getProblemList(inspectionId, object : ResultCallBack<List<ProblemlistVo>> {
|
override fun onSuccess(result: List<ProblemlistVo>?) {
|
result?.let {
|
val list = ArrayList<ProblemlistVo>().apply { addAll(it) }
|
problemList.value = list
|
}
|
}
|
|
override fun onFailure() {
|
application.toast("获取问题失败,请检查网络")
|
}
|
|
})
|
}
|
|
/**
|
* 获取场景信息
|
*/
|
fun getSceneInfo(sceneId: String?) {
|
sceneId?.let {
|
sceneRepository.getScene(sceneId, object : ResultCallBack<Scense> {
|
override fun onSuccess(result: Scense?) {
|
result?.let {
|
scene.value = it
|
}
|
}
|
|
override fun onFailure() {
|
}
|
|
})
|
}
|
}
|
|
/**
|
* 开始或结束巡查任务
|
*/
|
fun startOrEndTask(status: String?) {
|
when (status) {
|
Domain.TASK_STATUS_WAITING -> {
|
subTask.value?.apply {
|
this.status = getNextStatus(status)
|
executionstarttime = Date()
|
}
|
inspection.value?.executionstarttime = Date()
|
}
|
Domain.TASK_STATUS_RUNNING -> {
|
subTask.value?.apply {
|
this.status = getNextStatus(status)
|
executionendtime = Date()
|
}
|
inspection.value?.executionendtime = Date()
|
}
|
}
|
|
updateSubTask()
|
updateInspection(inspection.value)
|
|
//更新界面
|
subTask.value = subTask.value
|
}
|
|
/**
|
* 更新子任务
|
*/
|
fun updateSubTask() {
|
subTask.value?.let {
|
inspectionRepository.updateSubTask(it, object : ResultCallBack<String> {
|
override fun onSuccess(result: String?) {
|
|
}
|
|
override fun onFailure() {
|
application.toast("提交子任务失败,请检查网络")
|
}
|
|
})
|
}
|
}
|
|
/**
|
* 更新巡查信息
|
*/
|
fun updateInspection(inspection: Inspection?) {
|
inspection?.let {
|
inspectionRepository.updateInspection(it, object : ResultCallBack<ResponseBody> {
|
override fun onSuccess(result: ResponseBody?) {
|
|
}
|
|
override fun onFailure() {
|
application.toast("提交巡查信息失败,请检查网络")
|
}
|
|
})
|
}
|
}
|
|
/**
|
* 更新场景信息
|
*/
|
fun updateScene(scene: Scense?) {
|
scene?.let {
|
inspectionRepository.updateScene(it, object : ResultCallBack<ResponseBody> {
|
override fun onSuccess(result: ResponseBody?) {
|
|
}
|
|
override fun onFailure() {
|
application.toast("更新场景信息失败,请检查网络")
|
}
|
})
|
}
|
}
|
|
private fun getNextStatus(status: String): String {
|
return when (status) {
|
Domain.TASK_STATUS_WAITING -> {
|
Domain.TASK_STATUS_RUNNING
|
}
|
Domain.TASK_STATUS_RUNNING -> {
|
Domain.TASK_STATUS_FINISHED
|
}
|
Domain.TASK_STATUS_FINISHED -> {
|
Domain.TASK_STATUS_FINISHED
|
}
|
else -> {
|
status
|
}
|
}
|
}
|
}
|