Browse Source

修改交换机查询接口

develop
Mr.Wang 7 months ago
parent
commit
3a722ebc82
  1. 5
      zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java
  2. 50
      zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java
  3. 21
      zc-business/src/main/java/com/zc/business/domain/DcSwitch.java
  4. 2
      zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java
  5. 6
      zc-business/src/main/java/com/zc/business/service/DcSwitchService.java
  6. 5
      zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java
  7. 71
      zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java
  8. 17
      zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml

5
zc-business/src/main/java/com/zc/business/constant/RedisKeyConstants.java

@ -11,6 +11,11 @@ public class RedisKeyConstants
*/
public static final String DC_DEVICES = "dc:devices";
/**
* dc设备
*/
public static final String DC_DEVICE_ID = "dc:deviceId";
/**
* 路段
*/

50
zc-business/src/main/java/com/zc/business/controller/DcSwitchController.java

@ -6,12 +6,20 @@ import com.zc.business.domain.DcSwitch;
import com.zc.business.service.DcSwitchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
/**
* 交换机Controller
@ -46,10 +54,52 @@ public class DcSwitchController extends BaseController {
/**
* 查询所有数据
*
* @return
*/
@GetMapping("/list")
public AjaxResult getSwitchListAll() {
return AjaxResult.success(dcSwitchService.getSwitchListAll());
}
/**
* 定时更新交换机网络状态
*/
@Scheduled(cron = "0 0/30 * * * ?")
public void updateNetWorkStatus() {
List<DcSwitch> switchList = dcSwitchService.getSwitchList(new DcSwitch());
ExecutorService executor = Executors.newFixedThreadPool(100);
List<DcSwitch> collect = switchList.stream()
.filter(dcSwitch -> {
return dcSwitch.getAncestors().split(",").length > 1;
}).collect(Collectors.toList());
CountDownLatch latch = new CountDownLatch(collect.size());
collect.forEach(dcSwitch -> {
executor.execute(() -> {
try {
InetAddress inet = InetAddress.getByName(dcSwitch.getSwitchIp());
if (inet.isReachable(5000)) {
// 成功
dcSwitch.setNetWorkStatus(1);
} else {
// 失败
dcSwitch.setNetWorkStatus(0);
}
} catch (IOException e) {
e.getMessage();
} finally {
latch.countDown();
}
});
});
try {
latch.await(); // 等待所有线程执行完毕
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
executor.shutdown();
}
// 批量修改
dcSwitchService.updateBatchByNetWorkStatus(collect);
}
}

21
zc-business/src/main/java/com/zc/business/domain/DcSwitch.java

@ -10,6 +10,7 @@ import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 交换机对象 dc_switch
@ -72,6 +73,20 @@ public class DcSwitch {
@ApiModelProperty("交换机ip")
private String switchIp;
/**
* 网路状态
*/
@Excel(name = "网路状态")
@ApiModelProperty("网路状态")
private Integer netWorkStatus;
/**
* 环网
*/
@Excel(name = "环网")
@ApiModelProperty("环网")
private Integer ringNetWork;
/**
* 使用状态
*/
@ -98,4 +113,10 @@ public class DcSwitch {
private List<DcSwitch> children;
/**
* 故障数量
*/
private int numberOfFaults;
private Map<String, List<DcDevice>> dcDeviceList;
}

2
zc-business/src/main/java/com/zc/business/mapper/DcSwitchMapper.java

@ -14,4 +14,6 @@ import java.util.List;
@Mapper
public interface DcSwitchMapper extends BaseMapper<DcSwitch> {
List<DcSwitch> getSwitchList(DcSwitch dcSwitch);
int updateBatchByNetWorkStatus(List<DcSwitch> list);
}

6
zc-business/src/main/java/com/zc/business/service/DcSwitchService.java

@ -28,4 +28,10 @@ public interface DcSwitchService {
* @return
*/
List<DcSwitch> getSwitchListAll();
/**
* 批量修改网络状态
* @return
*/
int updateBatchByNetWorkStatus(List<DcSwitch> list);
}

5
zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java

@ -68,13 +68,18 @@ public class DcDeviceServiceImpl extends ServiceImpl<DcDeviceMapper, DcDevice> i
// 清楚 redis 缓存数据
redisCache.deleteObject(RedisKeyConstants.DC_DEVICES);
redisCache.deleteObject(RedisKeyConstants.DC_DEVICE_ID);
// 添加 redis 缓存数据
dcDevices.forEach(val -> {
String iotDeviceId = val.getIotDeviceId();
Long id = val.getId();
if (StringUtils.hasText(iotDeviceId)) {
redisCache.setCacheMapValue(RedisKeyConstants.DC_DEVICES, val.getIotDeviceId(), val);
}
if (StringUtils.hasText(id.toString())) {
redisCache.setCacheMapValue(RedisKeyConstants.DC_DEVICE_ID, val.getId(), val);
}
});
}

71
zc-business/src/main/java/com/zc/business/service/impl/DcSwitchServiceImpl.java

