This commit is contained in:
zxj
2025-07-08 15:40:39 +08:00
parent d2698ed60f
commit 4501b6d5fc
132 changed files with 8998 additions and 0 deletions
@@ -0,0 +1,237 @@
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.model.request.UploadInfo
import com.sw.inbound.model.response.DictType
import com.sw.inbound.model.response.GoodsInfo
import com.sw.inbound.model.response.PurchaseInfo
import com.sw.inbound.model.response.SearchGoodsInfo
import com.sw.inbound.repository.RemoteRepository
import com.sw.inbound.sdk.SensorScaleUtils
import com.sw.inbound.utils.ToastUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import timber.log.Timber
import javax.inject.Inject
@HiltViewModel
class ReceiptViewModel @Inject constructor(
private val repository: RemoteRepository
) : BaseViewModel(repository) {
// 调整状态
private val _adjustState = MutableStateFlow<Boolean>(true)
val adjustState: StateFlow<Boolean> = _adjustState
private val _showReceiptDialog = MutableStateFlow(false)
val showReceiptDialog: StateFlow<Boolean> = _showReceiptDialog
// 当前要收货的供应商
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()
fun initOrders() {
_orders.value = _currentPurchaseInfo.value?.receiveGoodsInfoList ?: emptyList()
}
// 已调整列表
val adjustedOrders: StateFlow<List<GoodsInfo>> = _orders
.map { orders -> orders.filter { it.isAdjusted && it.goodId != null } }
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
// 未调整列表
val unadjustedOrders: StateFlow<List<GoodsInfo>> = _orders
.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
// 搜索物品列表
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)
/**
* 更新调整状态
* @param isAdjustState true 已调整 false 未调整
*/
fun updateAdjustState(isAdjustState: Boolean) {
_adjustState.value = isAdjustState
}
/**
* 更新收货提示弹窗
*/
fun updateReceiptDialog(showDialog: Boolean) {
_showReceiptDialog.value = showDialog
}
/**
* 更新选中的item
*/
fun updateSelectedItem(purchaseOrder: GoodsInfo?) {
_selectedItem.value = purchaseOrder
}
fun getReceiveDetail(id: Int) {
launchWithLoading {
val response = repository.getReceiveDetail(id)
if (response.isSuccess()) {
_currentPurchaseInfo.value = response.data
initOrders()
}
}
}
/**
* 更新当前供应商信息
*/
fun updateCurrentPurchaseInfo(purchaseInfo: PurchaseInfo?) {
_currentPurchaseInfo.value = purchaseInfo
}
// 添加订单
fun addPurchaseItem(purchaseOrder: GoodsInfo) {
_orders.update { currentList ->
// 当已经添加过则忽略
if (currentList.any { it.goodId == purchaseOrder.goodId }) {
currentList
} else {
currentList + purchaseOrder
}
}
}
// 更新订单
fun updatePurchaseItem(purchaseOrder: GoodsInfo) {
_orders.update { currentList ->
currentList.map { order ->
if (order.goodId == purchaseOrder.goodId) purchaseOrder else order
}
}
}
/**
* 有异常的订单数量
*/
fun hasWrongCount(): Boolean {
return _orders.value.any {
it.receiveCount != it.receivedNum
}
}
/**
* 更新所有商品仓库
*/
fun updateAllPurchaseStore(store: DictType, predicate: (GoodsInfo) -> Boolean = { true }) {
_orders.update { currentList ->
currentList.map { order ->
if (predicate(order)) order.copy(
warehouseId = store.id,
warehouseName = store.value
) else order
}
}
}
// 删除订单
fun removePurchaseOrder(orderId: Int) {
_orders.update { current ->
current.filterNot { it.goodId == orderId }
}
}
// 清空列表
fun clearPurchaseOrders() {
_orders.value = emptyList()
}
fun updateCountInputState(boolean: Boolean) {
_countUserInput.value = boolean
}
fun startSensorScale() {
Timber.d("开始称重")
SensorScaleUtils.startScale(callback = { weight ->
val currentItem = _selectedItem.value
currentItem?.let { info ->
val consumeValue = currentItem.consumeValue?.toDouble() ?: 1.0
val purchaseValue = currentItem.purchaseValue?.toDouble() ?: 1.0
val count =
weight * 1000 / consumeValue / purchaseValue
val finalCount = count.toSafeFloat(currentItem.unitName)
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
_selectedItem.value = info.copy(
goodsWeight = weight.toSafeBigDecimal(),
receivedNum = if (_countUserInput.value) info.receivedNum else finalCount
)
}
})
}
fun stopSensorScale() {
Timber.d("关闭称重")
SensorScaleUtils.stopContinuousRead()
}
fun partialReceipt(uploadInfo: UploadInfo) {
launchWithLoading {
val response = repository.partialReceipt(uploadInfo)
if (!response.isSuccess()) {
parseResponse(response)
return@launchWithLoading
}
if (response.success == true) {
ToastUtils.showToast("部分收货成功")
_receiptResult.value = true
} else {
ToastUtils.showToast("部分收货失败")
}
}
}
fun confirmReceipt(uploadInfo: UploadInfo) {
launchWithLoading {
val response = repository.confirmReceipt(uploadInfo)
if (!response.isSuccess()) {
parseResponse(response)
return@launchWithLoading
}
if (response.success == true) {
ToastUtils.showToast("收货成功")
_receiptResult.value = true
} else {
ToastUtils.showToast("收货失败")
}
}
}
}