添加了注释,完善了参数校验及空值处理

This commit is contained in:
zxj
2025-07-09 15:29:52 +08:00
parent de80a26c2c
commit 6cf7b4fb9b
42 changed files with 584 additions and 334 deletions
@@ -121,31 +121,4 @@ abstract class BaseViewModel(
}
}
}
fun getStoreList(): ArrayList<String> {
return arrayListOf(
"默认仓库",
"仓库1",
"仓库2",
"仓库3",
)
}
fun getPurchasingUnit(): ArrayList<String> {
return arrayListOf(
"",
"",
""
)
}
fun getProductList(): ArrayList<String> {
return arrayListOf<String>(
"胶东大白菜",
"玉田尖白菜1",
"玉田尖白菜2",
"玉田尖白菜3",
"玉田尖白菜4"
)
}
}
@@ -7,11 +7,17 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject
/**
* 采购订单
*/
@HiltViewModel
class ProductViewModel @Inject constructor(
class PurchaseOrderViewModel @Inject constructor(
private val repository: RemoteRepository
) : BaseViewModel(repository) {
/**
* 采购订单-供应商列表
*/
private val _supplierList = MutableStateFlow<List<SupplierInfo?>>(emptyList())
val supplierList: StateFlow<List<SupplierInfo?>> = _supplierList
@@ -3,6 +3,7 @@ package com.sw.inbound.viewmodel
import androidx.lifecycle.viewModelScope
import com.sw.inbound.ext.toSafeBigDecimal
import com.sw.inbound.ext.toSafeFloat
import com.sw.inbound.ext.toSafeString
import com.sw.inbound.model.request.UploadInfo
import com.sw.inbound.model.response.DictType
import com.sw.inbound.model.response.GoodsInfo
@@ -22,6 +23,9 @@ import kotlinx.coroutines.flow.update
import timber.log.Timber
import javax.inject.Inject
/**
* 收货
*/
@HiltViewModel
class ReceiptViewModel @Inject constructor(
private val repository: RemoteRepository
@@ -37,16 +41,6 @@ class ReceiptViewModel @Inject constructor(
private val _currentPurchaseInfo = MutableStateFlow<PurchaseInfo?>(null)
val currentPurchaseInfo: StateFlow<PurchaseInfo?> = _currentPurchaseInfo
// 自动从 currentPurchaseInfo 派生 orders
// private val _orders: StateFlow<List<GoodsInfo>> = currentPurchaseInfo
// .map { purchaseInfo ->
// purchaseInfo?.receiveGoodsInfoList ?: emptyList()
// }
// .stateIn(
// viewModelScope,
// SharingStarted.WhileSubscribed(5000), // 或者使用 Lazily/Eagerly 根据需求
// emptyList()
// )
private val _orders = MutableStateFlow<List<GoodsInfo>>(emptyList())
val orders: StateFlow<List<GoodsInfo>> = _orders.asStateFlow()
@@ -64,9 +58,7 @@ class ReceiptViewModel @Inject constructor(
.map { orders -> orders.filter { !it.isAdjusted && it.goodId != null } }
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
val mergedOrders: StateFlow<List<GoodsInfo>> = _orders
//
// 选中的商品
private val _selectedItem = MutableStateFlow<GoodsInfo?>(null)
val selectedItem: StateFlow<GoodsInfo?> = _selectedItem
@@ -74,9 +66,11 @@ class ReceiptViewModel @Inject constructor(
private val _searchListItems = MutableStateFlow<List<SearchGoodsInfo.Record>>(emptyList())
val searchListItems: StateFlow<List<SearchGoodsInfo.Record>> = _searchListItems
// 收货请求结果
private val _receiptResult = MutableStateFlow<Boolean>(false)
val receiptResult: StateFlow<Boolean> = _receiptResult
// 物品数量 是否由用户输入
private val _countUserInput = MutableStateFlow<Boolean>(false)
/**
@@ -87,10 +81,21 @@ class ReceiptViewModel @Inject constructor(
_adjustState.value = isAdjustState
}
fun StateFlow<List<GoodsInfo>>.getValidOrders(): List<GoodsInfo> {
return this.value.filter { it.goodId != null }
}
/**
* 更新收货提示弹窗
*/
fun updateReceiptDialog(showDialog: Boolean) {
if (_orders.getValidOrders().isEmpty()) {
ToastUtils.showToast("订单为空或包含异常数据")
return
}
if (!unadjustedOrders.value.isEmpty()) {
ToastUtils.showToast("请先调整物品信息")
return
}
_showReceiptDialog.value = showDialog
}
@@ -151,12 +156,12 @@ class ReceiptViewModel @Inject constructor(
/**
* 更新所有商品仓库
*/
fun updateAllPurchaseStore(store: DictType, predicate: (GoodsInfo) -> Boolean = { true }) {
fun updateAllPurchaseStore(warehouse: DictType, predicate: (GoodsInfo) -> Boolean = { true }) {
_orders.update { currentList ->
currentList.map { order ->
if (predicate(order)) order.copy(
warehouseId = store.id,
warehouseName = store.value
warehouseId = warehouse.id,
warehouseName = warehouse.value
) else order
}
}
@@ -192,6 +197,9 @@ class ReceiptViewModel @Inject constructor(
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
_selectedItem.value = info.copy(
goodsWeight = weight.toSafeBigDecimal(),
receivedNumTemp = if (_countUserInput.value) info.receivedNumTemp else count.toSafeString(
currentItem.unitName
),
receivedNum = if (_countUserInput.value) info.receivedNum else finalCount
)
}
@@ -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)) {