feat: buy package and auth
This commit is contained in:
@@ -8,6 +8,7 @@ import org.wfc.user.domain.vo.UCdrClientVo;
|
|||||||
import org.wfc.user.domain.vo.UCdrHistoryUserVo;
|
import org.wfc.user.domain.vo.UCdrHistoryUserVo;
|
||||||
import org.wfc.user.domain.vo.UCdrUserVo;
|
import org.wfc.user.domain.vo.UCdrUserVo;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,7 +21,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface UCdrMapper extends BaseMapper<UCdr> {
|
public interface UCdrMapper extends BaseMapper<UCdr> {
|
||||||
|
|
||||||
List<UCdrUserVo> getByUser(@Param("userId") Long userId);
|
List<UCdrUserVo> getByUser(@Param("userId") Long userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
|
||||||
|
|
||||||
List<UCdrClientVo> getByClient(@Param("client") UCdrClientBo client);
|
List<UCdrClientVo> getByClient(@Param("client") UCdrClientBo client);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.wfc.user.service;
|
package org.wfc.user.service;
|
||||||
|
|
||||||
|
import org.wfc.user.api.domain.bo.UClientBo;
|
||||||
import org.wfc.user.domain.UAccount;
|
import org.wfc.user.domain.UAccount;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
@@ -13,4 +14,15 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||||||
*/
|
*/
|
||||||
public interface IUAccountService extends IService<UAccount> {
|
public interface IUAccountService extends IService<UAccount> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计和取消授权用户
|
||||||
|
*/
|
||||||
|
void statAndCancelAuthUser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权设备和限速
|
||||||
|
* @param client client
|
||||||
|
*/
|
||||||
|
void authClientAndRateLimit(UClientBo client);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,30 @@
|
|||||||
package org.wfc.user.service.impl;
|
package org.wfc.user.service.impl;
|
||||||
|
|
||||||
import org.wfc.user.domain.UAccount;
|
import cn.hutool.core.date.DateTime;
|
||||||
import org.wfc.user.mapper.UAccountMapper;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import org.wfc.user.service.IUAccountService;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.wfc.omada.api.client.OmadaClientApi;
|
||||||
|
import org.wfc.omada.api.client.model.ClientRateLimitSetting;
|
||||||
|
import org.wfc.omada.api.client.model.CustomRateLimitEntity;
|
||||||
|
import org.wfc.omada.api.hotspot.OmadaAuthorizedClientApi;
|
||||||
|
import org.wfc.user.api.domain.bo.UClientBo;
|
||||||
|
import org.wfc.user.domain.UAccount;
|
||||||
|
import org.wfc.user.domain.UClient;
|
||||||
|
import org.wfc.user.domain.vo.UCdrUserVo;
|
||||||
|
import org.wfc.user.mapper.UAccountMapper;
|
||||||
|
import org.wfc.user.mapper.UCdrMapper;
|
||||||
|
import org.wfc.user.mapper.UClientMapper;
|
||||||
|
import org.wfc.user.service.IUAccountService;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -17,4 +37,77 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> implements IUAccountService {
|
public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> implements IUAccountService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UCdrMapper ucdrMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OmadaAuthorizedClientApi omadaAuthorizedClientApi;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UClientMapper clientMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OmadaClientApi omadaClientApi;
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void statAndCancelAuthUser() {
|
||||||
|
// 定时任务查询所有未过期或刚过期(失效时间+定时任务间隔时间)的账户套餐,套餐过期/流量用完/时长用完则取消授权
|
||||||
|
Date current = new Date();
|
||||||
|
DateTime endTime = DateUtil.offsetSecond(current, -30);
|
||||||
|
List<UAccount> accounts = this.list(Wrappers.<UAccount>lambdaQuery().gt(UAccount::getEndTime, endTime));
|
||||||
|
// 更新账户已使用流量,已使用时长
|
||||||
|
List<UCdrUserVo> statCdr = ucdrMapper.getByUser(null, current, endTime);
|
||||||
|
for (UAccount account : accounts) {
|
||||||
|
for (UCdrUserVo stat : statCdr) {
|
||||||
|
if (Objects.equals(account.getUserId(), stat.getId())) {
|
||||||
|
account.setTrafficUsed(stat.getTrafficUp() + stat.getTrafficDown());
|
||||||
|
account.setDurationUsed(stat.getDuration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.updateBatchById(accounts);
|
||||||
|
|
||||||
|
// 过期/流量已用完/时长已用完应取消授权
|
||||||
|
List<Long> userIds = accounts.stream().filter(account -> (account.getEndTime().before(endTime) && account.getEndTime().after(current))
|
||||||
|
|| (account.getTrafficEnable() && account.getTrafficUsed() > account.getTraffic())
|
||||||
|
|| (account.getDurationEnable() && account.getDurationUsed() > account.getDuration()))
|
||||||
|
.map(UAccount::getUserId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
List<UClient> clients = clientMapper.selectList(Wrappers.<UClient>lambdaQuery().in(UClient::getUserId, userIds));
|
||||||
|
for (UClient client : clients) {
|
||||||
|
omadaAuthorizedClientApi.cancelAuthClient(client.getSiteId(), client.getClientMac());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void authClientAndRateLimit(UClientBo client) {
|
||||||
|
UAccount account = this.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, client.getUserId()), false);
|
||||||
|
if (ObjectUtil.isNull(account)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Date current = new Date();
|
||||||
|
if (account.getStartTime().before(current) && account.getEndTime().after(current)
|
||||||
|
&& (!account.getTrafficEnable() || account.getTrafficUsed() <= account.getTraffic())
|
||||||
|
&& (!account.getDurationEnable() || account.getDurationUsed() <= account.getDuration())) {
|
||||||
|
omadaAuthorizedClientApi.authClient(client.getSiteId(), client.getClientMac());
|
||||||
|
|
||||||
|
// 带宽限速
|
||||||
|
if (account.getRateLimitEnable()) {
|
||||||
|
ClientRateLimitSetting clientRateLimitSetting = new ClientRateLimitSetting();
|
||||||
|
clientRateLimitSetting.setMode(0);
|
||||||
|
CustomRateLimitEntity customRateLimitEntity = new CustomRateLimitEntity();
|
||||||
|
customRateLimitEntity.setDownEnable(account.getDownLimitEnable());
|
||||||
|
customRateLimitEntity.setDownLimit(account.getDownLimit() == null ? 0 : account.getDownLimit().intValue());
|
||||||
|
customRateLimitEntity.setDownUnit(1);
|
||||||
|
customRateLimitEntity.setUpEnable(account.getUpLimitEnable());
|
||||||
|
customRateLimitEntity.setUpLimit(account.getUpLimit() == null ? 0 : account.getUpLimit().intValue());
|
||||||
|
customRateLimitEntity.setUpUnit(1);
|
||||||
|
clientRateLimitSetting.setCustomRateLimit(customRateLimitEntity);
|
||||||
|
omadaClientApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.wfc.user.domain.vo.UCdrClientVo;
|
|||||||
import org.wfc.user.domain.vo.UCdrHistoryUserVo;
|
import org.wfc.user.domain.vo.UCdrHistoryUserVo;
|
||||||
import org.wfc.user.domain.vo.UCdrUserVo;
|
import org.wfc.user.domain.vo.UCdrUserVo;
|
||||||
import org.wfc.user.mapper.UCdrMapper;
|
import org.wfc.user.mapper.UCdrMapper;
|
||||||
|
import org.wfc.user.service.IUAccountService;
|
||||||
import org.wfc.user.service.IUCdrHistoryService;
|
import org.wfc.user.service.IUCdrHistoryService;
|
||||||
import org.wfc.user.service.IUCdrService;
|
import org.wfc.user.service.IUCdrService;
|
||||||
import org.wfc.user.service.IUClientService;
|
import org.wfc.user.service.IUClientService;
|
||||||
@@ -68,10 +69,13 @@ public class UCdrServiceImpl extends ServiceImpl<UCdrMapper, UCdr> implements IU
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IUCdrHistoryService cdrHistoryService;
|
private IUCdrHistoryService cdrHistoryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IUAccountService accountService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UCdrUserVo getByUser() {
|
public UCdrUserVo getByUser() {
|
||||||
LoginUser<Object> loginUser = SecurityUtils.getLoginUser();
|
LoginUser<Object> loginUser = SecurityUtils.getLoginUser();
|
||||||
List<UCdrUserVo> cdrUsers = this.baseMapper.getByUser(loginUser.getUserid());
|
List<UCdrUserVo> cdrUsers = this.baseMapper.getByUser(loginUser.getUserid(), null, null);
|
||||||
return cdrUsers.stream().findFirst().orElse(null);
|
return cdrUsers.stream().findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +120,8 @@ public class UCdrServiceImpl extends ServiceImpl<UCdrMapper, UCdr> implements IU
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accountService.statAndCancelAuthUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCdrHistory(SiteSummaryInfo site, String mac, Long cdrId) {
|
private void addCdrHistory(SiteSummaryInfo site, String mac, Long cdrId) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.wfc.common.core.domain.LoginUser;
|
import org.wfc.common.core.domain.LoginUser;
|
||||||
import org.wfc.common.security.utils.SecurityUtils;
|
import org.wfc.common.security.utils.SecurityUtils;
|
||||||
@@ -13,6 +14,7 @@ import org.wfc.user.domain.UClient;
|
|||||||
import org.wfc.user.domain.vo.UClientCurrentVo;
|
import org.wfc.user.domain.vo.UClientCurrentVo;
|
||||||
import org.wfc.user.domain.vo.UClientHistoryUserVo;
|
import org.wfc.user.domain.vo.UClientHistoryUserVo;
|
||||||
import org.wfc.user.mapper.UClientMapper;
|
import org.wfc.user.mapper.UClientMapper;
|
||||||
|
import org.wfc.user.service.IUAccountService;
|
||||||
import org.wfc.user.service.IUClientService;
|
import org.wfc.user.service.IUClientService;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -29,6 +31,9 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class UClientServiceImpl extends ServiceImpl<UClientMapper, UClient> implements IUClientService {
|
public class UClientServiceImpl extends ServiceImpl<UClientMapper, UClient> implements IUClientService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IUAccountService accountService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean recordClientUser(UClientBo uClientBo) {
|
public boolean recordClientUser(UClientBo uClientBo) {
|
||||||
if (StrUtil.isBlank(uClientBo.getClientMac())) {
|
if (StrUtil.isBlank(uClientBo.getClientMac())) {
|
||||||
@@ -41,6 +46,8 @@ public class UClientServiceImpl extends ServiceImpl<UClientMapper, UClient> impl
|
|||||||
if (hasUClient != null) {
|
if (hasUClient != null) {
|
||||||
uClient.setId(hasUClient.getId());
|
uClient.setId(hasUClient.getId());
|
||||||
}
|
}
|
||||||
|
// 登录时如果当前用户有可用套餐和余额授权当前设备访问wifi,且根据套餐限制带宽
|
||||||
|
accountService.authClientAndRateLimit(uClientBo);
|
||||||
return this.saveOrUpdate(uClient);
|
return this.saveOrUpdate(uClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean saveOrder(UOrder order) {
|
public boolean saveOrder(UOrder order) {
|
||||||
|
order.setStatus(OrderStatusEnum.UNPAID.getCode());
|
||||||
this.save(order);
|
this.save(order);
|
||||||
// 支付成功回调
|
// 支付成功回调
|
||||||
paySuccess(order.getId());
|
paySuccess(order.getId());
|
||||||
|
|||||||
@@ -3,32 +3,42 @@
|
|||||||
<mapper namespace="org.wfc.user.mapper.UCdrMapper">
|
<mapper namespace="org.wfc.user.mapper.UCdrMapper">
|
||||||
|
|
||||||
<select id="getByUser" resultType="org.wfc.user.domain.vo.UCdrUserVo">
|
<select id="getByUser" resultType="org.wfc.user.domain.vo.UCdrUserVo">
|
||||||
|
|
||||||
|
SELECT t.* FROM (
|
||||||
SELECT
|
SELECT
|
||||||
cdr.user_id id,
|
cdr.user_id id,
|
||||||
min( ch.start_time ) start_time,
|
min( ch.start_time ) start_time,
|
||||||
max( cdr.last_seen_time ) end_time,
|
max( cdr.last_seen_time ) end_time,
|
||||||
max( cdr.activity ) activity,
|
max( cdr.activity ) activity,
|
||||||
sum(
|
sum(
|
||||||
ifnull( cdr.up_time, 0 ))+ sum(
|
ifnull( cdr.up_time, 0 ))+ sum(
|
||||||
ifnull( ch.duration, 0 )) duration,
|
ifnull( ch.duration, 0 )) duration,
|
||||||
sum(
|
sum(
|
||||||
ifnull( cdr.traffic_down, 0 )) + sum(
|
ifnull( cdr.traffic_down, 0 )) + sum(
|
||||||
ifnull( ch.traffic_down, 0 )) traffic_down,
|
ifnull( ch.traffic_down, 0 )) traffic_down,
|
||||||
sum(
|
sum(
|
||||||
ifnull( cdr.traffic_up, 0 )) + sum(
|
ifnull( cdr.traffic_up, 0 )) + sum(
|
||||||
ifnull( ch.traffic_up, 0 )) traffic_up
|
ifnull( ch.traffic_up, 0 )) traffic_up
|
||||||
FROM
|
FROM
|
||||||
u_cdr cdr
|
u_cdr cdr
|
||||||
LEFT JOIN u_cdr_history ch ON cdr.id = ch.cdr_id
|
LEFT JOIN u_cdr_history ch ON cdr.id = ch.cdr_id
|
||||||
AND ch.del_flag = 0
|
AND ch.del_flag = 0
|
||||||
WHERE
|
WHERE
|
||||||
cdr.del_flag = 0
|
cdr.del_flag = 0
|
||||||
AND cdr.last_seen_time != ifnull( ch.end_time, 0 )
|
AND cdr.last_seen_time != ifnull( ch.end_time, 0 )
|
||||||
<if test="userId != null and userId != ''">
|
<if test="userId != null and userId != ''">AND cdr.user_id = #{userId}
|
||||||
AND cdr.user_id = #{userId}
|
|
||||||
</if>
|
</if>
|
||||||
GROUP BY
|
GROUP BY
|
||||||
cdr.user_id
|
cdr.user_id
|
||||||
|
) t
|
||||||
|
<where>
|
||||||
|
<if test="endTime != null">
|
||||||
|
t.last_seen_time <= #{endTime}
|
||||||
|
</if>
|
||||||
|
<if test="startTime != null">
|
||||||
|
t.start_time >= #{startTime}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getByClient" resultType="org.wfc.user.domain.vo.UCdrClientVo">
|
<select id="getByClient" resultType="org.wfc.user.domain.vo.UCdrClientVo">
|
||||||
|
|||||||
Reference in New Issue
Block a user