refactor(form): 重构表单字段模型和映射逻辑
- 移除 FormField 中的 submitValueId 和 valueId 字段 - 在 DictType 中添加操作符函数支持字段名取值 - 简化下拉框选择时的 extraValues 映射逻辑 - 更新日期选择器只保存显示格式值 - 调整表单提交时的参数构建逻辑 - 为入库时间和操作时间字段移除 ISO 格式提交配置 - 添加 materId 参数到入库提交数据中
This commit is contained in:
@@ -25,6 +25,9 @@ import java.text.SimpleDateFormat
|
|||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
import kotlin.text.clear
|
||||||
|
import kotlin.text.get
|
||||||
|
import kotlin.text.set
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态表单字段 Adapter,配合 FlexboxLayoutManager 实现两列自动换行布局
|
* 动态表单字段 Adapter,配合 FlexboxLayoutManager 实现两列自动换行布局
|
||||||
@@ -123,18 +126,11 @@ class FormFieldAdapter(
|
|||||||
popHeight = popHeight
|
popHeight = popHeight
|
||||||
) { dictType ->
|
) { dictType ->
|
||||||
item.value = dictType.value ?: ""
|
item.value = dictType.value ?: ""
|
||||||
item.valueId = dictType.id ?: ""
|
|
||||||
b.tvDropdownValue.text = item.value
|
b.tvDropdownValue.text = item.value
|
||||||
// 将 extraApiKeys 映射的 DictType 字段值写入 extraValues
|
// 将 extraApiKeys 映射的 DictType 字段值写入 extraValues
|
||||||
item.extraValues.clear()
|
item.extraValues.clear()
|
||||||
item.extraApiKeys.forEach { (apiKey, dictField) ->
|
item.extraApiKeys.forEach { (apiKey, dictField) ->
|
||||||
val fieldValue = when (dictField) {
|
item.extraValues[apiKey] = dictType[dictField]
|
||||||
"id" -> dictType.id ?: ""
|
|
||||||
"value" -> dictType.value ?: ""
|
|
||||||
"type" -> dictType.type
|
|
||||||
else -> ""
|
|
||||||
}
|
|
||||||
item.extraValues[apiKey] = fieldValue
|
|
||||||
}
|
}
|
||||||
}.also {
|
}.also {
|
||||||
it.bgLayout = b.flDropdown
|
it.bgLayout = b.flDropdown
|
||||||
@@ -161,7 +157,6 @@ class FormFieldAdapter(
|
|||||||
}
|
}
|
||||||
// value 存显示格式,valueId 存 ISO 格式供提交使用
|
// value 存显示格式,valueId 存 ISO 格式供提交使用
|
||||||
item.value = displayDateFormat.format(selected.time)
|
item.value = displayDateFormat.format(selected.time)
|
||||||
item.valueId = isoFormat.format(selected.time)
|
|
||||||
b.tvDateValue.text = item.value
|
b.tvDateValue.text = item.value
|
||||||
},
|
},
|
||||||
cal.get(Calendar.YEAR),
|
cal.get(Calendar.YEAR),
|
||||||
|
|||||||
@@ -133,8 +133,7 @@ class InboundGoodsStoreDialog(
|
|||||||
FieldType.DATE_PICKER,
|
FieldType.DATE_PICKER,
|
||||||
apiKey = "inboundTime",
|
apiKey = "inboundTime",
|
||||||
required = true,
|
required = true,
|
||||||
hint = "请选择入库时间",
|
hint = "请选择入库时间"
|
||||||
submitValueId = true
|
|
||||||
),
|
),
|
||||||
FormField(
|
FormField(
|
||||||
"存放区域", FieldType.DROPDOWN,
|
"存放区域", FieldType.DROPDOWN,
|
||||||
@@ -205,15 +204,13 @@ class InboundGoodsStoreDialog(
|
|||||||
"操作时间",
|
"操作时间",
|
||||||
FieldType.DATE_PICKER,
|
FieldType.DATE_PICKER,
|
||||||
apiKey = "inboundTime",
|
apiKey = "inboundTime",
|
||||||
hint = "请选择操作时间",
|
hint = "请选择操作时间"
|
||||||
submitValueId = true
|
|
||||||
),
|
),
|
||||||
FormField(
|
FormField(
|
||||||
"保质期至",
|
"保质期至",
|
||||||
FieldType.DATE_PICKER,
|
FieldType.DATE_PICKER,
|
||||||
apiKey = "expiryDate",
|
apiKey = "expiryDate",
|
||||||
hint = "请选择保质期",
|
hint = "请选择保质期"
|
||||||
submitValueId = true
|
|
||||||
),
|
),
|
||||||
FormField(
|
FormField(
|
||||||
"存放区域", FieldType.DROPDOWN,
|
"存放区域", FieldType.DROPDOWN,
|
||||||
@@ -304,13 +301,13 @@ class InboundGoodsStoreDialog(
|
|||||||
val map = mutableMapOf<String, Any>()
|
val map = mutableMapOf<String, Any>()
|
||||||
for (field in fields) {
|
for (field in fields) {
|
||||||
if (field.extraApiKeys.isNotEmpty()) {
|
if (field.extraApiKeys.isNotEmpty()) {
|
||||||
field.extraValues.forEach { (apiKey, raw) ->
|
field.extraValues.forEach { (apiKey, value) ->
|
||||||
if (raw.isNotBlank()) map[apiKey] = raw
|
map[apiKey] = value
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (field.apiKey.isBlank()) continue
|
if (field.apiKey.isBlank()) continue
|
||||||
val raw = if (field.submitValueId) field.valueId else field.value
|
val raw = field.value
|
||||||
if (raw.isBlank()) continue
|
if (raw.isBlank()) continue
|
||||||
val converted: Any = when (field.apiKey) {
|
val converted: Any = when (field.apiKey) {
|
||||||
"quantity" -> raw.toIntOrNull() ?: continue
|
"quantity" -> raw.toIntOrNull() ?: continue
|
||||||
@@ -326,7 +323,7 @@ class InboundGoodsStoreDialog(
|
|||||||
val param = buildParamMap()
|
val param = buildParamMap()
|
||||||
// weight 来自秤,不在表单中,额外注入
|
// weight 来自秤,不在表单中,额外注入
|
||||||
param["weight"] = realWeight / 1000.0
|
param["weight"] = realWeight / 1000.0
|
||||||
|
param["materId"] = goods?.materId ?:""
|
||||||
Loading.show(storeActivity ?: return)
|
Loading.show(storeActivity ?: return)
|
||||||
storeActivity?.storeViewModel?.submitRawInbound(param) { success, msg ->
|
storeActivity?.storeViewModel?.submitRawInbound(param) { success, msg ->
|
||||||
Loading.dismiss()
|
Loading.dismiss()
|
||||||
@@ -346,6 +343,7 @@ class InboundGoodsStoreDialog(
|
|||||||
param["weight"] = realWeight / 1000.0
|
param["weight"] = realWeight / 1000.0
|
||||||
// inboundTime 净菜取当前时间,不在表单中
|
// inboundTime 净菜取当前时间,不在表单中
|
||||||
param["inboundTime"] = isoFormat.format(Date())
|
param["inboundTime"] = isoFormat.format(Date())
|
||||||
|
param["materId"] = goods?.materId ?:""
|
||||||
|
|
||||||
Loading.show(storeActivity ?: return)
|
Loading.show(storeActivity ?: return)
|
||||||
storeActivity?.storeViewModel?.submitCleanInbound(param) { success, msg ->
|
storeActivity?.storeViewModel?.submitCleanInbound(param) { success, msg ->
|
||||||
|
|||||||
@@ -26,9 +26,7 @@ enum class FieldType {
|
|||||||
* @param hidden 是否隐藏,hidden=true 时不渲染该字段
|
* @param hidden 是否隐藏,hidden=true 时不渲染该字段
|
||||||
* @param hint 校验失败提示文案,同时作为输入框 hint
|
* @param hint 校验失败提示文案,同时作为输入框 hint
|
||||||
* @param options 下拉框选项列表(DROPDOWN 类型使用)
|
* @param options 下拉框选项列表(DROPDOWN 类型使用)
|
||||||
* @param submitValueId 提交时使用 valueId 而非 value;DATE_PICKER 选 ISO 格式时设为 true
|
|
||||||
* @param value 当前显示值/输入值;DATE_PICKER 存 yyyy-MM-dd 显示格式
|
* @param value 当前显示值/输入值;DATE_PICKER 存 yyyy-MM-dd 显示格式
|
||||||
* @param valueId DATE_PICKER 存 ISO 格式 yyyy-MM-dd'T'HH:mm:ss
|
|
||||||
* @param extraValues 下拉选中后按 extraApiKeys 映射存储的提交值,key=接口参数名
|
* @param extraValues 下拉选中后按 extraApiKeys 映射存储的提交值,key=接口参数名
|
||||||
*/
|
*/
|
||||||
data class FormField(
|
data class FormField(
|
||||||
@@ -40,8 +38,6 @@ data class FormField(
|
|||||||
val hidden: Boolean = false,
|
val hidden: Boolean = false,
|
||||||
val hint: String = "",
|
val hint: String = "",
|
||||||
val options: List<DictType> = emptyList(),
|
val options: List<DictType> = emptyList(),
|
||||||
val submitValueId: Boolean = false,
|
|
||||||
var value: String = "",
|
var value: String = "",
|
||||||
var valueId: String = "",
|
|
||||||
val extraValues: MutableMap<String, String> = mutableMapOf()
|
val extraValues: MutableMap<String, String> = mutableMapOf()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,4 +11,14 @@ data class DictType(
|
|||||||
val id: String? = null,
|
val id: String? = null,
|
||||||
val value: String? = null,
|
val value: String? = null,
|
||||||
val type: String = ""
|
val type: String = ""
|
||||||
) : Parcelable
|
) : Parcelable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字段名取值,供 extraApiKeys 映射直接使用
|
||||||
|
*/
|
||||||
|
operator fun get(fieldName: String): String = when (fieldName) {
|
||||||
|
"id" -> id ?: ""
|
||||||
|
"value" -> value ?: ""
|
||||||
|
else -> type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user