Files
platforms/frontend/platform_admin/src/views/resource/resource-relation-linkage-policy.ts

113 lines
3.7 KiB
TypeScript
Raw Normal View History

2026-08-11 14:28:13 +08:00
/**
*
* 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 staffOrganizationFilters(
gasIdentity: string,
deliveryIdentity: string,
) {
return {
...(gasIdentity
? { gas_basic_identities: gasIdentity }
: { gas_basic_unassigned: '1' }),
...(deliveryIdentity
? { delivery_basic_identities: deliveryIdentity }
: { delivery_basic_unassigned: '1' }),
};
}
/** 校验服务人员记录与当前气站、配送点完全一致,空字符串代表可以提交。 */
export function staffOrganizationValidationMessage(
gasIdentity: string,
deliveryIdentity: string,
staffIdentity: string,
option: ResourceRow | undefined,
) {
if (!staffIdentity) return '';
if (!option) return '无法确认所选服务人员的组织归属,请重新选择';
if (optionParentIdentity(option, 'gas_basic_identity') !== gasIdentity) {
return '所选服务人员不属于当前气站,请重新选择';
}
if (
optionParentIdentity(option, 'delivery_basic_identity') !==
deliveryIdentity
) {
return '所选服务人员不属于当前配送点,请重新选择';
}
return '';
}
2026-08-11 14:28:13 +08:00
/** 为同一关联资源生成递增版本号,用于识别并丢弃过期响应。 */
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;
},
};
}