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