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
package cn.flightfeather.thirdappmodule.module.task
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.bean.entity.*
import cn.flightfeather.thirdappmodule.bean.vo.BaseSubScene
import cn.flightfeather.thirdappmodule.bean.vo.SceneConstructionSite
import cn.flightfeather.thirdappmodule.bean.vo.SceneDetailStrVo
import cn.flightfeather.thirdappmodule.bean.vo.SceneDetailVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.model.event.SceneEvent
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.CommonRepository
import cn.flightfeather.thirdappmodule.repository.SceneRepository
import com.google.gson.Gson
import okhttp3.ResponseBody
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.toast
 
/**
 * @author riku
 * Date: 2019/8/7
 */
class SceneDetailViewModel : BaseViewModel() {
    private val sceneRepository = SceneRepository()
    private val commonRepository = CommonRepository.instance
 
    val allProvinceList = ArrayList<Province>()
    val allCityList = ArrayList<City>()
    val allDistrictList = ArrayList<District>()
    val allTownList = ArrayList<Town>()
 
    val provinceList = MutableLiveData<ArrayList<Province>>().apply { value = ArrayList() }
    val cityList = ArrayList<City>()
    val districtList = ArrayList<District>()
    val townList = ArrayList<Town>()
 
    val allSceneTypeList = MutableLiveData<ArrayList<Domainitem>>().apply { value = ArrayList() }
 
    val subScene = MutableLiveData<BaseSubScene>()
    val sceneStatus = ArrayList<String>()
 
    /**
     * 获取行政区划可选项
     */
    fun getData() {
        sceneRepository.getDivision(object : ResultCallBack<Division> {
            override fun onSuccess(result: Division?) {
                result?.let {
                    allProvinceList.clear()
                    allCityList.clear()
                    allDistrictList.clear()
                    allTownList.clear()
 
                    allProvinceList.addAll(it.provinceList)
                    allCityList.addAll(it.cityList)
                    allDistrictList.addAll(it.districtList)
                    allTownList.addAll(it.townList)
 
                    provinceList.value?.addAll(allProvinceList)
                    provinceList.value = provinceList.value
                }
            }
 
            override fun onFailure() {
 
            }
 
        })
    }
 
    /**
     * 获取场景类型可选项
     */
    fun getSceneType() {
        sceneRepository.getSceneType(object : ResultCallBack<ArrayList<Domainitem>> {
            override fun onSuccess(result: ArrayList<Domainitem>?) {
                result?.let {
                    allSceneTypeList.value?.apply {
                        clear()
                        addAll(it)
                    }
                    allSceneTypeList.value = allSceneTypeList.value
                }
            }
 
            override fun onFailure() {
 
            }
 
        })
    }
 
    fun putScene(scene: Scense?) {
        scene?.let {
            sceneRepository.putScene(it, object : ResultCallBack<ResponseBody> {
                override fun onSuccess(result: ResponseBody?) {
                    application.toast("场景上传成功")
 
                    EventBus.getDefault().post(SceneEvent(it))
                }
 
                override fun onFailure() {
                    application.toast("场景上传失败")
                }
 
            })
        }
    }
 
    fun updateScene(scene: Scense?) {
        scene?.let {
            sceneRepository.updateScene(it, object : ResultCallBack<ResponseBody> {
                override fun onSuccess(result: ResponseBody?) {
                    application.toast("场景修改成功")
 
                    EventBus.getDefault().post(SceneEvent(it))
                }
 
                override fun onFailure() {
                    application.toast("场景修改失败")
                }
 
            })
        }
    }
 
    fun getSceneDetail(scene: Scense?) {
        scene?.let {
            sceneRepository.getSceneDetail(it, object : ResultCallBack<SceneDetailVo<SceneConstructionSite>> {
                override fun onSuccess(result: SceneDetailVo<SceneConstructionSite>?) {
                    result?.let { r -> subScene.value = r.subScene }
                }
 
                override fun onFailure() {
                    application.toast("施工阶段没有记录")
                }
            })
        }
    }
 
    fun updateSceneDetail(scene: Scense?, subScene: BaseSubScene) {
        scene?.let {
            val vo = SceneDetailStrVo().apply {
                this.scene = it
                this.subScene = Gson().toJson(subScene)
            }
            sceneRepository.updateSceneDetail(vo, object : ResultCallBack<String> {
                override fun onSuccess(result: String?) {
                    application.toast("施工阶段修改成功")
                }
 
                override fun onFailure() {
                    application.toast("施工阶段修改失败")
                }
            })
        }
    }
 
    fun getSceneStatus(success: () -> Unit) {
        commonRepository.getDomainItem("工地工期", object : ResultCallBack<ArrayList<Domainitem>> {
            override fun onSuccess(result: ArrayList<Domainitem>?) {
                result?.let { arr ->
                    sceneStatus.addAll(arr.map { it.text })
                    success()
                }
            }
 
            override fun onFailure() {
                application.toast("施工阶段选项无记录")
            }
        })
    }
}
 
data class Division(
        val provinceList: ArrayList<Province>,
        val cityList: ArrayList<City>,
        val districtList: ArrayList<District>,
        val townList: ArrayList<Town>
)