refactor(activity): 统一 cook/submit 中调料数据处理逻辑,先清除 materialType==3 再从 adapter 覆盖写入

This commit is contained in:
2026-05-09 10:51:04 +08:00
parent 3b1dc0a03f
commit 7a03ab8108
@@ -293,11 +293,6 @@ class SubmitFoodActivity : BaseActivity() {
toast("未获取到菜品或构成信息")
return
}
val seasoningData = seasoningAdapter.items
// if (seasoningData.isEmpty()) {
// toast("未获取到调料信息")
// return
// }
showLoading()
val isSamplingData = food!!.cookMode == 1
@@ -316,23 +311,11 @@ class SubmitFoodActivity : BaseActivity() {
else -> it.dinnerType
}
}
// 将调料数据转为 CookFoodGoodsEntity 追加到 goodsList
val tempSeasoningList = seasoningData.map { item ->
val seasoning = appViewModel.getSeasoningByGoodsId(item.goodsId)
CookFoodGoodsEntity().also { entity ->
entity.goodsId = item.goodsId
entity.goodsName = item.goodsName
entity.useWeight = item.useWeight
entity.materialType = 3
entity.popularName = seasoning?.popularName
entity.zjmCode = seasoning?.zjmCode
entity.materId = seasoning?.materId
entity.relateionType = seasoning?.relateionType ?: 0
entity.allEdible = seasoning?.allEdible ?: true
entity.goodsOrRelationCode = seasoning?.goodsOrRelationCode
}
}
goodsList?.addAll(tempSeasoningList)
// 先移除 goodsList 中已有的调料数据(materialType==3),再以 adapter 中最新调料覆盖
goodsList?.removeAll { it.materialType == 3 }
seasoningAdapter.items
.filter { it.useWeight > 0.0 }
.forEach { goodsList?.add(buildSeasoningEntity(it)) }
appViewModel.saveCookFoodAndGoods(
cookMode = food!!.cookMode,
entity = cookFoodEntity,
@@ -348,28 +331,14 @@ class SubmitFoodActivity : BaseActivity() {
toast("未获取到菜品或构成信息")
return
}
val seasoningData = seasoningAdapter.items
showLoading()
lifecycleScope.launch {
// 将重量大于0且不重复的调料追加到 goodsList
seasoningData.forEach { item ->
if (item.useWeight > 0.0 && goodsList?.none { it.goodsId == item.goodsId } == true) {
val seasoning = appViewModel.getSeasoningByGoodsId(item.goodsId)
goodsList?.add(CookFoodGoodsEntity().also { entity ->
entity.goodsId = item.goodsId
entity.goodsName = item.goodsName
entity.useWeight = item.useWeight
entity.materialType = 3
entity.popularName = seasoning?.popularName
entity.zjmCode = seasoning?.zjmCode
entity.materId = seasoning?.materId
entity.relateionType = seasoning?.relateionType ?: 0
entity.allEdible = seasoning?.allEdible ?: true
entity.goodsOrRelationCode = seasoning?.goodsOrRelationCode
})
}
}
// 先移除 goodsList 中已有的调料数据(materialType==3),再以 adapter 中最新调料覆盖
goodsList?.removeAll { it.materialType == 3 }
seasoningAdapter.items
.filter { it.useWeight > 0.0 }
.forEach { goodsList?.add(buildSeasoningEntity(it)) }
cookFoodEntity.let {
it.matchingConstituteInfoList = this@SubmitFoodActivity.goodsList
@@ -392,6 +361,27 @@ class SubmitFoodActivity : BaseActivity() {
}
}
/**
* 将 adapter 中的调料 item 转换为 CookFoodGoodsEntity
* 从本地 dm_seasoning 表补充完整字段(popularName、zjmCode、materId、goodsCode 等)
*/
private suspend fun buildSeasoningEntity(item: SeasoningWeightAdapter.Item): CookFoodGoodsEntity {
val seasoning = appViewModel.getSeasoningByGoodsId(item.goodsId)
return CookFoodGoodsEntity().also { entity ->
entity.goodsId = item.goodsId
entity.goodsName = item.goodsName
entity.useWeight = item.useWeight
entity.materialType = 3
entity.popularName = seasoning?.popularName
entity.zjmCode = seasoning?.zjmCode
entity.materId = seasoning?.materId
entity.goodsCode = seasoning?.goodsCode
entity.relateionType = seasoning?.relateionType ?: 0
entity.allEdible = seasoning?.allEdible ?: true
entity.goodsOrRelationCode = seasoning?.goodsOrRelationCode
}
}
private fun submitSuccess(isSamplingData: Boolean) {
if (food?.foodId.isNullOrBlank()) {
dismissLoading()