From 7a03ab81086c5cf3123306735d75dc3817309e7b Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Sat, 9 May 2026 10:51:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(activity):=20=E7=BB=9F=E4=B8=80=20cook?= =?UTF-8?q?/submit=20=E4=B8=AD=E8=B0=83=E6=96=99=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E5=85=88=E6=B8=85?= =?UTF-8?q?=E9=99=A4=20materialType=3D=3D3=20=E5=86=8D=E4=BB=8E=20adapter?= =?UTF-8?q?=20=E8=A6=86=E7=9B=96=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dish/match/ui/SubmitFoodActivity.kt | 72 ++++++++----------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt index cff2ed3..b1f80c0 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SubmitFoodActivity.kt @@ -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()