添加了注释,完善了参数校验及空值处理
This commit is contained in:
@@ -4,6 +4,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.sw.inbound.GlobalData
|
||||
import com.sw.inbound.ext.toSafeBigDecimal
|
||||
import com.sw.inbound.ext.toSafeDouble
|
||||
import com.sw.inbound.ext.toSafeString
|
||||
import com.sw.inbound.model.request.GoodsAddParam
|
||||
import com.sw.inbound.model.request.PurchaseWarehouseParam
|
||||
import com.sw.inbound.model.response.DictType
|
||||
@@ -15,12 +16,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* 自采购
|
||||
*/
|
||||
@HiltViewModel
|
||||
class SelfProcurementViewModel @Inject constructor(
|
||||
private val repository: RemoteRepository
|
||||
@@ -39,6 +42,7 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
private val _globalWarehouse = MutableStateFlow<DictType>(DictType(-1, "选择仓库"))
|
||||
val globalWarehouse: StateFlow<DictType> = _globalWarehouse
|
||||
|
||||
// 采购列表
|
||||
private val _purchaseList = MutableStateFlow<List<PurchaseWarehouseParam>>(emptyList())
|
||||
val purchaseList: StateFlow<List<PurchaseWarehouseParam>> = _purchaseList
|
||||
|
||||
@@ -58,14 +62,6 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
private val _weightInfo = MutableStateFlow<Double?>(0.0)
|
||||
val weightInfo: StateFlow<Double?> = _weightInfo
|
||||
|
||||
|
||||
private val _showCameraPreview = MutableStateFlow(true)
|
||||
val showCameraPreview = _showCameraPreview.asStateFlow()
|
||||
|
||||
fun updateCameraPreviewShow(show: Boolean) {
|
||||
_showCameraPreview.value = show
|
||||
}
|
||||
|
||||
/**
|
||||
* 数量是否是用户输入的值
|
||||
*/
|
||||
@@ -95,6 +91,9 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
|
||||
_selectedItem.value = info.copy(
|
||||
goodsWeight = weight.toSafeBigDecimal(),
|
||||
goodsCountTemp = if (_countUserInput.value) info.goodsCountTemp else count.toSafeString(
|
||||
currentItem.unitName
|
||||
),
|
||||
goodsCount = if (_countUserInput.value) info.goodsCount else finalCount
|
||||
)
|
||||
}
|
||||
@@ -124,6 +123,10 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
|
||||
// 添加订单
|
||||
fun addPurchaseItem(purchaseOrder: PurchaseWarehouseParam) {
|
||||
if (_purchaseList.value.any { it.goodsId == purchaseOrder.goodsId }) {
|
||||
ToastUtils.showToast("添加失败,当前物品已添加")
|
||||
return
|
||||
}
|
||||
_purchaseList.update { currentList ->
|
||||
// 当已经添加过则忽略
|
||||
if (currentList.any { it.goodsId == purchaseOrder.goodsId }) {
|
||||
@@ -181,9 +184,9 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
delay(50)
|
||||
_selectedItem.value = PurchaseWarehouseParam(
|
||||
warehouseId = warehouse.id,
|
||||
goodsId = searchFirst.goodsId!!,
|
||||
goodsId = searchFirst.goodsId ?: 0,
|
||||
goodsName = searchFirst.goodsName,
|
||||
kcUnitId = searchFirst.kcUnitId!!,
|
||||
kcUnitId = searchFirst.kcUnitId ?: 0,
|
||||
unitList = searchFirst.unitVoList,
|
||||
)
|
||||
}
|
||||
@@ -220,6 +223,7 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
return@launchWithLoading
|
||||
}
|
||||
_showAddProductDialog.value = false
|
||||
_goodsAddParam.value = GoodsAddParam()
|
||||
val searchFirst = response.data!!.records?.get(0)
|
||||
searchFirst?.let {
|
||||
updateSelectedItemWithSearch(searchFirst)
|
||||
@@ -229,7 +233,10 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun addToWarehouse() {
|
||||
if (_purchaseList.value.isEmpty()) return
|
||||
if (_purchaseList.value.isEmpty()) {
|
||||
ToastUtils.showToast("采购列表为空")
|
||||
return
|
||||
}
|
||||
launchWithLoading {
|
||||
val response = repository.selfPurchaseWarehousing(_purchaseList.value)
|
||||
if (parseResponse(response)) {
|
||||
|
||||
Reference in New Issue
Block a user