WholeProcessPlatform/frontend-sjgl/src/modules/map/domain/map-layer-rules.ts
2026-07-20 16:02:51 +08:00

81 lines
2.3 KiB
TypeScript

type ApplyLayerMutualExclusionRulesOptions = {
rawKeys?: string[];
previousKeys?: string[];
triggerKey?: string;
checked?: boolean;
getLayerBranchKeys?: (rootKey: string) => string[];
};
// 备注:统一按当前业务规则收口图层互斥关系,返回归一化后的勾选结果。
export const applyLayerMutualExclusionRules = ({
rawKeys = [],
previousKeys = [],
triggerKey,
checked,
getLayerBranchKeys
}: ApplyLayerMutualExclusionRulesOptions): string[] => {
let normalizedKeys = Array.from(new Set(rawKeys.filter(Boolean)));
const hasAny = (keys: string[]) =>
keys.length > 0 && keys.some(key => normalizedKeys.includes(key));
const hadAny = (keys: string[]) =>
keys.length > 0 && keys.some(key => previousKeys.includes(key));
const removeKeys = (keys: string[]) => {
if (keys.length === 0) return;
normalizedKeys = normalizedKeys.filter(key => !keys.includes(key));
};
// 备注:统一处理两组图层的互斥关系,并优先保留当前触发的一组。
const resolveConflict = (
primaryKeys: string[],
secondaryKeys: string[],
preferredGroup: 'primary' | 'secondary' = 'primary'
) => {
if (!hasAny(primaryKeys) || !hasAny(secondaryKeys)) {
return;
}
if (triggerKey && checked !== false) {
if (primaryKeys.includes(triggerKey)) {
removeKeys(secondaryKeys);
return;
}
if (secondaryKeys.includes(triggerKey)) {
removeKeys(primaryKeys);
return;
}
}
const primaryWasChecked = hadAny(primaryKeys);
const secondaryWasChecked = hadAny(secondaryKeys);
if (primaryWasChecked && !secondaryWasChecked) {
removeKeys(secondaryKeys);
return;
}
if (!primaryWasChecked && secondaryWasChecked) {
removeKeys(primaryKeys);
return;
}
if (preferredGroup === 'primary') {
removeKeys(secondaryKeys);
} else {
removeKeys(primaryKeys);
}
};
resolveConflict(['rare_fish_point'], ['fish_along_point'], 'primary');
resolveConflict(
['stinfo', 'stinfo_video_point'],
['stinfo_ai_video_point'],
'secondary'
);
const facilityKeys = getLayerBranchKeys?.('facilities') || [];
const facilityBuiltKeys = getLayerBranchKeys?.('facilities_built') || [];
resolveConflict(facilityKeys, facilityBuiltKeys, 'primary');
return normalizedKeys;
};