package com.ard.alarm.external.service.impl; import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import com.ard.alarm.camera.domain.ArdCameras; import com.ard.alarm.external.domain.ArdEquipExternal; import com.ard.alarm.external.mapper.ArdEquipExternalMapper; import com.ard.alarm.external.service.IArdEquipExternalService; import com.ard.utils.hiksdk.service.impl.HikClientUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Service; /** * externalService业务层处理 * * @author zj * @date 2023-03-13 */ @Service @Slf4j(topic = "external") @Order(5) public class ArdEquipExternalServiceImpl implements IArdEquipExternalService, ApplicationRunner { private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static List ardAlarmHostList = new ArrayList<>(); public static List ardAccessHostList = new ArrayList<>(); @Autowired private ArdEquipExternalMapper ardEquipExternalMapper; @Override public void run(ApplicationArguments args){ //获取全部海康报警主机尝试登录 ArdEquipExternal alarmHost = new ArdEquipExternal(); alarmHost.setFactory("1"); alarmHost.setType("1"); ardAlarmHostList = selectArdEquipExternalList(alarmHost); HikClientUtil.loginAllAlarmHost(ardAlarmHostList); log.debug("报警主机尝试登录"); //获取全部门禁主机尝试登录 ArdEquipExternal accessControlHost = new ArdEquipExternal(); accessControlHost.setFactory("1"); accessControlHost.setType("6"); ardAccessHostList = selectArdEquipExternalList(accessControlHost); HikClientUtil.loginAllAccessControlHost(ardAccessHostList); log.debug("门禁主机尝试登录"); //同步任务 syncTask(); } /** * 同步任务 * 刘苏义 * 2023/8/11 9:09:27 */ private void syncTask() { scheduler.scheduleAtFixedRate(() -> { try { //region 定时同步报警主机 ArdEquipExternal alarmHost = new ArdEquipExternal(); alarmHost.setFactory("1"); alarmHost.setType("1"); List newAlarmHostList = selectArdEquipExternalList(alarmHost); //需要更新的数据 List updateList = sameListWithDifferent(ardAlarmHostList, newAlarmHostList); if (updateList.size() > 0) { HikClientUtil.logoutAllAlarmHost(updateList); HikClientUtil.loginAllAlarmHost(updateList); ardAlarmHostList.clear(); ardAlarmHostList.addAll(newAlarmHostList); } //需要删除的数据 List delList = diffList(ardAlarmHostList, newAlarmHostList); if (delList.size() > 0) { HikClientUtil.logoutAllAlarmHost(delList); for (ArdEquipExternal ardEquipExternal : delList) { ardAlarmHostList.remove(ardEquipExternal); } } //需要新增的数据 List inserList = diffList(newAlarmHostList, ardAlarmHostList); if (inserList.size() > 0) { HikClientUtil.loginAllAlarmHost(inserList); for (ArdEquipExternal ardEquipExternal : inserList) { ardAlarmHostList.add(ardEquipExternal); } } //endregion //region 定时同步门禁主机 alarmHost = new ArdEquipExternal(); alarmHost.setFactory("1"); alarmHost.setType("6"); List newAccessHostList = selectArdEquipExternalList(alarmHost); //需要更新的数据 updateList = sameListWithDifferent(ardAccessHostList, newAccessHostList); if (updateList.size() > 0) { HikClientUtil.logoutAllAlarmHost(updateList); HikClientUtil.loginAllAlarmHost(updateList); ardAccessHostList.clear(); ardAccessHostList.addAll(newAccessHostList); } //需要删除的数据 delList = diffList(ardAccessHostList, newAccessHostList); if (delList.size() > 0) { HikClientUtil.logoutAllAccessControlHost(delList); for (ArdEquipExternal ardEquipExternal : delList) { ardAccessHostList.remove(ardEquipExternal); } } //需要新增的数据 inserList = diffList(newAccessHostList, ardAccessHostList); if (inserList.size() > 0) { HikClientUtil.loginAllAccessControlHost(inserList); for (ArdEquipExternal ardEquipExternal : inserList) { ardAccessHostList.add(ardEquipExternal); } } //endregion } catch (Exception e) { log.error("同步外联任务执行出错" + e.getMessage()); } }, 10, 10, TimeUnit.SECONDS); } /** * 查询external * * @param id external主键 * @return external */ @Override public ArdEquipExternal selectArdEquipExternalById(String id) { return ardEquipExternalMapper.selectById(id); } @Override public ArdEquipExternal selectArdEquipExternal(ArdEquipExternal ardEquipExternal) { QueryWrapper queryWrapper=new QueryWrapper<>(ardEquipExternal); return ardEquipExternalMapper.selectOne(queryWrapper); } /** * 查询external列表 * * @param ardEquipExternal external * @return external */ @Override public List selectArdEquipExternalList(ArdEquipExternal ardEquipExternal) { QueryWrapper queryWrapper=new QueryWrapper<>(ardEquipExternal); return ardEquipExternalMapper.selectList(queryWrapper); } //求两个对象List的交集字段不同Id相同 private List sameListWithDifferent(List oldList, List newList) { List differentFieldsList = newList.stream() .filter(newItem -> { ArdEquipExternal oldItem = oldList.stream() .filter(item -> item.getId().equals(newItem.getId())) .findFirst() .orElse(null); return oldItem == null || !Objects.equals(oldItem.getUpdateTime(), newItem.getUpdateTime()) || !Objects.equals(oldItem.getIp(), newItem.getIp()) || !Objects.equals(oldItem.getPort(), newItem.getPort()) || !Objects.equals(oldItem.getUsername(), newItem.getUsername()) || !Objects.equals(oldItem.getPassword(), newItem.getPassword()); }) .collect(Collectors.toList()); return differentFieldsList; } // 求两个对象List的差集 private List diffList(List oldArrayList, List newArrayList) { List resultList = oldArrayList.stream() .filter(item -> !newArrayList.stream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId())) .collect(Collectors.toList()); return resultList; } }