diff --git a/app/src/main/java/com/lukouguoji/aerologic/page/flight/query/details/FlightQueryDetailsViewModel.kt b/app/src/main/java/com/lukouguoji/aerologic/page/flight/query/details/FlightQueryDetailsViewModel.kt index 4f98f53..545bc03 100644 --- a/app/src/main/java/com/lukouguoji/aerologic/page/flight/query/details/FlightQueryDetailsViewModel.kt +++ b/app/src/main/java/com/lukouguoji/aerologic/page/flight/query/details/FlightQueryDetailsViewModel.kt @@ -7,6 +7,8 @@ import com.lukouguoji.module_base.bean.FlightBean import com.lukouguoji.module_base.common.Constant import com.lukouguoji.module_base.http.net.NetApply import com.lukouguoji.module_base.ktx.launchLoadingCollect +import com.lukouguoji.module_base.util.DictUtils +import dev.utils.app.info.KeyValue class FlightQueryDetailsViewModel : BaseViewModel() { @@ -14,9 +16,24 @@ class FlightQueryDetailsViewModel : BaseViewModel() { var dataBean = MutableLiveData() + /////////////////////////////////////////////////////////////////////////// + // 枚举字段展示文案:接口已返回名称时原样显示,只返回值(code)时按字典反查名称 + /////////////////////////////////////////////////////////////////////////// + + val countryTypeText = MutableLiveData("") + val serviceTypeText = MutableLiveData("") + val portCodeText = MutableLiveData("") + val flightStatusText = MutableLiveData("") + + private var countryTypeDict = emptyList() + private var serviceTypeDict = emptyList() + private var portCodeDict = emptyList() + private var flightStatusDict = emptyList() + fun initOnCreated(intent: Intent) { id = intent.getStringExtra(Constant.Key.ID) ?: "" + loadDictLists() getData() } @@ -27,8 +44,52 @@ class FlightQueryDetailsViewModel : BaseViewModel() { onSuccess = { it.data?.let { data -> dataBean.value = data + refreshEnumText() } } } } -} \ No newline at end of file + + /** + * 加载四个枚举字典(与新增/编辑页同源),用于把 code 反查成 label + */ + private fun loadDictLists() { + DictUtils.getCountryTypeList(addAll = false) { + countryTypeDict = it + refreshEnumText() + } + DictUtils.getFlightServiceTypeList(addAll = false) { + serviceTypeDict = it + refreshEnumText() + } + DictUtils.getFlightPortCodeList(addAll = false) { + portCodeDict = it + refreshEnumText() + } + DictUtils.getFlightStatusList(addAll = false) { + flightStatusDict = it + refreshEnumText() + } + } + + /** + * 数据或字典任一就绪都重算一次,避免两者返回先后顺序影响展示 + */ + private fun refreshEnumText() { + val bean = dataBean.value ?: return + countryTypeText.value = toLabel(countryTypeDict, bean.countryType) + serviceTypeText.value = toLabel(serviceTypeDict, bean.serviceType) + portCodeText.value = toLabel(portCodeDict, bean.portCode) + flightStatusText.value = toLabel(flightStatusDict, bean.flightStatus) + } + + /** + * 值转 label:先按 code 反查,命中不了再判断是否本身就是 label,仍不命中则原样返回 + */ + private fun toLabel(dict: List, raw: String?): String { + if (raw.isNullOrEmpty()) return "" + dict.firstOrNull { it.value == raw }?.let { return it.key } + dict.firstOrNull { it.key == raw }?.let { return it.key } + return raw + } +} diff --git a/app/src/main/res/layout/activity_flight_query_details.xml b/app/src/main/res/layout/activity_flight_query_details.xml index 888509d..89548cb 100644 --- a/app/src/main/res/layout/activity_flight_query_details.xml +++ b/app/src/main/res/layout/activity_flight_query_details.xml @@ -37,7 +37,7 @@ android:orientation="vertical" android:padding="15dp"> - + + value='@{viewModel.dataBean.aircraftCode}' /> - + + + + + + + + + + + + + + + value='@{viewModel.countryTypeText}' /> + value='@{viewModel.serviceTypeText}' /> + + + + + + value='@{viewModel.portCodeText}' /> + + + + - + - + - + - - - - - - - - - - - - - - - + + + + diff --git a/module_base/src/main/java/com/lukouguoji/module_base/http/net/Api.kt b/module_base/src/main/java/com/lukouguoji/module_base/http/net/Api.kt index 7423bd4..93b29d1 100644 --- a/module_base/src/main/java/com/lukouguoji/module_base/http/net/Api.kt +++ b/module_base/src/main/java/com/lukouguoji/module_base/http/net/Api.kt @@ -312,6 +312,24 @@ interface Api { @POST("typeCode/countryType") suspend fun getCountryTypeList(): DictListBean + /** + * 获取飞行状态 + */ + @POST("typeCode/flightPortCode") + suspend fun getFlightPortCodeList(): DictListBean + + /** + * 获取航班服务种类 + */ + @POST("typeCode/flightServiceType") + suspend fun getFlightServiceTypeList(): DictListBean + + /** + * 获取航班状态 + */ + @POST("typeCode/flightStatus") + suspend fun getFlightStatusList(): DictListBean + /** * 获取国家代码 */ diff --git a/module_base/src/main/java/com/lukouguoji/module_base/util/DictUtils.kt b/module_base/src/main/java/com/lukouguoji/module_base/util/DictUtils.kt index 3170f62..a8da4d9 100644 --- a/module_base/src/main/java/com/lukouguoji/module_base/util/DictUtils.kt +++ b/module_base/src/main/java/com/lukouguoji/module_base/util/DictUtils.kt @@ -490,6 +490,60 @@ object DictUtils { } } + /** + * 飞行状态(0:前方起飞 1:过站登机 2:登机结束 6:到达 7:登机 9:正在值机 …) + */ + fun getFlightPortCodeList( + addAll: Boolean = true, + checkedValue: String? = null, + callBack: (List) -> Unit + ) { + launchCollect({ + NetApply.api + .getFlightPortCodeList() + }) { + onSuccess = { + handleCallBack(it, checkedValue, addAll, callBack) + } + } + } + + /** + * 航班服务种类(0:客机 1:货机 2:卡车) + */ + fun getFlightServiceTypeList( + addAll: Boolean = true, + checkedValue: String? = null, + callBack: (List) -> Unit + ) { + launchCollect({ + NetApply.api + .getFlightServiceTypeList() + }) { + onSuccess = { + handleCallBack(it, checkedValue, addAll, callBack) + } + } + } + + /** + * 航班状态(3:返航 4:地面滑回 5:备降 8:取消 12:延误 15:删除 16:未知) + */ + fun getFlightStatusList( + addAll: Boolean = true, + checkedValue: String? = null, + callBack: (List) -> Unit + ) { + launchCollect({ + NetApply.api + .getFlightStatusList() + }) { + onSuccess = { + handleCallBack(it, checkedValue, addAll, callBack) + } + } + } + /** * 获取角色列表 */ diff --git a/module_hangban/src/main/java/com/lukouguoji/hangban/page/edit/HbFlightEditViewModel.kt b/module_hangban/src/main/java/com/lukouguoji/hangban/page/edit/HbFlightEditViewModel.kt index 32cf5c3..e58056d 100644 --- a/module_hangban/src/main/java/com/lukouguoji/hangban/page/edit/HbFlightEditViewModel.kt +++ b/module_hangban/src/main/java/com/lukouguoji/hangban/page/edit/HbFlightEditViewModel.kt @@ -41,6 +41,7 @@ class HbFlightEditViewModel : BaseViewModel() { val fdest = MutableLiveData("") val countryType = MutableLiveData("") val serviceType = MutableLiveData("") + val portCode = MutableLiveData("") val scheduledTackOff = MutableLiveData("") val estimatedTakeOff = MutableLiveData("") val actualTakeOff = MutableLiveData("") @@ -64,42 +65,17 @@ class HbFlightEditViewModel : BaseViewModel() { val countryTypeList = MutableLiveData>(emptyList()) val serviceTypeList = MutableLiveData>(emptyList()) val flightStatusList = MutableLiveData>(emptyList()) + val portCodeList = MutableLiveData>(emptyList()) /////////////////////////////////////////////////////////////////////////// // 选项定义(服务端入库/过滤用 code,详情接口返回中文名,回填时需反查) /////////////////////////////////////////////////////////////////////////// - private val countryTypeOptions = listOf( - KeyValue("国内", "0"), - KeyValue("国际", "1"), - KeyValue("地区", "2"), - KeyValue("混合", "3"), - ) - - private val serviceTypeOptions = listOf( - KeyValue("客机", "0"), - KeyValue("货机", "1"), - KeyValue("卡车", "2"), - ) - private val statusOptions = listOf( KeyValue("出港", "0"), KeyValue("进港", "1"), ) - private val flightStatusOptions = listOf( - KeyValue("正常", "0"), - KeyValue("登机", "1"), - KeyValue("登机结束", "2"), - KeyValue("起飞", "3"), - KeyValue("到达", "4"), - KeyValue("取消", "5"), - KeyValue("备降", "6"), - KeyValue("删除发布", "7"), - KeyValue("延误", "8"), - KeyValue("未知", "9"), - ) - private val cmFlagOptions = listOf( KeyValue("有货邮", "0"), KeyValue("无货邮", "1"), @@ -138,8 +114,12 @@ class HbFlightEditViewModel : BaseViewModel() { fdep.value = bean.fdep jtz.value = bean.jtz fdest.value = bean.fdest - countryType.value = toCode(countryTypeOptions, bean.countryType) - serviceType.value = toCode(serviceTypeOptions, bean.serviceType) + // 枚举字段(地区类型/服务类型/航班状态/飞行状态)由详情接口返回中文名, + // 统一在字典加载回调里转 code,此处先原样存放 + countryType.value = bean.countryType + serviceType.value = bean.serviceType + flightStatus.value = bean.flightStatus + portCode.value = bean.portCode status = toCode(statusOptions, bean.status) scheduledTackOff.value = bean.scheduledTackOff estimatedTakeOff.value = bean.estimatedTakeOff @@ -150,7 +130,6 @@ class HbFlightEditViewModel : BaseViewModel() { standId.value = bean.standId registration.value = bean.registration prefix = bean.prefix.noNull() - flightStatus.value = toCode(flightStatusOptions, bean.flightStatus) cmFlag = toCode(cmFlagOptions, bean.cmFlag) delayFreeText.value = bean.delayFreeText } @@ -161,27 +140,36 @@ class HbFlightEditViewModel : BaseViewModel() { } /** - * 加载下拉列表(编辑模式传 checkedValue 置顶选中项) + * 加载下拉列表(四个数据源均来自 typeCode 字典接口) */ private fun loadDictLists() { val isModify = pageType.value == DetailsPageType.Modify - DictUtils.getCountryTypeList( - addAll = false, - checkedValue = if (isModify) countryType.value else null - ) { - countryTypeList.postValue(if (isModify) it else listOf(KeyValue("", "")) + it) - } - - serviceTypeList.value = - buildStaticList(serviceTypeOptions, if (isModify) serviceType.value else null) - - flightStatusList.value = - buildStaticList(flightStatusOptions, if (isModify) flightStatus.value else null) + DictUtils.getCountryTypeList(addAll = false) { applyDict(it, countryType, countryTypeList, isModify) } + DictUtils.getFlightServiceTypeList(addAll = false) { applyDict(it, serviceType, serviceTypeList, isModify) } + DictUtils.getFlightStatusList(addAll = false) { applyDict(it, flightStatus, flightStatusList, isModify) } + DictUtils.getFlightPortCodeList(addAll = false) { applyDict(it, portCode, portCodeList, isModify) } } /** - * 值转 code:详情接口可能返回中文名(如“国际”“客机”),统一转为 code 用于回填与提交 + * 字典就绪后:编辑模式把详情返回的中文名统一转成 code,再把列表交给下拉框 + * + * 下拉框带 hint,value 为空时自动停在 hint 项,因此新增模式无需补空项 + */ + private fun applyDict( + list: List, + field: MutableLiveData, + listField: MutableLiveData>, + isModify: Boolean, + ) { + if (isModify) { + field.value = toCode(list, field.value) + } + listField.value = list + } + + /** + * 值转 code:详情接口返回的是中文名(如“国际”“客机”“延误”),统一转为 code 用于回填与提交 */ private fun toCode(options: List, raw: String?): String { if (raw.isNullOrEmpty()) return "" @@ -190,16 +178,6 @@ class HbFlightEditViewModel : BaseViewModel() { ?: raw } - /** - * 静态下拉列表:新增模式首位置空项;编辑模式将选中项置顶 - */ - private fun buildStaticList(list: List, checkedValue: String?): List { - if (checkedValue.isNullOrEmpty()) { - return listOf(KeyValue("", "")) + list - } - return list.filter { it.value == checkedValue } + list.filter { it.value != checkedValue } - } - /** * 保存 点击 */ @@ -239,6 +217,7 @@ class HbFlightEditViewModel : BaseViewModel() { "actualArrival" to actualArrival.value, "standId" to standId.value?.trim(), "registration" to registration.value?.trim(), + "portCode" to portCode.value, "flightStatus" to flightStatus.value, "delayFreeText" to delayFreeText.value?.trim(), ) diff --git a/module_hangban/src/main/res/layout/activity_hb_flight_edit.xml b/module_hangban/src/main/res/layout/activity_hb_flight_edit.xml index f344c19..c4b0d0b 100644 --- a/module_hangban/src/main/res/layout/activity_hb_flight_edit.xml +++ b/module_hangban/src/main/res/layout/activity_hb_flight_edit.xml @@ -171,7 +171,50 @@ - + + + + + + + + + + + + - + - + - - - - + +