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()
|
}
|
}
|
|
//是否已经筛选
|
fun hadFilter(): Boolean {
|
return selectArea.isNotEmpty() || selectStatus.count { it != filterStatus[0] } > 0
|
}
|
|
/**
|
* 获取车辆列表数据
|
*
|
* 根据当前搜索词、选中区域和运营状态组装请求参数,支持分页加载。
|
*
|
* @param index 页码,1 表示刷新,大于 1 表示加载更多
|
*/
|
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 // 查询参数
|
// 1=运营中,2=出车中,3=接单中,4=停止接单,5=已收车,6=车辆异常,7=维修中,8=充电中
|
map["status"] =
|
if (selectStatus.contains("全部")) null
|
else {
|
if (selectStatus.isNotEmpty()) selectStatus.map { filterStatus.indexOf(it) }.joinToString(",")
|
else null
|
}
|
// 围栏 id:多个区域用逗号拼接
|
map["electronicId"] =
|
if (selectArea.isNotEmpty()) selectArea.map { it.id ?: 0L }.joinToString(",")
|
else null
|
|
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
|
}
|
}
|
|
/**
|
* 查询区域列表
|
*
|
* 若已缓存则直接回调,否则请求接口并缓存结果。
|
*
|
* @param block 数据准备好后的回调,用于展示筛选弹窗
|
*/
|
fun queryAreas(block: () -> Unit) {
|
if (filterAreas.isEmpty()) {
|
launchRequest {
|
workRepository.queryArea()?.let {
|
filterAreas.addAll(it)
|
runOnMainThread { block() }
|
}
|
}
|
} else {
|
block()
|
}
|
}
|
|
}
|