|
|
@@ -1,15 +1,13 @@
|
|
|
package org.jetlinks.community.device.service;
|
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
|
|
|
-import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
|
|
|
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
|
|
+import org.hswebframework.web.exception.BusinessException;
|
|
|
import org.jetlinks.community.device.entity.DeviceTagEntity;
|
|
|
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.reactivestreams.Publisher;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
@@ -28,58 +26,74 @@ public class DeviceTagsService extends GenericReactiveCrudService<DeviceTagEnti
|
|
|
private final LocalDeviceProductService productService;
|
|
|
private final ReactiveRepository<GeoProperty, String> propertyRepository;
|
|
|
|
|
|
- public Mono<Void> saveOrUpdate(Publisher<DeviceTagEntity> entityPublisher) {
|
|
|
- return Mono.from(entityPublisher)
|
|
|
- //判断地理位置
|
|
|
- .doOnNext(entity->{
|
|
|
- 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(ignore->{
|
|
|
- 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();
|
|
|
- });
|
|
|
- }
|
|
|
- })
|
|
|
- .flatMap(entity->{
|
|
|
- if(StrUtil.isNotEmpty(entity.getId())){
|
|
|
- return this.updateById(entity.getId(),Mono.just(entity));
|
|
|
- }
|
|
|
- return this.getRepository().save(entity);
|
|
|
- })
|
|
|
- .then();
|
|
|
+ public Mono<Void> saveOrUpdate(DeviceTagEntity entity) {
|
|
|
+ return
|
|
|
+ //先判断是否存在
|
|
|
+ this.createQuery()
|
|
|
+ .where(DeviceTagEntity::getKey,entity.getKey())
|
|
|
+ .where(DeviceTagEntity::getDeviceId,entity.getDeviceId())
|
|
|
+ .count()
|
|
|
+ .flatMap(count->{
|
|
|
+ if(count==0){
|
|
|
+ return getRepository()
|
|
|
+ .save(entity)
|
|
|
+ .then();
|
|
|
+ }
|
|
|
+ else if(!entity.isAutoUpdate()){
|
|
|
+ //存在且不能自动更新
|
|
|
+ throw new BusinessException(String.format("标签[%s]已存在",entity.getKey()));
|
|
|
+ }else {
|
|
|
+ return this.createUpdate()
|
|
|
+ .where(DeviceTagEntity::getKey,entity.getKey())
|
|
|
+ .where(DeviceTagEntity::getDeviceId,entity.getDeviceId())
|
|
|
+ .set(DeviceTagEntity::getValue,entity.getValue())
|
|
|
+ .execute()
|
|
|
+ .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();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then();
|
|
|
}
|
|
|
|
|
|
//判断是否为设备坐标标签
|