fix: 计重明细重量与托盘自重改为和恒定双向联动并校验非负

原逻辑托盘自重变化时重量同向增减,且修改重量不联动托盘自重。
改为两者之和恒等于接口初始值,修改任一方另一方反向同步变动;
任一方将小于 0 时拒绝输入、提示并回退显示。

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-09-11 18:25:02 +08:00
parent ee0606230f
commit 8853405ffc

View File

@@ -3,6 +3,7 @@ package com.lukouguoji.module_base.bean
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.library.baseAdapters.BR
import com.lukouguoji.module_base.ktx.showToast
/**
* 国际出港计重记录明细Bean
@@ -43,40 +44,76 @@ data class GjcCheckInRecord(
}
}
// 重量的字符串表示(用于双向绑定
@get:Bindable
var weightStr: String
get() = if (weight == 0.0) "" else weight.toString()
set(value) {
val newWeight = value.toDoubleOrNull() ?: 0.0
if (weight != newWeight) {
weight = newWeight
notifyPropertyChanged(BR.weightStr)
onDataChanged?.invoke()
}
}
// 托盘自重编辑过程中的原始输入串(保持回显与输入一致,避免光标跳位)
// 不作为构造参数data class copy 备份/恢复时自动丢弃,回退到 trayTotalWeight 显示
// 重量 / 托盘自重编辑过程中的原始输入串(保持回显与输入一致,避免光标跳位
// 不作为构造参数data class copy 备份/恢复时自动丢弃,回退到数值显示
// @Transient仅界面内部状态不参与 Gson 序列化提交
@Transient
private var weightInput: String? = null
@Transient
private var trayTotalWeightInput: String? = null
// 托盘自重的字符串表示(用于双向绑定)
// 业务规则:重量包含托盘自重,托盘自重编辑后,重量同步增减相同差值
// 重的字符串表示(用于双向绑定)
// 业务规则:重量 + 托盘自重 恒定(等于接口返回初始值之和),修改一方另一方反向同步变动
// 校验:任一方不能小于 0非法输入直接拒绝并回退显示
@get:Bindable
var weightStr: String
get() = weightInput ?: if (weight == 0.0) "" else weight.toString()
set(value) {
val newWeight = value.toDoubleOrNull() ?: 0.0
val newTrayWeight = round2(weight + trayTotalWeight - newWeight)
when {
newWeight < 0 -> {
showToast("重量不能小于 0")
notifyPropertyChanged(BR.weightStr)
}
newTrayWeight < 0 -> {
showToast("重量不能超过 ${round2(weight + trayTotalWeight)},否则托盘自重将小于 0")
notifyPropertyChanged(BR.weightStr)
}
else -> {
weightInput = value
if (weight != newWeight) {
weight = newWeight
trayTotalWeight = newTrayWeight
trayTotalWeightInput = null
notifyPropertyChanged(BR.weightStr)
notifyPropertyChanged(BR.trayTotalWeightStr)
onDataChanged?.invoke()
}
}
}
}
// 托盘自重的字符串表示(用于双向绑定),联动规则同上
@get:Bindable
var trayTotalWeightStr: String
get() = trayTotalWeightInput
?: if (trayTotalWeight == 0.0) "" else trayTotalWeight.toString()
get() = trayTotalWeightInput ?: if (trayTotalWeight == 0.0) "" else trayTotalWeight.toString()
set(value) {
trayTotalWeightInput = value
val newTrayWeight = value.toDoubleOrNull() ?: 0.0
val newWeight = round2(weight + trayTotalWeight - newTrayWeight)
when {
newTrayWeight < 0 -> {
showToast("托盘自重不能小于 0")
notifyPropertyChanged(BR.trayTotalWeightStr)
}
newWeight < 0 -> {
showToast("托盘自重不能超过 ${round2(weight + trayTotalWeight)},否则重量将小于 0")
notifyPropertyChanged(BR.trayTotalWeightStr)
}
else -> {
trayTotalWeightInput = value
if (trayTotalWeight != newTrayWeight) {
weight = Math.round((weight + newTrayWeight - trayTotalWeight) * 100) / 100.0
trayTotalWeight = newTrayWeight
weight = newWeight
weightInput = null
notifyPropertyChanged(BR.trayTotalWeightStr)
notifyPropertyChanged(BR.weightStr)
onDataChanged?.invoke()
}
}
}
}
private fun round2(v: Double): Double = Math.round(v * 100) / 100.0
}