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
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.bean.entity.Inspection
import cn.flightfeather.thirdappmodule.bean.entity.Scense
import cn.flightfeather.thirdappmodule.bean.entity.Subtask
import cn.flightfeather.thirdappmodule.bean.vo.InspectionVo
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.InspectionRepository
import cn.flightfeather.thirdappmodule.repository.SceneRepository
import cn.flightfeather.thirdappmodule.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 {
            //更新界面
            subTask.value = subTask.value
        }
    }
 
    /**
     * 更新子任务
     */
    fun updateSubTask(s: () -> Unit) {
        subTask.value?.let {
            inspectionRepository.updateSubTask(it, object : ResultCallBack<String> {
                override fun onSuccess(result: String?) {
                    result?.let {
                        updateInspection(inspection.value){
                            s()
                        }
                    }
                }
 
                override fun onFailure() {
                    application.toast("提交子任务失败,请检查网络")
                }
 
            })
        }
    }
 
    /**
     * 更新巡查信息
     */
    fun updateInspection(inspection: Inspection?, s: () -> Unit = {}) {
        inspection?.let {
            inspectionRepository.updateInspection(it, object : ResultCallBack<ResponseBody> {
                override fun onSuccess(result: ResponseBody?) {
                    result?.let {r ->
                        val n = r.string().toIntOrNull()
                        if (n == 1) {
                            s()
                        } else {
                            application.toast("提交巡查信息失败,请检查网络")
                        }
                    }
                }
 
                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
            }
        }
    }
}