2
0

feat: 添加设备数限制

This commit is contained in:
caiyuchao
2025-02-24 11:30:33 +08:00
parent 4de27cf581
commit 5f8bdab765
5 changed files with 91 additions and 7 deletions

View File

@@ -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<UClientCurrentVo> result = clientService.getCurrentClients();
LoginUser<Object> loginUser = SecurityUtils.getLoginUser();
List<UClientCurrentVo> result = clientService.getCurrentClients(loginUser.getUserid());
return getDataTable(result);
}

View File

@@ -17,4 +17,5 @@ public class UClientCurrentVo {
private Long trafficDown;
private Long trafficUp;
private Long activity;
private String SiteId;
}

View File

@@ -20,7 +20,7 @@ public interface IUClientService extends IService<UClient> {
boolean recordClientUser(UClientBo uClientBo);
List<UClientCurrentVo> getCurrentClients();
List<UClientCurrentVo> getCurrentClients(Long userId);
List<UClientHistoryUserVo> getHistoryByUser();
}

View File

@@ -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<UAccountMapper, UAccount> 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<UAccountMapper, UAccount> i
if (!AccountUtil.isValid(account, current)) {
wifiApi.cancelAuthClient(site.getSiteId(), client.getMac());
}
// 设备数超出限制的话,取消在线时间短的设备
if (account.getClientNumEnable()) {
List<UClientCurrentVo> currentClients = uClientService.getCurrentClients(account.getUserId());
if (currentClients.size() <= account.getClientNum()) {
continue;
}
int limitNum = currentClients.size() - account.getClientNum();
List<UClientCurrentVo> 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<UAccountMapper, UAccount> 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());
// 带宽限速

View File

@@ -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;
/**
* <p>
@@ -34,6 +44,9 @@ public class UClientServiceImpl extends ServiceImpl<UClientMapper, UClient> 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<UClientMapper, UClient> impl
}
@Override
public List<UClientCurrentVo> getCurrentClients() {
LoginUser<Object> loginUser = SecurityUtils.getLoginUser();
if (ObjectUtil.isNull(loginUser)) {
return Collections.emptyList();
public List<UClientCurrentVo> getCurrentClients(Long userId) {
List<UClient> clients = this.list(Wrappers.<UClient>lambdaQuery()
.eq(UClient::getUserId, userId)
.isNotNull(UClient::getClientMac)
.isNotNull(UClient::getSiteId));
// 根据site分组
Map<String, List<UClient>> siteMap = clients.stream().collect(Collectors.groupingBy(UClient::getSiteId));
List<UClientCurrentVo> clientVos = new ArrayList<>();
for (Map.Entry<String, List<UClient>> siteEntry : siteMap.entrySet()) {
String siteId = siteEntry.getKey();
// 查出在线client
ResponseEntity<OperationResponseClientGridVoClientInfo> activeClientRes = omadaClientApi.getGridActiveClients(siteId, 1, 1000);
if (activeClientRes.getBody() == null) {
continue;
}
if (activeClientRes.getBody().getResult() == null) {
continue;
}
List<ClientInfo> activeClients = activeClientRes.getBody().getResult().getData();
List<UClient> loginClients = siteEntry.getValue();
long i = 0;
for (UClient loginClient : loginClients) {
Optional<ClientInfo> 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