init
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
package com.sw.inbound.viewmodel
|
||||
|
||||
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.model.request.GoodsAddParam
|
||||
import com.sw.inbound.model.request.PurchaseWarehouseParam
|
||||
import com.sw.inbound.model.response.DictType
|
||||
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.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
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
|
||||
) : BaseViewModel(repository) {
|
||||
|
||||
private val _addGoodsResult = MutableStateFlow<Boolean>(false)
|
||||
val addGoodsResult: StateFlow<Boolean> = _addGoodsResult
|
||||
|
||||
private val _addToWarehouseResult = MutableStateFlow<Boolean>(false)
|
||||
val addToWarehouseResult: StateFlow<Boolean> = _addToWarehouseResult
|
||||
|
||||
private val _selectedItem = MutableStateFlow<PurchaseWarehouseParam?>(null)
|
||||
val selectedItem: StateFlow<PurchaseWarehouseParam?> = _selectedItem
|
||||
|
||||
// 全局仓库信息
|
||||
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
|
||||
|
||||
// 表单状态
|
||||
private val _goodsAddParam = MutableStateFlow<GoodsAddParam>(GoodsAddParam())
|
||||
val goodsAddParam: StateFlow<GoodsAddParam> = _goodsAddParam
|
||||
|
||||
// 快速添加弹窗状态
|
||||
private val _showAddProductDialog = MutableStateFlow<Boolean>(false)
|
||||
val showAddProductDialog: StateFlow<Boolean> = _showAddProductDialog
|
||||
|
||||
// 搜索物品列表
|
||||
private val _searchListItems = MutableStateFlow<List<SearchGoodsInfo.Record>>(emptyList())
|
||||
val searchListItems: StateFlow<List<SearchGoodsInfo.Record>> = _searchListItems
|
||||
|
||||
// 称重结果
|
||||
private val _weightInfo = MutableStateFlow<Double?>(0.0)
|
||||
val weightInfo: StateFlow<Double?> = _weightInfo
|
||||
|
||||
/**
|
||||
* 数量是否是用户输入的值
|
||||
*/
|
||||
private val _countUserInput = MutableStateFlow<Boolean>(false)
|
||||
|
||||
/**
|
||||
* 更新全局仓库
|
||||
*/
|
||||
fun updateGlobalWarehouse(dictType: DictType) {
|
||||
_globalWarehouse.value = dictType
|
||||
}
|
||||
|
||||
fun startSensorScale() {
|
||||
Timber.d("开始称重")
|
||||
SensorScaleUtils.startScale(callback = { weight ->
|
||||
val currentItem = _selectedItem.value
|
||||
|
||||
currentItem?.let { info ->
|
||||
val selectUnitType = currentItem.selectUnitType
|
||||
if (selectUnitType == null) return@startScale
|
||||
|
||||
val consumeValue = selectUnitType.consumeValue?.toDouble() ?: 1.0
|
||||
val purchaseValue = selectUnitType.purchaseValue?.toDouble() ?: 1.0
|
||||
val count =
|
||||
weight * 1000 / consumeValue / purchaseValue
|
||||
val finalCount = count.toSafeDouble(currentItem.unitName)
|
||||
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
|
||||
_selectedItem.value = info.copy(
|
||||
goodsWeight = weight.toSafeBigDecimal(),
|
||||
goodsCount = if (_countUserInput.value) info.goodsCount else finalCount
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun stopSensorScale() {
|
||||
Timber.d("关闭称重")
|
||||
SensorScaleUtils.stopContinuousRead()
|
||||
}
|
||||
|
||||
fun updateAddProductDialog(show: Boolean) {
|
||||
_showAddProductDialog.value = show
|
||||
}
|
||||
|
||||
fun updateSelectedItem(purchaseOrder: PurchaseWarehouseParam?) {
|
||||
_selectedItem.value = purchaseOrder
|
||||
}
|
||||
|
||||
fun updateGoodsAddParam(newState: GoodsAddParam) {
|
||||
_goodsAddParam.value = newState
|
||||
}
|
||||
|
||||
fun cleanGoodsAddParam() {
|
||||
_goodsAddParam.value = GoodsAddParam()
|
||||
}
|
||||
|
||||
// 添加订单
|
||||
fun addPurchaseItem(purchaseOrder: PurchaseWarehouseParam) {
|
||||
_purchaseList.update { currentList ->
|
||||
// 当已经添加过则忽略
|
||||
if (currentList.any { it.goodsId == purchaseOrder.goodsId }) {
|
||||
currentList
|
||||
} else {
|
||||
currentList + purchaseOrder
|
||||
}
|
||||
}
|
||||
updateSelectedItem(null)
|
||||
}
|
||||
|
||||
// 更新订单
|
||||
fun updatePurchaseItem(purchaseOrder: PurchaseWarehouseParam) {
|
||||
_purchaseList.update { currentList ->
|
||||
currentList.map { order ->
|
||||
if (order.goodsId == purchaseOrder.goodsId) purchaseOrder else order
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除订单
|
||||
fun removePurchaseOrder(orderId: Int) {
|
||||
_purchaseList.update { current ->
|
||||
current.filterNot { it.goodsId == orderId }
|
||||
}
|
||||
}
|
||||
|
||||
// 清空列表
|
||||
fun clearPurchaseOrders() {
|
||||
_purchaseList.value = emptyList()
|
||||
}
|
||||
|
||||
fun searchGoodsInfoList(
|
||||
goodsName: String,
|
||||
pageNo: Int = 0,
|
||||
pageSize: Int = 10
|
||||
) {
|
||||
launch {
|
||||
val response = repository.searchGoodsInfoList(goodsName, pageNo, pageSize)
|
||||
if (response.isSuccess()) {
|
||||
val data = response.data
|
||||
if (data != null) {
|
||||
_searchListItems.value = data.records ?: emptyList<SearchGoodsInfo.Record>()
|
||||
}
|
||||
} else {
|
||||
_searchListItems.value = emptyList<SearchGoodsInfo.Record>()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSelectedItemWithSearch(searchFirst: SearchGoodsInfo.Record) {
|
||||
val warehouse = _globalWarehouse.value
|
||||
viewModelScope.launch {
|
||||
updateSelectedItem(null)
|
||||
delay(50)
|
||||
_selectedItem.value = PurchaseWarehouseParam(
|
||||
warehouseId = warehouse.id,
|
||||
goodsId = searchFirst.goodsId!!,
|
||||
goodsName = searchFirst.goodsName,
|
||||
kcUnitId = searchFirst.kcUnitId!!,
|
||||
unitList = searchFirst.unitVoList,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateCountInputState(boolean: Boolean) {
|
||||
_countUserInput.value = boolean
|
||||
}
|
||||
|
||||
fun addGoodsInfo() {
|
||||
val imageUri = GlobalData.imageUri
|
||||
if (imageUri == null) {
|
||||
ToastUtils.showToast("请先进行图片采集")
|
||||
return
|
||||
}
|
||||
val errInfo = _goodsAddParam.value.hasNullField()
|
||||
if (errInfo != null) {
|
||||
ToastUtils.showToast(errInfo)
|
||||
return
|
||||
}
|
||||
|
||||
launchWithLoading {
|
||||
val uploadResponse = repository.uploadImage(imageUri)
|
||||
if (!uploadResponse.isSuccess()) {
|
||||
parseResponse(uploadResponse)
|
||||
return@launchWithLoading
|
||||
}
|
||||
// ToastUtils.showToast("图片上传成功,${uploadResponse.data}")
|
||||
GlobalData.imageUri = null
|
||||
_goodsAddParam.value.relativeUrl = uploadResponse.data
|
||||
val response = repository.selfPurchaseGoodsAdd(_goodsAddParam.value)
|
||||
if (!response.isSuccess()) {
|
||||
parseResponse(response)
|
||||
return@launchWithLoading
|
||||
}
|
||||
_showAddProductDialog.value = false
|
||||
val searchFirst = response.data!!.records?.get(0)
|
||||
searchFirst?.let {
|
||||
updateSelectedItemWithSearch(searchFirst)
|
||||
}
|
||||
_addGoodsResult.value = true
|
||||
}
|
||||
}
|
||||
|
||||
fun addToWarehouse() {
|
||||
if (_purchaseList.value.isEmpty()) return
|
||||
launchWithLoading {
|
||||
val response = repository.selfPurchaseWarehousing(_purchaseList.value)
|
||||
if (parseResponse(response)) {
|
||||
_addToWarehouseResult.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user