18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
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
package com.xw.yunwei.ui.task.create
 
import android.app.Application
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.amap.api.location.AMapLocation
import com.blankj.utilcode.util.GsonUtils
import com.xw.common.base.BaseViewModel
import com.xw.common.base.FileSelectBean
import com.xw.common.base.ImageEventListener
import com.xw.common.base.ImageSelectAdapter
import com.xw.common.utils.MMKVManager
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.R
import com.xw.yunwei.model.CacheAccidentModel
import com.xw.yunwei.model.CarModel
import com.xw.yunwei.model.Category
import com.xw.yunwei.model.FinishPage
import com.xw.yunwei.model.Item
import com.xw.yunwei.model.SwitchTabMain
import com.xw.yunwei.model.SwitchTabTask
import com.xw.yunwei.net.CommonRepository
import com.xw.yunwei.net.TaskRepository
import com.xw.yunwei.net.WorkRepository
import com.xw.yunwei.ui.MainActivity
import com.xw.yunwei.ui.task.selectCar.SelectCarActivity
import kotlinx.coroutines.delay
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import org.greenrobot.eventbus.EventBus
import org.koin.core.component.inject
import java.io.File
import kotlin.collections.joinToString
import kotlin.collections.map
import kotlin.getValue
 
class CreateTaskVM(myApplication: Application) : BaseViewModel(myApplication) {
 
    val workRepository: WorkRepository by inject()
    val taskRepository: TaskRepository by inject()
    val commonRepository: CommonRepository by inject()
 
    var categories = mutableListOf<Category>()
 
    var selectCar: MutableLiveData<CarModel> = MutableLiveData()
    var location: MutableLiveData<AMapLocation?> = MutableLiveData(null)
    val address = location.map {
        it?.address ?: "定位中..."
    }
    // 0 = P0(最紧急), 1 = P1 , 2 = P2 , 3 = P3(最不紧急)
    val taskLevel: MutableLiveData<Int> = MutableLiveData(3)
    val taskDesc = MutableLiveData("")
 
    var onClick = View.OnClickListener {
        when (it.id) {
            R.id.imgSelectCar -> {
//                startActivityForResult(
//                    SelectCarActivity::class.java,
//                    110,
//                    bundleOf("car" to selectCar.value)
//                )
                onBackPressed()
            }
 
            R.id.level0 -> taskLevel.value = 0
            R.id.level1 -> taskLevel.value = 1
            R.id.level2 -> taskLevel.value = 2
            R.id.level3 -> taskLevel.value = 3
            R.id.submit -> submit()
        }
    }
 
    lateinit var adapter: ImageSelectAdapter
    val maxSize = 6
 
    fun submit() {
        if (selectCar.value == null) {
            ToastUtilCommon.showWarn("请选择车辆")
            return
        }
        if (location.value == null) {
            ToastUtilCommon.showWarn("未获取到定位")
            return
        }
        if (getSelectType().isEmpty()) {
            ToastUtilCommon.showWarn("请选择事故类型")
            return
        }
        if (taskLevel.value !in arrayOf(0, 1, 2, 3)) {
            ToastUtilCommon.showWarn("请选择任务等级")
            return
        }
        if (taskDesc.value.isNullOrEmpty()) {
            ToastUtilCommon.showWarn("请填写任务描述")
            return
        }
        if (adapter.data.isEmpty()) {
            ToastUtilCommon.showWarn("请上传相关照片")
            return
        }
 
        showDialog("上传中...")
        uploadFile(adapter.data) {
            dismissDialog()
            launchRequest("提交中") {
                val map = mutableMapOf<String, Any?>()
                map["id"] = selectCar.value?.id
                map["taskDescription"] = taskDesc.value
                map["taskImage"] = "${adapter.data.map { it.url }}"
                map["taskTypeId"] = getSelectType()
                map["taskUrgencyLevel"] = taskLevel.value
                taskRepository.saveVehicleAccident(map)
                ToastUtilCommon.showSuc("提交成功")
                EventBus.getDefault().post(SwitchTabMain(1))
                EventBus.getDefault().post(SwitchTabTask(0))
//                delay(1000)
                EventBus.getDefault().post(FinishPage())
                finish()
            }
        }
 
    }
 
    private fun uploadFile(files: List<FileSelectBean>, succ: () -> Unit) {
        val result = files.find { it.url.isEmpty() }
        if (result == null) {
            succ()
        } else {
            launchRequest(showLoading = false, error = { _, errMsg ->
                ToastUtilCommon.showError(errMsg)
                dismissDialog()
            }) {
                val file = File(result.localPath)
                val fileRequestBody = file.asRequestBody("image/jpg".toMediaType())
                val mBuilder = MultipartBody.Builder()
                mBuilder.addFormDataPart("file", file.name, fileRequestBody)
                mBuilder.setType(MultipartBody.FORM)
                val data = commonRepository.uploadFile(mBuilder.build())
                result.url = GsonUtils.toJson(data)
                uploadFile(files, succ)
            }
        }
    }
 
    fun getTypeList(block: () -> Unit) {
        if (categories.isNotEmpty()) {
            block()
        } else {
            launchRequest {
                taskRepository.getTypeList()?.forEachIndexed { index, type ->
                    val items = mutableListOf<Item>()
                    type.child?.forEach {
                        items.add(Item(it.id ?: 0L, it.errorName ?: ""))
                    }
                    categories.add(Category(index, type.errorName ?: "", items))
                    runOnMainThread { block() }
                }
            }
        }
    }
 
    fun getSelectType(): String {
        val items = mutableListOf<Item>()
        categories.forEach { items.addAll(it.items) }
        return items.filter { it.isSelected }.map { it.id }.joinToString()
    }
 
    /*获取数据填写情况*/
    fun getDataWrite(): CacheAccidentModel? {
        // 事故类型/任务等级/任务描述/相关照片 暂时定义为这几个填了就算填了事故
        return if (getSelectType().isEmpty() && taskLevel.value !in arrayOf(0, 1, 2, 3)
            && taskDesc.value.isNullOrEmpty() && adapter.data.isEmpty()
        ) {
            null
        } else {
            CacheAccidentModel(
                selectCar.value,
                location.value,
                categories,
                taskLevel.value!!,
                taskDesc.value!!,
                adapter.data
            )
        }
    }
}