定时任务

This commit is contained in:
Flow 2025-08-25 10:44:04 +08:00
parent 029b9e332a
commit a5df6ba6cd
4 changed files with 78 additions and 1 deletions

View File

@ -0,0 +1,32 @@
package cn.iocoder.yudao.module.system.job;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.service.person.PersonService;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
@Component
public class vip implements JobHandler {
private static final Logger log = LoggerFactory.getLogger(vip.class);
@Resource
private PersonService personService;
@Override
public String execute(String param) {
log.info("开始执行VIP过期检查定时任务");
try {
// 批量处理过期VIP用户
personService.processExpiredVipUsers();
log.info("VIP过期检查定时任务执行成功");
return "VIP过期检查执行成功";
} catch (Exception e) {
log.error("VIP过期检查定时任务执行失败", e);
return "VIP过期检查执行失败: " + e.getMessage();
}
}
}

View File

@ -117,4 +117,10 @@ public interface PersonService {
* @param userid 用户编号 * @param userid 用户编号
*/ */
void cancelVip(Integer userid); void cancelVip(Integer userid);
/**
* 批量处理过期VIP用户
* 查询所有VIP到期时间已过的用户并将其isvip字段设置为0
*/
void processExpiredVipUsers();
} }

View File

@ -18,6 +18,11 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* 用户基本信息 Service 实现类 * 用户基本信息 Service 实现类
* *
@ -27,6 +32,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@Validated @Validated
public class PersonServiceImpl implements PersonService { public class PersonServiceImpl implements PersonService {
private static final Logger log = LoggerFactory.getLogger(PersonServiceImpl.class);
@Resource @Resource
private PersonMapper personMapper; private PersonMapper personMapper;
@ -184,4 +191,36 @@ public class PersonServiceImpl implements PersonService {
.set(PersonDO::getVipstarttime, null) // 清空会员开始时间 .set(PersonDO::getVipstarttime, null) // 清空会员开始时间
.set(PersonDO::getVipendtime, null)); // 清空会员结束时间 .set(PersonDO::getVipendtime, null)); // 清空会员结束时间
} }
@Override
public void processExpiredVipUsers() {
// 获取当前时间
String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("开始检查VIP过期用户当前时间: {}", currentTime);
// 查询所有过期的VIP用户
List<PersonDO> expiredVipUsers = personMapper.selectList(new LambdaQueryWrapper<PersonDO>()
.eq(PersonDO::getIsvip, 1) // 当前是VIP用户
.isNotNull(PersonDO::getVipendtime) // VIP结束时间不为空
.lt(PersonDO::getVipendtime, currentTime)); // VIP结束时间小于当前时间
if (expiredVipUsers.isEmpty()) {
log.info("没有发现过期的VIP用户");
return; // 没有过期用户直接返回
}
log.info("发现 {} 个过期的VIP用户开始批量处理", expiredVipUsers.size());
// 提取所有过期用户的ID
List<Integer> expiredUserIds = expiredVipUsers.stream()
.map(PersonDO::getId)
.collect(java.util.stream.Collectors.toList());
// 批量更新过期VIP用户
personMapper.update(null, new LambdaUpdateWrapper<PersonDO>()
.in(PersonDO::getId, expiredUserIds)
.set(PersonDO::getIsvip, 0));// 设置为非会员状态
log.info("成功处理 {} 个过期VIP用户", expiredUserIds.size());
}
} }

View File

@ -7,7 +7,7 @@ spring:
# noinspection SpringBootApplicationYaml # noinspection SpringBootApplicationYaml
exclude: exclude:
- com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源 - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源
- org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置 # - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置
- de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration # 禁用 Spring Boot Admin 的 Server 的自动配置 - de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration # 禁用 Spring Boot Admin 的 Server 的自动配置
- de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration # 禁用 Spring Boot Admin 的 Server UI 的自动配置 - de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration # 禁用 Spring Boot Admin 的 Server UI 的自动配置
- de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration # 禁用 Spring Boot Admin 的 Client 的自动配置 - de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration # 禁用 Spring Boot Admin 的 Client 的自动配置