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()
|
}
|
}
|
}
|