1.修改定时任务逻辑
This commit is contained in:
@@ -0,0 +1,93 @@
|
|||||||
|
package cn.iocoder.yudao.module.system.job;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUnit;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁用过期账号定时任务处理器
|
||||||
|
* <p>
|
||||||
|
* 该类用于定时检查并禁用已过期的AI账号和大哥账号。
|
||||||
|
* 通过查询租户表中已过期的账号,然后将对应用户的权限状态更新为禁用。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author ziin
|
||||||
|
* @date 2025/9/16 14:35
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DisableAIExpiredAccount implements JobHandler{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户数据访问对象
|
||||||
|
* 用于查询租户信息和过期时间
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
TenantMapper tenantMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员用户数据访问对象
|
||||||
|
* 用于查询和更新用户权限状态
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private AdminUserMapper userMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行禁用过期账号的定时任务
|
||||||
|
* <p>
|
||||||
|
* 该方法会执行以下操作:
|
||||||
|
* 1. 查询所有AI账号已过期的租户
|
||||||
|
* 2. 查询所有大哥账号已过期的租户
|
||||||
|
* 3. 分别禁用这些租户下的用户对应权限
|
||||||
|
* 4. 返回操作结果统计信息
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param param 任务参数(当前未使用)
|
||||||
|
* @return 操作结果字符串,包含禁用的AI账号和大哥账号数量
|
||||||
|
* @throws Exception 执行过程中可能抛出的异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@TenantJob
|
||||||
|
public String execute(String param) throws Exception {
|
||||||
|
Long tenantId = TenantContextHolder.getTenantId();
|
||||||
|
TenantDO tenant = tenantMapper.selectById(tenantId);
|
||||||
|
if (tenant.getAiExpireTime()==null) {
|
||||||
|
return "租户未配置AI过期时间";
|
||||||
|
}
|
||||||
|
Duration brotherDuration = LocalDateTimeUtil.between(tenant.getAiExpireTime(), LocalDateTime.now());
|
||||||
|
long minutes = brotherDuration.toMinutes();
|
||||||
|
LambdaQueryWrapper<AdminUserDO> aiUserQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
aiUserQueryWrapper.eq(AdminUserDO::getTenantId, tenantId);
|
||||||
|
List<AdminUserDO> aiUserList = userMapper.selectList(aiUserQueryWrapper);
|
||||||
|
int aiAccountNum = 0 ;
|
||||||
|
if (minutes >= 0) {
|
||||||
|
for (AdminUserDO adminUserDO : aiUserList) {
|
||||||
|
adminUserDO.setAiChat((byte) 0);
|
||||||
|
userMapper.updateById(adminUserDO);
|
||||||
|
aiAccountNum++;
|
||||||
|
log.info("禁用过期AI账号,账号ID:{}", adminUserDO.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回操作结果:包含禁用的AI账号和大哥账号数量统计
|
||||||
|
return "禁用过期账号成功,禁用了 " + aiAccountNum + " 个 AI 账号。";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package cn.iocoder.yudao.module.system.job;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁用过期账号定时任务处理器
|
||||||
|
* <p>
|
||||||
|
* 该类用于定时检查并禁用已过期的AI账号和大哥账号。
|
||||||
|
* 通过查询租户表中已过期的账号,然后将对应用户的权限状态更新为禁用。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author ziin
|
||||||
|
* @date 2025/9/16 14:35
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DisableBrotherExpiredAccount implements JobHandler{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户数据访问对象
|
||||||
|
* 用于查询租户信息和过期时间
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
TenantMapper tenantMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理员用户数据访问对象
|
||||||
|
* 用于查询和更新用户权限状态
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private AdminUserMapper userMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行禁用过期账号的定时任务
|
||||||
|
* <p>
|
||||||
|
* 该方法会执行以下操作:
|
||||||
|
* 1. 查询所有AI账号已过期的租户
|
||||||
|
* 2. 查询所有大哥账号已过期的租户
|
||||||
|
* 3. 分别禁用这些租户下的用户对应权限
|
||||||
|
* 4. 返回操作结果统计信息
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param param 任务参数(当前未使用)
|
||||||
|
* @return 操作结果字符串,包含禁用的AI账号和大哥账号数量
|
||||||
|
* @throws Exception 执行过程中可能抛出的异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@TenantJob
|
||||||
|
public String execute(String param) throws Exception {
|
||||||
|
Long tenantId = TenantContextHolder.getTenantId();
|
||||||
|
TenantDO tenant = tenantMapper.selectById(tenantId);
|
||||||
|
if (tenant.getBrotherExpireTime()==null) {
|
||||||
|
return "租户未配置大哥过期时间";
|
||||||
|
}
|
||||||
|
Duration brotherDuration = LocalDateTimeUtil.between(tenant.getBrotherExpireTime(), LocalDateTime.now());
|
||||||
|
long minutes = brotherDuration.toMinutes();
|
||||||
|
LambdaQueryWrapper<AdminUserDO> aiUserQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
aiUserQueryWrapper.eq(AdminUserDO::getTenantId, tenantId);
|
||||||
|
List<AdminUserDO> aiUserList = userMapper.selectList(aiUserQueryWrapper);
|
||||||
|
int brotherAccountNum = 0 ;
|
||||||
|
if (minutes >= 0) {
|
||||||
|
for (AdminUserDO adminUserDO : aiUserList) {
|
||||||
|
adminUserDO.setBigBrother((byte) 0);
|
||||||
|
userMapper.updateById(adminUserDO);
|
||||||
|
brotherAccountNum++;
|
||||||
|
log.info("禁用过期大哥账号,账号ID:{}", adminUserDO.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回操作结果:包含禁用的AI账号和大哥账号数量统计
|
||||||
|
return "禁用过期账号成功,禁用了 " + brotherAccountNum + " 个 大哥账号。";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.system.job;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
||||||
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 禁用过期账号定时任务处理器
|
|
||||||
* <p>
|
|
||||||
* 该类用于定时检查并禁用已过期的AI账号和大哥账号。
|
|
||||||
* 通过查询租户表中已过期的账号,然后将对应用户的权限状态更新为禁用。
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @author ziin
|
|
||||||
* @date 2025/9/16 14:35
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class DisableExpiredAccount implements JobHandler{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 租户数据访问对象
|
|
||||||
* 用于查询租户信息和过期时间
|
|
||||||
*/
|
|
||||||
@Resource
|
|
||||||
TenantMapper tenantMapper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 管理员用户数据访问对象
|
|
||||||
* 用于查询和更新用户权限状态
|
|
||||||
*/
|
|
||||||
@Resource
|
|
||||||
private AdminUserMapper userMapper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行禁用过期账号的定时任务
|
|
||||||
* <p>
|
|
||||||
* 该方法会执行以下操作:
|
|
||||||
* 1. 查询所有AI账号已过期的租户
|
|
||||||
* 2. 查询所有大哥账号已过期的租户
|
|
||||||
* 3. 分别禁用这些租户下的用户对应权限
|
|
||||||
* 4. 返回操作结果统计信息
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param param 任务参数(当前未使用)
|
|
||||||
* @return 操作结果字符串,包含禁用的AI账号和大哥账号数量
|
|
||||||
* @throws Exception 执行过程中可能抛出的异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
@TenantJob
|
|
||||||
public String execute(String param) throws Exception {
|
|
||||||
// 获取今天的开始时间(00:00:00)和结束时间(23:59:59.999999999)
|
|
||||||
LocalDateTime todayStart = LocalDate.now().atStartOfDay();
|
|
||||||
LocalDateTime todayEnd = LocalDate.now().atTime(LocalTime.MAX);
|
|
||||||
|
|
||||||
Long tenantId = TenantContextHolder.getTenantId();
|
|
||||||
|
|
||||||
// 构建AI账号过期查询条件:查询AI过期时间在今天的租户
|
|
||||||
LambdaQueryWrapper<TenantDO> aiQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
aiQueryWrapper.between(TenantDO::getAiExpireTime, todayStart, todayEnd)
|
|
||||||
.eq(TenantDO::getId, tenantId)
|
|
||||||
.eq(TenantDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
|
|
||||||
// 构建大哥账号过期查询条件:查询大哥过期时间在今天的租户
|
|
||||||
LambdaQueryWrapper<TenantDO> brotherQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
brotherQueryWrapper.between(TenantDO::getBrotherExpireTime, todayStart, todayEnd)
|
|
||||||
.eq(TenantDO::getId, tenantId)
|
|
||||||
.eq(TenantDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
|
|
||||||
|
|
||||||
// 初始化计数器:记录禁用的AI账号和大哥账号数量
|
|
||||||
int aiAccountNum = 0 ;
|
|
||||||
int brotherAccountNum = 0 ;
|
|
||||||
|
|
||||||
// 处理AI账号过期:查询所有AI账号已过期的租户,禁用其下所有用户的AI聊天权限
|
|
||||||
TenantDO aiTenantDOS = tenantMapper.selectOne(aiQueryWrapper);
|
|
||||||
if (aiTenantDOS != null) {
|
|
||||||
LambdaQueryWrapper<AdminUserDO> aiUserQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
aiUserQueryWrapper.eq(AdminUserDO::getTenantId, aiTenantDOS.getId());
|
|
||||||
List<AdminUserDO> aiUserList = userMapper.selectList(aiUserQueryWrapper);
|
|
||||||
// 禁用每个用户的AI聊天权限
|
|
||||||
for (AdminUserDO adminUserDO : aiUserList) {
|
|
||||||
adminUserDO.setAiChat((byte) 0);
|
|
||||||
userMapper.updateById(adminUserDO);
|
|
||||||
aiAccountNum++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 处理大哥账号过期:查询所有大哥账号已过期的租户,禁用其下所有用户的大哥权限
|
|
||||||
TenantDO brotherTenantDOS = tenantMapper.selectOne(brotherQueryWrapper);
|
|
||||||
if (brotherTenantDOS != null) {
|
|
||||||
LambdaQueryWrapper<AdminUserDO> brotherUserQueryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
brotherUserQueryWrapper.eq(AdminUserDO::getTenantId, brotherTenantDOS.getId());
|
|
||||||
List<AdminUserDO> brotherUserList = userMapper.selectList(brotherUserQueryWrapper);
|
|
||||||
// 禁用每个用户的大哥权限
|
|
||||||
for (AdminUserDO adminUserDO : brotherUserList) {
|
|
||||||
adminUserDO.setBigBrother((byte) 0);
|
|
||||||
userMapper.updateById(adminUserDO);
|
|
||||||
brotherAccountNum++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回操作结果:包含禁用的AI账号和大哥账号数量统计
|
|
||||||
return "禁用过期账号成功,禁用了 " + aiAccountNum + " 个 AI 账号,禁用了 " + brotherAccountNum + " 个 大哥账号。";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user