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
package com.xw.yunwei.ui.task.accident.handle
 
import android.app.Application
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
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.ToastUtilCommon
import com.xw.yunwei.R
import com.xw.yunwei.net.CommonRepository
import com.xw.yunwei.net.TaskRepository
import com.xw.yunwei.ui.MainActivity
import kotlinx.coroutines.delay
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import org.koin.core.component.inject
import java.io.File
import kotlin.getValue
 
 
class AccidentProcessVM(myApplication: Application) : BaseViewModel(myApplication) {
    val commonRepository: CommonRepository by inject()
    val taskRepository: TaskRepository by inject()
 
    var taskId = 0L
    val death = MutableLiveData("0") // 死
    val badlyHurt = MutableLiveData("0") // 重伤
    val minorInjuries = MutableLiveData("0") // 轻伤
    val siteDesc = MutableLiveData("") // 现场情况
    val disposalDesc = MutableLiveData("") // 处置情况
    val siteNums = siteDesc.map { "${it.length}/200" }
    val disposalNums = disposalDesc.map { "${it.length}/200" }
 
    val dataZrpd = listOf("我方主责", "我方无责", "双方同责")
    var selectZrpd = ""
 
    val adapters = mutableListOf<ImageSelectAdapter>()
    val imageEvents = mutableListOf<ImageEventListener<FileSelectBean>>()
    val maxSizes = listOf(8, 8, 8)
    var operateIndex = -1
 
 
    var onClick = View.OnClickListener {
        when (it.id) {
            R.id.cut1 -> death.cut()
            R.id.cut2 -> badlyHurt.cut()
            R.id.cut3 -> minorInjuries.cut()
            R.id.add1 -> death.add()
            R.id.add2 -> badlyHurt.add()
            R.id.add3 -> minorInjuries.add()
            R.id.tvControl -> submit()
        }
    }
 
    private fun submit() {
        if (selectZrpd.isEmpty()) {
            ToastUtilCommon.showWarn("请选择责任判断")
            return
        }
        if(adapters[0].data.isEmpty()){
            ToastUtilCommon.showWarn("请上传交警判责照片")
            return
        }
        if (siteDesc.value.isNullOrEmpty() && adapters[1].data.isEmpty()) {
            ToastUtilCommon.showWarn("请填写现场情况或上传现场情况照片")
            return
        }
        if (disposalDesc.value.isNullOrEmpty() && adapters[2].data.isEmpty()) {
            ToastUtilCommon.showWarn("请填写处置情况或上传处置情况照片")
            return
        }
        val files = mutableListOf<FileSelectBean>()
        adapters.forEach {
            files.addAll(it.data)
        }
        showDialog("上传中...")
        uploadFile(files) {
            dismissDialog()
            launchRequest("提交中") {
                val map = mutableMapOf<String, Any?>()
                map["id"] = taskId
                map["deathCount"] = death.value
                map["severeCount"] = badlyHurt.value
                map["minorCount"] = minorInjuries.value
                map["situationDescription"] = siteDesc.value
                map["disposalDescription"] = disposalDesc.value
                map["responsibilityJudgment"] = dataZrpd.indexOf(selectZrpd) + 1
                map["responsibleImage"] = "${adapters[0].data.map { it.url }}"
                map["situationImage"] = "${adapters[1].data.map { it.url }}"
                map["disposalImage"] = "${adapters[2].data.map { it.url }}"
                taskRepository.handleVehicleAccident(map)
                ToastUtilCommon.showSuc("提交成功")
 
                startActivity(MainActivity::class.java)
            }
        }
 
    }
 
    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 MutableLiveData<String>.cut() {
        val current = value?.toInt() ?: 0
        if (current <= 0) {
            ToastUtilCommon.showWarn("不能再减了")
        } else {
            value = (current - 1).toString()
        }
    }
 
    fun MutableLiveData<String>.add() {
        val current = value?.toInt() ?: 0
        if (current >= 99) {
            ToastUtilCommon.showWarn("不能再加了")
        } else {
            value = (current + 1).toString()
        }
    }
}