Parcourir la source

fixed 标签、地理位置联动更新

18339543638 il y a 4 ans
Parent
commit
7d85b0112d

+ 89 - 44
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/DeviceTagsService.java

@@ -9,7 +9,9 @@ import org.jetlinks.community.device.entity.geo.GeoProperty;
 import org.jetlinks.community.device.entity.geo.Geometry;
 import org.jetlinks.core.metadata.types.GeoType;
 import org.springframework.stereotype.Service;
+import reactor.core.Disposable;
 import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
 
 /**
  * @author lifang
@@ -36,8 +38,9 @@ public class DeviceTagsService  extends GenericReactiveCrudService<DeviceTagEnti
                 .flatMap(count->{
                     if(count==0){
                         return getRepository()
-                                .save(entity)
-                                .then();
+                            .save(entity)
+                            .doOnNext(ignore->updateTagGeo(entity))
+                            .then();
                     }
                     else if(!entity.isAutoUpdate()){
                         //存在且不能自动更新
@@ -48,51 +51,52 @@ public class DeviceTagsService  extends GenericReactiveCrudService<DeviceTagEnti
                             .where(DeviceTagEntity::getDeviceId,entity.getDeviceId())
                             .set(DeviceTagEntity::getValue,entity.getValue())
                             .execute()
+                            .doOnNext(ignore->updateTagGeo(entity))
                             .then();
                     }
                 })
-                .doOnNext(ignore->{
-                    if (judgementGeo(entity)) {
-                        GeoProperty geoProperty = new GeoProperty();
-                        Geometry geometry = new Geometry();
-                        String[] split = entity.getValue().split(",");
-                        geometry.getCoordinates().add(split[0]);
-                        geometry.getCoordinates().add(split[1]);
-                        geometry.setType("Point");
-                        instanceService.findById(entity.getDeviceId())
-                            .flatMap(device->{
-                                geoProperty.setFrom("tag");
-                                geoProperty.setFromId(entity.getId());
-                                geoProperty.setObjectId(device.getId());
-                                geoProperty.setObjectType("device");
-                                geoProperty.setDeviceName(device.getName());
-                                return productService.findById(device.getProductId());
-                            })
-                            .doOnNext(product->{
-                                geoProperty.setProductName(product.getName());
-                                geoProperty.setProductId(product.getId());
-                                geoProperty.setGeometry(geometry);
-                            })
-                            .subscribe(ignore0->{
-                                propertyRepository.createQuery()
-                                    .where(GeoProperty::getFrom,"tag")
-                                    .where(GeoProperty::getFromId,entity.getId())
-                                    .count()
-                                    .flatMap(count->{
-                                        if(count==0){
-                                            return propertyRepository.save(geoProperty);
-                                        }else {
-                                            return propertyRepository.createUpdate()
-                                                .where(GeoProperty::getFrom,"tag")
-                                                .where(GeoProperty::getFromId,entity.getId())
-                                                .set(GeoProperty::getGeometry,geometry)
-                                                .execute();
-                                        }
-                                    })
-                                    .subscribe();
-                            });
-                    }
-                })
+//                .doOnNext(ignore->{
+//                    if (judgementGeo(entity)) {
+//                        GeoProperty geoProperty = new GeoProperty();
+//                        Geometry geometry = new Geometry();
+//                        String[] split = entity.getValue().split(",");
+//                        geometry.getCoordinates().add(split[0]);
+//                        geometry.getCoordinates().add(split[1]);
+//                        geometry.setType("Point");
+//                        instanceService.findById(entity.getDeviceId())
+//                            .flatMap(device->{
+//                                geoProperty.setFrom("tag");
+//                                geoProperty.setFromId(entity.getId());
+//                                geoProperty.setObjectId(device.getId());
+//                                geoProperty.setObjectType("device");
+//                                geoProperty.setDeviceName(device.getName());
+//                                return productService.findById(device.getProductId());
+//                            })
+//                            .doOnNext(product->{
+//                                geoProperty.setProductName(product.getName());
+//                                geoProperty.setProductId(product.getId());
+//                                geoProperty.setGeometry(geometry);
+//                            })
+//                            .subscribe(ignore0->{
+//                                propertyRepository.createQuery()
+//                                    .where(GeoProperty::getFrom,"tag")
+//                                    .where(GeoProperty::getFromId,entity.getId())
+//                                    .count()
+//                                    .flatMap(count->{
+//                                        if(count==0){
+//                                            return propertyRepository.save(geoProperty);
+//                                        }else {
+//                                            return propertyRepository.createUpdate()
+//                                                .where(GeoProperty::getFrom,"tag")
+//                                                .where(GeoProperty::getFromId,entity.getId())
+//                                                .set(GeoProperty::getGeometry,geometry)
+//                                                .execute();
+//                                        }
+//                                    })
+//                                    .subscribe();
+//                            });
+//                    }
+//                })
                 .then();
     }
 
@@ -100,4 +104,45 @@ public class DeviceTagsService  extends GenericReactiveCrudService<DeviceTagEnti
     public boolean judgementGeo(DeviceTagEntity entity){
         return GeoType.ID.equals(entity.getType())&&"coordinate".equals(entity.getKey());
     }
+
+    public Disposable updateTagGeo(DeviceTagEntity entity){
+        GeoProperty geoProperty = new GeoProperty();
+        Geometry geometry = new Geometry();
+        String[] split = entity.getValue().split(",");
+        geometry.getCoordinates().add(split[0]);
+        geometry.getCoordinates().add(split[1]);
+        geometry.setType("Point");
+        return instanceService.findById(entity.getDeviceId())
+            .flatMap(device->{
+                geoProperty.setFrom("tag");
+                geoProperty.setFromId(entity.getId());
+                geoProperty.setObjectId(device.getId());
+                geoProperty.setObjectType("device");
+                geoProperty.setDeviceName(device.getName());
+                return productService.findById(device.getProductId());
+            })
+            .doOnNext(product->{
+                geoProperty.setProductName(product.getName());
+                geoProperty.setProductId(product.getId());
+                geoProperty.setGeometry(geometry);
+            })
+            .subscribe(ignore0->{
+                propertyRepository.createQuery()
+                    .where(GeoProperty::getFrom,"tag")
+                    .where(GeoProperty::getFromId,entity.getId())
+                    .count()
+                    .flatMap(count->{
+                        if(count==0){
+                            return propertyRepository.save(geoProperty);
+                        }else {
+                            return propertyRepository.createUpdate()
+                                .where(GeoProperty::getFrom,"tag")
+                                .where(GeoProperty::getFromId,entity.getId())
+                                .set(GeoProperty::getGeometry,geometry)
+                                .execute();
+                        }
+                    })
+                    .subscribe();
+            });
+    }
 }