18034727885
2026-06-21 f32d25d2a605df16abe0e53a1b43b74649bda668
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
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车辆异常;选择"全部"或不选时不传
            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()
        }
    }
 
}