From 5f8bdab765721f86ad82f01ef9dd1ca45bc22d7e Mon Sep 17 00:00:00 2001 From: caiyuchao Date: Mon, 24 Feb 2025 11:30:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=95=B0=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UClientController.java | 6 +- .../wfc/user/domain/vo/UClientCurrentVo.java | 1 + .../org/wfc/user/service/IUClientService.java | 2 +- .../service/impl/UAccountServiceImpl.java | 28 +++++++++ .../user/service/impl/UClientServiceImpl.java | 61 +++++++++++++++++-- 5 files changed, 91 insertions(+), 7 deletions(-) diff --git a/wfc-modules/wfc-user/src/main/java/org/wfc/user/controller/UClientController.java b/wfc-modules/wfc-user/src/main/java/org/wfc/user/controller/UClientController.java index 186b919..0ae8918 100644 --- a/wfc-modules/wfc-user/src/main/java/org/wfc/user/controller/UClientController.java +++ b/wfc-modules/wfc-user/src/main/java/org/wfc/user/controller/UClientController.java @@ -8,9 +8,11 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.wfc.common.core.domain.LoginUser; import org.wfc.common.core.domain.R; import org.wfc.common.core.web.controller.BaseController; import org.wfc.common.core.web.page.TableDataInfo; +import org.wfc.common.security.utils.SecurityUtils; import org.wfc.user.api.domain.bo.UClientBo; import org.wfc.user.domain.vo.UClientCurrentVo; import org.wfc.user.domain.vo.UClientHistoryUserVo; @@ -52,7 +54,9 @@ public class UClientController extends BaseController { @GetMapping("/pageCurrentClient") public TableDataInfo getCurrentClients() { startPage(); - List result = clientService.getCurrentClients(); + LoginUser loginUser = SecurityUtils.getLoginUser(); + + List result = clientService.getCurrentClients(loginUser.getUserid()); return getDataTable(result); } diff --git a/wfc-modules/wfc-user/src/main/java/org/wfc/user/domain/vo/UClientCurrentVo.java b/wfc-modules/wfc-user/src/main/java/org/wfc/user/domain/vo/UClientCurrentVo.java index b3e7d4b..581ebbb 100644 --- a/wfc-modules/wfc-user/src/main/java/org/wfc/user/domain/vo/UClientCurrentVo.java +++ b/wfc-modules/wfc-user/src/main/java/org/wfc/user/domain/vo/UClientCurrentVo.java @@ -17,4 +17,5 @@ public class UClientCurrentVo { private Long trafficDown; private Long trafficUp; private Long activity; + private String SiteId; } diff --git a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/IUClientService.java b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/IUClientService.java index bf20d9c..be2ec11 100644 --- a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/IUClientService.java +++ b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/IUClientService.java @@ -20,7 +20,7 @@ public interface IUClientService extends IService { boolean recordClientUser(UClientBo uClientBo); - List getCurrentClients(); + List getCurrentClients(Long userId); List getHistoryByUser(); } diff --git a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UAccountServiceImpl.java b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UAccountServiceImpl.java index 56de5d6..6aaecdb 100644 --- a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UAccountServiceImpl.java +++ b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UAccountServiceImpl.java @@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -33,17 +34,20 @@ import org.wfc.user.domain.UClient; import org.wfc.user.domain.vo.UAccountDashboardVo; import org.wfc.user.domain.vo.UCdrLatestHistoryVo; import org.wfc.user.domain.vo.UCdrUserVo; +import org.wfc.user.domain.vo.UClientCurrentVo; import org.wfc.user.mapper.UAccountMapper; import org.wfc.user.mapper.UBillMapper; import org.wfc.user.mapper.UBillRuleMapper; import org.wfc.user.mapper.UCdrMapper; import org.wfc.user.mapper.UClientMapper; import org.wfc.user.service.IUAccountService; +import org.wfc.user.service.IUClientService; import org.wfc.user.util.AccountUtil; import org.wfc.user.util.BillRuleUtil; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Optional; @@ -82,6 +86,10 @@ public class UAccountServiceImpl extends ServiceImpl i @Autowired private OmadaClientApi omadaClientApi; + @Autowired + @Lazy + private IUClientService uClientService; + // 定时任务时间间隔 private static final int JOB_PERIOD = -30; @@ -269,6 +277,19 @@ public class UAccountServiceImpl extends ServiceImpl i if (!AccountUtil.isValid(account, current)) { wifiApi.cancelAuthClient(site.getSiteId(), client.getMac()); } + // 设备数超出限制的话,取消在线时间短的设备 + if (account.getClientNumEnable()) { + List currentClients = uClientService.getCurrentClients(account.getUserId()); + if (currentClients.size() <= account.getClientNum()) { + continue; + } + int limitNum = currentClients.size() - account.getClientNum(); + List cancelClients = currentClients.stream().sorted(Comparator.comparing(UClientCurrentVo::getUpTime)) + .limit(limitNum).collect(Collectors.toList()); + for (UClientCurrentVo cancelClient : cancelClients) { + wifiApi.cancelAuthClient(cancelClient.getSiteId(), cancelClient.getClientMac()); + } + } } } } @@ -281,6 +302,13 @@ public class UAccountServiceImpl extends ServiceImpl i } Date current = new Date(); if (AccountUtil.isValid(account, current)) { + + if (account.getClientNumEnable()) { + int onlineClientNum = uClientService.getCurrentClients(client.getUserId()).size(); + if (onlineClientNum > account.getClientNum()) { + return; + } + } wifiApi.authClient(client.getSiteId(), client.getClientMac()); // 带宽限速 diff --git a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UClientServiceImpl.java b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UClientServiceImpl.java index 3e33ea5..ff7932d 100644 --- a/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UClientServiceImpl.java +++ b/wfc-modules/wfc-user/src/main/java/org/wfc/user/service/impl/UClientServiceImpl.java @@ -6,9 +6,14 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.wfc.common.core.constant.WifiConstants; import org.wfc.common.core.domain.LoginUser; import org.wfc.common.security.utils.SecurityUtils; +import org.wfc.omada.api.client.OmadaClientApi; +import org.wfc.omada.api.client.model.ClientInfo; +import org.wfc.omada.api.client.model.OperationResponseClientGridVoClientInfo; import org.wfc.user.api.domain.bo.UClientBo; import org.wfc.user.domain.UClient; import org.wfc.user.domain.vo.UClientCurrentVo; @@ -17,8 +22,13 @@ import org.wfc.user.mapper.UClientMapper; import org.wfc.user.service.IUAccountService; import org.wfc.user.service.IUClientService; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; /** *

@@ -34,6 +44,9 @@ public class UClientServiceImpl extends ServiceImpl impl @Autowired private IUAccountService accountService; + @Autowired + private OmadaClientApi omadaClientApi; + @Override public boolean recordClientUser(UClientBo uClientBo) { if (StrUtil.isBlank(uClientBo.getClientMac())) { @@ -55,12 +68,50 @@ public class UClientServiceImpl extends ServiceImpl impl } @Override - public List getCurrentClients() { - LoginUser loginUser = SecurityUtils.getLoginUser(); - if (ObjectUtil.isNull(loginUser)) { - return Collections.emptyList(); + public List getCurrentClients(Long userId) { + + List clients = this.list(Wrappers.lambdaQuery() + .eq(UClient::getUserId, userId) + .isNotNull(UClient::getClientMac) + .isNotNull(UClient::getSiteId)); + + // 根据site分组 + Map> siteMap = clients.stream().collect(Collectors.groupingBy(UClient::getSiteId)); + List clientVos = new ArrayList<>(); + for (Map.Entry> siteEntry : siteMap.entrySet()) { + String siteId = siteEntry.getKey(); + // 查出在线client + ResponseEntity activeClientRes = omadaClientApi.getGridActiveClients(siteId, 1, 1000); + if (activeClientRes.getBody() == null) { + continue; + } + if (activeClientRes.getBody().getResult() == null) { + continue; + } + List activeClients = activeClientRes.getBody().getResult().getData(); + List loginClients = siteEntry.getValue(); + long i = 0; + for (UClient loginClient : loginClients) { + Optional clientOptional = activeClients.stream() + .filter(c -> Objects.equals(c.getMac(), loginClient.getClientMac()) && c.getAuthStatus() == WifiConstants.AUTH_STATUS_AUTHORIZED).findFirst(); + if (clientOptional.isPresent()) { + ClientInfo clientInfo = clientOptional.get(); + UClientCurrentVo clientVo = new UClientCurrentVo(); + clientVo.setId(i); + clientVo.setClientMac(clientInfo.getMac()); + clientVo.setClientName(clientInfo.getName()); + clientVo.setActivity(clientInfo.getActivity()); + clientVo.setTrafficDown(clientInfo.getTrafficDown()); + clientVo.setTrafficUp(clientInfo.getTrafficUp()); + clientVo.setUpTime(clientInfo.getActivity()); + clientVo.setSiteId(siteId); + clientVos.add(clientVo); + i++; + } + } } - return this.baseMapper.getCurrentClients(loginUser.getUserid()); + + return clientVos; } @Override