648540858
2022-01-13 f99c9cac6afc899c244aa66897cb3bf35a2da7d6
src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
@@ -23,10 +23,7 @@
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**    
 * @description:视频设备数据存储-jdbc实现
@@ -110,6 +107,7 @@
    */
   @Override
   public synchronized boolean create(Device device) {
      redisCatchStorage.updateDevice(device);
      return deviceMapper.add(device) > 0;
   }
@@ -128,10 +126,13 @@
      Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId());
      if (deviceByDeviceId == null) {
         device.setCreateTime(now);
         redisCatchStorage.updateDevice(device);
         return deviceMapper.add(device) > 0;
      }else {
         redisCatchStorage.updateDevice(device);
         return deviceMapper.update(device) > 0;
      }
   }
@@ -152,7 +153,7 @@
   }
   @Override
   public void updateChannels(String deviceId, List<DeviceChannel> channels) {
   public int updateChannels(String deviceId, List<DeviceChannel> channels) {
      List<DeviceChannel> addChannels = new ArrayList<>();
      List<DeviceChannel> updateChannels = new ArrayList<>();
      HashMap<String, DeviceChannel> channelsInStore = new HashMap<>();
@@ -185,13 +186,88 @@
               }
            }
         }
         int limitCount = 300;
         if (addChannels.size() > 0) {
            deviceChannelMapper.batchAdd(addChannels);
            if (addChannels.size() > limitCount) {
               for (int i = 0; i < addChannels.size(); i += limitCount) {
                  int toIndex = i + limitCount;
                  if (i + limitCount > addChannels.size()) {
                     toIndex = addChannels.size();
                  }
                  deviceChannelMapper.batchAdd(addChannels.subList(i, toIndex));
               }
            }else {
               deviceChannelMapper.batchAdd(addChannels);
            }
         }
         if (updateChannels.size() > 0) {
            deviceChannelMapper.batchUpdate(updateChannels);
            if (updateChannels.size() > limitCount) {
               for (int i = 0; i < updateChannels.size(); i += limitCount) {
                  int toIndex = i + limitCount;
                  if (i + limitCount > updateChannels.size()) {
                     toIndex = updateChannels.size();
                  }
                  deviceChannelMapper.batchUpdate(updateChannels.subList(i, toIndex));
               }
            }else {
               deviceChannelMapper.batchUpdate(updateChannels);
            }
         }
      }
      return addChannels.size() + updateChannels.size();
   }
   @Override
   public boolean resetChannels(String deviceId, List<DeviceChannel> deviceChannelList) {
      TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
      // 数据去重
      List<DeviceChannel> channels = new ArrayList<>();
      StringBuilder stringBuilder = new StringBuilder();
      if (deviceChannelList.size() > 1) {
         // 数据去重
         Set<String> gbIdSet = new HashSet<>();
         for (DeviceChannel deviceChannel : deviceChannelList) {
            if (!gbIdSet.contains(deviceChannel.getChannelId())) {
               gbIdSet.add(deviceChannel.getChannelId());
               channels.add(deviceChannel);
            }else {
               stringBuilder.append(deviceChannel.getChannelId() + ",");
            }
         }
      }else {
         channels = deviceChannelList;
      }
      if (stringBuilder.length() > 0) {
         logger.debug("[目录查询]收到的数据存在重复: {}" , stringBuilder);
      }
      try {
         int cleanChannelsResult = deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
         int limitCount = 1;
         boolean result = cleanChannelsResult < 0;
         if (!result && channels.size() > 0) {
            if (channels.size() > limitCount) {
               for (int i = 0; i < channels.size(); i += limitCount) {
                  int toIndex = i + limitCount;
                  if (i + limitCount > channels.size()) {
                     toIndex = channels.size();
                  }
                  result = result || deviceChannelMapper.batchAdd(channels.subList(i, toIndex)) < 0;
               }
            }else {
               result = result || deviceChannelMapper.batchAdd(channels) < 0;
            }
         }
         if (result) {
            //事务回滚
            dataSourceTransactionManager.rollback(transactionStatus);
         }
         dataSourceTransactionManager.commit(transactionStatus);     //手动提交
         return true;
      }catch (Exception e) {
         dataSourceTransactionManager.rollback(transactionStatus);
         return false;
      }
   }
   @Override
@@ -231,6 +307,11 @@
      PageHelper.startPage(page, count);
      List<DeviceChannel> all = deviceChannelMapper.queryChannels(deviceId, null, query, hasSubChannel, online);
      return new PageInfo<>(all);
   }
   @Override
   public List<DeviceChannel> queryChannelsByDeviceIdWithStartAndLimit(String deviceId, String query, Boolean hasSubChannel, Boolean online, int start, int limit) {
      return deviceChannelMapper.queryChannelsByDeviceIdWithStartAndLimit(deviceId, null, query, hasSubChannel, online, start, limit);
   }
   @Override
@@ -322,6 +403,7 @@
      }
      device.setOnline(1);
      logger.info("更新设备在线: " + deviceId);
      redisCatchStorage.updateDevice(device);
      return deviceMapper.update(device) > 0;
   }
@@ -337,6 +419,7 @@
      Device device = deviceMapper.getDeviceByDeviceId(deviceId);
      if (device == null) return false;
      device.setOnline(0);
      redisCatchStorage.updateDevice(device);
      return deviceMapper.update(device) > 0;
   }
@@ -679,7 +762,6 @@
               if (streamProxyItems == null) {
                  platformGbStreamMapper.add(streamPushItem);
               }
            }
         }
      }
@@ -738,4 +820,5 @@
   public StreamProxyItem getStreamProxyByAppAndStream(String app, String streamId) {
      return streamProxyMapper.selectOne(app, streamId);
   }
}