package com.xw.yunwei.ui.task.selectCar
|
|
import android.app.Application
|
import android.view.View
|
import androidx.lifecycle.MutableLiveData
|
import com.xw.common.base.BasePagingViewModel
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.R
|
import com.xw.yunwei.model.AreaModel
|
import com.xw.yunwei.model.CarModel
|
import com.xw.yunwei.net.WorkRepository
|
import org.koin.core.component.inject
|
import kotlin.getValue
|
|
class SelectCarVM(myApplication: Application) : BasePagingViewModel(myApplication) {
|
val workRepository: WorkRepository by inject()
|
|
val search = MutableLiveData("")
|
val dataList: MutableLiveData<List<CarModel>> = MutableLiveData()
|
var selectCar: CarModel? = null
|
|
val filterAreas = mutableListOf<AreaModel>()
|
val filterStatus = listOf(
|
"全部",
|
"运营中",
|
"维修中",
|
"已收车",
|
"车辆异常",
|
)
|
val selectArea = mutableListOf<AreaModel>()
|
val selectStatus = mutableListOf<String>()
|
|
var onClick = View.OnClickListener {
|
when (it.id) {
|
R.id.search -> refreshData()
|
}
|
}
|
|
override fun getDataList(index: Int) {
|
if (index == 1) {
|
refreshLiveData.refresh()
|
} else {
|
refreshLiveData.loading()
|
}
|
launchRequest(showLoading = false, error = { code, msg ->
|
ToastUtilCommon.showError(msg)
|
refreshLiveData.done()
|
}) {
|
val map = mutableMapOf<String, Any?>()
|
map["pageNum"] = index
|
map["pageSize"] = pageSize
|
map["plateNumber"] = search.value // 查询参数
|
map["status"] =
|
if (selectStatus.contains("全部")) null
|
else {
|
if (selectStatus.isNotEmpty()) selectStatus.map { filterStatus.indexOf(it) }.joinToString(",")
|
else null // 运营状态 1运营中 2维修中 3已收车 4车辆异常
|
}
|
map["electronicId"] =
|
if (selectArea.isNotEmpty()) selectArea.map { it.id ?: 0L }.joinToString(",")
|
else null// 围栏id
|
|
val data = workRepository.queryCars(map)
|
val list: MutableList<CarModel> = ArrayList()
|
if (index == 1) {
|
refreshLiveData.first()
|
} else {
|
refreshLiveData.other()
|
list.addAll(dataList.value ?: ArrayList())
|
}
|
for (item in data?.records ?: ArrayList()) {
|
list.add(item)
|
}
|
if ((data?.total ?: 0) <= list.size) {
|
refreshLiveData.done()
|
}
|
dataList.postValue(list)
|
pageNo = index
|
}
|
}
|
|
fun queryAreas(block: () -> Unit) {
|
if (filterAreas.isEmpty()) {
|
launchRequest {
|
workRepository.queryArea()?.let {
|
filterAreas.addAll(it)
|
runOnMainThread { block() }
|
}
|
}
|
} else {
|
block()
|
}
|
}
|
|
}
|