Pārlūkot izejas kodu

fix 新增规则引擎

18339543638 3 gadi atpakaļ
vecāks
revīzija
5f47d144b6

+ 25 - 15
jetlinks-manager/rule-engine-manager/src/main/java/org/jetlinks/community/rule/engine/service/RuleInstanceService.java

@@ -1,7 +1,9 @@
 package org.jetlinks.community.rule.engine.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.ezorm.core.param.QueryParam;
 import org.hswebframework.web.api.crud.entity.PagerResult;
@@ -16,6 +18,7 @@ import org.jetlinks.community.rule.engine.event.handler.RuleEngineLoggerIndexPro
 import org.jetlinks.core.AbstractClusterServer;
 import org.jetlinks.core.cluster.ClusterManager;
 import org.jetlinks.core.cluster.ClusterService;
+import org.jetlinks.core.utils.IdUtils;
 import org.jetlinks.rule.engine.api.RuleData;
 import org.jetlinks.rule.engine.api.RuleEngine;
 import org.jetlinks.rule.engine.api.model.RuleEngineModelParser;
@@ -148,21 +151,28 @@ public class RuleInstanceService extends GenericReactiveCrudService<RuleInstance
     }
 
     public Mono<Void> update(RuleInstanceEntity instance) {
-        return this.findById(instance.getId())
-            .flatMap(oldInstance->{
-                if(ObjectUtil.equal(instance.getModelMeta(),oldInstance.getModelMeta())){
-                    return this.update(instance);
-                }else {
-                    instance.setState(oldInstance.getState());
-                    RuleModel model = instance.toRule(modelParser);
-                    return Flux.fromIterable(new ScheduleJobCompiler(instance.getId(), model).compile())
-                        .flatMap(scheduler::schedule)
-                        .collectList()
-                        .flatMapIterable(Function.identity())
-                        .filter(ignore->instance.getState()==RuleInstanceState.started)
-                        .flatMap(Task::start)
-                        .then();
-                }
+        return StrUtil.isNotEmpty(instance.getId())?
+            this.findById(instance.getId())
+                .flatMap(oldInstance->{
+                    if(ObjectUtil.equal(instance.getModelMeta(),oldInstance.getModelMeta())){
+                        return this.update(instance);
+                    }else {
+                        instance.setState(oldInstance.getState());
+                        RuleModel model = instance.toRule(modelParser);
+                        return Flux.fromIterable(new ScheduleJobCompiler(instance.getId(), model).compile())
+                            .flatMap(scheduler::schedule)
+                            .collectList()
+                            .flatMapIterable(Function.identity())
+                            .filter(ignore->instance.getState()==RuleInstanceState.started)
+                            .flatMap(Task::start)
+                            .then();
+                    }
+                }):
+            Mono.defer(()->{
+                instance.setId(String.valueOf(IdUtil.getSnowflake().nextId()));
+                return this.save(instance)
+                    .then(doStart(instance));
             });
+
     }
 }