77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
/**
|
||
* 功能:提供父子关联下拉的纯业务判断,避免页面层重复推断组织归属。
|
||
* 版本:v1.0.0
|
||
*/
|
||
import type { ResourceRow } from '@/api/resource-page-rules';
|
||
|
||
/** 将表单或关联记录中的标识规范化为空字符串或稳定字符串。 */
|
||
export function relationIdentity(value: unknown) {
|
||
return value == null ? '' : String(value).trim();
|
||
}
|
||
|
||
/** 读取子选项记录声明的父级标识。 */
|
||
export function optionParentIdentity(
|
||
option: ResourceRow | undefined,
|
||
optionParentKey: string,
|
||
) {
|
||
return option ? relationIdentity(option[optionParentKey]) : '';
|
||
}
|
||
|
||
/**
|
||
* 仅在已取得子选项记录且父子关系明确不兼容时返回 true。
|
||
* 父级为空时只允许平台主管子项;父级有值时只允许其直属子项。
|
||
*/
|
||
export function shouldClearLinkedOption(
|
||
parentIdentity: string,
|
||
option: ResourceRow | undefined,
|
||
optionParentKey: string,
|
||
) {
|
||
if (!option) return false;
|
||
const optionParent = optionParentIdentity(option, optionParentKey);
|
||
return parentIdentity ? optionParent !== parentIdentity : Boolean(optionParent);
|
||
}
|
||
|
||
/** 返回父子组合的保存前提示;无法取得子选项时交由后端最终校验。 */
|
||
export function linkedRelationValidationMessage(
|
||
parentIdentity: string,
|
||
childIdentity: string,
|
||
option: ResourceRow | undefined,
|
||
optionParentKey: string,
|
||
) {
|
||
if (!childIdentity || !option) return '';
|
||
const optionParent = optionParentIdentity(option, optionParentKey);
|
||
if (parentIdentity && !optionParent) {
|
||
return '平台主管配送点不能与所属气站同时选择';
|
||
}
|
||
if (parentIdentity && optionParent !== parentIdentity) {
|
||
return '所选配送点不属于所属气站,请重新选择';
|
||
}
|
||
if (!parentIdentity && optionParent) {
|
||
return '所选配送点已有所属气站,请确认所属气站';
|
||
}
|
||
return '';
|
||
}
|
||
|
||
/** 根据当前父级生成服务端筛选条件;父级为空时保留全量可选范围。 */
|
||
export function linkedRelationFilters(
|
||
parentIdentity: string,
|
||
filterKey: string,
|
||
) {
|
||
return parentIdentity ? { [filterKey]: parentIdentity } : undefined;
|
||
}
|
||
|
||
/** 为同一关联资源生成递增版本号,用于识别并丢弃过期响应。 */
|
||
export function createRelationRequestVersionGuard() {
|
||
const versions = new Map<string, number>();
|
||
return {
|
||
begin(resource: string) {
|
||
const version = (versions.get(resource) ?? 0) + 1;
|
||
versions.set(resource, version);
|
||
return version;
|
||
},
|
||
isLatest(resource: string, version: number) {
|
||
return versions.get(resource) === version;
|
||
},
|
||
};
|
||
}
|