/** * 功能:提供父子关联下拉的纯业务判断,避免页面层重复推断组织归属。 * 版本: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 ''; } /** 为同一关联资源生成递增版本号,用于识别并丢弃过期响应。 */ export function createRelationRequestVersionGuard() { const versions = new Map(); 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; }, }; }