from ...sync_system.strategy import DefaultSyncStrategy from ...sync_system.config import StrategyConfig, OrphanAction, UpdateDirection from ...sync_system.strategy_ops.compare_ops import normalized_data_for_compare, collect_payload_diffs from schemas.project_extensions.preparation import PreparationDetail, PostPreparation class PreparationSyncStrategy(DefaultSyncStrategy[PreparationDetail]): """ Preparation 同步策略(项目扩展)。 """ schema = PreparationDetail default_config = StrategyConfig( auto_bind=True, auto_bind_fields=["project_id", "code"], depend_fields={"project_id": "project"}, local_orphan_action=OrphanAction.NONE, remote_orphan_action=OrphanAction.NONE, update_direction=UpdateDirection.PUSH, ) def should_update_pair(self, source_node, target_node, *, source_data, target_data, data_id_map=None) -> bool: if target_data is None: return False # 这里只按 PostPreparation 可写字段判定是否需要 update,避免 response-only 字段差异触发空更新。 update_fields = set(PostPreparation.model_fields.keys()) source_payload = {key: value for key, value in source_data.items() if key in update_fields} target_payload = {key: value for key, value in target_data.items() if key in update_fields} normalized_source = normalized_data_for_compare(source_payload, data_id_map or {}) normalized_target = normalized_data_for_compare(target_payload, data_id_map or {}) diff_map = collect_payload_diffs(normalized_source, normalized_target, max_items=8) return bool(diff_map)