package com.xw.yunwei.ui.work.car.detail.operations.repair
|
|
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.BindUtils.formatDate
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.R
|
import com.xw.yunwei.model.CarModel
|
import com.xw.yunwei.model.ChargeRecordModel
|
import com.xw.yunwei.model.RepairRecordModel
|
import com.xw.yunwei.net.CommonRepository
|
import com.xw.yunwei.net.WorkRepository
|
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MultipartBody
|
import okhttp3.RequestBody.Companion.asRequestBody
|
import org.koin.core.component.inject
|
import java.io.File
|
import java.time.LocalDate
|
import java.time.format.DateTimeFormatter
|
import kotlin.getValue
|
|
class RepairCarVM(application: Application) : BaseViewModel(application) {
|
val commonRepository: CommonRepository by inject()
|
val workRepository: WorkRepository by inject()
|
|
var taskId = -1L
|
var carId = 0L
|
val carInfo: MutableLiveData<CarModel> = MutableLiveData()
|
val repairRecord: MutableLiveData<RepairRecordModel> = MutableLiveData()
|
|
val carStatus = repairRecord.map {
|
it.status ?: 1
|
} // 车辆状态 1:维修 2:维修取回
|
val startTime = MutableLiveData("") // 开始时间
|
val endTime = MutableLiveData("") // 结束时间
|
val reason = MutableLiveData("") // 原因
|
val repairFees = MutableLiveData("") // 维修费用
|
|
val repairTime = repairRecord.map {
|
formatDate(it.xpVehicleRepair?.startTime ?: ""
|
) + " ~ " + formatDate(it.xpVehicleRepair?.endTime ?: "")
|
}
|
|
val adapters = mutableListOf<ImageSelectAdapter>()
|
val imageEvents = mutableListOf<ImageEventListener<FileSelectBean>>()
|
val maxSizes = listOf(6, 6) // 先默认每个都是最大9张
|
var operateIndex = -1
|
|
|
val onClick = View.OnClickListener {
|
when (it.id) {
|
R.id.submitSend -> submitSend()
|
R.id.submitGet -> submitGet()
|
}
|
}
|
|
fun queryInfo() {
|
launchRequest {
|
workRepository.queryCarInfo(carId)?.let {
|
carInfo.postValue(it)
|
}
|
}
|
}
|
|
fun queryStatus() {
|
launchRequest {
|
workRepository.queryRepairStatus(carId)?.let {
|
repairRecord.postValue(it)
|
}
|
}
|
}
|
|
private fun submitSend() {
|
if (startTime.value.isNullOrEmpty() || endTime.value.isNullOrEmpty()) {
|
ToastUtilCommon.showError("请选择预计维修停运时间")
|
return
|
}
|
if (reason.value.isNullOrEmpty()) {
|
ToastUtilCommon.showError("请填写本次维修原因")
|
return
|
}
|
if (adapters[0].data.isEmpty()) {
|
ToastUtilCommon.showError("请上传维修前照片")
|
return
|
}
|
val files = mutableListOf<FileSelectBean>()
|
files.addAll(adapters[0].data)
|
|
showDialog("上传中...")
|
uploadFile(files) {
|
dismissDialog()
|
launchRequest("操作中..") {
|
val map = mapOf(
|
"startTime" to startTime.value,
|
"endTime" to endTime.value,
|
"repairReason" to reason.value,
|
"startRepairPicture" to "${files.map { it.url }}",
|
"vehicleId" to carId,
|
).toMutableMap()
|
if (taskId != -1L) {
|
map["taskId"] = taskId
|
}
|
workRepository.saveRepair(map)
|
ToastUtilCommon.showSuc("送修成功")
|
// if (taskId != -1L) {
|
finish()
|
// } else {
|
// runOnMainThread {
|
// adapters[0].setList(ArrayList())
|
// }
|
// queryStatus()
|
// }
|
}
|
}
|
}
|
|
private fun submitGet() {
|
if (repairFees.value.isNullOrEmpty()) {
|
ToastUtilCommon.showError("请输入维修总费用")
|
return
|
}
|
if (adapters[1].data.isEmpty()) {
|
ToastUtilCommon.showError("请上传维修费用明细单")
|
return
|
}
|
val files = mutableListOf<FileSelectBean>()
|
files.addAll(adapters[1].data)
|
|
showDialog("上传中...")
|
uploadFile(files) {
|
dismissDialog()
|
launchRequest("操作中..") {
|
val map = mapOf(
|
"repairMoney" to repairFees.value,
|
"repairMoneyDetail" to GsonUtils.toJson(files.map { it.url }),
|
"id" to repairRecord.value?.xpVehicleRepair?.id,
|
).toMutableMap()
|
if (taskId != -1L) {
|
map["taskId"] = taskId
|
}
|
workRepository.saveReturnRepair(map)
|
ToastUtilCommon.showSuc("取回成功")
|
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)
|
}
|
}
|
}
|
|
}
|