Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(场景联动):修复设备选择器条件会无限叠加问题 #334

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(场景联动):修复设备选择器条件会无线叠加问题
  • Loading branch information
bestfeng1020 committed Jul 11, 2023
commit 8b6bf174ad58661ddd5d18e048d0fc9040aca5d5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

public class CompositeDeviceSelectorProvider implements DeviceSelectorProvider {

Expand Down Expand Up @@ -49,4 +50,28 @@ public <T extends Conditional<T>> Mono<NestConditional<T>> applyCondition(Device
}
return handler == null ? Mono.just(conditional) : handler;
}

@Override
public <T extends Conditional<T>> BiFunction<
NestConditional<T>,
Map<String, Object>,
Mono<NestConditional<T>>> createLazy(DeviceSelectorSpec source) {

BiFunction<NestConditional<T>, Map<String, Object>, Mono<NestConditional<T>>> function = null;

for (SelectorValue selectorValue : source.getSelectorValues()) {
DeviceSelectorSpec spec = FastBeanCopier.copy(selectorValue.getValue(), new DeviceSelectorSpec());
DeviceSelectorProvider provider = DeviceSelectorProviders.getProviderNow(spec.getSelector());

BiFunction<NestConditional<T>, Map<String, Object>, Mono<NestConditional<T>>> that = provider.createLazy(spec);
if (function == null) {
function = that;
} else {
BiFunction<NestConditional<T>, Map<String, Object>, Mono<NestConditional<T>>> temp = function;

function = (condition, ctx) -> temp.apply(condition, ctx).flatMap(ctd -> that.apply(condition, ctx));
}
}
return function == null ? (condition, ignore) -> Mono.just(condition) : function;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -24,10 +25,9 @@ <T extends Conditional<T>> Mono<NestConditional<T>> applyCondition(DeviceSelecto
NestConditional<T> conditional);


default <T extends Conditional<T>> Function<Map<String, Object>, Mono<NestConditional<T>>> createLazy(
DeviceSelectorSpec source,
Supplier<NestConditional<T>> conditionalSupplier) {
return ctx -> applyCondition(source, ctx, conditionalSupplier.get());
default <T extends Conditional<T>> BiFunction<NestConditional<T>, Map<String, Object>, Mono<NestConditional<T>>> createLazy(
DeviceSelectorSpec source) {
return (condition, ctx) -> applyCondition(source, ctx, condition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -62,15 +63,19 @@ public DeviceSelector createSelector(DeviceSelectorSpec spec) {
.flatMap(registry::getDevice);
};
}
Function<Map<String, Object>, Mono<NestConditional<ReactiveQuery<DeviceInstanceEntity>>>> lazy = provider
.createLazy(spec,
Lazy.of(() -> deviceRepository
.createQuery()
.select(DeviceInstanceEntity::getId)
.nest()));

return context -> lazy
.apply(context)
BiFunction<NestConditional<ReactiveQuery<DeviceInstanceEntity>>,
Map<String, Object>,
Mono<NestConditional<ReactiveQuery<DeviceInstanceEntity>>>> function = provider
.createLazy(spec);

return context -> function
.apply(deviceRepository
.createQuery()
.select(DeviceInstanceEntity::getId)
.nest(),
context
)
.flatMapMany(ctd -> ctd.end().fetch().map(DeviceInstanceEntity::getId))
.flatMap(registry::getDevice);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetlinks.community.device.function;

import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections.CollectionUtils;
import org.hswebframework.ezorm.core.Conditional;
import org.hswebframework.ezorm.core.NestConditional;
import org.jetlinks.core.things.relation.ObjectSpec;
Expand Down Expand Up @@ -81,8 +81,13 @@ public <T extends Conditional<T>> Mono<NestConditional<T>> applyCondition(Flux<S
}))
.map(RelationObject::getId)
.collectList()
.filter(CollectionUtils::isNotEmpty)
.doOnNext(deviceIdList -> conditional.in(DeviceInstanceEntity::getId, deviceIdList))
.doOnNext(deviceIdList -> {
if (CollectionUtils.isNotEmpty(deviceIdList)){
conditional.in(DeviceInstanceEntity::getId, deviceIdList);
}else {
conditional.isNull(DeviceInstanceEntity::getId);
}
})
.thenReturn(conditional);
}
}