|
|
package com.sunyo.wlpt.message.bus.service.service.view;
|
|
|
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.BusServer;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.view.ViewExchangeInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper;
|
|
|
import com.sunyo.wlpt.message.bus.service.mapper.VirtualHostMapper;
|
|
|
import com.sunyo.wlpt.message.bus.service.rabbit.utils.ClientUtils;
|
|
|
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author 子诚
|
|
|
* Description:
|
|
|
* 时间:2020/8/27 17:08
|
|
|
*/
|
|
|
@Service
|
|
|
public class ExchangeFactory {
|
|
|
|
|
|
@Resource
|
|
|
private BusServerMapper busServerMapper;
|
|
|
|
|
|
@Resource
|
|
|
private VirtualHostMapper virtualHostMapper;
|
|
|
|
|
|
public ResultJson getViewExchangeList(String serverName, String virtualHostName, Integer pageNum, Integer pageSize) throws IOException, URISyntaxException
|
|
|
{
|
|
|
List<ViewExchangeInfo> list = new ArrayList<>();
|
|
|
|
|
|
// 服务器名称、虚拟主机名称,均为空
|
|
|
if (StringUtil.isNullOrEmpty(serverName) && StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
List<BusServer> serverList = busServerMapper.getServerList();
|
|
|
for (BusServer busServer : serverList) {
|
|
|
List<ViewExchangeInfo> viewExchanges = ClientUtils.getViewExchanges(busServer);
|
|
|
list.addAll(viewExchanges);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 仅,服务器名称不为空
|
|
|
if (!StringUtil.isNullOrEmpty(serverName) && StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
BusServer busServer = busServerMapper.selectByServerName(serverName);
|
|
|
if (busServer == null) {
|
|
|
return new ResultJson("400", "该服务器名称不存在,请仔细检查");
|
|
|
}
|
|
|
List<ViewExchangeInfo> viewExchanges = ClientUtils.getViewExchanges(busServer);
|
|
|
list.addAll(viewExchanges);
|
|
|
}
|
|
|
|
|
|
// 仅,虚拟主机名称不为空
|
|
|
if (StringUtil.isNullOrEmpty(serverName) && !StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
VirtualHost virtualHost = virtualHostMapper.selectByVirtualHostName(virtualHostName);
|
|
|
if (virtualHost == null) {
|
|
|
return new ResultJson("400", "该虚拟主机名称不存在,请仔细检查");
|
|
|
}
|
|
|
BusServer busServer = busServerMapper.selectByPrimaryKey(virtualHost.getServerId());
|
|
|
List<ViewExchangeInfo> viewExchanges = ClientUtils.getViewExchanges(busServer, virtualHostName);
|
|
|
list.addAll(viewExchanges);
|
|
|
}
|
|
|
|
|
|
// 服务器名称、虚拟主机名称,均不为空
|
|
|
if (!StringUtil.isNullOrEmpty(serverName) && !StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
BusServer busServer = busServerMapper.selectByServerName(serverName);
|
|
|
if (busServer == null) {
|
|
|
return new ResultJson("400", "该服务器名称不存在,请仔细检查");
|
|
|
}
|
|
|
VirtualHost virtualHost = virtualHostMapper.selectByVirtualHostName(virtualHostName);
|
|
|
if (virtualHost == null) {
|
|
|
return new ResultJson("400", "该虚拟主机名称不存在,请仔细检查");
|
|
|
}
|
|
|
if (!virtualHost.getServerId().equals(busServer.getId())) {
|
|
|
return new ResultJson("400", "该虚拟主机不属于该服务器,请仔细检查");
|
|
|
}
|
|
|
List<ViewExchangeInfo> viewExchanges = ClientUtils.getViewExchanges(busServer, virtualHostName);
|
|
|
list.addAll(viewExchanges);
|
|
|
}
|
|
|
|
|
|
Integer total = list.size();
|
|
|
List<ViewExchangeInfo> resultList = subAndSortList(pageNum, pageSize, list);
|
|
|
return resultList.size() > 0
|
|
|
? new ResultJson<>("200", "查询交换机监控,成功!", resultList, total)
|
|
|
: new ResultJson<>("500", "查询交换机监控,失败!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 实现排序与分页效果
|
|
|
*
|
|
|
* @param pageNum 当前的页数
|
|
|
* @param pageSize 每页的大小
|
|
|
* @param list List<ViewExchangeInfo>
|
|
|
* @return
|
|
|
*/
|
|
|
private List<ViewExchangeInfo> subAndSortList(Integer pageNum, Integer pageSize, List<ViewExchangeInfo> list)
|
|
|
{
|
|
|
Integer start = (pageNum - 1) * pageSize;
|
|
|
Integer end = start + pageSize;
|
|
|
Integer total = list.size();
|
|
|
if (start > total) {
|
|
|
start = 0;
|
|
|
end = total;
|
|
|
}
|
|
|
if (end > total) {
|
|
|
end = total;
|
|
|
}
|
|
|
List<ViewExchangeInfo> sortedList =
|
|
|
list.stream().sorted(Comparator.comparing(ViewExchangeInfo::getPublishIn).reversed())
|
|
|
.collect(Collectors.toList());
|
|
|
List<ViewExchangeInfo> pageList = sortedList.subList(start, end);
|
|
|
return pageList;
|
|
|
}
|
|
|
} |
...
|
...
|
|