package org.hswebframework.web.system.authorization.defaults.service;

import org.hswebframework.ezorm.rdb.mapping.ReactiveDelete;
import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.hswebframework.web.system.authorization.api.entity.DimensionUserEntity;
import org.hswebframework.web.system.authorization.api.event.ClearUserAuthorizationCacheEvent;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collection;
import java.util.stream.Collectors;

public class DefaultDimensionUserService extends GenericReactiveCrudService<DimensionUserEntity, String> {

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Override
    public Mono<SaveResult> save(Publisher<DimensionUserEntity> entityPublisher) {
        return Flux.from(entityPublisher)
                .doOnNext(entity -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(entity.getUserId())))
                .as(super::save);
    }

    @Override
    public Mono<Integer> updateById(String id, Mono<DimensionUserEntity> entityPublisher) {
        return entityPublisher
                .doOnNext(entity -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(entity.getUserId())))
                .as(e -> super.updateById(id, e));
    }

    @Override
    public Mono<Integer> insert(Publisher<DimensionUserEntity> entityPublisher) {
        return Flux.from(entityPublisher)
                .doOnNext(entity -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(entity.getUserId())))
                .as(super::insert);
    }

    @Override
    public Mono<Integer> insertBatch(Publisher<? extends Collection<DimensionUserEntity>> entityPublisher) {
        return Flux.from(entityPublisher)
                .doOnNext(entity -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(entity
                        .stream()
                        .map(DimensionUserEntity::getUserId)
                        .collect(Collectors.toSet()))))
                .as(super::insertBatch);
    }

    @Override
    public Mono<Integer> deleteById(Publisher<String> idPublisher) {
        return findById(Flux.from(idPublisher))
                .doOnNext(entity -> eventPublisher.publishEvent(ClearUserAuthorizationCacheEvent.of(entity.getUserId())))
                .map(DimensionUserEntity::getId)
                .as(super::deleteById);
    }

    @Override
    @SuppressWarnings("all")
    public ReactiveUpdate<DimensionUserEntity> createUpdate() {
        return super.createUpdate()
                .onExecute((update, r) -> r.doOnSuccess(i -> {
                    createQuery()
                            .select(DimensionUserEntity::getUserId)
                            .setParam(update.toQueryParam())
                            .fetch()
                            .map(DimensionUserEntity::getUserId)
                            .collectList()
                            .map(ClearUserAuthorizationCacheEvent::of)
                            .subscribe();
                }));
    }

    @Override
    @SuppressWarnings("all")
    public ReactiveDelete createDelete() {
        return super.createDelete()
                .onExecute((delete, r) -> r.doOnSuccess(i -> {
                    createQuery()
                            .select(DimensionUserEntity::getUserId)
                            .setParam(delete.toQueryParam())
                            .fetch()
                            .map(DimensionUserEntity::getUserId)
                            .collectList()
                            .map(ClearUserAuthorizationCacheEvent::of)
                            .subscribe();
                }));
    }
}

@@ -64,16 +64,50 @@
 rvice;%0A%0A
+import lombok.extern.slf4j.Slf4j;%0A
 import o
@@ -154,24 +154,24 @@
 tiveDelete;%0A
-
 import org.h
@@ -532,16 +532,95 @@
 eEvent;%0A
+import org.hswebframework.web.system.authorization.api.event.UserDeletedEvent;%0A
 import o
@@ -770,24 +770,80 @@
 tPublisher;%0A
+import org.springframework.context.event.EventListener;%0A
 import react
@@ -969,16 +969,23 @@
 ctors;%0A%0A
+@Slf4j%0A
 public c
@@ -1099,16 +1099,16 @@
 towired%0A
-
     priv
@@ -1154,16 +1154,319 @@
 isher;%0A%0A
+    @EventListener%0A    public void handleUserDeleteEntity(UserDeletedEvent event) %7B%0A        createDelete()%0A                .where(DimensionUserEntity::getUserId, event.getUser().getId())%0A                .execute()%0A                .subscribe(i -%3E log.debug(%22user deleted,clear user dimension!%22));%0A    %7D%0A%0A
     @Ove

删除用户时，清空用户维度信息