class PermissionSynchronization implements CommandLineRunner {

    private final ReactiveRepository<PermissionEntity, String> permissionRepository;

    private final AuthorizeDefinitionCustomizer customizer;

    private final MergedAuthorizeDefinition definition = new MergedAuthorizeDefinition();

    private final Map<String, List<OptionalField>> entityFieldsMapping = new HashMap<>();

    public PermissionSynchronization(ReactiveRepository<PermissionEntity, String> permissionRepository,
                                     AuthorizeDefinitionCustomizer customizer) {
        this.permissionRepository = permissionRepository;
        this.customizer = customizer;
    }

    @EventListener
    public void handleResourceParseEvent(AuthorizeDefinitionInitializedEvent event) {
        definition.merge(event.getAllDefinition());
        for (AuthorizeDefinition authorizeDefinition : event.getAllDefinition()) {
            if (authorizeDefinition.getResources().getResources().isEmpty()) {
                continue;
            }
            String id = authorizeDefinition.getResources().getResources().iterator().next().getId();
            if (entityFieldsMapping.containsKey(id)) {
                return;
            }
            if (authorizeDefinition instanceof AopAuthorizeDefinition) {
                Class<?> target = ((AopAuthorizeDefinition) authorizeDefinition).getTargetClass();
                if (ReactiveQueryController.class.isAssignableFrom(target)
                        || ReactiveServiceQueryController.class.isAssignableFrom(target)) {
                    Class<?> entity = ClassUtils.getGenericType(target);
                    if (Entity.class.isAssignableFrom(entity)) {
                        Set<OptionalField> fields = new HashSet<>();
                        ReflectionUtils.doWithFields(entity, field -> {
                            if (null != field.getAnnotation(Column.class) && !"id".equals(field.getName())) {
                                OptionalField optionalField = new OptionalField();
                                optionalField.setName(field.getName());
                                Optional.ofNullable(field.getAnnotation(Schema.class))
                                        .map(Schema::description)
                                        .ifPresent(optionalField::setDescribe);
                                fields.add(optionalField);
                            }
                        });
                        entityFieldsMapping.put(id, new ArrayList<>(fields));
                    }
                }
            }
        }
    }

    public static PermissionEntity convert(Map<String, PermissionEntity> old, ResourceDefinition definition,Map<String, List<OptionalField>>  entityFieldsMapping) {<FILL_FUNCTION_BODY>}

    @Override
    public void run(String... args) throws Exception {
        if (definition.getResources().isEmpty()) {
            return;
        }
        customizer.custom(definition);

        permissionRepository
                .createQuery()
                .fetch()
                .collect(Collectors.toMap(PermissionEntity::getId, Function.identity()))
                .flatMap(group -> Flux.fromIterable(definition.getResources())
                        .map(d -> PermissionSynchronization.convert(group, d,entityFieldsMapping))
                        .as(permissionRepository::save))
                .doOnError(err -> log.warn("sync permission error", err))
                .subscribe(l -> {
                    log.info("sync permission success:{}", l);
                });

    }
}

        PermissionEntity entity = old.getOrDefault(definition.getId(), PermissionEntity.builder()
                .name(definition.getName())
                .describe(definition.getDescription())
                .status((byte) 1)
                .build());
        entity.setId(definition.getId());


        if (CollectionUtils.isEmpty(entity.getOptionalFields())) {
            entity.setOptionalFields(entityFieldsMapping.get(entity.getId()));
        }

        Map<String, ActionEntity> oldAction = new HashMap<>();
        if (entity.getActions() != null) {
            entity.getActions().forEach(a -> oldAction.put(a.getAction(), a));
        }

        for (ResourceActionDefinition definitionAction : definition.getActions()) {
            ActionEntity action = oldAction.getOrDefault(definitionAction.getId(), ActionEntity
                    .builder()
                    .action(definitionAction.getId())
                    .name(definitionAction.getName())
                    .describe(definitionAction.getName())
                    .build());
            Map<String, Object> properties = Optional.ofNullable(action.getProperties()).orElse(new HashMap<>());
            @SuppressWarnings("all")
            Set<Object> types = (Set)Optional.of(properties.computeIfAbsent("supportDataAccessTypes", t -> new HashSet<>()))
                    .filter(Collection.class::isInstance)
                    .map(Collection.class::cast)
                    .map(HashSet::new)
                    .orElseGet(HashSet::new);

            types.addAll(definitionAction.getDataAccess().getDataAccessTypes().stream().map(DataAccessTypeDefinition::getId).collect(Collectors.toSet()));
            action.setProperties(properties);
            oldAction.put(action.getAction(), action);
        }
        entity.setActions(new ArrayList<>(oldAction.values()));


        return entity;
    
<no_super_class>