@ -1,19 +1,20 @@
package com.zc.business.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.zc.business.constant.RedisKeyConstants;
import com.zc.business.domain.DcDevice;
import com.zc.business.domain.DcSwitch;
import com.zc.business.mapper.DcDeviceMapper;
import com.zc.business.mapper.DcSwitchMapper;
import com.zc.business.service.DcSwitchService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
/**
@ -29,14 +30,14 @@ public class DcSwitchServiceImpl extends ServiceImpl<DcSwitchMapper, DcSwitch> i
private DcSwitchMapper dcSwitchMapper;
@Resource
private DcDeviceMapper dcDeviceMapper;
private RedisCache redisCache;
/**
* 查询交换机
*/
@Override
public List<DcSwitch> getSwitchList(DcSwitch dcSwitch) {
return dcSwitchMapper.getSwitchList(dcSwitch);
return getDeviceListBySwitch(dcSwitchMapper.getSwitchList(dcSwitch));
}
/**
@ -48,7 +49,12 @@ public class DcSwitchServiceImpl extends ServiceImpl<DcSwitchMapper, DcSwitch> i
return new ArrayList<>();
}
List<String> devices = Arrays.stream(deviceList.split(",")).collect(Collectors.toList());
return dcDeviceMapper.selectBatchIds(devices);
List<DcDevice> dcDevices = new ArrayList<>();
devices.forEach(device -> {
DcDevice cacheMapValue = redisCache.getCacheMapValue(RedisKeyConstants.DC_DEVICE_ID,device);
dcDevices.add(cacheMapValue);
});
return dcDevices;
}
/**
@ -58,7 +64,8 @@ public class DcSwitchServiceImpl extends ServiceImpl<DcSwitchMapper, DcSwitch> i
*/
@Override
public List<DcSwitch> getSwitchListAll() {
List<DcSwitch> dcSwitches = dcSwitchMapper.getSwitchList(new DcSwitch());
List<DcSwitch> deviceListBySwitch = dcSwitchMapper.getSwitchList(new DcSwitch());
List<DcSwitch> dcSwitches = getDeviceListBySwitch(deviceListBySwitch);
List<DcSwitch> returnList = new ArrayList<>();
List<Integer> tempList = new ArrayList<>();
for (DcSwitch switche : dcSwitches) {
@ -77,6 +84,52 @@ public class DcSwitchServiceImpl extends ServiceImpl<DcSwitchMapper, DcSwitch> i
return returnList;
}
/**
* 处置交换机数据-获取设备数据
* @param list
* @return
*/
public List<DcSwitch> getDeviceListBySwitch(List<DcSwitch> list) {
ExecutorService executor = Executors.newFixedThreadPool(100);
CountDownLatch latch = new CountDownLatch(list.size());
list.forEach(dcSwitch -> {
executor.execute(() ->{
try {
if (StringUtils.isNotEmpty(dcSwitch.getDeviceList())) {
// 说明是设备交换机
List<DcDevice> deviceList = getDeviceList(dcSwitch.getDeviceList());
if (deviceList.size() > 0) {
Map<String, List<DcDevice>> collect = deviceList.stream().collect(Collectors.groupingBy(DcDevice::getDeviceType));
dcSwitch.setDcDeviceList(collect);
}
}
}catch (Exception e) {
e.printStackTrace();
}finally {
latch.countDown();
}
});
});
try {
latch.await(); // 等待所有线程执行完毕
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
executor.shutdown();
}
return list;
}
/**
* 批量修改网络状态
* @return
*/
@Override
public int updateBatchByNetWorkStatus(List<DcSwitch> list) {
return dcSwitchMapper.updateBatchByNetWorkStatus(list);
}
private void recursionFn(List<DcSwitch> list, DcSwitch t) {
// 得到子节点列表
List<DcSwitch> childList = getChildList(list, t);

17
zc-business/src/main/resources/mapper/business/DcSwitchMapper.xml

@ -12,6 +12,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="stakeMark" column="stake_mark"/>
<result property="deviceList" column="device_list"/>
<result property="switchIp" column="switch_ip"/>
<result property="ringNetWork" column="ring_netWork"/>
<result property="netWorkStatus" column="netWork_status"/>
<result property="userState" column="user_state"/>
<result property="stakeMark" column="stake_mark"/>
<result property="createTime" column="create_time"/>
@ -26,6 +28,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
stake_mark,
device_list,
switch_ip,
ring_netWork,
netWork_status,
user_state,
create_time,
update_time
@ -40,4 +44,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where>
</select>
<update id="updateBatchByNetWorkStatus" parameterType="List">
update dc_switch set netWork_status =
<foreach collection="list" item="item" index="index"
separator=" " open="case switch_id" close="end">
when #{item.switchId} then #{item.netWorkStatus}
</foreach>
where switch_id in
<foreach collection="list" item="item" index="index"
separator="," open="(" close=")">
#{item.switchId}
</foreach>
</update>
</mapper>

Loading…
Cancel
Save