Ver código fonte

新增 添加修改删除设备时候同时更新地理位置数据表

zhaiyifei 11 meses atrás
pai
commit
2dd0adc576

+ 24 - 0
src/main/java/com/yunfeiyun/agmp/iot/common/service/IotDeviceGeoLocationService.java

@@ -0,0 +1,24 @@
+package com.yunfeiyun.agmp.iot.common.service;
+
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceGeoLocation;
+
+import java.util.List;
+
+public interface IotDeviceGeoLocationService {
+    /**
+     * 插入数据
+     */
+    public void insertBatchData(List<IotDeviceGeoLocation> dataList);
+
+    /**
+     * 更新数据
+     */
+    public void updateBatchData(List<IotDeviceGeoLocation> dataList);
+
+    public void insertOrUpdateBatchData(List<IotDevice> iotDeviceList);
+
+    public List<IotDeviceGeoLocation> selectByDevBidList(List<String> devBidList);
+
+    public void deleteByDevBidList(List<String> devBidList);
+}

+ 158 - 0
src/main/java/com/yunfeiyun/agmp/iot/common/service/impl/IotDeviceGeoLocationServiceImpl.java

@@ -0,0 +1,158 @@
+package com.yunfeiyun.agmp.iot.common.service.impl;
+
+import com.yunfeiyun.agmp.common.utils.DateUtils;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevGeoPoint;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotDeviceGeoLocation;
+import com.yunfeiyun.agmp.iot.common.domain.dto.MgUpdateBatchDto;
+import com.yunfeiyun.agmp.iot.common.service.IotDeviceGeoLocationService;
+import com.yunfeiyun.agmp.iot.common.service.MongoService;
+import com.yunfeiyun.agmp.iot.common.util.BigDecimalUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.*;
+
+@Service
+public class IotDeviceGeoLocationServiceImpl implements IotDeviceGeoLocationService {
+
+    @Autowired
+    private MongoService mongoService;
+
+    /**
+     * 插入或更新数据
+     */
+    @Override
+    public void insertBatchData(List<IotDeviceGeoLocation> dataList) {
+        if(!dataList.isEmpty()) {
+            mongoService.insertList(IotDeviceGeoLocation.class, dataList);
+        }
+    }
+
+    /**
+     * 更新数据
+     *
+     * @param dataList
+     */
+    @Override
+    public void updateBatchData(List<IotDeviceGeoLocation> dataList) {
+        List<MgUpdateBatchDto> updateBatchDtoList = new ArrayList<>();
+        for(IotDeviceGeoLocation item : dataList) {
+            String devGeoBid = item.getDevGeoBid();
+            IotDevGeoPoint geoPoint = item.getDevGeoLocation();
+            String devGeoUpdateddate = DateUtils.dateTimeNow();
+            String devtypeBid = item.getDevtypeBid();
+
+            Query query = new Query(Criteria.where("devGeoBid").is(devGeoBid));
+            Map<String, Object> updateField = new HashMap<>();
+            updateField.put("devGeoUpdateddate", devGeoUpdateddate);
+            updateField.put("devtypeBid", devtypeBid);
+            updateField.put("devGeoLocation", geoPoint);
+
+            MgUpdateBatchDto updateBatchDto = new MgUpdateBatchDto();
+            updateBatchDto.setQuery(query);
+            updateBatchDto.setUpdateField(updateField);
+            updateBatchDtoList.add(updateBatchDto);
+        }
+        if(!updateBatchDtoList.isEmpty()) {
+            mongoService.updateBatch(IotDeviceGeoLocation.class, updateBatchDtoList);
+        }
+    }
+
+    @Override
+    public void insertOrUpdateBatchData(List<IotDevice> iotDeviceList) {
+        List<IotDevice> dataList = new ArrayList<>();
+        List<String> devBidList = new ArrayList<>();
+        for(IotDevice iotDevice : iotDeviceList) {
+            BigDecimal devLng = iotDevice.getDevLngalign();
+            BigDecimal devLat = iotDevice.getDevLatalign();
+            if(devLng == null || devLat == null) {
+                devLng = iotDevice.getDevLng();
+                devLat = iotDevice.getDevLat();
+            }
+            if (devLng == null || devLat == null){
+                continue;
+            }
+
+            String devBid = iotDevice.getDevBid();
+            devBidList.add(devBid);
+            dataList.add(iotDevice);
+        }
+        Map<String, IotDeviceGeoLocation> iotDeviceGeoLocationMap = new HashMap<>();
+        if(!devBidList.isEmpty()) {
+            List<IotDeviceGeoLocation> iotDeviceGeoLocationList = selectByDevBidList(devBidList);
+            for(IotDeviceGeoLocation iotDeviceGeoLocation : iotDeviceGeoLocationList) {
+                iotDeviceGeoLocationMap.put(iotDeviceGeoLocation.getDevBid(), iotDeviceGeoLocation);
+            }
+        }
+        List<IotDeviceGeoLocation> updateList = new ArrayList<>();
+        List<IotDeviceGeoLocation> insertList = new ArrayList<>();
+        for(IotDevice iotDevice : dataList) {
+            BigDecimal devLng = iotDevice.getDevLngalign();
+            BigDecimal devLat = iotDevice.getDevLatalign();
+            if(devLng == null || devLat == null) {
+                devLng = iotDevice.getDevLng();
+                devLat = iotDevice.getDevLat();
+            }
+
+            String devBid = iotDevice.getDevBid();
+            IotDeviceGeoLocation iotDeviceGeoLocation = iotDeviceGeoLocationMap.get(devBid);
+            if (iotDeviceGeoLocation == null) {
+                iotDeviceGeoLocation = new IotDeviceGeoLocation();
+                iotDeviceGeoLocation.setDevGeoBid(iotDeviceGeoLocation.getUUId());
+                iotDeviceGeoLocation.setTid(iotDevice.getTid());
+                iotDeviceGeoLocation.setDevBid(devBid);
+                iotDeviceGeoLocation.setDevtypeBid(iotDevice.getDevtypeBid());
+                iotDeviceGeoLocation.setDevGeoCreateddate(DateUtils.dateTimeNow());
+                iotDeviceGeoLocation.setDevGeoUpdateddate(DateUtils.dateTimeNow());
+                IotDevGeoPoint geoPoint = new IotDevGeoPoint();
+                geoPoint.setType("Point");
+
+                double[] coordinates = new double[2];
+                coordinates[0] = BigDecimalUtil.toDouble(devLng);
+                coordinates[1] = BigDecimalUtil.toDouble(devLat);
+                geoPoint.setCoordinates(coordinates);
+                iotDeviceGeoLocation.setDevGeoLocation(geoPoint);
+
+                insertList.add(iotDeviceGeoLocation);
+            }else{
+                double[] coordinates = iotDeviceGeoLocation.getDevGeoLocation().getCoordinates();
+                BigDecimal devLngOld = BigDecimalUtil.format(coordinates[0]);
+                BigDecimal devLatOld = BigDecimalUtil.format(coordinates[1]);
+                if(!devLng.equals(devLngOld) || !devLat.equals(devLatOld)) {
+                    coordinates[0] = BigDecimalUtil.toDouble(devLng);
+                    coordinates[1] = BigDecimalUtil.toDouble(devLat);
+
+                    iotDeviceGeoLocation.getDevGeoLocation().setCoordinates(coordinates);
+                    iotDeviceGeoLocation.setDevGeoUpdateddate(DateUtils.dateTimeNow());
+                    iotDeviceGeoLocation.setDevtypeBid(iotDevice.getDevtypeBid());
+
+                    updateList.add(iotDeviceGeoLocation);
+                }
+            }
+        }
+        if(!updateList.isEmpty()) {
+            updateBatchData(updateList);
+        }
+        if(!insertList.isEmpty()) {
+            insertBatchData(insertList);
+        }
+    }
+
+    @Override
+    public List<IotDeviceGeoLocation> selectByDevBidList(List<String> devBidList) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("list_devBid", devBidList);
+        return mongoService.findAll(IotDeviceGeoLocation.class, null);
+    }
+
+    @Override
+    public void deleteByDevBidList(List<String> devBidList) {
+        if(!devBidList.isEmpty()) {
+            mongoService.removeAllByBatch("devBid", devBidList, "IotDeviceGeoLocation");
+        }
+    }
+}