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
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.bean.entity.*
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.model.event.ProblemEvent
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.ProblemRepository
import okhttp3.ResponseBody
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.toast
import java.io.File
 
/**
 * @author riku
 * Date: 2019/8/1
 */
class MenuEvidenceViewModel : BaseViewModel() {
 
    private val problemRepository = ProblemRepository()
 
    //所有问题可能出现的位置
    val locationList = MutableLiveData<ArrayList<Domainitem>>()
 
    //所有问题的分类集合
    val problemFatherType = MutableLiveData<ArrayList<String>>()
 
    //当前所选问题分类下的详细问题集合
    val problemType = MutableLiveData<ArrayList<Problemtype>>()
 
    val suggestionList = MutableLiveData<ArrayList<String>>()
 
    val problemMap = HashMap<String, ArrayList<Problemtype>>()
 
    val mediaFileList = MutableLiveData<List<Mediafile>>()
 
    //所有数据加载完成通知
    val loadingOver = MutableLiveData<Boolean>()
    //记录各个数据加载是否完成的状态
    private val loadingStatus = BooleanArray(2)
 
    /**
     * 获取场景问题可选位置(目前只有工地,但所有场景都使用)
     */
    fun getLocationList() {
        problemRepository.getLocationList(object : ResultCallBack<ArrayList<Domainitem>> {
            override fun onSuccess(result: ArrayList<Domainitem>?) {
                result?.let {
                    locationList.value = it
                    onLoaded(0)
                }
            }
 
            override fun onFailure() {
 
            }
 
        })
    }
 
    /**
     * 获取对应场景下所有问题类型
     */
    fun getProblemType(taskTypeId: Byte, cityCode: String, districtCode: String, sceneTypeId: Byte) {
        problemRepository.getProblemType(taskTypeId, cityCode, districtCode, sceneTypeId, object : ResultCallBack<ArrayList<Problemtype>> {
            override fun onSuccess(result: ArrayList<Problemtype>?) {
                result?.let {
                    if (it.isEmpty()) {
                        it.add(Problemtype().apply {
                            guid = "0"
                            typename = "其他"
                            name = "其他"
                        })
                    }
 
                    val typeList = ArrayList<String>()
 
                    it.forEach {p ->
                        if (problemMap.containsKey(p.typename)) {
                            problemMap[p.typename]?.add(p)
                        } else {
                            typeList.add(p.typename)
                            problemMap[p.typename] = ArrayList<Problemtype>().apply { add(p) }
                        }
                    }
 
                    problemFatherType.value = typeList
 
                    onLoaded(1)
                }
            }
 
            override fun onFailure() {
            }
 
        })
    }
 
    /**
     * 获取问题对应的整改建议
     */
    fun getSuggestionList(ptGuid: String) {
        problemRepository.getSuggestion(ptGuid, object : ResultCallBack<ArrayList<ChangeAdvice>> {
            override fun onSuccess(result: ArrayList<ChangeAdvice>?) {
                result?.let {
                    val advices = ArrayList<String>()
                    it.forEach {c ->
                        advices.add(c.adName)
                    }
                    advices.add("暂无建议")
                    suggestionList.value = advices
 
                }
            }
 
            override fun onFailure() {
            }
 
        })
    }
 
    /**
     * 根据问题找到服务器和本地的所有图片
     */
    fun getMediaFileList(p: ProblemlistVo) {
        problemRepository.getMediaFileLocal(p.guid, object : ResultCallBack<List<Mediafile>> {
            override fun onSuccess(result: List<Mediafile>?) {
                result?.let {
                    p.mediafileList.addAll(it)
                    mediaFileList.value = p.mediafileList
                }
            }
 
            override fun onFailure() {
            }
        })
    }
 
    /**
     * 下载问题图片
     *  fixme: 2020/8/6 目前由于原程序设置图片的方式为手动下载图片,因此沿用,之后统一用Glide等第三方库代替
     */
    fun downLoadMediaFile(mediaFile: Mediafile, s: (file: File) -> Unit) {
        problemRepository.downloadMediaFile(mediaFile, object : ResultCallBack<File> {
            override fun onSuccess(result: File?) {
                result?.let {
                    s(it)
                }
            }
 
            override fun onFailure() {
 
            }
        })
    }
 
    /**
     * 删除问题图片
     */
    fun deleteMediaFile(mediaFile: List<Mediafile>) {
        mediaFile.forEach {
            problemRepository.deleteMediaFile(it, object : ResultCallBack<Boolean> {
                override fun onSuccess(result: Boolean?) {
 
                }
 
                override fun onFailure() {
                }
            })
        }
    }
 
    /**
     * 新增一个问题
     */
    fun putProblem(problem: Problemlist) {
        problemRepository.putOneProblemList(problem, object : ResultCallBack<ResponseBody> {
            override fun onSuccess(result: ResponseBody?) {
                result?.let {
                    application.toast("提交成功")
 
                    EventBus.getDefault().post(ProblemEvent(problem))
                }
            }
 
            override fun onFailure() {
                application.toast("提交失败")
            }
 
        })
    }
 
    /**
     * 更新一个问题
     */
    fun updateProblem(problem: ProblemlistVo) {
        problemRepository.updateProblem(problem, object : ResultCallBack<ResponseBody> {
            override fun onSuccess(result: ResponseBody?) {
                result?.let {
                    application.toast("修改成功")
                    EventBus.getDefault().post(ProblemEvent(problem.voToEntity()))
                }
            }
 
            override fun onFailure() {
                application.toast("修改失败,请检查网络")
            }
        })
    }
 
    /**
     * 新增本地多媒体文件记录
     */
    fun putMediaFile(mediaFile: Mediafile) {
        problemRepository.putMediaFileLocal(mediaFile, object : ResultCallBack<Int> {
            override fun onSuccess(result: Int?) {
 
            }
 
            override fun onFailure() {
 
            }
 
        })
    }
 
    /**
     * 下拉框选择一个问题类型时,需展示对应类别的具体问题
     */
    fun refreshProblems(problemFatherType: String?) {
        problemMap[problemFatherType]?.let {
            problemType.value = it
        }
    }
 
    /**
     * 判断是否所有的数据都加载完成
     */
    fun onLoaded(i: Int) {
        if (i < loadingStatus.size) {
            loadingStatus[i] = true
        }
        var isLoadOver = true
        loadingStatus.forEach {
            isLoadOver = isLoadOver.and(it)
        }
        if (isLoadOver) {
            loadingOver.value = isLoadOver
            for (y in loadingStatus.indices) {
                loadingStatus[y] = false
            }
        }
    }
}