|
|
@@ -1,33 +1,72 @@
|
|
|
package com.usky.sas.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.usky.common.core.bean.CommonPage;
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
import com.usky.common.mybatis.core.AbstractCrudService;
|
|
|
+import com.usky.sas.domain.SasAlarsasGroupType;
|
|
|
import com.usky.sas.domain.SasDevice;
|
|
|
+import com.usky.sas.domain.SasGis;
|
|
|
+import com.usky.sas.domain.SasConfig;
|
|
|
+import com.usky.sas.domain.SasVideoMonitorGroupType;
|
|
|
+import com.usky.sas.enums.SystemTypeCodeEnum;
|
|
|
+import com.usky.sas.mapper.SasAlarsasGroupTypeMapper;
|
|
|
import com.usky.sas.mapper.SasDeviceMapper;
|
|
|
+import com.usky.sas.mapper.SasGisMapper;
|
|
|
+import com.usky.sas.mapper.SasVideoMonitorGroupTypeMapper;
|
|
|
+import com.usky.sas.service.SasConfigService;
|
|
|
import com.usky.sas.service.SasDeviceService;
|
|
|
-import com.usky.sas.service.vo.DeviceConfigPageRequest;
|
|
|
-import com.usky.sas.service.vo.DeviceConfigSaveRequest;
|
|
|
-import com.usky.sas.service.vo.DeviceConfigVO;
|
|
|
-import com.usky.sas.service.vo.DeviceHeartbeatRequest;
|
|
|
-import com.usky.sas.service.vo.DeviceHeartbeatResponse;
|
|
|
+import com.usky.sas.service.dto.agbox.AgboxChannelListVo;
|
|
|
+import com.usky.sas.service.dto.agbox.AgboxDeviceListVo;
|
|
|
+import com.usky.sas.service.dto.agbox.JsonRpcRequest;
|
|
|
+import com.usky.sas.service.vo.*;
|
|
|
+import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
|
|
+import cn.afterturn.easypoi.excel.entity.ImportParams;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 设备信息 服务实现
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class SasDeviceServiceImpl extends AbstractCrudService<SasDeviceMapper, SasDevice>
|
|
|
implements SasDeviceService {
|
|
|
|
|
|
+ private static final String AGBOX_DEVICE_PATH = "/agbox/device";
|
|
|
+ private static final int MAX_RETRIES = 3;
|
|
|
+ private static final int HTTP_TIMEOUT_MS = 30000;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SasAlarsasGroupTypeMapper alarsasGroupTypeMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SasVideoMonitorGroupTypeMapper videoMonitorGroupTypeMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SasConfigService sasConfigService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SasGisMapper sasGisMapper;
|
|
|
+
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
@Override
|
|
|
public CommonPage<DeviceHeartbeatResponse> heartbeatPage(DeviceHeartbeatRequest request) {
|
|
|
IPage<SasDevice> page = new Page<>(request.getCurrent(), request.getSize());
|
|
|
@@ -83,7 +122,9 @@ public class SasDeviceServiceImpl extends AbstractCrudService<SasDeviceMapper, S
|
|
|
wrapper.orderByDesc("create_time");
|
|
|
page = this.page(page, wrapper);
|
|
|
|
|
|
- List<DeviceConfigVO> list = page.getRecords().stream().map(this::toConfigVO).collect(Collectors.toList());
|
|
|
+ List<DeviceConfigVO> list = page.getRecords().stream()
|
|
|
+ .map(this::toConfigVOEnriched)
|
|
|
+ .collect(Collectors.toList());
|
|
|
return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());
|
|
|
}
|
|
|
|
|
|
@@ -115,6 +156,430 @@ public class SasDeviceServiceImpl extends AbstractCrudService<SasDeviceMapper, S
|
|
|
this.removeById(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void deleteConfigBatch(List<String> ids) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.removeByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DeviceDetailVO getDetail(String id) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ DeviceDetailVO vo = new DeviceDetailVO();
|
|
|
+ vo.setId(device.getId());
|
|
|
+ vo.setDeviceId(device.getDeviceId());
|
|
|
+ vo.setChannel(device.getChannel());
|
|
|
+ vo.setDeviceType(device.getDeviceType());
|
|
|
+ vo.setDeviceTypeName(deviceTypeName(device.getDeviceType()));
|
|
|
+ vo.setIpAddr(device.getIpAddr());
|
|
|
+ vo.setPort(device.getPort());
|
|
|
+ vo.setUsername(device.getUsername());
|
|
|
+ vo.setShield(device.getShield());
|
|
|
+ vo.setVillageCode(device.getVillageCode());
|
|
|
+ vo.setHouseCode(device.getHouseCode());
|
|
|
+ vo.setAddress(device.getAddress());
|
|
|
+ vo.setVideoGroupType(device.getVideoGroupType());
|
|
|
+ vo.setVideoGroupTypeName(groupName(device.getVideoGroupType(), true));
|
|
|
+ vo.setAlarmGroupType(device.getAlarsasGroupType());
|
|
|
+ vo.setAlarmGroupTypeName(groupName(device.getAlarsasGroupType(), false));
|
|
|
+ vo.setNote(device.getNote());
|
|
|
+ vo.setBindingIpc(device.getBindingIpc());
|
|
|
+ vo.setIsBinding(device.getIsBinding());
|
|
|
+ vo.setCreateTime(device.getCreateTime());
|
|
|
+ vo.setUpdateTime(device.getUpdateTime());
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void setAlarmGroup(String id, String alarmGroupType) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ device.setAlarsasGroupType(alarmGroupType);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void setAlarmGroupBatch(List<String> ids, String alarmGroupType) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String id : ids) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device != null) {
|
|
|
+ device.setAlarsasGroupType(alarmGroupType);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void setVideoGroup(String id, String videoGroupType) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ device.setVideoGroupType(videoGroupType);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void setVideoGroupBatch(List<String> ids, String videoGroupType) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String id : ids) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device != null) {
|
|
|
+ device.setVideoGroupType(videoGroupType);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<DeviceIPCItemVO> getNotBindIPCList() {
|
|
|
+ LambdaQueryWrapper<SasDevice> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(SasDevice::getDeviceType, SystemTypeCodeEnum.video.getCode())
|
|
|
+ .and(w -> w.isNull(SasDevice::getIsBinding).or().eq(SasDevice::getIsBinding, false));
|
|
|
+ List<SasDevice> list = this.list(wrapper);
|
|
|
+ if (list == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return list.stream().map(d -> {
|
|
|
+ DeviceIPCItemVO vo = new DeviceIPCItemVO();
|
|
|
+ vo.setId(d.getId());
|
|
|
+ vo.setDeviceId(d.getDeviceId());
|
|
|
+ vo.setChannel(d.getChannel());
|
|
|
+ vo.setIpAddr(d.getIpAddr());
|
|
|
+ vo.setNote(d.getNote());
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void bindingIPC(String id, String ipcId) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ipcId != null && ipcId.equals(device.getBindingIpc())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ device.setBindingIpc(ipcId);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ SasDevice ipc = this.getById(ipcId);
|
|
|
+ if (ipc != null) {
|
|
|
+ ipc.setIsBinding(true);
|
|
|
+ ipc.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(ipc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void unbindIPC(String id) {
|
|
|
+ SasDevice device = this.getById(id);
|
|
|
+ if (device == null || device.getBindingIpc() == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String ipcId = device.getBindingIpc();
|
|
|
+ device.setBindingIpc(null);
|
|
|
+ device.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(device);
|
|
|
+ SasDevice ipc = this.getById(ipcId);
|
|
|
+ if (ipc != null) {
|
|
|
+ ipc.setIsBinding(false);
|
|
|
+ ipc.setUpdateTime(LocalDateTime.now());
|
|
|
+ this.updateById(ipc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void syncAgboxDevice() {
|
|
|
+ SasConfig config = sasConfigService.getConfig();
|
|
|
+ if (config == null || config.getHost() == null || config.getHost().isEmpty()) {
|
|
|
+ log.warn("同步 AG 设备:未配置 Agbox 或 host 为空,跳过");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String baseUrl = buildAgboxBaseUrl(config.getHost(), config.getPort());
|
|
|
+ String key = config.getKeyds() != null ? config.getKeyds() : "";
|
|
|
+ for (SystemTypeCodeEnum type : SystemTypeCodeEnum.values()) {
|
|
|
+ String deviceUrl = baseUrl + AGBOX_DEVICE_PATH + "/" + type.getAgboxType();
|
|
|
+ try {
|
|
|
+ AgboxDeviceListVo listVo = postDeviceList(deviceUrl, key);
|
|
|
+ if (listVo == null || listVo.getResult() == null || listVo.getResult().getDeviceList() == null
|
|
|
+ || listVo.getResult().getDeviceList().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (AgboxDeviceListVo.AgboxDeviceInfo dev : listVo.getResult().getDeviceList()) {
|
|
|
+ if (dev.getDeviceId() == null || dev.getDeviceId().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<AgboxChannelListVo.AgboxChannelInfo> channels = postChannelList(deviceUrl, key, dev.getDeviceId());
|
|
|
+ if (channels != null && !channels.isEmpty()) {
|
|
|
+ for (AgboxChannelListVo.AgboxChannelInfo ch : channels) {
|
|
|
+ saveDeviceFromAgbox(dev, ch, type.getCode());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ saveDeviceFromAgbox(dev, null, type.getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("同步 AG 设备 type={} 失败: {}", type.getAgboxType(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private AgboxDeviceListVo postDeviceList(String deviceUrl, String key) throws Exception {
|
|
|
+ JsonRpcRequest rpc = new JsonRpcRequest("getDeviceList", null, null);
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("key", key);
|
|
|
+ body.put("json", JSONUtil.toJsonStr(rpc));
|
|
|
+ String res = postWithRetry(deviceUrl, body);
|
|
|
+ return objectMapper.readValue(res, AgboxDeviceListVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<AgboxChannelListVo.AgboxChannelInfo> postChannelList(String deviceUrl, String key, String deviceId) {
|
|
|
+ try {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("deviceId", deviceId);
|
|
|
+ JsonRpcRequest rpc = new JsonRpcRequest("getDeviceChannelList", params, null);
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("key", key);
|
|
|
+ body.put("json", JSONUtil.toJsonStr(rpc));
|
|
|
+ String res = postWithRetry(deviceUrl, body);
|
|
|
+ AgboxChannelListVo vo = objectMapper.readValue(res, AgboxChannelListVo.class);
|
|
|
+ if (vo != null && vo.getResult() != null && vo.getResult().getChannelList() != null) {
|
|
|
+ return vo.getResult().getChannelList();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.debug("getDeviceChannelList deviceId={} 失败: {}", deviceId, e.getMessage());
|
|
|
+ }
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveDeviceFromAgbox(AgboxDeviceListVo.AgboxDeviceInfo dev,
|
|
|
+ AgboxChannelListVo.AgboxChannelInfo channel,
|
|
|
+ int deviceTypeCode) {
|
|
|
+ String deviceId = dev.getDeviceId();
|
|
|
+ int channelNum = channel != null ? (channel.getChannel() != null ? channel.getChannel() : 0) : 0;
|
|
|
+ LambdaQueryWrapper<SasDevice> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(SasDevice::getDeviceId, deviceId)
|
|
|
+ .eq(SasDevice::getDeviceType, deviceTypeCode)
|
|
|
+ .eq(SasDevice::getChannel, channelNum);
|
|
|
+ if (this.count(wrapper) > 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String gisId = ensureGis(dev);
|
|
|
+ SasDevice device = new SasDevice();
|
|
|
+ device.setId(UUID.randomUUID().toString());
|
|
|
+ device.setDeviceId(deviceId);
|
|
|
+ device.setChannel(channelNum);
|
|
|
+ device.setDeviceType(deviceTypeCode);
|
|
|
+ String ipAddr = (channel != null && channel.getAddr() != null && !channel.getAddr().isEmpty())
|
|
|
+ ? channel.getAddr() : dev.getIpAddr();
|
|
|
+ device.setIpAddr(ipAddr);
|
|
|
+ device.setVillageCode(dev.getVillageCode());
|
|
|
+ device.setAddress(dev.getAddress());
|
|
|
+ device.setNote(channel != null && channel.getPlaceName() != null ? channel.getPlaceName() : dev.getNote());
|
|
|
+ device.setGisId(gisId);
|
|
|
+ device.setTriggerTime(parseTriggerTime(dev.getTriggerTime()));
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ device.setCreateTime(now);
|
|
|
+ device.setUpdateTime(now);
|
|
|
+ this.save(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String ensureGis(AgboxDeviceListVo.AgboxDeviceInfo dev) {
|
|
|
+ if (dev.getGis() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ AgboxDeviceListVo.AgboxGisInfo g = dev.getGis();
|
|
|
+ if (g.getLon() == null && g.getLat() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String gisId = UUID.randomUUID().toString();
|
|
|
+ SasGis gis = new SasGis();
|
|
|
+ gis.setId(gisId);
|
|
|
+ gis.setLon(g.getLon() != null ? BigDecimal.valueOf(g.getLon()) : BigDecimal.ZERO);
|
|
|
+ gis.setLat(g.getLat() != null ? BigDecimal.valueOf(g.getLat()) : BigDecimal.ZERO);
|
|
|
+ gis.setAlt(g.getAlt() != null ? BigDecimal.valueOf(g.getAlt()) : BigDecimal.ZERO);
|
|
|
+ sasGisMapper.insert(gis);
|
|
|
+ return gisId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private LocalDateTime parseTriggerTime(String triggerTime) {
|
|
|
+ if (triggerTime == null || triggerTime.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String s = triggerTime.trim().replace(' ', 'T');
|
|
|
+ if (s.length() > 19) {
|
|
|
+ s = s.substring(0, 19);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ try {
|
|
|
+ return LocalDateTime.parse(triggerTime.trim(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ } catch (Exception e2) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildAgboxBaseUrl(String host, String port) {
|
|
|
+ if (host == null || host.isEmpty()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (host.startsWith("http://") || host.startsWith("https://")) {
|
|
|
+ return host;
|
|
|
+ }
|
|
|
+ return "http://" + host;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String postWithRetry(String url, Map<String, Object> requestBody) {
|
|
|
+ Exception lastEx = null;
|
|
|
+ for (int i = 0; i < MAX_RETRIES; i++) {
|
|
|
+ try {
|
|
|
+ return HttpRequest.post(url)
|
|
|
+ .form(requestBody)
|
|
|
+ .timeout(HTTP_TIMEOUT_MS)
|
|
|
+ .execute()
|
|
|
+ .body();
|
|
|
+ } catch (Exception e) {
|
|
|
+ lastEx = e;
|
|
|
+ if (i < MAX_RETRIES - 1) {
|
|
|
+ log.warn("AG 设备接口请求失败,第 {} 次重试:{}", i + 1, e.getMessage());
|
|
|
+ try {
|
|
|
+ Thread.sleep(1000L * (i + 1));
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ throw new RuntimeException("重试被中断", ie);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("AG 接口请求失败,重试 " + MAX_RETRIES + " 次后仍失败", lastEx);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public String importDeviceByExcel(MultipartFile file) {
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ throw new BusinessException("请选择一个Excel文件上传");
|
|
|
+ }
|
|
|
+ ImportParams params = new ImportParams();
|
|
|
+ params.setHeadRows(1);
|
|
|
+ params.setTitleRows(1);
|
|
|
+ String err = "文件导入失败";
|
|
|
+ try {
|
|
|
+ List<DeviceImportTemplateVO> rows = ExcelImportUtil.importExcel(
|
|
|
+ file.getInputStream(), DeviceImportTemplateVO.class, params);
|
|
|
+ if (rows == null || rows.isEmpty()) {
|
|
|
+ throw new BusinessException("文件不能为空");
|
|
|
+ }
|
|
|
+ int index = 0;
|
|
|
+ for (DeviceImportTemplateVO row : rows) {
|
|
|
+ index++;
|
|
|
+ if (row.getDeviceType() == null
|
|
|
+ || row.getDeviceId() == null || row.getDeviceId().isEmpty()
|
|
|
+ || row.getIpAddr() == null || row.getIpAddr().isEmpty()
|
|
|
+ || row.getPort() == null) {
|
|
|
+ throw new BusinessException("第" + (index + 1) + "行数据导入失败,设备类型、设备编号、设备IP、端口为必填");
|
|
|
+ }
|
|
|
+ int channel = row.getChannel() != null ? row.getChannel() : 0;
|
|
|
+ LambdaQueryWrapper<SasDevice> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(SasDevice::getDeviceId, row.getDeviceId())
|
|
|
+ .eq(SasDevice::getDeviceType, row.getDeviceType())
|
|
|
+ .eq(SasDevice::getChannel, channel);
|
|
|
+ if (this.count(wrapper) > 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String gisId = null;
|
|
|
+ if (row.getLon() != null || row.getLat() != null || row.getAlt() != null) {
|
|
|
+ SasGis gis = new SasGis();
|
|
|
+ gisId = UUID.randomUUID().toString();
|
|
|
+ gis.setId(gisId);
|
|
|
+ gis.setLon(row.getLon() != null ? row.getLon() : BigDecimal.ZERO);
|
|
|
+ gis.setLat(row.getLat() != null ? row.getLat() : BigDecimal.ZERO);
|
|
|
+ gis.setAlt(row.getAlt() != null ? row.getAlt() : BigDecimal.ZERO);
|
|
|
+ sasGisMapper.insert(gis);
|
|
|
+ }
|
|
|
+ SasDevice device = new SasDevice();
|
|
|
+ device.setId(UUID.randomUUID().toString());
|
|
|
+ device.setDeviceId(row.getDeviceId());
|
|
|
+ device.setDeviceType(row.getDeviceType());
|
|
|
+ device.setChannel(channel);
|
|
|
+ device.setIpAddr(row.getIpAddr());
|
|
|
+ device.setPort(row.getPort());
|
|
|
+ device.setUsername(row.getUsername());
|
|
|
+ device.setPassword(row.getPassword());
|
|
|
+ device.setVideoType(row.getVideoDeviceType());
|
|
|
+ device.setVideoProtocol(row.getVideoProtocol());
|
|
|
+ device.setNote(row.getNote());
|
|
|
+ device.setAddress(row.getAddress());
|
|
|
+ device.setHouseCode(row.getHouseCode());
|
|
|
+ device.setGisId(gisId);
|
|
|
+ device.setIsBinding(false);
|
|
|
+ device.setShield(false);
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ device.setCreateTime(now);
|
|
|
+ device.setUpdateTime(now);
|
|
|
+ this.save(device);
|
|
|
+ }
|
|
|
+ } catch (BusinessException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量导入设备失败", e);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ return "设备批量导入成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String deviceTypeName(Integer code) {
|
|
|
+ SystemTypeCodeEnum e = SystemTypeCodeEnum.getByCode(code);
|
|
|
+ return e != null ? e.getMessage() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String groupName(String groupId, boolean video) {
|
|
|
+ if (groupId == null || groupId.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (video) {
|
|
|
+ SasVideoMonitorGroupType g = videoMonitorGroupTypeMapper.selectById(groupId);
|
|
|
+ return g != null ? g.getName() : null;
|
|
|
+ } else {
|
|
|
+ SasAlarsasGroupType g = alarsasGroupTypeMapper.selectById(groupId);
|
|
|
+ return g != null ? g.getName() : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private DeviceConfigVO toConfigVOEnriched(SasDevice d) {
|
|
|
+ DeviceConfigVO vo = toConfigVO(d);
|
|
|
+ vo.setDeviceTypeName(deviceTypeName(d.getDeviceType()));
|
|
|
+ vo.setAlarmGroupTypeName(groupName(d.getAlarsasGroupType(), false));
|
|
|
+ vo.setVideoGroupTypeName(groupName(d.getVideoGroupType(), true));
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
private void applyConfig(SasDevice device, DeviceConfigSaveRequest request) {
|
|
|
device.setDeviceId(request.getDeviceId());
|
|
|
device.setChannel(request.getChannel());
|